KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > server > ContextServlet


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.webservice.server;
8
9 // $Id: ContextServlet.java,v 1.1.2.2 2005/03/02 14:32:32 tdiesler Exp $
10

11 import org.jboss.axis.AxisEngine;
12 import org.jboss.axis.AxisFault;
13 import org.jboss.axis.MessageContext;
14 import org.jboss.axis.handlers.soap.SOAPService;
15 import org.jboss.logging.Logger;
16
17 import javax.servlet.ServletException JavaDoc;
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22
23 /**
24  * The servlet that that is associated with context /ws4ee
25  *
26  * It manages the service list and the 'Version' service
27  *
28  * @author Thomas.Diesler@jboss.org
29  * @since 09-Feb-2005
30  */

31 public class ContextServlet extends AbstractServlet
32 {
33    // provide logging
34
protected final Logger log = Logger.getLogger(ContextServlet.class);
35
36    /** Denies access to the generic Axis way of accessing services
37     *
38     * /context-root/services/ServiceName
39     *
40     * J2EE web services are deployed within their own context-root that may have authentication
41     * requirements.
42     */

43    private boolean assertServiceAccess(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
44            throws IOException JavaDoc
45    {
46       String JavaDoc contextPath = req.getContextPath();
47       String JavaDoc requestURI = req.getRequestURI();
48       String JavaDoc pathInfo = req.getPathInfo();
49
50       if (requestURI.startsWith(contextPath + "/services/") && "/Version".equals(pathInfo) == false)
51       {
52          reportTrouble(new IllegalAccessException JavaDoc(requestURI), res, res.getWriter());
53          return false;
54       }
55
56       return true;
57    }
58
59    /**
60     * Reject POST requests to '/ws4ee/services/someService'
61     */

62    public void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc
63    {
64       if (assertServiceAccess(req, res) == false)
65          return;
66
67       super.doPost(req, res);
68    }
69
70    /**
71     * Process GET requests.
72     */

73    public void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
74            throws ServletException JavaDoc, IOException JavaDoc
75    {
76       if (assertServiceAccess(req, res) == false)
77          return;
78
79       String JavaDoc url = req.getRequestURL().toString();
80       String JavaDoc queryString = req.getQueryString();
81       log.debug("doGet: " + url + (queryString != null ? "?" + queryString : ""));
82
83       PrintWriter JavaDoc writer = res.getWriter();
84       try
85       {
86          AxisEngine engine = getEngine();
87
88          // Get the SOAP servie
89
String JavaDoc serviceName = getServiceName(req);
90          SOAPService service = (serviceName != null ? engine.getService(serviceName) : null);
91
92          boolean wsdlRequested = req.getParameter("wsdl") != null || req.getParameter("WSDL") != null;
93
94          if (!wsdlRequested)
95          {
96             log.debug("Report available services");
97
98             // If the user requested the servlet (i.e. /ws4ee/services)
99
// with no service name, present the user with a list of deployed services to be helpful
100
// Don't do this if we are doing WSDL or list.
101
reportAvailableServices(res, writer, req);
102             return;
103          }
104
105          if (service == null)
106          {
107             log.error("Cannot get axis service: " + serviceName);
108             reportCantGetAxisService(req, res, writer);
109             return;
110          }
111
112          // get message context w/ various properties set
113
MessageContext msgContext = createMessageContext(engine, req, res);
114
115          // we found the service, so we can set it in the msg context
116
// whoever comes after, won't have to retry finding it
117
if (service != null)
118             msgContext.setTargetService(serviceName);
119
120          String JavaDoc transportURL = getTransportURL(req, serviceName);
121          if (transportURL != null)
122          {
123             msgContext.setProperty(MessageContext.TRANS_URL, transportURL);
124             log.debug("Set transport.url=" + transportURL);
125          }
126
127          if (wsdlRequested)
128          {
129             String JavaDoc wsdlResource = req.getParameter("resource");
130             if (wsdlResource != null)
131             {
132                log.debug("Process wsdl import request: " + wsdlResource);
133                msgContext.setProperty(MessageContext.WSDLGEN_RESOURCE, wsdlResource);
134             }
135             else
136             {
137                log.debug("Process wsdl request");
138             }
139
140             processWsdlRequest(msgContext, res, writer);
141             return;
142          }
143
144          // If nothing else was done previously
145
log.debug("Report service info");
146          reportServiceInfo(res, writer, service, serviceName);
147       }
148       catch (AxisFault fault)
149       {
150          reportTrouble(fault, res, writer);
151       }
152       catch (Exception JavaDoc e)
153       {
154          reportTrouble(e, res, writer);
155       }
156       finally
157       {
158          // Make sure the MessageContext is removed from the calling ThreadLocal
159
AxisEngine.setCurrentMessageContext(null);
160          writer.close();
161       }
162    }
163
164    /**
165     * Get the service name as it is known to Axis
166     * <p/>
167     * For JSE service endpoints it is obtained from the generated init parameter in web.xml
168     */

169    protected String JavaDoc getServiceName(HttpServletRequest JavaDoc req)
170    {
171       String JavaDoc serviceName = null;
172       if (req.getRequestURI().equals(req.getContextPath() + "/services/Version"))
173          serviceName = "Version";
174
175       return serviceName;
176    }
177 }
178
Popular Tags