/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.myfaces.orchestra.frameworkAdapter.jsf;
import org.apache.myfaces.orchestra.conversation.ConversationMessager;
import org.apache.myfaces.orchestra.conversation.jsf.JsfConversationMessager;
import org.apache.myfaces.orchestra.frameworkAdapter.basic.BasicFrameworkAdapter;
import javax.faces.context.FacesContext;
import java.io.IOException;
/**
* An implementation of the FrameworkAdapter for JSF environments.
* <p>
* This class requires the JsfFrameworkAdapterFilter to be configured to run
* or every JSF request.
* <p>
* This class defaults to using a JsfConversationMessager instance.
*/
public class JsfFrameworkAdapter extends BasicFrameworkAdapter
{
public JsfFrameworkAdapter(String conversationMessager)
{
super(null, conversationMessager);
}
protected ConversationMessager createDefaultConversationMessager()
{
return new JsfConversationMessager();
}
protected FacesContext getFacesContext()
{
return FacesContext.getCurrentInstance();
}
public String getInitParameter(String key)
{
FacesContext context = getFacesContext();
return context.getExternalContext().getInitParameter(key);
}
public Object getRequestParameterAttribute(String key)
{
FacesContext context = getFacesContext();
if (context != null)
{
return context.getExternalContext().getRequestParameterMap().get(key);
}
// Fall back to standard request stuff. Note that this is necessary as this
// method might be called before the FacesServletFilter has been run, ie
// the FacesContext might not yet exist.
return super.getRequestParameterAttribute(key);
}
public boolean containsRequestParameterAttribute(String key)
{
FacesContext context = getFacesContext();
if (context != null)
{
return context.getExternalContext().getRequestParameterMap().containsKey(key);
}
return super.containsRequestParameterAttribute(key);
}
public Object getRequestAttribute(String key)
{
FacesContext context = getFacesContext();
if (context != null)
{
return context.getExternalContext().getRequestMap().get(key);
}
return super.getRequestAttribute(key);
}
public void setRequestAttribute(String key, Object value)
{
FacesContext context = getFacesContext();
if (context != null)
{
context.getExternalContext().getRequestMap().put(key, value);
return;
}
super.setRequestAttribute(key, value);
}
public boolean containsRequestAttribute(String key)
{
FacesContext context = getFacesContext();
if (context != null)
{
return context.getExternalContext().getRequestMap().containsKey(key);
}
return super.containsRequestAttribute(key);
}
public Object getSessionAttribute(String key)
{
FacesContext context = getFacesContext();
if (context != null)
{
return context.getExternalContext().getSessionMap().get(key);
}
return super.getSessionAttribute(key);
}
public void setSessionAttribute(String key, Object value)
{
FacesContext context = getFacesContext();
if (context != null)
{
context.getExternalContext().getSessionMap().put(key, value);
return;
}
super.setSessionAttribute(key, value);
}
public boolean containsSessionAttribute(String key)
{
FacesContext context = getFacesContext();
if (context != null)
{
return context.getExternalContext().getSessionMap().containsKey(key);
}
return super.containsSessionAttribute(key);
}
protected String getRequestContextPath()
{
FacesContext context = getFacesContext();
if (context != null)
{
return context.getExternalContext().getRequestContextPath();
}
return super.getRequestContextPath();
}
public void redirect(String url) throws IOException
{
StringBuffer redir = new StringBuffer();
if (url.startsWith("/"))
{
redir.append(getRequestContextPath());
}
redir.append(url);
FacesContext context = getFacesContext();
String actionUrl = context.getExternalContext().encodeActionURL(redir.toString());
context.getExternalContext().redirect(actionUrl);
context.responseComplete();
}
public Object getBean(String name)
{
FacesContext context = getFacesContext();
if (context == null)
{
throw new IllegalStateException("getBean invoked before FacesServlet");
}
return context.getApplication()
.getVariableResolver().resolveVariable(context, name);
}
public void invokeNavigation(String navigationName)
{
FacesContext context = getFacesContext();
context.getApplication().getNavigationHandler().handleNavigation(context, null, navigationName);
}
}