KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > context > ExternalContextImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.faces.context;
17
18 import org.apache.cocoon.environment.Context;
19 import org.apache.cocoon.environment.Request;
20 import org.apache.cocoon.environment.Response;
21 import org.apache.cocoon.faces.FacesAction;
22 import org.apache.cocoon.faces.FacesRedirector;
23
24 import org.apache.commons.collections.iterators.EnumerationIterator;
25
26 import javax.faces.context.ExternalContext;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.security.Principal JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Locale JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37
38 /**
39  * Implementation of the Java Server Faces ExternalContext
40  *
41  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
42  * @version CVS $Id: ExternalContextImpl.java 46947 2004-09-20 20:13:51Z vgritsenko $
43  */

44 public class ExternalContextImpl extends ExternalContext {
45
46     private Context context;
47     private Request request;
48     private Response response;
49
50
51     public ExternalContextImpl(Context context, Request request, Response response) {
52         this.context = context;
53         this.request = request;
54         this.response = response;
55     }
56
57     public void dispatch(String JavaDoc url) throws IOException JavaDoc {
58         FacesRedirector redirector = (FacesRedirector)request.getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE);
59         if (redirector == null) {
60             throw new IOException JavaDoc("Can not dispatch to <" + url + ">: Redirector missing.");
61         }
62
63         redirector.dispatch(url);
64     }
65
66     public String JavaDoc encodeActionURL(String JavaDoc url) {
67         FacesRedirector redirector = (FacesRedirector)request.getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE);
68         if (redirector == null) {
69             throw new RuntimeException JavaDoc("Can not encode action URL <" + url + ">: Redirector missing.");
70         }
71
72         return redirector.encodeActionURL(url);
73     }
74
75     public String JavaDoc encodeNamespace(String JavaDoc ns) {
76         return ns;
77     }
78
79     public String JavaDoc encodeResourceURL(String JavaDoc url) {
80         FacesRedirector redirector = (FacesRedirector)request.getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE);
81         if (redirector == null) {
82             throw new RuntimeException JavaDoc("Can not encode resource URL <" + url + ">: Redirector missing.");
83         }
84
85         return redirector.encodeResourceURL(url);
86     }
87
88     public Map JavaDoc getApplicationMap() {
89         return new ApplicationMap(this.context);
90     }
91
92     public String JavaDoc getAuthType() {
93         return this.request.getAuthType();
94     }
95
96     public Object JavaDoc getContext() {
97         return this.context;
98     }
99
100     public String JavaDoc getInitParameter(String JavaDoc parameter) {
101         return this.context.getInitParameter(parameter);
102     }
103
104     public Map JavaDoc getInitParameterMap() {
105         return new InitParameterMap(this.context);
106     }
107
108     public String JavaDoc getRemoteUser() {
109         return this.request.getRemoteUser();
110     }
111
112     public Object JavaDoc getRequest() {
113         return this.request;
114     }
115
116     public String JavaDoc getRequestContextPath() {
117         return this.request.getContextPath();
118     }
119
120     public Map JavaDoc getRequestCookieMap() {
121         // TODO: Implement getRequestCookieMap
122
System.err.println("WARNING: getRequestCookieMap called.");
123         return Collections.EMPTY_MAP;
124     }
125
126     public Map JavaDoc getRequestHeaderMap() {
127         return new RequestHeaderMap(this.request);
128     }
129
130     public Map JavaDoc getRequestHeaderValuesMap() {
131         return new RequestHeaderValuesMap(this.request);
132     }
133
134     public Locale JavaDoc getRequestLocale() {
135         return this.request.getLocale();
136     }
137
138     public Iterator getRequestLocales() {
139         return new EnumerationIterator(this.request.getLocales());
140     }
141
142     public Map JavaDoc getRequestMap() {
143         return new RequestMap(this.request);
144     }
145
146     public Map JavaDoc getRequestParameterMap() {
147         return new RequestParameterMap(this.request);
148     }
149
150     public Iterator getRequestParameterNames() {
151         return new EnumerationIterator(this.request.getParameterNames());
152     }
153
154     public Map JavaDoc getRequestParameterValuesMap() {
155         return new RequestParameterValuesMap(this.request);
156     }
157
158     public String JavaDoc getRequestPathInfo() {
159         // HACK (VG):
160
// Emulate servlet prefix mapping. This allows to bypass suffix mapping calculations,
161
// as well as helps with WebSphere servlet container bugs (it treats default servlet
162
// as prefix mapped servlet).
163

164         // JSF Specification, 2.2.1 Restore View:
165
// o Derive the view identifier that corresponds to this request, as follows:
166
// o If prefix mapping (such as ?/faces/*?) is used for FacesServlet, the viewId
167
// is set from the extra path information of the request URI.
168

169         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
170
171         boolean slash = false;
172         String JavaDoc s = request.getServletPath();
173         if (s != null) {
174             path.append(s);
175             slash = s.endsWith("/");
176         }
177
178         s = request.getPathInfo();
179         if (s != null) {
180             if (s.startsWith("/")) {
181                 if (slash){
182                     s = s.substring(1);
183                 }
184             } else {
185                 if (!slash) {
186                     path.append('/');
187                 }
188             }
189             path.append(s);
190         }
191
192         return path.toString();
193     }
194
195     public String JavaDoc getRequestServletPath() {
196         // See #getRequestPathInfo
197
return "";
198     }
199
200     public URL JavaDoc getResource(String JavaDoc resource) throws MalformedURLException JavaDoc {
201         return this.context.getResource(resource);
202     }
203
204     public InputStream JavaDoc getResourceAsStream(String JavaDoc resource) {
205         return this.context.getResourceAsStream(resource);
206     }
207
208     public Set JavaDoc getResourcePaths(String JavaDoc path) {
209         // TODO: Implement getResourcePaths
210
System.err.println("WARNING: getResourcePaths(" + path + ") called.");
211         throw new UnsupportedOperationException JavaDoc();
212     }
213
214     public Object JavaDoc getResponse() {
215         return this.response;
216     }
217
218     public Object JavaDoc getSession(boolean create) {
219         return this.request.getSession(create);
220     }
221
222     public Map JavaDoc getSessionMap() {
223         return new SessionMap(request.getSession());
224     }
225
226     public Principal JavaDoc getUserPrincipal() {
227         return this.request.getUserPrincipal();
228     }
229
230     public boolean isUserInRole(String JavaDoc role) {
231         return this.request.isUserInRole(role);
232     }
233
234     public void log(String JavaDoc message) {
235         // TODO: Implement
236
System.err.println("WARNING: log(" + message + ") called.");
237     }
238
239     public void log(String JavaDoc message, Throwable JavaDoc e) {
240         // TODO: Implement
241
System.err.println("WARNING: log(" + message + ", " + e + ") called.");
242     }
243
244     public void redirect(String JavaDoc url) throws IOException JavaDoc {
245         FacesRedirector redirector = (FacesRedirector)request.getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE);
246         if (redirector == null) {
247             throw new IOException JavaDoc("Can not redirect to <" + url + ">: Redirector missing.");
248         }
249
250         redirector.redirect(url);
251     }
252 }
253
Popular Tags