| Method from org.apache.axis2.transport.http.AxisServlet Detail: |
protected MessageContext createMessageContext(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
return createMessageContext(req, resp, true);
}
This method assumes, that the created MessageContext will be used in only SOAP invocation. |
protected MessageContext createMessageContext(HttpServletRequest request,
HttpServletResponse response,
boolean invocationType) throws IOException {
MessageContext msgContext = configContext.createMessageContext();
String requestURI = request.getRequestURI();
String trsPrefix = null;
int sepindex = -1;
// Support older servlet API's
try {
trsPrefix = request.getRequestURL().toString();
} catch (Throwable t){
log.info("Old Servlet API (Fallback to HttpServletRequest.getRequestURI) :" + t);
trsPrefix = request.getRequestURI();
}
sepindex = trsPrefix.indexOf(':");
if (sepindex > -1) {
trsPrefix = trsPrefix.substring(0, sepindex);
msgContext.setIncomingTransportName(trsPrefix);
} else {
msgContext.setIncomingTransportName(Constants.TRANSPORT_HTTP);
trsPrefix = Constants.TRANSPORT_HTTP;
}
TransportInDescription transportIn =
axisConfiguration.getTransportIn(msgContext.getIncomingTransportName());
//set the default output description. This will be http
TransportOutDescription transportOut = axisConfiguration.getTransportOut(trsPrefix);
if (transportOut == null) {
// if the req coming via https but we do not have a https sender
transportOut = axisConfiguration.getTransportOut(Constants.TRANSPORT_HTTP);
}
msgContext.setTransportIn(transportIn);
msgContext.setTransportOut(transportOut);
msgContext.setServerSide(true);
if (!invocationType) {
String query = request.getQueryString();
if (query != null) {
requestURI = requestURI + "?" + query;
}
}
msgContext.setTo(new EndpointReference(requestURI));
msgContext.setFrom(new EndpointReference(request.getRemoteAddr()));
msgContext.setProperty(MessageContext.REMOTE_ADDR, request.getRemoteAddr());
msgContext.setProperty(Constants.OUT_TRANSPORT_INFO,
new ServletBasedOutTransportInfo(response));
// set the transport Headers
msgContext.setProperty(MessageContext.TRANSPORT_HEADERS, getTransportHeaders(request));
msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST, request);
msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE, response);
try {
ServletContext context = getServletContext();
if(context != null) {
msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT, context);
}
} catch (Exception e){
log.debug(e.getMessage(), e);
}
//setting the RequestResponseTransport object
msgContext.setProperty(RequestResponseTransport.TRANSPORT_CONTROL,
new ServletRequestResponseTransport(response));
return msgContext;
}
|
public void destroy() {
//stoping listner manager
try {
if (configContext != null) {
configContext.terminate();
}
} catch (AxisFault axisFault) {
log.info(axisFault.getMessage());
}
try {
super.destroy();
} catch (Exception e) {
log.info(e.getMessage());
}
}
distroy the ConfigurationContext |
protected void doDelete(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
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 DELETE interface |
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
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) && new QueryStringParser(query).search(metadataQueryParamNames)) {
// 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_SUFFIX) ||
requestURI.endsWith(LIST_FAULTY_SERVICES_SUFFIX)) {
// 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 for GET interface |
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
//set the initial buffer for a larger value
try {
response.setBufferSize(BUFFER_SIZE);
} catch (Throwable t){
log.info("Old Servlet API :" + t);
}
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;
String url;
try {
url = request.getRequestURL().toString();
} catch (Throwable t){
log.info("Old Servlet API (fallback to HttpServletRequest.getRequestURI) :" + t);
url = request.getRequestURI();
}
InvocationResponse pi = HTTPTransportUtils.
processHTTPPostRequest(msgContext,
new BufferedInputStream(request.getInputStream()),
new BufferedOutputStream(out),
contentType,
request.getHeader(HTTPConstants.HEADER_SOAP_ACTION),
url);
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);
}
}
}
Implementaion of POST interface |
protected void doPut(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
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);
}
}
Implementation of PUT interface |
public EndpointReference getEPRForService(String serviceName,
String ip) throws AxisFault {
return getEPRsForService(serviceName, ip)[0];
}
|
public EndpointReference[] getEPRsForService(String serviceName,
String ip) throws AxisFault {
//RUNNING_PORT
String port = (String) configContext.getProperty(ListingAgent.RUNNING_PORT);
if (port == null) {
port = "8080";
}
if (ip == null) {
try {
ip = Utils.getIpAddress(axisConfiguration);
if (ip == null) {
ip = "localhost";
}
} catch (SocketException e) {
throw AxisFault.makeFault(e);
}
}
String endpointRefernce = "http://" + ip + ":" + port;
if (configContext.getServiceContextPath().startsWith("/")) {
endpointRefernce = endpointRefernce +
configContext.getServiceContextPath() + "/" + serviceName;
} else {
endpointRefernce = endpointRefernce + '/" +
configContext.getServiceContextPath() + "/" + serviceName;
}
EndpointReference endpoint = new EndpointReference(endpointRefernce + "/");
return new EndpointReference[]{endpoint};
}
|
public SessionContext getSessionContext(MessageContext messageContext) {
HttpServletRequest req = (HttpServletRequest) messageContext.getProperty(
HTTPConstants.MC_HTTP_SERVLETREQUEST);
SessionContext sessionContext =
(SessionContext) req.getSession(true).getAttribute(
Constants.SESSION_CONTEXT_PROPERTY);
String sessionId = req.getSession().getId();
if (sessionContext == null) {
sessionContext = new SessionContext(null);
sessionContext.setCookieID(sessionId);
req.getSession().setAttribute(Constants.SESSION_CONTEXT_PROPERTY,
sessionContext);
}
messageContext.setSessionContext(sessionContext);
messageContext.setProperty(SESSION_ID, sessionId);
return sessionContext;
}
Transport session management. |
protected Map getTransportHeaders(HttpServletRequest req) {
return new TransportHeaders(req);
}
Get all transport headers. |
protected void handleFault(MessageContext msgContext,
OutputStream out,
AxisFault e) throws AxisFault {
msgContext.setProperty(MessageContext.TRANSPORT_OUT, out);
MessageContext faultContext =
MessageContextBuilder.createFaultMessageContext(msgContext, e);
// SOAP 1.2 specification mentions that we should send HTTP code 400 in a fault if the
// fault code Sender
HttpServletResponse response =
(HttpServletResponse) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE);
if (response != null) {
//TODO : Check for SOAP 1.2!
SOAPFaultCode code = faultContext.getEnvelope().getBody().getFault().getCode();
OMElement valueElement = null;
if (code != null) {
valueElement = code.getFirstChildWithName(new QName(
SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI,
SOAP12Constants.SOAP_FAULT_VALUE_LOCAL_NAME));
}
if (valueElement != null) {
if (SOAP12Constants.FAULT_CODE_SENDER.equals(valueElement.getTextAsQName().getLocalPart())
&& !msgContext.isDoingREST()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}
}
AxisEngine.sendFault(faultContext);
}
|
public void init() throws ServletException {
if (this.servletConfig != null
&&
!initCalled) {
init(this.servletConfig);
}
}
Convenient method to re-initialize the ConfigurationContext |
public void init(ServletConfig config) throws ServletException {
// prevent this method from being called more than once per instance
initCalled = true;
super.init(config);
try {
this.servletConfig = config;
ServletContext servletContext = servletConfig.getServletContext();
this.configContext =
(ConfigurationContext) servletContext.getAttribute(CONFIGURATION_CONTEXT);
if(configContext == null){
configContext = initConfigContext(config);
config.getServletContext().setAttribute(CONFIGURATION_CONTEXT, configContext);
}
axisConfiguration = configContext.getAxisConfiguration();
ListenerManager listenerManager = new ListenerManager();
listenerManager.init(configContext);
TransportInDescription transportInDescription = new TransportInDescription(
Constants.TRANSPORT_HTTP);
transportInDescription.setReceiver(this);
listenerManager.addListener(transportInDescription, true);
listenerManager.start();
ListenerManager.defaultConfigurationContext = configContext;
agent = new ListingAgent(configContext);
initParams();
} catch (Exception e) {
throw new ServletException(e);
}
}
|
public void init(ConfigurationContext axisConf,
TransportInDescription transprtIn) throws AxisFault {
}
init(); start() and stop() wouldn't do anything. |
protected ConfigurationContext initConfigContext(ServletConfig config) throws ServletException {
try {
ConfigurationContext configContext =
ConfigurationContextFactory
.createConfigurationContext(new WarBasedAxisConfigurator(config));
configContext.setProperty(Constants.CONTAINER_MANAGED, Constants.VALUE_TRUE);
return configContext;
} catch (Exception e) {
log.info(e);
throw new ServletException(e);
}
}
Initialize the Axis configuration context |
public void initContextRoot(HttpServletRequest req) {
if (contextRoot != null && contextRoot.trim().length() != 0) {
return;
}
String contextPath = null;
// Support older servlet API's
try {
contextPath = req.getContextPath();
} catch (Throwable t) {
log.info("Old Servlet API (Fallback to HttpServletRequest.getServletPath) :" + t);
contextPath = req.getServletPath();
}
//handling ROOT scenario, for servlets in the default (root) context, this method returns ""
if (contextPath != null && contextPath.length() == 0) {
contextPath = "/";
}
this.contextRoot = contextPath;
configContext.setContextRoot(contextRoot);
}
Set the context root if it is not set already. |
protected void initParams() {
Parameter parameter;
// do we need to completely disable REST support
parameter = axisConfiguration.getParameter(Constants.Configuration.DISABLE_REST);
if (parameter != null) {
disableREST = !JavaUtils.isFalseExplicitly(parameter.getValue());
}
// Should we close the reader(s)
parameter = axisConfiguration.getParameter("axis2.close.reader");
if (parameter != null) {
closeReader = JavaUtils.isTrueExplicitly(parameter.getValue());
}
}
Initializes the Axis2 parameters. |
protected void showRestDisabledErrorMessage(HttpServletResponse response) throws IOException {
PrintWriter writer = new PrintWriter(response.getOutputStream());
writer.println("< html >< body >< h2 >Please enable REST support in WEB-INF/conf/axis2.xml " +
"and WEB-INF/web.xml< /h2 >< /body >< /html >");
writer.flush();
response.setStatus(HttpServletResponse.SC_ACCEPTED);
}
Private method that deals with disabling of REST support. |
public void start() throws AxisFault {
}
|
public void stop() throws AxisFault {
}
|