|
Axis 2 example source code file (AxisServlet.java)
This example Axis 2 source code file (AxisServlet.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Axis 2 AxisServlet.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.axis2.transport.http;
import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXBuilder;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axiom.soap.SOAPFaultCode;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.AddressingHelper;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.OperationContext;
import org.apache.axis2.context.SessionContext;
import org.apache.axis2.deployment.WarBasedAxisConfigurator;
import org.apache.axis2.description.AxisBindingMessage;
import org.apache.axis2.description.AxisBindingOperation;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.description.TransportInDescription;
import org.apache.axis2.description.TransportOutDescription;
import org.apache.axis2.description.WSDL2Constants;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.axis2.engine.AxisEngine;
import org.apache.axis2.engine.Handler.InvocationResponse;
import org.apache.axis2.engine.ListenerManager;
import org.apache.axis2.transport.RequestResponseTransport;
import org.apache.axis2.transport.TransportListener;
import org.apache.axis2.transport.TransportUtils;
import org.apache.axis2.transport.http.server.HttpUtils;
import org.apache.axis2.transport.http.util.RESTUtil;
import org.apache.axis2.util.JavaUtils;
import org.apache.axis2.util.MessageContextBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.SocketException;
import java.util.Map;
/**
* Class AxisServlet
*/
public class AxisServlet extends HttpServlet implements TransportListener {
private static final Log log = LogFactory.getLog(AxisServlet.class);
public static final String CONFIGURATION_CONTEXT = "CONFIGURATION_CONTEXT";
public static final String SESSION_ID = "SessionId";
protected transient ConfigurationContext configContext;
protected transient AxisConfiguration axisConfiguration;
protected transient ServletConfig servletConfig;
private transient ListingAgent agent;
private String contextRoot = null;
protected boolean disableREST = false;
private static final String LIST_SERVICES_SUFIX = "/services/listServices";
private static final String LIST_FAUKT_SERVICES_SUFIX = "/services/ListFaultyServices";
private boolean closeReader = true;
private static final int BUFFER_SIZE = 1024 * 8;
/**
* Implementaion of POST interface
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//set the initial buffer for a larger value
response.setBufferSize(BUFFER_SIZE);
initContextRoot(request);
MessageContext msgContext;
OutputStream out = response.getOutputStream();
String contentType = request.getContentType();
if (!HTTPTransportUtils.isRESTRequest(contentType)) {
msgContext = createMessageContext(request, response);
msgContext.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
try {
// adding ServletContext into msgContext;
InvocationResponse pi = HTTPTransportUtils.
processHTTPPostRequest(msgContext,
new BufferedInputStream(request.getInputStream()),
new BufferedOutputStream(out),
contentType,
request.getHeader(HTTPConstants.HEADER_SOAP_ACTION),
request.getRequestURL().toString());
Boolean holdResponse =
(Boolean) msgContext.getProperty(RequestResponseTransport.HOLD_RESPONSE);
if (pi.equals(InvocationResponse.SUSPEND) ||
(holdResponse != null && Boolean.TRUE.equals(holdResponse))) {
((RequestResponseTransport) msgContext
.getProperty(RequestResponseTransport.TRANSPORT_CONTROL))
.awaitResponse();
}
response.setContentType("text/xml; charset="
+ msgContext
.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING));
// if data has not been sent back and this is not a signal response
if (!TransportUtils.isResponseWritten(msgContext)
&& (((RequestResponseTransport)
msgContext.getProperty(
RequestResponseTransport.TRANSPORT_CONTROL)).
getStatus() != RequestResponseTransport.
RequestResponseTransportStatus.SIGNALLED)) {
response.setStatus(HttpServletResponse.SC_ACCEPTED);
}
} catch (AxisFault e) {
setResponseState(msgContext, response);
log.debug(e);
if (msgContext != null) {
processAxisFault(msgContext, response, out, e);
} else {
throw new ServletException(e);
}
} catch (Throwable t) {
log.error(t.getMessage(), t);
try {
// If the fault is not going along the back channel we should be 202ing
if (AddressingHelper.isFaultRedirected(msgContext)) {
response.setStatus(HttpServletResponse.SC_ACCEPTED);
} else {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
AxisBindingOperation axisBindingOperation =
(AxisBindingOperation) msgContext
.getProperty(Constants.AXIS_BINDING_OPERATION);
if (axisBindingOperation != null) {
AxisBindingMessage axisBindingMessage = axisBindingOperation.getFault(
(String) msgContext.getProperty(Constants.FAULT_NAME));
if(axisBindingMessage != null){
Integer code = (Integer) axisBindingMessage
.getProperty(WSDL2Constants.ATTR_WHTTP_CODE);
if (code != null) {
response.setStatus(code.intValue());
}
}
}
}
handleFault(msgContext, out, new AxisFault(t.toString(), t));
} catch (AxisFault e2) {
log.info(e2);
throw new ServletException(e2);
}
} finally {
closeStaxBuilder(msgContext);
TransportUtils.deleteAttachments(msgContext);
}
} else {
if (!disableREST) {
new RestRequestProcessor(Constants.Configuration.HTTP_METHOD_POST, request, response)
.processXMLRequest();
} else {
showRestDisabledErrorMessage(response);
}
}
}
/**
* Implementation for GET interface
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
initContextRoot(request);
// this method is also used to serve for the listServices request.
String requestURI = request.getRequestURI();
String query = request.getQueryString();
// There can be three different request coming to this.
// 1. wsdl, wsdl2 and xsd requests
// 2. list services requests
// 3. REST requests.
if ((query != null) && (query.indexOf("wsdl2") >= 0 ||
query.indexOf("wsdl") >= 0 || query.indexOf("xsd") >= 0 ||
query.indexOf("policy") >= 0)) {
// handling meta data exchange stuff
agent.initTransportListener(request);
agent.processListService(request, response);
} else if (requestURI.endsWith(".xsd") ||
requestURI.endsWith(".wsdl")) {
agent.processExplicitSchemaAndWSDL(request, response);
} else if (requestURI.endsWith(LIST_SERVICES_SUFIX) ||
requestURI.endsWith(LIST_FAUKT_SERVICES_SUFIX)) {
// handling list services request
try {
agent.handle(request, response);
} catch (Exception e) {
throw new ServletException(e);
}
} else if (!disableREST) {
new RestRequestProcessor(Constants.Configuration.HTTP_METHOD_GET, request, response)
.processURLRequest();
} else {
showRestDisabledErrorMessage(response);
}
}
/**
* Implementation of DELETE interface
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
initContextRoot(request);
// this method is also used to serve for the listServices request.
if (!disableREST) {
new RestRequestProcessor(Constants.Configuration.HTTP_METHOD_DELETE, request, response)
.processURLRequest();
} else {
showRestDisabledErrorMessage(response);
}
}
/**
* Implementation of PUT interface
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void doPut(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
initContextRoot(request);
// this method is also used to serve for the listServices request.
if (!disableREST) {
new RestRequestProcessor(Constants.Configuration.HTTP_METHOD_PUT, request, response)
.processXMLRequest();
} else {
showRestDisabledErrorMessage(response);
}
}
/**
* Private method that deals with disabling of REST support.
*
* @param response
* @throws IOException
*/
protected void showRestDisabledErrorMessage(HttpServletResponse response) throws IOException {
PrintWriter writer = new PrintWriter(response.getOutputStream());
writer.println("<html>Please enable REST support in WEB-INF/conf/axis2.xml " +
"and WEB-INF/web.xml</h2> |