KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > http > QSMethodHandler


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.transport.http;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.PrintWriter;
21 import java.util.Enumeration;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.apache.axis.AxisFault;
27 import org.apache.axis.Message;
28 import org.apache.axis.MessageContext;
29 import org.apache.axis.server.AxisServer;
30 import org.apache.axis.utils.Messages;
31
32 /**
33  * The QSMethodHandler class is a handler which executes a given method from an
34  * an AXIS service's WSDL definition when the query string "method" is
35  * encountered in an AXIS servlet invocation.
36  *
37  * @author Curtiss Howard (code mostly from AxisServlet class)
38  * @author Doug Davis (dug@us.ibm.com)
39  * @author Steve Loughran
40  */

41
42 public class QSMethodHandler extends AbstractQueryStringHandler {
43
44     /**
45      * Performs the action associated with this particular query string
46      * handler.
47      *
48      * @param msgContext a MessageContext object containing message context
49      * information for this query string handler.
50      * @throws AxisFault if an error occurs.
51      */

52
53     public void invoke (MessageContext msgContext) throws AxisFault {
54         // Obtain objects relevant to the task at hand from the provided
55
// MessageContext's bag.
56

57         configureFromContext(msgContext);
58         AxisServer engine = (AxisServer) msgContext.getProperty
59                 (HTTPConstants.PLUGIN_ENGINE);
60         PrintWriter writer = (PrintWriter) msgContext.getProperty
61                 (HTTPConstants.PLUGIN_WRITER);
62         HttpServletRequest request = (HttpServletRequest)
63                 msgContext.getProperty (HTTPConstants.MC_HTTP_SERVLETREQUEST);
64         HttpServletResponse response = (HttpServletResponse)
65                 msgContext.getProperty (HTTPConstants.MC_HTTP_SERVLETRESPONSE);
66
67
68         String method = null;
69         String args = "";
70         Enumeration enum = request.getParameterNames();
71
72         while (enum.hasMoreElements()) {
73             String param = (String) enum.nextElement();
74             if (param.equalsIgnoreCase ("method")) {
75                 method = request.getParameter (param);
76             }
77
78             else {
79                 args += "<" + param + ">" + request.getParameter (param) +
80                         "</" + param + ">";
81             }
82         }
83
84         if (method == null) {
85             response.setContentType ("text/html");
86             response.setStatus (HttpServletResponse.SC_BAD_REQUEST);
87
88             writer.println ("<h2>" + Messages.getMessage ("error00") +
89                     ": " + Messages.getMessage ("invokeGet00") + "</h2>");
90             writer.println ("<p>" + Messages.getMessage ("noMethod01") +
91                     "</p>");
92         }
93
94         else {
95             invokeEndpointFromGet (msgContext, response, writer, method, args);
96         }
97     }
98
99     /**
100      * invoke an endpoint from a get request by building an XML request and
101      * handing it down. If anything goes wrong, we generate an XML formatted
102      * axis fault
103      * @param msgContext current message
104      * @param response to return data
105      * @param writer output stream
106      * @param method method to invoke (may be null)
107      * @param args argument list in XML form
108      * @throws AxisFault iff something goes wrong when turning the response message
109      * into a SOAP string.
110      */

111
112     private void invokeEndpointFromGet (MessageContext msgContext,
113                                         HttpServletResponse response, PrintWriter writer, String method,
114                                         String args) throws AxisFault {
115         String body = "<" + method + ">" + args + "</" + method + ">";
116         String msgtxt = "<SOAP-ENV:Envelope" +
117                 " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
118                 "<SOAP-ENV:Body>" + body + "</SOAP-ENV:Body>" +
119                 "</SOAP-ENV:Envelope>";
120         ByteArrayInputStream istream =
121                 new ByteArrayInputStream (msgtxt.getBytes());
122         Message responseMsg = null;
123
124         try {
125             AxisServer engine = (AxisServer) msgContext.getProperty
126                     (HTTPConstants.PLUGIN_ENGINE);
127             Message msg = new Message (istream, false);
128
129             msgContext.setRequestMessage (msg);
130             engine.invoke (msgContext);
131
132             responseMsg = msgContext.getResponseMessage();
133
134             //turn off caching for GET requests
135

136             response.setHeader ("Cache-Control", "no-cache");
137             response.setHeader ("Pragma", "no-cache");
138
139             if (responseMsg == null) {
140                 //tell everyone that something is wrong
141

142                 throw new Exception (Messages.getMessage ("noResponse01"));
143             }
144
145         }
146
147         catch (AxisFault fault) {
148             processAxisFault (fault);
149
150             configureResponseFromAxisFault (response, fault);
151
152             if (responseMsg == null) {
153                 responseMsg = new Message (fault);
154                 responseMsg.setMessageContext(msgContext);
155             }
156         }
157
158         catch (Exception e) {
159             response.setStatus (HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
160             responseMsg = convertExceptionToAxisFault (e, responseMsg);
161         }
162
163         //this call could throw an AxisFault. We delegate it up, because
164
//if we cant write the message there is not a lot we can do in pure SOAP terms.
165

166         response.setContentType ("text/xml");
167
168         writer.println (responseMsg.getSOAPPartAsString());
169     }
170
171 }
172
Popular Tags