1 package org.codehaus.xfire.plexus;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.UnsupportedEncodingException;
6 import java.util.Collection;
7 import java.util.Iterator;
8 import javax.servlet.ServletContext;
9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import org.codehaus.plexus.servlet.PlexusServlet;
13 import org.codehaus.xfire.MessageContext;
14 import org.codehaus.xfire.XFire;
15 import org.codehaus.xfire.service.Service;
16 import org.codehaus.xfire.service.ServiceRegistry;
17 import org.codehaus.xfire.transport.TransportManager;
18 import org.codehaus.xfire.transport.http.SoapHttpTransport;
19 import org.codehaus.xfire.transport.http.XFireHttpSession;
20
21 /***
22 * Loads XFire and processes requests via a servlet.
23 *
24 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
25 * @since Feb 13, 2004
26 */
27 public class PlexusXFireServlet
28 extends PlexusServlet
29 {
30 private static ThreadLocal requests = new ThreadLocal();
31
32 private File webInfPath;
33
34 private XFire xfire;
35
36 public static HttpServletRequest getRequest()
37 {
38 return (HttpServletRequest) requests.get();
39 }
40
41 public File getWebappBase()
42 {
43 if ( webInfPath == null )
44 {
45 ServletContext context = getServletConfig().getServletContext();
46
47 webInfPath = new File( context.getRealPath("/WEB-INF") );
48 }
49
50 return webInfPath;
51 }
52
53 public void init() throws ServletException
54 {
55 super.init();
56
57 try
58 {
59 xfire = (XFire) lookup( XFire.ROLE );
60 }
61 catch (Exception e)
62 {
63 throw new ServletException("Couldn't start XFire.", e);
64 }
65
66 TransportManager service = getTransportManager();
67
68 String url = getInitParameter("url");
69 if ( url == null )
70 {
71 url = "Please provide a url in your web.xml config.";
72 }
73
74 service.register( new SoapHttpTransport() );
75 }
76
77 /***
78 * @return
79 * @throws ServletException
80 */
81 protected TransportManager getTransportManager()
82 throws ServletException
83 {
84 return xfire.getTransportManager();
85 }
86
87 /***
88 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
89 */
90 protected void doGet( HttpServletRequest request, HttpServletResponse response )
91 throws ServletException, IOException
92 {
93 String service = getService(request);
94 ServiceRegistry reg = getServiceRegistry();
95
96 String wsdl = request.getParameter("wsdl");
97 response.setHeader("Content-Type", "UTF-8");
98
99 requests.set(request);
100
101 if ( service == null
102 ||
103 service.equals("")
104 ||
105 !reg.hasService( service ) )
106 {
107 generateServices(response);
108 return;
109 }
110
111 try
112 {
113 if ( wsdl != null )
114 {
115 generateWSDL(response, service);
116 }
117 else
118 {
119 invoke(request, response, service);
120 }
121 }
122 catch (Exception e)
123 {
124
125 e.printStackTrace();
126 }
127 }
128
129 /***
130 * @param response
131 * @throws IOException
132 * @throws ServletException
133 */
134 private void generateServices(HttpServletResponse response)
135 throws IOException, ServletException
136 {
137 response.setContentType("text/html");
138 StringBuffer sb = new StringBuffer();
139 sb.append("<html><head><title>XFire Services</title></head>")
140 .append("<body>No such service.")
141 .append("<p>Services:<ul>.");
142
143 ServiceRegistry registry = getServiceRegistry();
144 Collection services = registry.getServices();
145
146 for ( Iterator itr = services.iterator(); itr.hasNext(); )
147 {
148 Service service = (Service) itr.next();
149 sb.append("<li>")
150 .append(service.getName())
151 .append("</li>");
152 }
153 sb.append("</ul></p></body></html>");
154
155 response.getWriter().write( sb.toString() );
156 response.getWriter().flush();
157
158
159 return;
160 }
161
162 /***
163 * @param request
164 * @param response
165 * @param service
166 * @throws ServletException
167 * @throws IOException
168 * @throws UnsupportedEncodingException
169 */
170 protected void invoke(HttpServletRequest request, HttpServletResponse response, String service)
171 throws ServletException, IOException, UnsupportedEncodingException
172 {
173 response.setStatus(200);
174
175 response.setContentType("text/xml; charset=UTF-8");
176
177 XFireHttpSession session = new XFireHttpSession(request);
178 MessageContext context =
179 new MessageContext( service,
180 null,
181 response.getOutputStream(),
182 session,
183 request.getRequestURI() );
184
185 getXFire().invoke( request.getInputStream(),
186 context );
187 }
188
189 /***
190 * @param response
191 * @param service
192 * @throws ServletException
193 * @throws IOException
194 */
195 protected void generateWSDL(HttpServletResponse response, String service)
196 throws ServletException, IOException
197 {
198 response.setStatus(200);
199 response.setContentType("text/xml");
200
201
202 getXFire().generateWSDL( service, response.getOutputStream() );
203 }
204
205 /***
206 * @param request
207 * @return
208 */
209 private String getService(HttpServletRequest request)
210 {
211 String pathInfo = request.getPathInfo();
212
213 if ( pathInfo == null )
214 return null;
215
216 String serviceName;
217
218 if (pathInfo.startsWith("/"))
219 {
220 serviceName = pathInfo.substring(1);
221 }
222 else
223 {
224 serviceName = pathInfo;
225 }
226
227 return serviceName;
228 }
229
230 /***
231 * @return
232 */
233 public XFire getXFire()
234 throws ServletException
235 {
236 return xfire;
237 }
238
239 public ServiceRegistry getServiceRegistry()
240 throws ServletException
241 {
242 return xfire.getServiceRegistry();
243 }
244
245 /***
246 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
247 */
248 protected void doPost(HttpServletRequest req, HttpServletResponse res)
249 throws ServletException,
250 IOException
251 {
252 doGet(req, res);
253 }
254 }