1 17 package org.apache.geronimo.tomcat; 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.ServletException ; 28 29 import org.apache.catalina.authenticator.BasicAuthenticator; 30 import org.apache.catalina.authenticator.DigestAuthenticator; 31 import org.apache.catalina.authenticator.SSLAuthenticator; 32 import org.apache.catalina.connector.Request; 33 import org.apache.catalina.connector.Response; 34 import org.apache.catalina.core.StandardContext; 35 import org.apache.catalina.deploy.LoginConfig; 36 import org.apache.catalina.deploy.SecurityCollection; 37 import org.apache.catalina.deploy.SecurityConstraint; 38 import org.apache.catalina.valves.ValveBase; 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 import org.apache.geronimo.tomcat.realm.TomcatEJBWSGeronimoRealm; 42 import org.apache.geronimo.webservices.WebServiceContainer; 43 44 public class TomcatEJBWebServiceContext extends StandardContext{ 45 46 private static final Log log = LogFactory.getLog(TomcatEJBWebServiceContext.class); 47 48 private final String contextPath; 49 private final WebServiceContainer webServiceContainer; 50 private final boolean isSecureTransportGuarantee; 51 private final ClassLoader classLoader; 52 53 public TomcatEJBWebServiceContext(String contextPath, WebServiceContainer webServiceContainer, String securityRealmName, String realmName, String transportGuarantee, String authMethod, ClassLoader classLoader) { 54 55 super(); 56 57 this.contextPath = contextPath; 58 this.webServiceContainer = webServiceContainer; 59 this.setPath(contextPath); 60 this.setDocBase(""); 61 this.setParentClassLoader(classLoader); 62 this.setDelegate(true); 63 64 log.debug("EJB Webservice Context = " + contextPath); 65 if (securityRealmName != null) { 66 67 TomcatEJBWSGeronimoRealm realm = new TomcatEJBWSGeronimoRealm(); 68 realm.setAppName(securityRealmName); 69 realm.setUserClassNames("org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal"); 70 realm.setRoleClassNames("org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal"); 71 setRealm(realm); 72 this.realm = realm; 73 74 if ("NONE".equals(transportGuarantee)) { 75 isSecureTransportGuarantee = false; 76 } else if ("INTEGRAL".equals(transportGuarantee) || 77 "CONFIDENTIAL".equals(transportGuarantee)) { 78 isSecureTransportGuarantee = true; 79 } else { 80 throw new IllegalArgumentException ("Invalid transport-guarantee: " + transportGuarantee); 81 } 82 83 if ("BASIC".equals(authMethod) || 84 "DIGEST".equals(authMethod) || 85 "CLIENT-CERT".equals(authMethod)) { 86 87 LoginConfig loginConfig = new LoginConfig(); 89 loginConfig.setAuthMethod(authMethod); 90 loginConfig.setRealmName(realmName); 91 this.setLoginConfig(loginConfig); 92 93 SecurityCollection collection = new SecurityCollection(); 95 collection.addMethod("GET"); 96 collection.addMethod("POST"); 97 collection.addPattern("/*"); 98 collection.setName("default"); 99 SecurityConstraint sc = new SecurityConstraint(); 100 sc.addAuthRole("*"); 101 sc.addCollection(collection); 102 sc.setAuthConstraint(true); 103 sc.setUserConstraint(transportGuarantee); 104 this.addConstraint(sc); 105 this.addSecurityRole("default"); 106 107 if ("BASIC".equals(authMethod) ){ 109 this.addValve(new BasicAuthenticator()); 110 } else if ("DIGEST".equals(authMethod) ){ 111 this.addValve(new DigestAuthenticator()); 112 } else if ("CLIENT-CERT".equals(authMethod) ){ 113 this.addValve(new SSLAuthenticator()); 114 } 115 116 } else { 117 throw new IllegalArgumentException ("Invalid authMethod: " + authMethod); 118 } 119 } else { 120 isSecureTransportGuarantee = false; 121 } 122 this.classLoader = classLoader; 123 this.addValve(new EJBWebServiceValve()); 124 125 } 126 127 public class EJBWebServiceValve extends ValveBase{ 128 129 public void invoke(Request req, Response res) throws IOException , ServletException { 130 req.setContentType("text/xml"); 131 RequestAdapter request = new RequestAdapter(req); 132 ResponseAdapter response = new ResponseAdapter(res); 133 req.finishRequest(); 134 if (req.getParameter("wsdl") != null) { 135 try { 136 webServiceContainer.getWsdl(request, response); 137 } catch (IOException e) { 139 throw e; 140 } catch (Exception e) { 141 log.error(e); 142 res.sendError(500,"Could not fetch wsdl!"); 143 return; 144 } 145 } else { 146 if (isSecureTransportGuarantee) { 147 if (!req.isSecure()) { 148 res.sendError(403); 149 return; 150 } 151 } 152 Thread currentThread = Thread.currentThread(); 153 ClassLoader oldClassLoader = currentThread.getContextClassLoader(); 154 currentThread.setContextClassLoader(classLoader); 155 try { 156 try { 157 webServiceContainer.invoke(request, response); 158 req.finishRequest(); 159 } catch (IOException e) { 160 throw e; 161 } catch (Exception e) { 162 res.sendError(500, "Could not process message!"); 163 } 164 } finally { 165 currentThread.setContextClassLoader(oldClassLoader); 166 } 167 } 168 } 169 170 } 171 172 public static class RequestAdapter implements WebServiceContainer.Request { 173 private final Request request; 174 private URI uri; 175 176 public RequestAdapter(Request request) { 177 this.request = request; 178 } 179 180 public String getHeader(String name) { 181 return request.getHeader(name); 182 } 183 184 public java.net.URI getURI() { 185 if (uri == null) { 186 try { 187 String uriString = request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort() + request.getRequestURI(); 188 uri = new java.net.URI (uriString); 190 } catch (URISyntaxException e) { 191 throw new IllegalStateException (e.getMessage()); 192 } 193 } 194 return uri; 195 } 196 197 public int getContentLength() { 198 return request.getContentLength(); 199 } 200 201 public String getContentType() { 202 return request.getContentType(); 203 } 204 205 public InputStream getInputStream() throws IOException { 206 return request.getInputStream(); 207 } 208 209 public int getMethod() { 210 Integer method = (Integer ) methods.get(request.getMethod()); 211 return method == null ? UNSUPPORTED : method.intValue(); 212 } 213 214 public String getParameter(String name) { 215 return request.getParameter(name); 216 } 217 218 public Map getParameters() { 219 return request.getParameterMap(); 220 } 221 222 public Object getAttribute(String name) { 223 return request.getAttribute(name); 224 } 225 226 public void setAttribute(String name, Object value) { 227 request.setAttribute(name, value); 228 } 229 230 231 private static final Map methods = new HashMap (); 232 233 static { 234 methods.put("OPTIONS", new Integer (OPTIONS)); 235 methods.put("GET", new Integer (GET)); 236 methods.put("HEAD", new Integer (HEAD)); 237 methods.put("POST", new Integer (POST)); 238 methods.put("PUT", new Integer (PUT)); 239 methods.put("DELETE", new Integer (DELETE)); 240 methods.put("TRACE", new Integer (TRACE)); 241 methods.put("CONNECT", new Integer (CONNECT)); 242 } 243 244 } 245 246 public static class ResponseAdapter implements WebServiceContainer.Response { 247 private final Response response; 248 249 public ResponseAdapter(Response response) { 250 this.response = response; 251 } 252 253 public void setHeader(String name, String value) { 254 response.setHeader(name, value); 255 } 256 257 public String getHeader(String name) { 258 return response.getHeader(name); 259 } 260 261 public OutputStream getOutputStream() { 262 return response.getStream(); 263 } 264 265 public void setStatusCode(int code) { 266 response.setStatus(code); 267 } 268 269 public int getStatusCode() { 270 return response.getStatus(); 271 } 272 273 public void setContentType(String type) { 274 response.setContentType(type); 275 } 276 277 public String getContentType() { 278 return response.getContentType(); 279 } 280 281 public void setStatusMessage(String responseString) { 282 response.setStatus(response.getStatus(), responseString); 283 } 284 } 285 286 } 287 | Popular Tags |