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