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