KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > http > AxisServlet


1 /*
2 * Copyright 2004,2005 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 package org.apache.axis2.transport.http;
17
18 import org.apache.axis2.Constants;
19 import org.apache.axis2.context.ConfigurationContext;
20 import org.apache.axis2.context.ConfigurationContextFactory;
21 import org.apache.axis2.context.MessageContext;
22 import org.apache.axis2.context.SessionContext;
23 import org.apache.axis2.engine.AxisEngine;
24 import org.apache.axis2.engine.AxisFault;
25
26 import javax.servlet.ServletConfig JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.http.HttpServlet JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.xml.namespace.QName JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.HashMap JavaDoc;
37
38 /**
39  * Class AxisServlet
40  */

41 public class AxisServlet extends HttpServlet JavaDoc {
42     /**
43      * Field engineRegistry
44      */

45
46     private ConfigurationContext configContext;
47
48     private ListingAgent lister;
49
50     /**
51      * Method init
52      *
53      * @param config
54      * @throws ServletException
55      */

56     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
57         try {
58             ServletContext JavaDoc context = config.getServletContext();
59             String JavaDoc repoDir = context.getRealPath("/WEB-INF");
60             ConfigurationContextFactory erfac = new ConfigurationContextFactory();
61             configContext = erfac.buildConfigurationContext(repoDir);
62             configContext.setProperty(Constants.CONTAINER_MANAGED, Constants.VALUE_TRUE);
63             lister = new ListingAgent(configContext);
64         } catch (Exception JavaDoc e) {
65             throw new ServletException JavaDoc(e);
66         }
67     }
68
69     /**
70      * Method doGet
71      *
72      * @param httpServletRequest
73      * @param httpServletResponse
74      * @throws ServletException
75      * @throws IOException
76      */

77     protected void doGet(
78             HttpServletRequest JavaDoc httpServletRequest,
79             HttpServletResponse JavaDoc httpServletResponse)
80             throws ServletException JavaDoc, IOException JavaDoc {
81         httpServletResponse.setContentType("text/xml; charset=utf-8");
82         MessageContext msgContext = null;
83         OutputStream JavaDoc out =null;
84         try {
85             Object JavaDoc sessionContext =
86                     httpServletRequest.getSession().getAttribute(Constants.SESSION_CONTEXT_PROPERTY);
87             if (sessionContext == null) {
88                 sessionContext = new SessionContext(null);
89                 httpServletRequest.getSession().setAttribute(
90                         Constants.SESSION_CONTEXT_PROPERTY,
91                         sessionContext);
92             }
93
94             Enumeration JavaDoc enu = httpServletRequest.getParameterNames();
95             HashMap JavaDoc map = new HashMap JavaDoc();
96             while (enu.hasMoreElements()) {
97                 String JavaDoc name = (String JavaDoc) enu.nextElement();
98                 String JavaDoc value = httpServletRequest.getParameter(name);
99                 map.put(name, value);
100             }
101
102             msgContext =
103                     new MessageContext(
104                             configContext,
105                             (SessionContext) sessionContext,
106                             configContext.getAxisConfiguration().getTransportIn(
107                                     new QName JavaDoc(Constants.TRANSPORT_HTTP)),
108                             configContext.getAxisConfiguration().getTransportOut(
109                                     new QName JavaDoc(Constants.TRANSPORT_HTTP)));
110             msgContext.setDoingREST(true);
111             msgContext.setServerSide(true);
112             msgContext.setProperty(HTTPConstants.HTTPOutTransportInfo,new ServletBasedOutTransportInfo(httpServletResponse));
113             out = httpServletResponse.getOutputStream();
114             boolean processed =
115                     HTTPTransportUtils.processHTTPGetRequest(
116                             msgContext,
117                             httpServletRequest.getInputStream(),
118                             out,
119                             httpServletRequest.getContentType(),
120                             httpServletRequest.getHeader(HTTPConstants.HEADER_SOAP_ACTION),
121                             httpServletRequest.getRequestURL().toString(),
122                             configContext,
123                             map);
124             if (!processed) {
125                 lister.handle(httpServletRequest, httpServletResponse,out);
126             }
127         } catch (Exception JavaDoc e) {
128             AxisEngine engine = new AxisEngine(configContext);
129             if(msgContext!= null){
130                 msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
131                 engine.handleFault(msgContext,e);
132             }
133         }
134
135     }
136
137     /*
138     * (non-Javadoc)
139     * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
140     */

141
142     /**
143      * Method doPost
144      *
145      * @param req
146      * @param res
147      * @throws ServletException
148      * @throws IOException
149      */

150     protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
151             throws ServletException JavaDoc, IOException JavaDoc {
152         MessageContext msgContext = null;
153         try {
154             Object JavaDoc sessionContext =
155                     req.getSession().getAttribute(Constants.SESSION_CONTEXT_PROPERTY);
156             if (sessionContext == null) {
157                 sessionContext = new SessionContext(null);
158                 req.getSession().setAttribute(Constants.SESSION_CONTEXT_PROPERTY, sessionContext);
159             }
160             msgContext =
161                     new MessageContext(
162                             configContext,
163                             (SessionContext) sessionContext,
164                             configContext.getAxisConfiguration().getTransportIn(
165                                     new QName JavaDoc(Constants.TRANSPORT_HTTP)),
166                             configContext.getAxisConfiguration().getTransportOut(
167                                     new QName JavaDoc(Constants.TRANSPORT_HTTP)));
168             msgContext.setProperty(HTTPConstants.HTTPOutTransportInfo,new ServletBasedOutTransportInfo(res));
169             res.setContentType("text/xml; charset=utf-8");
170             HTTPTransportUtils.processHTTPPostRequest(
171                     msgContext,
172                     req.getInputStream(),
173                     res.getOutputStream(),
174                     req.getContentType(),
175                     req.getHeader(HTTPConstants.HEADER_SOAP_ACTION),
176                     req.getRequestURL().toString(),
177                     configContext);
178             Object JavaDoc contextWritten = msgContext.getOperationContext().getProperty(Constants.RESPONSE_WRITTEN);
179             if (contextWritten == null || !Constants.VALUE_TRUE.equals(contextWritten)) {
180                 res.setStatus(HttpServletResponse.SC_ACCEPTED);
181             }
182         } catch (AxisFault e) {
183             AxisEngine engine = new AxisEngine(configContext);
184             if(msgContext!= null){
185                 msgContext.setProperty(MessageContext.TRANSPORT_OUT, res.getOutputStream());
186                 engine.handleFault(msgContext,e);
187             }
188         }
189     }
190 }
191
Popular Tags