1 17 package org.apache.geronimo.jetty6; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.net.URI ; 23 import java.net.URISyntaxException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 import javax.servlet.ServletException ; 30 31 import org.apache.geronimo.security.ContextManager; 32 import org.apache.geronimo.webservices.WebServiceContainer; 33 import org.mortbay.jetty.HttpException; 34 import org.mortbay.jetty.Request; 35 import org.mortbay.jetty.Response; 36 import org.mortbay.jetty.handler.ContextHandler; 37 import org.mortbay.jetty.security.Authenticator; 38 import org.mortbay.jetty.security.BasicAuthenticator; 39 import org.mortbay.jetty.security.ClientCertAuthenticator; 40 import org.mortbay.jetty.security.DigestAuthenticator; 41 42 66 public class JettyEJBWebServiceContext extends ContextHandler { 67 68 private final String contextPath; 69 private final WebServiceContainer webServiceContainer; 70 private final Authenticator authenticator; 71 private final JAASJettyRealm realm; 72 private final boolean isConfidentialTransportGuarantee; 73 private final boolean isIntegralTransportGuarantee; 74 private final ClassLoader classLoader; 75 76 77 public JettyEJBWebServiceContext(String contextPath, WebServiceContainer webServiceContainer, InternalJAASJettyRealm internalJAASJettyRealm, String realmName, String transportGuarantee, String authMethod, ClassLoader classLoader) { 78 this.contextPath = contextPath; 79 this.webServiceContainer = webServiceContainer; 80 if (internalJAASJettyRealm != null) { 81 realm = new JAASJettyRealm(realmName, internalJAASJettyRealm); 82 if ("NONE".equals(transportGuarantee)) { 87 isConfidentialTransportGuarantee = false; 88 isIntegralTransportGuarantee = false; 89 } else if ("INTEGRAL".equals(transportGuarantee)) { 90 isConfidentialTransportGuarantee = false; 91 isIntegralTransportGuarantee = true; 92 } else if ("CONFIDENTIAL".equals(transportGuarantee)) { 93 isConfidentialTransportGuarantee = true; 94 isIntegralTransportGuarantee = false; 95 } else { 96 throw new IllegalArgumentException ("Invalid transport-guarantee: " + transportGuarantee); 97 } 98 if ("BASIC".equals(authMethod)) { 99 authenticator = new BasicAuthenticator(); 100 } else if ("DIGEST".equals(authMethod)) { 101 authenticator = new DigestAuthenticator(); 102 } else if ("CLIENT-CERT".equals(authMethod)) { 103 authenticator = new ClientCertAuthenticator(); 104 } else if ("NONE".equals(authMethod)) { 105 authenticator = null; 106 } else { 107 throw new IllegalArgumentException ("Invalid authMethod: " + authMethod); 108 } 109 } else { 110 realm = null; 111 authenticator = null; 112 isConfidentialTransportGuarantee = false; 113 isIntegralTransportGuarantee = false; 114 } 115 this.classLoader = classLoader; 116 } 117 118 public String getName() { 119 return contextPath; 121 } 122 123 public void handle(String target, HttpServletRequest req, HttpServletResponse res, int dispatch) 124 throws IOException , ServletException 125 { 126 130 if (! target.startsWith(contextPath)) { 133 return; 134 } 135 Request jettyRequest = (Request ) req; 136 Response jettyResponse = (Response ) res; 137 RequestAdapter request = new RequestAdapter(jettyRequest); 138 ResponseAdapter response = new ResponseAdapter(jettyResponse); 139 140 if (req.getParameter("wsdl") != null) { 141 try { 142 webServiceContainer.getWsdl(request, response); 143 } catch (IOException e) { 145 throw e; 146 } catch (Exception e) { 147 throw (HttpException) new HttpException(500, "Could not fetch wsdl!").initCause(e); 148 } 149 } else { 150 if (isConfidentialTransportGuarantee) { 151 if (!req.isSecure()) { 152 throw new HttpException(403, null); 153 } 154 } else if (isIntegralTransportGuarantee) { 155 if (!jettyRequest.getConnection().isIntegral(jettyRequest)) { 156 throw new HttpException(403, null); 157 } 158 } 159 Thread currentThread = Thread.currentThread(); 160 ClassLoader oldClassLoader = currentThread.getContextClassLoader(); 161 currentThread.setContextClassLoader(classLoader); 162 try { 165 if (authenticator != null) { 166 String pathInContext = org.mortbay.util.URIUtil.canonicalPath(req.getContextPath()); 167 if (authenticator.authenticate(realm, pathInContext, jettyRequest, jettyResponse) == null) { 168 throw new HttpException(403, null); 169 } 170 } else { 171 ContextManager.popCallers(null); 175 } 176 try { 177 webServiceContainer.invoke(request, response); 178 jettyRequest.setHandled(true); 179 } catch (IOException e) { 180 throw e; 181 } catch (Exception e) { 182 throw (HttpException) new HttpException(500, "Could not process message!").initCause(e); 183 } 184 } finally { 185 currentThread.setContextClassLoader(oldClassLoader); 187 } 188 } 189 190 } 191 192 public String getContextPath() { 193 return contextPath; 194 } 195 196 public String getSecurityRealmName() { 197 if (realm == null) { 198 return null; 199 } else { 200 return realm.getSecurityRealmName(); 201 } 202 } 203 204 public static class RequestAdapter implements WebServiceContainer.Request { 205 private final Request request; 206 private URI uri; 207 208 public RequestAdapter(Request request) { 209 this.request = request; 210 } 211 212 public String getHeader(String name) { 213 return request.getHeader(name); 214 } 215 216 public java.net.URI getURI() { 217 if (uri == null) { 218 try { 219 String uriString = request.getScheme() + "://" + request.getRemoteHost() + ":" + request.getRemotePort() + request.getRequestURI(); 220 uri = new java.net.URI (uriString); 222 } catch (URISyntaxException e) { 223 throw new IllegalStateException (e.getMessage()); 224 } 225 } 226 return uri; 227 } 228 229 public int getContentLength() { 230 return request.getContentLength(); 231 } 232 233 public String getContentType() { 234 return request.getContentType(); 235 } 236 237 public InputStream getInputStream() throws IOException { 238 return request.getInputStream(); 239 } 240 241 public int getMethod() { 242 Integer method = (Integer ) methods.get(request.getMethod()); 243 return method == null ? UNSUPPORTED : method.intValue(); 244 } 245 246 public String getParameter(String name) { 247 return request.getParameter(name); 248 } 249 250 public Map getParameters() { 251 return request.getParameterMap(); 252 } 253 254 public Object getAttribute(String name) { 255 return request.getAttribute(name); 256 } 257 258 public void setAttribute(String name, Object value) { 259 request.setAttribute(name, value); 260 } 261 262 263 private static final Map methods = new HashMap (); 264 265 static { 266 methods.put("OPTIONS", new Integer (OPTIONS)); 267 methods.put("GET", new Integer (GET)); 268 methods.put("HEAD", new Integer (HEAD)); 269 methods.put("POST", new Integer (POST)); 270 methods.put("PUT", new Integer (PUT)); 271 methods.put("DELETE", new Integer (DELETE)); 272 methods.put("TRACE", new Integer (TRACE)); 273 methods.put("CONNECT", new Integer (CONNECT)); 274 } 275 276 } 277 278 public static class ResponseAdapter implements WebServiceContainer.Response { 279 private final Response response; 280 281 public ResponseAdapter(Response response) { 282 this.response = response; 283 } 284 285 public void setHeader(String name, String value) { 286 response.setHeader(name, value); 287 } 288 289 public String getHeader(String name) { 290 return response.getHeader(name); 291 } 292 293 public OutputStream getOutputStream() { 294 try { 295 return response.getOutputStream(); 296 } catch (IOException e) { 297 throw new IllegalStateException (e.getMessage()); 298 } 299 } 300 301 public void setStatusCode(int code) { 302 response.setStatus(code); 303 } 304 305 public int getStatusCode() { 306 return response.getStatus(); 307 } 308 309 public void setContentType(String type) { 310 response.setContentType(type); 311 } 312 313 public String getContentType() { 314 return response.getContentType(); 315 } 316 317 public void setStatusMessage(String responseString) { 318 response.setStatus(response.getStatus(), responseString); 319 } 320 } 321 322 } 323 | Popular Tags |