1 23 24 package com.sun.enterprise.webservice; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.Collection ; 29 import java.util.Map ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 35 import javax.servlet.*; 36 import javax.servlet.http.*; 37 38 import com.sun.enterprise.webservice.monitoring.WebServiceEngineImpl; 39 import com.sun.enterprise.webservice.monitoring.HttpResponseInfoImpl; 40 import com.sun.enterprise.webservice.monitoring.ThreadLocalInfo; 41 import com.sun.enterprise.webservice.monitoring.JAXWSEndpointImpl; 42 import com.sun.enterprise.webservice.monitoring.WebServiceTesterServlet; 43 import com.sun.enterprise.webservice.monitoring.Endpoint; 44 import com.sun.enterprise.webservice.monitoring.EndpointType; 45 import com.sun.enterprise.webservice.monitoring.EndpointImpl; 46 47 import com.sun.enterprise.Switch; 48 import com.sun.enterprise.ComponentInvocation; 49 import com.sun.enterprise.InvocationManager; 50 import com.sun.enterprise.deployment.WebServicesDescriptor; 51 import com.sun.enterprise.deployment.WebService; 52 import com.sun.enterprise.deployment.WebServiceEndpoint; 53 import com.sun.enterprise.deployment.WebBundleDescriptor; 54 import com.sun.enterprise.deployment.WebComponentDescriptor; 55 import com.sun.enterprise.deployment.ResourceReferenceDescriptor; 56 57 import com.sun.enterprise.util.InjectionManagerImpl; 58 59 import com.sun.enterprise.security.jauth.ServerAuthConfig; 60 61 import com.sun.xml.ws.spi.runtime.WSRtObjectFactory; 62 import com.sun.xml.ws.spi.runtime.RuntimeEndpointInfo; 63 import com.sun.xml.ws.spi.runtime.SystemHandlerDelegate; 64 import com.sun.xml.ws.spi.runtime.WSConnection; 65 import com.sun.xml.ws.spi.runtime.WebServiceContext; 66 import javax.xml.ws.handler.MessageContext; 67 import javax.xml.ws.handler.MessageContext.Scope; 68 import javax.xml.ws.soap.SOAPBinding; 69 import javax.xml.ws.http.HTTPBinding; 70 71 import org.apache.catalina.Loader; 73 74 import java.util.logging.Logger ; 75 import java.util.logging.Level ; 76 import com.sun.logging.LogDomains; 77 78 82 public class JAXWSServlet extends HttpServlet { 83 84 private static Logger logger = LogDomains.getLogger(LogDomains.WEB_LOGGER); 85 private ServletContext servletContext; 86 87 private WebServiceEndpoint endpoint_; 88 89 private WSRtObjectFactory rpcFactory_; 90 91 private String urlPattern; 92 93 private String contextRoot; 94 95 private boolean jaxwsInitDone = false; 96 97 private WebServiceEngineImpl wsEngine_; 98 private JAXWSEndpointImpl endpointImpl_; 99 100 ClassLoader classLoader; 101 102 private com.sun.xml.ws.server.Tie tie = new com.sun.xml.ws.server.Tie(); 103 104 public void init(ServletConfig servletConfig) throws ServletException { 105 try { 106 super.init(servletConfig); 107 this.servletContext = servletConfig.getServletContext(); 108 wsEngine_ = WebServiceEngineImpl.getInstance(); 109 doInit(servletConfig); 111 } catch (Throwable e) { 112 throw new ServletException(e); 113 } 114 } 115 116 public void destroy() { 117 JAXWSRuntimeEpiRegistry.getInstance().removeRuntimeInfo(contextRoot); 118 try { 119 (new WsUtil()).doPreDestroy(endpoint_, classLoader); 120 } catch (Throwable t) { 121 logger.log(Level.WARNING, "@PreDestroy lifecycle call failed for service" 122 + endpoint_.getName(), t); 123 } 124 wsEngine_.removeHandler(endpoint_); 125 } 126 127 protected void doPost(HttpServletRequest request, 128 HttpServletResponse response) 129 throws ServletException { 130 131 if (("Tester".equalsIgnoreCase(request.getQueryString())) && 132 (!(HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding())))) { 133 Endpoint endpt = wsEngine_.getEndpoint(request.getRequestURL().toString()); 134 if (endpt!=null && Boolean.parseBoolean(endpt.getDescriptor().getDebugging())) { 135 WebServiceTesterServlet.invoke(request, response, 136 endpt.getDescriptor()); 137 return; 138 } 139 } 140 141 String messageId=null; 143 if((wsEngine_.getGlobalMessageListener()!=null) && 144 (!(HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding())))) { 145 Endpoint endpt = wsEngine_.getEndpoint(request.getRequestURL().toString()); 146 messageId = wsEngine_.preProcessRequest(endpt); 147 if (messageId!=null) { 148 ThreadLocalInfo config = new ThreadLocalInfo(messageId, request); 149 wsEngine_.getThreadLocal().set(config); 150 } 151 } 152 153 if(!jaxwsInitDone) { 155 synchronized(this) { 157 if(!jaxwsInitDone) { 161 String path = request.getRequestURI().substring(request.getContextPath().length()); 162 JAXWSRuntimeEpiRegistry.getInstance().getRuntimeEndpointInfo(contextRoot, urlPattern, path).init(); 163 jaxwsInitDone = true; 164 } 165 } 166 } 167 168 try { 170 RuntimeEndpointInfo targetEndpoint = getEndpointFor(request); 171 if (targetEndpoint != null) { 172 WSConnection connection = rpcFactory_.createWSConnection(request, response); 173 WebServiceContext wsCtxt = targetEndpoint.getWebServiceContext(); 174 MessageContext msgCtxt = rpcFactory_.createMessageContext(); 175 msgCtxt.put(MessageContext.SERVLET_CONTEXT, servletContext); 176 msgCtxt.setScope(MessageContext.SERVLET_CONTEXT, Scope.APPLICATION); 177 msgCtxt.put(MessageContext.SERVLET_REQUEST, request); 178 msgCtxt.setScope(MessageContext.SERVLET_REQUEST, Scope.APPLICATION); 179 msgCtxt.put(MessageContext.SERVLET_RESPONSE, response); 180 msgCtxt.setScope(MessageContext.SERVLET_RESPONSE, Scope.APPLICATION); 181 msgCtxt.put(EndpointImpl.MESSAGE_ID, messageId); 182 msgCtxt.put(MessageContext.HTTP_REQUEST_METHOD, request.getMethod()); 183 msgCtxt.setScope(MessageContext.HTTP_REQUEST_METHOD, Scope.APPLICATION); 184 if (request.getQueryString() != null) { 185 msgCtxt.put(MessageContext.QUERY_STRING, request.getQueryString()); 186 msgCtxt.setScope(MessageContext.QUERY_STRING, Scope.APPLICATION); 187 } 188 if (request.getPathInfo() != null) { 189 msgCtxt.put(MessageContext.PATH_INFO, request.getPathInfo()); 190 msgCtxt.setScope(MessageContext.PATH_INFO, Scope.APPLICATION); 191 } 192 msgCtxt.put(MessageContext.HTTP_REQUEST_HEADERS, connection.getHeaders()); 193 msgCtxt.setScope(MessageContext.HTTP_REQUEST_HEADERS, Scope.APPLICATION); 194 wsCtxt.setMessageContext(msgCtxt); 195 tie.handle(connection, targetEndpoint); 196 } else { 197 throw new ServletException("Service not found"); 198 } 199 } catch(Throwable t) { 200 ServletException se = new ServletException(); 201 se.initCause(t); 202 throw se; 203 } 204 205 if (messageId!=null) { 206 HttpResponseInfoImpl info = new HttpResponseInfoImpl(response); 207 wsEngine_.postProcessResponse(messageId, info); 208 } 209 } 210 211 protected void doGet(HttpServletRequest request, 212 HttpServletResponse response) 213 throws ServletException, IOException { 214 215 if (("Tester".equalsIgnoreCase(request.getQueryString())) && 216 (!(HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding())))) { 217 218 Endpoint endpt = wsEngine_.getEndpoint(request.getRequestURL().toString()); 219 if( (endpt.getDescriptor().isSecure()) || 220 (endpt.getDescriptor().getMessageSecurityBinding() != null) ) { 221 String message = endpt.getDescriptor().getWebService().getName() + 223 " is a secured webservice; Tester feature is not supported for secured services"; 224 (new WsUtil()).writeInvalidMethodType(response, message); 225 return; 226 } 227 if (endpt!=null && Boolean.parseBoolean(endpt.getDescriptor().getDebugging())) { 228 Loader loader = (Loader) endpt.getDescriptor().getBundleDescriptor().getExtraAttribute("WEBLOADER"); 229 if (loader != null) { 230 endpt.getDescriptor().getBundleDescriptor().setClassLoader(loader.getClassLoader()); 231 endpt.getDescriptor().getBundleDescriptor().removeExtraAttribute("WEBLOADER"); 232 } 233 WebServiceTesterServlet.invoke(request, response, 234 endpt.getDescriptor()); 235 return; 236 } 237 } 238 239 if (!("WSDL".equalsIgnoreCase(request.getQueryString())) && 242 (HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding()))) { 243 doPost(request, response); 244 return; 245 } 246 247 WsUtil wsUtil = new WsUtil(); 249 try { 250 wsUtil.handleGet(request, response, endpoint_); 251 } catch(Exception e) { 252 logger.log(Level.WARNING, "Servlet web service endpoint '" + 253 endpoint_.getEndpointName() + "' HTTP GET error", e); 254 } 255 } 256 257 private void doInit(ServletConfig servletConfig) throws ServletException { 258 String servletName = "unknown"; 259 260 try { 261 InvocationManager invManager = 262 Switch.getSwitch().getInvocationManager(); 263 ComponentInvocation inv = invManager.getCurrentInvocation(); 264 Object containerContext = inv.getContainerContext(); 265 266 WebBundleDescriptor webBundle = (WebBundleDescriptor) 267 Switch.getSwitch().getDescriptorFor(containerContext); 268 classLoader = Thread.currentThread().getContextClassLoader(); 269 servletName = servletConfig.getServletName(); 270 contextRoot = webBundle.getContextRoot(); 271 WebComponentDescriptor webComponent = 272 webBundle.getWebComponentByCanonicalName(servletName); 273 274 if( webComponent != null ) { 275 WebServicesDescriptor webServices = webBundle.getWebServices(); 276 Collection endpoints = 277 webServices.getEndpointsImplementedBy(webComponent); 278 endpoint_ = (WebServiceEndpoint) endpoints.iterator().next(); 281 282 if(!(HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding()))) { 284 endpointImpl_ = (JAXWSEndpointImpl) 286 wsEngine_.createHandler((SystemHandlerDelegate)null, endpoint_); 287 } 288 289 registerEndpoint(); 290 291 } else { 292 throw new ServletException(servletName + " not found"); 293 } 294 } catch(Throwable t) { 295 logger.log(Level.WARNING, "Servlet web service endpoint '" + 296 servletName + "' failure", t); 297 ServletException se = new ServletException(); 298 se.initCause(t); 299 throw se; 300 } 301 } 302 303 private void registerEndpoint() 304 throws Exception { 305 306 311 rpcFactory_ = WSRtObjectFactory.newInstance(); 312 RuntimeEndpointInfo endpointInfo = 313 rpcFactory_.createRuntimeEndpointInfo(); 314 315 Object serviceEndpoint = 316 Class.forName(endpoint_.getServletImplClass(), true, classLoader).newInstance(); 317 318 new InjectionManagerImpl().injectInstance(serviceEndpoint); 320 321 endpointInfo.setImplementorClass(Class.forName(endpoint_.getServletImplClass(), true, classLoader)); 323 endpointInfo.setImplementor(serviceEndpoint); 324 325 WebServiceContext wsc = null; 329 WebBundleDescriptor bundle = (WebBundleDescriptor)endpoint_.getBundleDescriptor(); 330 Iterator <ResourceReferenceDescriptor> it = bundle.getResourceReferenceDescriptors().iterator(); 331 while(it.hasNext()) { 332 ResourceReferenceDescriptor r = it.next(); 333 if(r.isWebServiceContext()) { 334 try { 335 javax.naming.InitialContext ic = new javax.naming.InitialContext (); 336 wsc = (WebServiceContext) ic.lookup("java:comp/env/" + r.getName()); 337 } catch (Throwable t) {} } 339 } 340 if(wsc == null) { 341 wsc = new WebServiceContextImpl(); 342 } 343 endpointInfo.setWebServiceContext(wsc); 344 345 String uri = endpoint_.getEndpointAddressUri(); 348 urlPattern = uri.startsWith("/") ? uri : "/" + uri; 349 350 java.net.URL catalogURL = null; 352 File catalogFile = new File (endpoint_.getBundleDescriptor().getDeploymentDescriptorDir() + 353 File.separator + "jax-ws-catalog.xml"); 354 if(catalogFile.exists()) { 355 catalogURL = catalogFile.toURL(); 356 } 357 endpointInfo.setWsdlInfo(endpoint_.getWebService().getWsdlFileUrl(), 358 rpcFactory_.createEntityResolver(catalogURL)); 359 360 endpointInfo.setServiceName(endpoint_.getServiceName()); 362 endpointInfo.setPortName(endpoint_.getWsdlPort()); 363 364 endpointInfo.setBinding(rpcFactory_.createBinding(endpoint_.getProtocolBinding())); 366 367 WsUtil wsu = new WsUtil(); 369 wsu.setMtom(endpointInfo.getBinding(), endpoint_); 370 371 wsu.configureJAXWSServiceHandlers(endpoint_, endpointInfo); 373 374 SystemHandlerDelegate delegate = 375 JAXWSSystemHandlerDelegateFactory.getServletDelegate(endpoint_); 376 377 if (endpointImpl_ != null) { 379 endpointImpl_.setParent(delegate); 380 endpointInfo.getBinding().setSystemHandlerDelegate(endpointImpl_); 381 } else { 382 endpointInfo.getBinding().setSystemHandlerDelegate(delegate); 383 } 384 385 registerEndpointUrlPattern(endpointInfo); 386 wsu.doPostConstruct(endpointInfo.getImplementorClass(), 387 serviceEndpoint); 388 } 389 390 private void registerEndpointUrlPattern(RuntimeEndpointInfo info) { 391 JAXWSRuntimeEpiRegistry.getInstance(). 392 addRuntimeEndpointInfo(contextRoot, urlPattern,info); 393 } 394 395 private RuntimeEndpointInfo getEndpointFor(HttpServletRequest request) { 396 String path = request.getRequestURI().substring(request.getContextPath().length()); 397 return JAXWSRuntimeEpiRegistry.getInstance(). 398 getRuntimeEndpointInfo(contextRoot, urlPattern, path); 399 } 400 401 } 402 | Popular Tags |