1 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 ; 27 import javax.servlet.ServletContext ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.xml.namespace.QName ; 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 import java.util.Enumeration ; 36 import java.util.HashMap ; 37 38 41 public class AxisServlet extends HttpServlet { 42 45 46 private ConfigurationContext configContext; 47 48 private ListingAgent lister; 49 50 56 public void init(ServletConfig config) throws ServletException { 57 try { 58 ServletContext context = config.getServletContext(); 59 String 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 e) { 65 throw new ServletException (e); 66 } 67 } 68 69 77 protected void doGet( 78 HttpServletRequest httpServletRequest, 79 HttpServletResponse httpServletResponse) 80 throws ServletException , IOException { 81 httpServletResponse.setContentType("text/xml; charset=utf-8"); 82 MessageContext msgContext = null; 83 OutputStream out =null; 84 try { 85 Object 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 enu = httpServletRequest.getParameterNames(); 95 HashMap map = new HashMap (); 96 while (enu.hasMoreElements()) { 97 String name = (String ) enu.nextElement(); 98 String 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 (Constants.TRANSPORT_HTTP)), 108 configContext.getAxisConfiguration().getTransportOut( 109 new QName (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 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 141 142 150 protected void doPost(HttpServletRequest req, HttpServletResponse res) 151 throws ServletException , IOException { 152 MessageContext msgContext = null; 153 try { 154 Object 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 (Constants.TRANSPORT_HTTP)), 166 configContext.getAxisConfiguration().getTransportOut( 167 new QName (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 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 |