KickJava   Java API By Example, From Geeks To Geeks.

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


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: AbstractServlet.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.ConfigurationException;
14 import org.jboss.axis.MessageContext;
15 import org.jboss.axis.description.OperationDesc;
16 import org.jboss.axis.description.ServiceDesc;
17 import org.jboss.axis.server.AxisServer;
18 import org.jboss.axis.transport.http.AxisServlet;
19 import org.jboss.axis.utils.XMLUtils;
20 import org.jboss.logging.Logger;
21 import org.jboss.mx.util.MBeanProxy;
22 import org.jboss.mx.util.MBeanProxyCreationException;
23 import org.jboss.mx.util.MBeanServerLocator;
24 import org.jboss.webservice.AxisServiceMBean;
25 import org.jboss.webservice.PortComponentInfo;
26 import org.w3c.dom.Document JavaDoc;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * The servlet that that is associated with context /ws4ee
37  *
38  * It manages the 'Version' service
39  *
40  * @author Thomas.Diesler@jboss.org
41  * @since 09-Feb-2005
42  */

43 public abstract class AbstractServlet extends AxisServlet
44 {
45    // provide logging
46
protected final Logger log = Logger.getLogger(AbstractServlet.class);
47
48    /**
49     * Get the transport URL
50     * <p/>
51     * For JSE service endpoints
52     * e.g. http://localhost:8080/whatever-url-mapping
53     */

54    protected String JavaDoc getTransportURL(HttpServletRequest JavaDoc req, String JavaDoc serviceName)
55    {
56       return req.getRequestURI();
57    }
58
59    protected void setupHTMLResponseHeader(HttpServletResponse JavaDoc res, PrintWriter JavaDoc writer)
60    {
61       writer.println("<head>");
62       writer.println("<meta http-equiv='Content-Type content='text/html; charset=iso-8859-1'>");
63       writer.println("<title>JBossWS</title>");
64       writer.println("<link rel='stylesheet' HREF='/ws4ee/styles.css'>");
65       writer.println("</head>");
66    }
67
68    /**
69     * This method lists the available services; it is called when there is
70     * nothing to execute on a GET
71     */

72    protected void reportAvailableServices(HttpServletResponse JavaDoc res, PrintWriter JavaDoc writer, HttpServletRequest JavaDoc req)
73            throws ConfigurationException, AxisFault
74    {
75       AxisEngine engine = getEngine();
76
77       res.setContentType("text/html");
78       setupHTMLResponseHeader(res, writer);
79       writer.println("<h2>And now... Some Services</h2>");
80       writer.println("<ul>");
81
82       // Get the JBossWS AxisService
83
AxisServiceMBean axisService = null;
84       try
85       {
86          MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
87          axisService = (AxisServiceMBean)MBeanProxy.get(AxisServiceMBean.class, AxisServiceMBean.OBJECT_NAME, server);
88       }
89       catch (MBeanProxyCreationException e)
90       {
91          throw new IllegalStateException JavaDoc("Cannot obtain: " + AxisServiceMBean.OBJECT_NAME);
92       }
93
94       Iterator JavaDoc it = engine.getConfig().getDeployedServices();
95       while (it.hasNext())
96       {
97          ServiceDesc sd = (ServiceDesc)it.next();
98          String JavaDoc serviceName = sd.getName();
99
100          PortComponentInfo pcInfo = axisService.getPortComponentInfo(serviceName);
101          String JavaDoc serviceURL = null;
102          if (pcInfo != null)
103          {
104             serviceURL = pcInfo.getServiceEndpointURL();
105          }
106          else
107          {
108             serviceURL = getWebappBase(req) + "/services/" + serviceName;
109          }
110
111          StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<li>" + serviceName + "<a HREF='" + serviceURL + "?wsdl'>&nbsp;<i>(wsdl)</i></a></li>");
112
113          ArrayList JavaDoc operations = sd.getOperations();
114          if (!operations.isEmpty())
115          {
116             sb.append("<ul>");
117             for (Iterator JavaDoc itOp = operations.iterator(); itOp.hasNext();)
118             {
119                OperationDesc desc = (OperationDesc)itOp.next();
120                sb.append("<li>" + desc.getName() + "</li>");
121             }
122             sb.append("</ul>");
123          }
124          writer.println(sb.toString());
125       }
126       writer.println("</ul>");
127    }
128
129    /**
130     * Handle a wsdl request
131     */

132    protected void processWsdlRequest(MessageContext msgContext, HttpServletResponse JavaDoc res, PrintWriter JavaDoc writer)
133            throws AxisFault
134    {
135       String JavaDoc serviceURL = (String JavaDoc)msgContext.getProperty(MessageContext.TRANS_URL);
136
137       AxisEngine engine = getEngine();
138       engine.generateWSDL(msgContext);
139
140       Document JavaDoc doc = (Document JavaDoc)msgContext.getProperty("WSDL");
141       if (doc == null)
142          throw new AxisFault("Cannot get wsdl document for service: " + serviceURL);
143
144       res.setContentType("text/xml");
145       XMLUtils.DocumentToWriter(doc, writer);
146    }
147
148    /**
149     * Get an AxisServer with the EngineConfiguration from the JMX AxisService
150     */

151    public AxisServer getEngine() throws AxisFault
152    {
153       try
154       {
155          MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
156          axisServer = (AxisServer)server.getAttribute(AxisServiceMBean.OBJECT_NAME, "AxisServer");
157       }
158       catch (Exception JavaDoc e)
159       {
160          log.warn("Cannot access AxisService, using default server config");
161          axisServer = super.getEngine();
162       }
163
164       return axisServer;
165    }
166 }
167
Popular Tags