1 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 ; 28 import java.io.InputStream ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.security.Principal ; 32 import java.util.Collections ; 33 import java.util.Iterator ; 34 import java.util.Locale ; 35 import java.util.Map ; 36 import java.util.Set ; 37 38 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 url) throws IOException { 58 FacesRedirector redirector = (FacesRedirector)request.getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE); 59 if (redirector == null) { 60 throw new IOException ("Can not dispatch to <" + url + ">: Redirector missing."); 61 } 62 63 redirector.dispatch(url); 64 } 65 66 public String encodeActionURL(String url) { 67 FacesRedirector redirector = (FacesRedirector)request.getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE); 68 if (redirector == null) { 69 throw new RuntimeException ("Can not encode action URL <" + url + ">: Redirector missing."); 70 } 71 72 return redirector.encodeActionURL(url); 73 } 74 75 public String encodeNamespace(String ns) { 76 return ns; 77 } 78 79 public String encodeResourceURL(String url) { 80 FacesRedirector redirector = (FacesRedirector)request.getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE); 81 if (redirector == null) { 82 throw new RuntimeException ("Can not encode resource URL <" + url + ">: Redirector missing."); 83 } 84 85 return redirector.encodeResourceURL(url); 86 } 87 88 public Map getApplicationMap() { 89 return new ApplicationMap(this.context); 90 } 91 92 public String getAuthType() { 93 return this.request.getAuthType(); 94 } 95 96 public Object getContext() { 97 return this.context; 98 } 99 100 public String getInitParameter(String parameter) { 101 return this.context.getInitParameter(parameter); 102 } 103 104 public Map getInitParameterMap() { 105 return new InitParameterMap(this.context); 106 } 107 108 public String getRemoteUser() { 109 return this.request.getRemoteUser(); 110 } 111 112 public Object getRequest() { 113 return this.request; 114 } 115 116 public String getRequestContextPath() { 117 return this.request.getContextPath(); 118 } 119 120 public Map getRequestCookieMap() { 121 System.err.println("WARNING: getRequestCookieMap called."); 123 return Collections.EMPTY_MAP; 124 } 125 126 public Map getRequestHeaderMap() { 127 return new RequestHeaderMap(this.request); 128 } 129 130 public Map getRequestHeaderValuesMap() { 131 return new RequestHeaderValuesMap(this.request); 132 } 133 134 public Locale getRequestLocale() { 135 return this.request.getLocale(); 136 } 137 138 public Iterator getRequestLocales() { 139 return new EnumerationIterator(this.request.getLocales()); 140 } 141 142 public Map getRequestMap() { 143 return new RequestMap(this.request); 144 } 145 146 public Map 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 getRequestParameterValuesMap() { 155 return new RequestParameterValuesMap(this.request); 156 } 157 158 public String getRequestPathInfo() { 159 164 169 StringBuffer path = new StringBuffer (); 170 171 boolean slash = false; 172 String 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 getRequestServletPath() { 196 return ""; 198 } 199 200 public URL getResource(String resource) throws MalformedURLException { 201 return this.context.getResource(resource); 202 } 203 204 public InputStream getResourceAsStream(String resource) { 205 return this.context.getResourceAsStream(resource); 206 } 207 208 public Set getResourcePaths(String path) { 209 System.err.println("WARNING: getResourcePaths(" + path + ") called."); 211 throw new UnsupportedOperationException (); 212 } 213 214 public Object getResponse() { 215 return this.response; 216 } 217 218 public Object getSession(boolean create) { 219 return this.request.getSession(create); 220 } 221 222 public Map getSessionMap() { 223 return new SessionMap(request.getSession()); 224 } 225 226 public Principal getUserPrincipal() { 227 return this.request.getUserPrincipal(); 228 } 229 230 public boolean isUserInRole(String role) { 231 return this.request.isUserInRole(role); 232 } 233 234 public void log(String message) { 235 System.err.println("WARNING: log(" + message + ") called."); 237 } 238 239 public void log(String message, Throwable e) { 240 System.err.println("WARNING: log(" + message + ", " + e + ") called."); 242 } 243 244 public void redirect(String url) throws IOException { 245 FacesRedirector redirector = (FacesRedirector)request.getAttribute(FacesAction.REQUEST_REDIRECTOR_ATTRIBUTE); 246 if (redirector == null) { 247 throw new IOException ("Can not redirect to <" + url + ">: Redirector missing."); 248 } 249 250 redirector.redirect(url); 251 } 252 } 253
| Popular Tags
|