001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.faces.context;
018:
019: import org.apache.cocoon.environment.Context;
020: import org.apache.cocoon.environment.Request;
021: import org.apache.cocoon.environment.Response;
022: import org.apache.cocoon.faces.FacesAction;
023: import org.apache.cocoon.faces.FacesRedirector;
024:
025: import org.apache.commons.collections.iterators.EnumerationIterator;
026:
027: import javax.faces.context.ExternalContext;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.security.Principal;
033: import java.util.Collections;
034: import java.util.Iterator;
035: import java.util.Locale;
036: import java.util.Map;
037: import java.util.Set;
038:
039: /**
040: * Implementation of the Java Server Faces ExternalContext
041: *
042: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
043: * @version CVS $Id: ExternalContextImpl.java 433543 2006-08-22 06:22:54Z crossley $
044: */
045: public class ExternalContextImpl extends ExternalContext {
046:
047: private Context context;
048: private Request request;
049: private Response response;
050:
051: public ExternalContextImpl(Context context, Request request,
052: Response response) {
053: this .context = context;
054: this .request = request;
055: this .response = response;
056: }
057:
058: public void dispatch(String url) throws IOException {
059: FacesRedirector redirector = (FacesRedirector) request
060: .getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE);
061: if (redirector == null) {
062: throw new IOException("Can not dispatch to <" + url
063: + ">: Redirector missing.");
064: }
065:
066: redirector.dispatch(url);
067: }
068:
069: public String encodeActionURL(String url) {
070: FacesRedirector redirector = (FacesRedirector) request
071: .getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE);
072: if (redirector == null) {
073: throw new RuntimeException("Can not encode action URL <"
074: + url + ">: Redirector missing.");
075: }
076:
077: return redirector.encodeActionURL(url);
078: }
079:
080: public String encodeNamespace(String ns) {
081: return ns;
082: }
083:
084: public String encodeResourceURL(String url) {
085: FacesRedirector redirector = (FacesRedirector) request
086: .getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE);
087: if (redirector == null) {
088: throw new RuntimeException("Can not encode resource URL <"
089: + url + ">: Redirector missing.");
090: }
091:
092: return redirector.encodeResourceURL(url);
093: }
094:
095: public Map getApplicationMap() {
096: return new ApplicationMap(this .context);
097: }
098:
099: public String getAuthType() {
100: return this .request.getAuthType();
101: }
102:
103: public Object getContext() {
104: return this .context;
105: }
106:
107: public String getInitParameter(String parameter) {
108: return this .context.getInitParameter(parameter);
109: }
110:
111: public Map getInitParameterMap() {
112: return new InitParameterMap(this .context);
113: }
114:
115: public String getRemoteUser() {
116: return this .request.getRemoteUser();
117: }
118:
119: public Object getRequest() {
120: return this .request;
121: }
122:
123: public String getRequestContextPath() {
124: return this .request.getContextPath();
125: }
126:
127: public Map getRequestCookieMap() {
128: // TODO: Implement getRequestCookieMap
129: System.err.println("WARNING: getRequestCookieMap called.");
130: return Collections.EMPTY_MAP;
131: }
132:
133: public Map getRequestHeaderMap() {
134: return new RequestHeaderMap(this .request);
135: }
136:
137: public Map getRequestHeaderValuesMap() {
138: return new RequestHeaderValuesMap(this .request);
139: }
140:
141: public Locale getRequestLocale() {
142: return this .request.getLocale();
143: }
144:
145: public Iterator getRequestLocales() {
146: return new EnumerationIterator(this .request.getLocales());
147: }
148:
149: public Map getRequestMap() {
150: return new RequestMap(this .request);
151: }
152:
153: public Map getRequestParameterMap() {
154: return new RequestParameterMap(this .request);
155: }
156:
157: public Iterator getRequestParameterNames() {
158: return new EnumerationIterator(this .request.getParameterNames());
159: }
160:
161: public Map getRequestParameterValuesMap() {
162: return new RequestParameterValuesMap(this .request);
163: }
164:
165: public String getRequestPathInfo() {
166: // HACK (VG):
167: // Emulate servlet prefix mapping. This allows to bypass suffix mapping calculations,
168: // as well as helps with WebSphere servlet container bugs (it treats default servlet
169: // as prefix mapped servlet).
170:
171: // JSF Specification, 2.2.1 Restore View:
172: // o Derive the view identifier that corresponds to this request, as follows:
173: // o If prefix mapping (such as ?/faces/*?) is used for FacesServlet, the viewId
174: // is set from the extra path information of the request URI.
175:
176: StringBuffer path = new StringBuffer();
177:
178: boolean slash = false;
179: String s = request.getServletPath();
180: if (s != null) {
181: path.append(s);
182: slash = s.endsWith("/");
183: }
184:
185: s = request.getPathInfo();
186: if (s != null) {
187: if (s.startsWith("/")) {
188: if (slash) {
189: s = s.substring(1);
190: }
191: } else {
192: if (!slash) {
193: path.append('/');
194: }
195: }
196: path.append(s);
197: }
198:
199: return path.toString();
200: }
201:
202: public String getRequestServletPath() {
203: // See #getRequestPathInfo
204: return "";
205: }
206:
207: public URL getResource(String resource)
208: throws MalformedURLException {
209: return this .context.getResource(resource);
210: }
211:
212: public InputStream getResourceAsStream(String resource) {
213: return this .context.getResourceAsStream(resource);
214: }
215:
216: public Set getResourcePaths(String path) {
217: // TODO: Implement getResourcePaths
218: System.err.println("WARNING: getResourcePaths(" + path
219: + ") called.");
220: throw new UnsupportedOperationException();
221: }
222:
223: public Object getResponse() {
224: return this .response;
225: }
226:
227: public Object getSession(boolean create) {
228: return this .request.getSession(create);
229: }
230:
231: public Map getSessionMap() {
232: return new SessionMap(request.getSession());
233: }
234:
235: public Principal getUserPrincipal() {
236: return this .request.getUserPrincipal();
237: }
238:
239: public boolean isUserInRole(String role) {
240: return this .request.isUserInRole(role);
241: }
242:
243: public void log(String message) {
244: // TODO: Implement
245: System.err.println("WARNING: log(" + message + ") called.");
246: }
247:
248: public void log(String message, Throwable e) {
249: // TODO: Implement
250: System.err.println("WARNING: log(" + message + ", " + e
251: + ") called.");
252: }
253:
254: public void redirect(String url) throws IOException {
255: FacesRedirector redirector = (FacesRedirector) request
256: .getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE);
257: if (redirector == null) {
258: throw new IOException("Can not redirect to <" + url
259: + ">: Redirector missing.");
260: }
261:
262: redirector.redirect(url);
263: }
264: }
|