View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.orchestra.frameworkAdapter.jsf;
20  
21  import org.apache.myfaces.orchestra.conversation.ConversationMessager;
22  import org.apache.myfaces.orchestra.conversation.jsf.JsfConversationMessager;
23  import org.apache.myfaces.orchestra.frameworkAdapter.basic.BasicFrameworkAdapter;
24  
25  import javax.faces.context.FacesContext;
26  import java.io.IOException;
27  
28  /***
29   * An implementation of the FrameworkAdapter for JSF environments.
30   * <p>
31   * This class requires the JsfFrameworkAdapterFilter to be configured to run
32   * or every JSF request.
33   * <p>
34   * This class defaults to using a JsfConversationMessager instance. 
35   */
36  public class JsfFrameworkAdapter extends BasicFrameworkAdapter
37  {
38  	public JsfFrameworkAdapter(String conversationMessager)
39  	{
40  		super(null, conversationMessager);
41  	}
42  
43  	protected ConversationMessager createDefaultConversationMessager()
44  	{
45  		return new JsfConversationMessager();
46  	}
47  
48  	protected FacesContext getFacesContext()
49  	{
50  		return FacesContext.getCurrentInstance();
51  	}
52  
53  	public String getInitParameter(String key)
54  	{
55  		FacesContext context = getFacesContext();
56  
57  		return context.getExternalContext().getInitParameter(key);
58  	}
59  
60  	public Object getRequestParameterAttribute(String key)
61  	{
62  		FacesContext context = getFacesContext();
63  		if (context != null)
64  		{
65  			return context.getExternalContext().getRequestParameterMap().get(key);
66  		}
67  
68  		// Fall back to standard request stuff. Note that this is necessary as this
69  		// method might be called before the FacesServletFilter has been run, ie
70  		// the FacesContext might not yet exist.
71  		return super.getRequestParameterAttribute(key);
72  	}
73  
74  	public boolean containsRequestParameterAttribute(String key)
75  	{
76  		FacesContext context = getFacesContext();
77  		if (context != null)
78  		{
79  			return context.getExternalContext().getRequestParameterMap().containsKey(key);
80  		}
81  
82  		return super.containsRequestParameterAttribute(key);
83  	}
84  
85  	public Object getRequestAttribute(String key)
86  	{
87  		FacesContext context = getFacesContext();
88  		if (context != null)
89  		{
90  			return context.getExternalContext().getRequestMap().get(key);
91  		}
92  
93  		return super.getRequestAttribute(key);
94  	}
95  
96  	public void setRequestAttribute(String key, Object value)
97  	{
98  		FacesContext context = getFacesContext();
99  		if (context != null)
100 		{
101 			context.getExternalContext().getRequestMap().put(key, value);
102 			return;
103 		}
104 
105 		super.setRequestAttribute(key, value);
106 	}
107 
108 	public boolean containsRequestAttribute(String key)
109 	{
110 		FacesContext context = getFacesContext();
111 		if (context != null)
112 		{
113 			return context.getExternalContext().getRequestMap().containsKey(key);
114 		}
115 
116 		return super.containsRequestAttribute(key);
117 	}
118 
119 	public Object getSessionAttribute(String key)
120 	{
121 		FacesContext context = getFacesContext();
122 		if (context != null)
123 		{
124 			return context.getExternalContext().getSessionMap().get(key);
125 		}
126 
127 		return super.getSessionAttribute(key);
128 	}
129 
130 	public void setSessionAttribute(String key, Object value)
131 	{
132 		FacesContext context = getFacesContext();
133 		if (context != null)
134 		{
135 			context.getExternalContext().getSessionMap().put(key, value);
136 			return;
137 		}
138 
139 		super.setSessionAttribute(key, value);
140 	}
141 
142 	public boolean containsSessionAttribute(String key)
143 	{
144 		FacesContext context = getFacesContext();
145 		if (context != null)
146 		{
147 			return context.getExternalContext().getSessionMap().containsKey(key);
148 		}
149 
150 		return super.containsSessionAttribute(key);
151 	}
152 
153 	protected String getRequestContextPath()
154 	{
155 		FacesContext context = getFacesContext();
156 		if (context != null)
157 		{
158 			return context.getExternalContext().getRequestContextPath();
159 		}
160 
161 		return super.getRequestContextPath();
162 	}
163 
164 	public void redirect(String url) throws IOException
165 	{
166 		StringBuffer redir = new StringBuffer();
167 		if (url.startsWith("/"))
168 		{
169 			redir.append(getRequestContextPath());
170 		}
171 		redir.append(url);
172 
173 
174 		FacesContext context = getFacesContext();
175 
176 		String actionUrl = context.getExternalContext().encodeActionURL(redir.toString());
177 		context.getExternalContext().redirect(actionUrl);
178 		context.responseComplete();
179 	}
180 
181 	public Object getBean(String name)
182 	{
183 		FacesContext context = getFacesContext();
184 		if (context == null)
185 		{
186 			throw new IllegalStateException("getBean invoked before FacesServlet");
187 		}
188 
189 		return context.getApplication()
190 	        	.getVariableResolver().resolveVariable(context, name);
191 	}
192 
193 	public void invokeNavigation(String navigationName)
194 	{
195 		FacesContext context = getFacesContext();
196 
197 		context.getApplication().getNavigationHandler().handleNavigation(context, null, navigationName);
198 	}
199 }