1 17 package org.apache.geronimo.jetty; 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 org.apache.geronimo.webservices.WebServiceContainer; 28 import org.mortbay.http.HttpContext; 29 import org.mortbay.http.HttpException; 30 import org.mortbay.http.HttpHandler; 31 import org.mortbay.http.HttpRequest; 32 import org.mortbay.http.HttpResponse; 33 34 58 public class JettyEJBWebServiceContext extends HttpContext implements HttpHandler { 59 60 private final String contextPath; 61 private final WebServiceContainer webServiceContainer; 62 63 private HttpContext httpContext; 64 65 public JettyEJBWebServiceContext(String contextPath, WebServiceContainer webServiceContainer) { 66 this.contextPath = contextPath; 67 this.webServiceContainer = webServiceContainer; 68 } 69 70 public String getName() { 71 return contextPath; 73 } 74 75 public HttpContext getHttpContext() { 76 return httpContext; 77 } 78 79 public void initialize(HttpContext httpContext) { 80 this.httpContext = httpContext; 81 } 82 83 public void handle(HttpRequest req, HttpResponse res) throws HttpException, IOException { 84 req.setContentType("text/xml"); 85 RequestAdapter request = new RequestAdapter(req); 86 ResponseAdapter response = new ResponseAdapter(res); 87 88 if (req.getParameter("wsdl") != null) { 89 try { 90 webServiceContainer.getWsdl(request,response); 91 } catch (IOException e) { 92 throw e; 93 } catch (Exception e) { 94 throw (HttpException) new HttpException(500, "Could not fetch wsdl!").initCause(e); 95 } 96 } else { 97 try { 98 webServiceContainer.invoke(request,response); 99 req.setHandled(true); 100 } catch (IOException e) { 101 throw e; 102 } catch (Exception e) { 103 throw (HttpException) new HttpException(500, "Could not process message!").initCause(e); 104 } 105 } 106 107 } 108 109 public String getContextPath() { 110 return contextPath; 111 } 112 113 public static class RequestAdapter implements WebServiceContainer.Request { 114 private final HttpRequest request; 115 private URI uri; 116 117 public RequestAdapter(HttpRequest request) { 118 this.request = request; 119 } 120 121 public String getHeader(String name) { 122 return request.getField(name); 123 } 124 125 public java.net.URI getURI() { 126 if( uri==null ) { 127 try { 128 String uriString = request.getScheme()+"://"+request.getHost()+":"+request.getPort()+request.getURI(); 129 uri = new java.net.URI (uriString); 131 } catch (URISyntaxException e) { 132 throw new IllegalStateException (e.getMessage()); 133 } 134 } 135 return uri; 136 } 137 138 public int getContentLength() { 139 return request.getContentLength(); 140 } 141 142 public String getContentType() { 143 return request.getContentType(); 144 } 145 146 public InputStream getInputStream() throws IOException { 147 return request.getInputStream(); 148 } 149 150 public int getMethod() { 151 Integer method = (Integer ) methods.get(request.getMethod()); 152 return method == null ? UNSUPPORTED: method.intValue(); 153 } 154 155 public String getParameter(String name) { 156 return request.getParameter(name); 157 } 158 159 public Map getParameters() { 160 return request.getParameters(); 161 } 162 163 public Object getAttribute(String name) { 164 return request.getAttribute(name); 165 } 166 167 public void setAttribute(String name, Object value){ 168 request.setAttribute(name, value); 169 } 170 171 172 private static final Map methods = new HashMap (); 173 174 static { 175 methods.put("OPTIONS", new Integer (OPTIONS)); 176 methods.put("GET", new Integer (GET)); 177 methods.put("HEAD", new Integer (HEAD)); 178 methods.put("POST", new Integer (POST)); 179 methods.put("PUT", new Integer (PUT)); 180 methods.put("DELETE", new Integer (DELETE)); 181 methods.put("TRACE", new Integer (TRACE)); 182 methods.put("CONNECT", new Integer (CONNECT)); 183 } 184 185 } 186 187 public static class ResponseAdapter implements WebServiceContainer.Response { 188 private final HttpResponse response; 189 190 public ResponseAdapter(HttpResponse response) { 191 this.response = response; 192 } 193 194 public void setHeader(String name, String value) { 195 response.setField(name, value); 196 } 197 198 public String getHeader(String name) { 199 return response.getField(name); 200 } 201 202 public OutputStream getOutputStream() { 203 return response.getOutputStream(); 204 } 205 206 public void setStatusCode(int code) { 207 response.setStatus(code); 208 } 209 210 public int getStatusCode() { 211 return response.getStatus(); 212 } 213 214 public void setContentType(String type) { 215 response.setContentType(type); 216 } 217 218 public String getContentType() { 219 return response.getContentType(); 220 } 221 222 public void setStatusMessage(String responseString) { 223 response.setStatus(response.getStatus(), responseString); 224 } 225 } 226 227 } 228 | Popular Tags |