KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.axis.MessageContext;
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.Constants;
22 import org.apache.axis.Message;
23 import org.apache.axis.utils.Messages;
24 import org.apache.axis.utils.XMLUtils;
25 import org.apache.commons.logging.Log;
26 import org.w3c.dom.Element JavaDoc;
27
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30
31 /**
32  * An optional base class for query string handlers; provides various helper methods
33  * and extracts things from the the message context
34  */

35 public abstract class AbstractQueryStringHandler implements QSHandler {
36     /** cache of development flag */
37     private boolean development;
38     /** log for exceptions */
39     protected Log exceptionLog;
40
41     /** the other log */
42     protected Log log;
43
44     /**
45      * probe for the system being 'production'
46      * @return true for a dev system.
47      */

48
49     protected boolean isDevelopment () {
50          return this.development;
51     }
52
53     /**
54      * configure our elements from the context. Call this in the invoke()
55      * implementation to set up the base class
56      * @param msgContext
57      */

58     protected void configureFromContext(MessageContext msgContext) {
59         this.development = ((Boolean JavaDoc) msgContext.getProperty
60              (HTTPConstants.PLUGIN_IS_DEVELOPMENT)).booleanValue();
61         this.exceptionLog = (Log) msgContext.getProperty
62              (HTTPConstants.PLUGIN_EXCEPTION_LOG);
63         this.log = (Log) msgContext.getProperty(HTTPConstants.PLUGIN_LOG);
64     }
65
66     /**
67      * routine called whenever an axis fault is caught; where they
68      * are logged and any other business. The method may modify the fault
69      * in the process
70      * @param fault what went wrong.
71      */

72
73     protected void processAxisFault (AxisFault fault) {
74          //log the fault
75

76          Element JavaDoc runtimeException = fault.lookupFaultDetail
77               (Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION);
78
79          if (runtimeException != null) {
80               exceptionLog.info (Messages.getMessage ("axisFault00"), fault);
81
82               //strip runtime details
83

84               fault.removeFaultDetail
85                    (Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION);
86          }
87
88          else if (exceptionLog.isDebugEnabled()) {
89               exceptionLog.debug (Messages.getMessage ("axisFault00"), fault);
90          }
91
92          //dev systems only give fault dumps
93

94          if (!isDevelopment()) {
95               //strip out the stack trace
96

97               fault.removeFaultDetail (Constants.QNAME_FAULTDETAIL_STACKTRACE);
98          }
99     }
100
101     /**
102       * Configure the servlet response status code and maybe other headers
103       * from the fault info.
104       * @param response response to configure
105       * @param fault what went wrong
106       */

107
108      protected void configureResponseFromAxisFault (HttpServletResponse JavaDoc response,
109           AxisFault fault) {
110           // then get the status code
111
// It's been suggested that a lack of SOAPAction
112
// should produce some other error code (in the 400s)...
113

114           int status = getHttpServletResponseStatus (fault);
115
116           if (status == HttpServletResponse.SC_UNAUTHORIZED) {
117                // unauth access results in authentication request
118
// TODO: less generic realm choice?
119

120                response.setHeader ("WWW-Authenticate", "Basic realm=\"AXIS\"");
121           }
122
123           response.setStatus (status);
124      }
125
126     /**
127       * turn any Exception into an AxisFault, log it, set the response
128       * status code according to what the specifications say and
129       * return a response message for posting. This will be the response
130       * message passed in if non-null; one generated from the fault otherwise.
131       *
132       * @param exception what went wrong
133       * @param responseMsg what response we have (if any)
134       * @return a response message to send to the user
135       */

136
137      protected Message convertExceptionToAxisFault (Exception JavaDoc exception,
138           Message responseMsg) {
139           logException (exception);
140
141           if (responseMsg == null) {
142                AxisFault fault = AxisFault.makeFault (exception);
143
144                processAxisFault (fault);
145
146                responseMsg = new Message (fault);
147           }
148
149           return responseMsg;
150      }
151
152     /**
153       * Extract information from AxisFault and map it to a HTTP Status code.
154       *
155       * @param af Axis Fault
156       * @return HTTP Status code.
157       */

158
159      private int getHttpServletResponseStatus (AxisFault af) {
160           // TODO: Should really be doing this with explicit AxisFault
161
// subclasses... --Glen
162

163           return af.getFaultCode().getLocalPart().startsWith ("Server.Unauth")
164                ? HttpServletResponse.SC_UNAUTHORIZED
165                : HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
166
167           // This will raise a 401 for both
168
// "Unauthenticated" & "Unauthorized"...
169
}
170
171     /**
172       * log any exception to our output log, at our chosen level
173       * @param e what went wrong
174       */

175
176      private void logException (Exception JavaDoc e) {
177           exceptionLog.info (Messages.getMessage ("exception00"), e);
178      }
179
180     /**
181      * this method writes a fault out to an HTML stream. This includes
182      * escaping the strings to defend against cross-site scripting attacks
183      * @param writer
184      * @param axisFault
185      */

186
187     protected void writeFault (PrintWriter JavaDoc writer, AxisFault axisFault) {
188         String JavaDoc localizedMessage = XMLUtils.xmlEncodeString
189                 (axisFault.getLocalizedMessage());
190
191         writer.println ("<pre>Fault - " + localizedMessage + "<br>");
192         writer.println (axisFault.dumpToString());
193         writer.println ("</pre>");
194     }
195 }
196
Popular Tags