1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20
21 package org.apache.axis2.transport.http;
22
23 import org.apache.axis2.AxisFault;
24 import org.apache.axis2.Constants;
25 import org.apache.axis2.context.ConfigurationContext;
26 import org.apache.axis2.deployment.DeploymentConstants;
27 import org.apache.axis2.description.AxisDescription;
28 import org.apache.axis2.description.AxisService;
29 import org.apache.axis2.description.PolicyInclude;
30 import org.apache.axis2.description.TransportInDescription;
31 import org.apache.axis2.util.ExternalPolicySerializer;
32 import org.apache.axis2.util.IOUtils;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.neethi.Policy;
36 import org.apache.neethi.PolicyRegistry;
37
38 import javax.servlet.ServletException;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.xml.stream.FactoryConfigurationError;
42 import javax.xml.stream.XMLOutputFactory;
43 import javax.xml.stream.XMLStreamException;
44 import javax.xml.stream.XMLStreamWriter;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.OutputStream;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.List;
51
52 public class ListingAgent extends AbstractAgent {
53
54 private static final Log log = LogFactory.getLog(ListingAgent.class);
55
56 private static final String LIST_MULTIPLE_SERVICE_JSP_NAME =
57 "listServices.jsp";
58 private static final String LIST_SINGLE_SERVICE_JSP_NAME =
59 "listSingleService.jsp";
60 private static final String LIST_FAULTY_SERVICES_JSP_NAME = "listFaultyService.jsp";
61
62 public static final String RUNNING_PORT = "RUNNING_PORT";
63
64 public ListingAgent(ConfigurationContext aConfigContext) {
65 super(aConfigContext);
66 }
67
68 private void addTransportListener(String schema, int port) {
69 try {
70 TransportInDescription trsIn =
71 configContext.getAxisConfiguration().getTransportIn(schema);
72 if (trsIn == null) {
73 trsIn = new TransportInDescription(schema);
74 CustomListener httspReceiver = new CustomListener(port, schema);
75 httspReceiver.init(configContext, trsIn);
76 trsIn.setReceiver(httspReceiver);
77 configContext.getListenerManager().addListener(trsIn, true);
78 }
79 } catch (AxisFault axisFault) {
80 //
81 }
82 }
83
84 public void handle(HttpServletRequest httpServletRequest,
85 HttpServletResponse httpServletResponse)
86 throws IOException, ServletException {
87
88 initTransportListener(httpServletRequest);
89
90 String query = httpServletRequest.getQueryString();
91 if (query != null) {
92 if (query.indexOf("wsdl2") > 0 || query.indexOf("wsdl") > 0 ||
93 query.indexOf("xsd") > 0 || query.indexOf("policy") > 0) {
94 processListService(httpServletRequest, httpServletResponse);
95 } else {
96 super.handle(httpServletRequest, httpServletResponse);
97 }
98 } else {
99 super.handle(httpServletRequest, httpServletResponse);
100 }
101 }
102
103 protected void initTransportListener(HttpServletRequest httpServletRequest) {
104 // httpServletRequest.getLocalPort() , giving me a build error so I had to use the followin
105 String filePart;
106 try {
107 filePart = httpServletRequest.getRequestURL().toString();
108 } catch (Throwable t){
109 log.info("Old Servlet API (fallback to HttpServletRequest.getRequestURI) :" + t);
110 filePart = httpServletRequest.getRequestURI();
111 }
112 int ipindex = filePart.indexOf("//");
113 String ip;
114 if (ipindex >= 0) {
115 ip = filePart.substring(ipindex + 2, filePart.length());
116 int seperatorIndex = ip.indexOf(":");
117 int slashIndex = ip.indexOf("/");
118 String portstr;
119 if (seperatorIndex >= 0) {
120 portstr = ip.substring(seperatorIndex + 1, slashIndex);
121 } else {
122 portstr = "80";
123 }
124 try {
125 addTransportListener(httpServletRequest.getScheme(), Integer.parseInt(portstr));
126 } catch (NumberFormatException e) {
127 log.debug(e.toString(), e);
128 }
129 }
130 }
131
132 protected void processListFaultyServices(HttpServletRequest req, HttpServletResponse res)
133 throws IOException, ServletException {
134 String serviceName = req.getParameter("serviceName");
135 if (serviceName != null) {
136 AxisService service = configContext.getAxisConfiguration().getService(serviceName);
137 try {
138 req.getSession().setAttribute(Constants.SINGLE_SERVICE, service);
139 } catch (Throwable t) {
140 log.info("Old Servlet API :" + t);
141 }
142 }
143 renderView(LIST_FAULTY_SERVICES_JSP_NAME, req, res);
144 }
145
146
147 protected void processIndex(HttpServletRequest httpServletRequest,
148 HttpServletResponse httpServletResponse)
149 throws IOException, ServletException {
150 processListServices(httpServletRequest, httpServletResponse);
151 }
152
153 private String extractHostAndPort(String filePart, boolean isHttp) {
154 int ipindex = filePart.indexOf("//");
155 String ip = null;
156 if (ipindex >= 0) {
157 ip = filePart.substring(ipindex + 2, filePart.length());
158 int seperatorIndex = ip.indexOf(":");
159 int slashIndex = ip.indexOf("/");
160 String port;
161 if (seperatorIndex >= 0) {
162 port = ip.substring(seperatorIndex + 1, slashIndex);
163 ip = ip.substring(0, seperatorIndex);
164 } else {
165 ip = ip.substring(0, slashIndex);
166 port = "80";
167 }
168 if (isHttp) {
169 configContext.setProperty(RUNNING_PORT, port);
170 }
171 }
172 return ip;
173 }
174
175 public void processExplicitSchemaAndWSDL(HttpServletRequest req,
176 HttpServletResponse res)
177 throws IOException, ServletException {
178 HashMap services = configContext.getAxisConfiguration().getServices();
179 String filePart = req.getRequestURL().toString();
180 String schema = filePart.substring(filePart.lastIndexOf("/") + 1,
181 filePart.length());
182 if ((services != null) && !services.isEmpty()) {
183 Iterator i = services.values().iterator();
184 while (i.hasNext()) {
185 AxisService service = (AxisService) i.next();
186 InputStream stream = service.getClassLoader().getResourceAsStream("META-INF/" + schema);
187 if (stream != null) {
188 OutputStream out = res.getOutputStream();
189 res.setContentType("text/xml");
190 IOUtils.copy(stream, out, true);
191 return;
192 }
193 }
194 }
195 }
196
197 /**
198 * Copies the input stream to the output stream
199 *
200 * @param stream the <code>InputStream</code>
201 * @param ostream the <code>OutputStream</code>
202 */
203 public static void copy(InputStream stream, OutputStream ostream) throws IOException {
204 IOUtils.copy(stream, ostream, false);
205 }
206
207 public String extractServiceName(String urlString) {
208 int n = urlString.indexOf(configContext.getServiceContextPath());
209 if (n != -1) {
210 String serviceName = urlString.substring(n + configContext.getServiceContextPath().length(),
211 urlString.length());
212 if (serviceName.length() > 0) {
213 if(serviceName.charAt(0)=='/'){
214 serviceName = serviceName.substring(1);
215 }
216 return serviceName;
217 }
218 }
219 return urlString.substring(urlString.lastIndexOf("/") + 1,
220 urlString.length());
221 }
222
223 public void processListService(HttpServletRequest req,
224 HttpServletResponse res)
225 throws IOException, ServletException {
226
227 String url;
228 try {
229 url = req.getRequestURL().toString();
230 } catch (Throwable t) {
231 log.info("Old Servlet API (Fallback to HttpServletRequest.getRequestURI) :" + t);
232 url = req.getRequestURI();
233 }
234 String serviceName = extractServiceName(url);
235 HashMap services = configContext.getAxisConfiguration().getServices();
236 String query = req.getQueryString();
237 int wsdl2 = query.indexOf("wsdl2");
238 int wsdl = query.indexOf("wsdl");
239 int xsd = query.indexOf("xsd");
240 int policy = query.indexOf("policy");
241
242 if ((services != null) && !services.isEmpty()) {
243 Object serviceObj = services.get(serviceName);
244 if (serviceObj != null) {
245 AxisService axisService = (AxisService) serviceObj;
246 boolean isHttp = "http".equals(req.getScheme());
247 if (wsdl2 >= 0) {
248 res.setContentType("text/xml");
249 String ip = extractHostAndPort(url, isHttp);
250 String wsdlName = req.getParameter("wsdl2");
251
252 int ret = axisService.printWSDL2(res.getOutputStream(), ip, wsdlName);
253 if (ret == 0) {
254 res.sendRedirect("");
255 } else if (ret == -1) {
256 res.sendError(HttpServletResponse.SC_NOT_FOUND);
257 }
258 return;
259 } else if (wsdl >= 0) {
260 OutputStream out = res.getOutputStream();
261 res.setContentType("text/xml");
262 String ip = extractHostAndPort(url, isHttp);
263 String wsdlName = req.getParameter("wsdl");
264
265 if (wsdlName != null && wsdlName.length()>0) {
266 axisService.printUserWSDL(out, wsdlName);
267 out.flush();
268 out.close();
269 } else {
270 axisService.printWSDL(out, ip);
271 out.flush();
272 out.close();
273 }
274 return;
275 } else if (xsd >= 0) {
276 res.setContentType("text/xml");
277 int ret = axisService.printXSD(res.getOutputStream(), req.getParameter("xsd"));
278 if (ret == 0) {
279 //multiple schemas are present and the user specified
280 //no name - in this case we cannot possibly pump a schema
281 //so redirect to the service root
282 res.sendRedirect("");
283 } else if (ret == -1) {
284 res.sendError(HttpServletResponse.SC_NOT_FOUND);
285 }
286 return;
287 } else if (policy >= 0) {
288
289 ExternalPolicySerializer serializer = new ExternalPolicySerializer();
290 serializer.setAssertionsToFilter(configContext
291 .getAxisConfiguration().getLocalPolicyAssertions());
292
293 // check whether Id is set
294 String idParam = req.getParameter("id");
295
296 if (idParam != null) {
297 // Id is set
298
299 Policy targetPolicy = findPolicy(idParam, axisService);
300
301 if (targetPolicy != null) {
302 XMLStreamWriter writer;
303
304 try {
305 OutputStream out = res.getOutputStream();
306 writer = XMLOutputFactory.newInstance()
307 .createXMLStreamWriter(out);
308
309 res.setContentType("application/wspolicy+xml");
310 targetPolicy.serialize(writer);
311 writer.flush();
312
313 } catch (XMLStreamException e) {
314 throw new ServletException(
315 "Error occured when serializing the Policy",
316 e);
317
318 } catch (FactoryConfigurationError e) {
319 throw new ServletException(
320 "Error occured when serializing the Policy",
321 e);
322 }
323
324 } else {
325
326 OutputStream out = res.getOutputStream();
327 res.setContentType("text/html");
328 String outStr = "<b>No policy found for id="
329 + idParam + "</b>";
330 out.write(outStr.getBytes());
331 }
332
333 } else {
334
335 PolicyInclude policyInclude = axisService.getPolicyInclude();
336 Policy effecPolicy = policyInclude.getEffectivePolicy();
337
338 if (effecPolicy != null) {
339 XMLStreamWriter writer;
340
341 try {
342 OutputStream out = res.getOutputStream();
343 writer = XMLOutputFactory.newInstance()
344 .createXMLStreamWriter(out);
345
346 res.setContentType("application/wspolicy+xml");
347 effecPolicy.serialize(writer);
348 writer.flush();
349
350 } catch (XMLStreamException e) {
351 throw new ServletException(
352 "Error occured when serializing the Policy",
353 e);
354
355 } catch (FactoryConfigurationError e) {
356 throw new ServletException(
357 "Error occured when serializing the Policy",
358 e);
359 }
360 } else {
361
362 OutputStream out = res.getOutputStream();
363 res.setContentType("text/html");
364 String outStr = "<b>No effective policy for "
365 + serviceName + " service</b>";
366 out.write(outStr.getBytes());
367 }
368 }
369
370 return;
371 } else {
372 try {
373 req.getSession().setAttribute(Constants.SINGLE_SERVICE,
374 serviceObj);
375 } catch (Throwable t) {
376 log.info("Old Servlet API :" + t);
377 }
378 }
379 } else {
380 try {
381 req.getSession().setAttribute(Constants.SINGLE_SERVICE, null);
382 } catch (Throwable t){
383 log.info("Old Servlet API :" + t);
384 }
385
386 res.sendError(HttpServletResponse.SC_NOT_FOUND, url);
387 }
388 }
389
390 renderView(LIST_SINGLE_SERVICE_JSP_NAME, req, res);
391 }
392
393 protected void processListServices(HttpServletRequest req,
394 HttpServletResponse res)
395 throws IOException, ServletException {
396
397 populateSessionInformation(req);
398 try {
399 req.getSession().setAttribute(Constants.ERROR_SERVICE_MAP,
400 configContext.getAxisConfiguration().getFaultyServices());
401 } catch (Throwable t){
402 log.info("Old Servlet API :" + t);
403 }
404 renderView(LIST_MULTIPLE_SERVICE_JSP_NAME, req, res);
405 }
406
407 private Policy findPolicy(String id, AxisDescription des) {
408
409 List policyElements = des.getPolicyInclude().getPolicyElements();
410 PolicyRegistry registry = des.getPolicyInclude().getPolicyRegistry();
411
412 Object policyComponent;
413
414 Policy policy = registry.lookup(id);
415
416 if (policy != null) {
417 return policy;
418 }
419
420 for (Iterator iterator = policyElements.iterator(); iterator.hasNext();) {
421 policyComponent = iterator.next();
422
423 if (policyComponent instanceof Policy) {
424 // policy found for the id
425
426 if (id.equals(((Policy) policyComponent).getId())) {
427 return (Policy) policyComponent;
428 }
429 }
430 }
431
432 AxisDescription child;
433
434 for (Iterator iterator = des.getChildren(); iterator.hasNext();) {
435 child = (AxisDescription) iterator.next();
436 policy = findPolicy(id, child);
437
438 if (policy != null) {
439 return policy;
440 }
441 }
442
443 return null;
444 }
445
446 }