public boolean processRequest(final SimpleHttpServerConnection conn,
final SimpleRequest request)
throws IOException {
MessageContext msgContext = null;
SimpleResponse response = new SimpleResponse();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
if (configurationContext == null) {
throw new AxisFault(Messages.getMessage("cannotBeNullConfigurationContext"));
}
InputStream inStream = request.getBody();
TransportOutDescription transportOut =
configurationContext.getAxisConfiguration().getTransportOut(
new QName(Constants.TRANSPORT_HTTP));
String cookieID = request.getCookieID();
SessionContext sessionContext = getSessionContext(cookieID);
msgContext = new MessageContext();
msgContext.setIncomingTransportName(Constants.TRANSPORT_HTTP);
msgContext.setConfigurationContext(configurationContext);
msgContext.setSessionContext(sessionContext);
msgContext.setTransportIn(configurationContext.getAxisConfiguration().getTransportIn(
new QName(Constants.TRANSPORT_HTTP)));
msgContext.setTransportOut(transportOut);
msgContext.setServerSide(true);
HttpVersion ver = request.getRequestLine().getHttpVersion();
if (ver == null) {
throw new AxisFault("HTTP version can not be Null");
}
if (HttpVersion.HTTP_1_0.equals(ver)) {
// httpVersion = HTTPConstants.HEADER_PROTOCOL_10;
} else if (HttpVersion.HTTP_1_1.equals(ver)) {
// httpVersion = HTTPConstants.HEADER_PROTOCOL_11;
/**
* Transport Sender configuration via axis2.xml
*/
this.transportOutConfiguration(configurationContext, response);
} else {
throw new AxisFault("Unknown supported protocol version " + ver);
}
msgContext.setProperty(MessageContext.TRANSPORT_OUT, baos);
// set the transport Headers
msgContext.setProperty(MessageContext.TRANSPORT_HEADERS, getHeaders(request));
msgContext.setServiceGroupContextId(UUIDGenerator.getUUID());
// This is way to provide access to the transport information to the transport Sender
msgContext.setProperty(Constants.OUT_TRANSPORT_INFO,
new SimpleHTTPOutTransportInfo(response));
msgContext.setProperty(Constants.Configuration.TRANSPORT_IN_URL, request.getRequestLine().getUri());
String soapAction = null;
if (request.getFirstHeader(HTTPConstants.HEADER_SOAP_ACTION) != null) {
soapAction = request.getFirstHeader(HTTPConstants.HEADER_SOAP_ACTION).getValue();
}
if (HTTPConstants.HEADER_GET.equals(request.getRequestLine().getMethod())) {
String uri = request.getRequestLine().getUri();
log.debug("HTTP GET:" + uri);
if (uri.equals("/favicon.ico")) {
response.setStatusLine(request.getRequestLine().getHttpVersion(), 301, "Redirect");
response.addHeader(new Header("Location", "http://ws.apache.org/favicon.ico"));
conn.writeResponse(response);
return true;
}
if (!uri.startsWith("/axis2/services/")) {
response.setStatusLine(request.getRequestLine().getHttpVersion(), 301, "Redirect");
response.addHeader(new Header("Location", "/axis2/services/"));
conn.writeResponse(response);
return true;
}
if (uri.indexOf("?") < 0) {
if (!(uri.endsWith("/axis2/services/") || uri.endsWith("/axis2/services"))) {
String serviceName = uri.replaceAll("/axis2/services/", "");
if (serviceName.indexOf("/") < 0) {
response.addHeader(new Header("Content-Type", "text/html"));
String res = HTTPTransportReceiver.printServiceHTML(serviceName, configurationContext);
byte[] buf = res.getBytes();
response.setBody(new ByteArrayInputStream(buf));
conn.writeResponse(response);
return true;
}
}
}
if (uri.endsWith("?wsdl")) {
String serviceName = uri.substring(uri.lastIndexOf("/") + 1, uri.length() - 5);
HashMap services = configurationContext.getAxisConfiguration().getServices();
AxisService service = (AxisService) services.get(serviceName);
if (service != null) {
response.addHeader(new Header("Content-Type", "text/xml"));
// String url = conn.getURL(uri.substring(1, uri.length() - 5));
String url = conn.getURL("");
int ipindex = url.indexOf("//");
String ip = null;
if (ipindex >= 0) {
ip = url.substring(ipindex + 2, url.length());
int seperatorIndex = ip.indexOf(":");
if (seperatorIndex > 0) {
ip = ip.substring(0, seperatorIndex);
}
}
service.printWSDL(baos, ip);
byte[] buf = baos.toByteArray();
response.setBody(new ByteArrayInputStream(buf));
conn.writeResponse(response);
return true;
}
}
if (uri.endsWith("?xsd")) {
String serviceName = uri.substring(uri.lastIndexOf("/") + 1, uri.length() - 4);
HashMap services = configurationContext.getAxisConfiguration().getServices();
AxisService service = (AxisService) services.get(serviceName);
if (service != null) {
response.addHeader(new Header("Content-Type", "text/xml"));
service.printSchema(baos);
byte[] buf = baos.toByteArray();
response.setBody(new ByteArrayInputStream(buf));
conn.writeResponse(response);
return true;
}
}
//cater for named xsds - check for the xsd name
if (uri.indexOf("?xsd=")>0) {
String serviceName = uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf("?xsd="));
String schemaName = uri.substring(uri.lastIndexOf("=") + 1);
HashMap services = configurationContext.getAxisConfiguration().getServices();
AxisService service = (AxisService) services.get(serviceName);
if (service != null) {
//run the population logic just to be sure
service.populateSchemaMappings();
//write out the correct schema
Hashtable schemaTable = service.getSchemaMappingTable();
XmlSchema schema = (XmlSchema)schemaTable.get(schemaName);
//schema found - write it to the stream
if (schema!=null){
response.addHeader(new Header("Content-Type", "text/xml"));
schema.write(baos);
byte[] buf = baos.toByteArray();
response.setBody(new ByteArrayInputStream(buf));
conn.writeResponse(response);
}else{
// no schema available by that name - send 404
response.setStatusLine(
request.getRequestLine().getHttpVersion(),
404, "Schema Not Found!");
}
return true;
}
}
// It is GET handle the Get request
boolean processed = HTTPTransportUtils.processHTTPGetRequest(
msgContext, baos,
soapAction, request.getRequestLine().getUri(),
configurationContext,
HTTPTransportReceiver.getGetRequestParameters(
request.getRequestLine().getUri()));
if (!processed) {
response.setStatusLine(request.getRequestLine().getHttpVersion(), 200, "OK");
response.addHeader(new Header("Content-Type", "text/html"));
response.setBodyString(
HTTPTransportReceiver.getServicesHTML(configurationContext));
setResponseHeaders(conn, request, response, 0, msgContext);
conn.writeResponse(response);
return true;
}
} else {
ByteArrayOutputStream baosIn = new ByteArrayOutputStream();
byte[] bytes = new byte[8192];
int size;
while ((size = inStream.read(bytes)) > 0) {
baosIn.write(bytes, 0, size);
}
inStream = new ByteArrayInputStream(baosIn.toByteArray());
// It is POST, handle it
HTTPTransportUtils.processHTTPPostRequest(msgContext, inStream, baos,
request.getContentType(), soapAction, request.getRequestLine().getUri());
}
OperationContext operationContext = msgContext.getOperationContext();
Object contextWritten = null;
if (operationContext != null) {
contextWritten = operationContext.getProperty(Constants.RESPONSE_WRITTEN);
}
if ((contextWritten != null) && Constants.VALUE_TRUE.equals(contextWritten)) {
response.setStatusLine(request.getRequestLine().getHttpVersion(), 200, "OK");
} else {
response.setStatusLine(request.getRequestLine().getHttpVersion(), 202, "OK");
}
byte[] buf = baos.toByteArray();
response.setBody(new ByteArrayInputStream(buf));
setResponseHeaders(conn, request, response, buf.length, msgContext);
conn.writeResponse(response);
} catch (Throwable e) {
if (!(e instanceof java.net.SocketException)) {
log.debug(e.getMessage(), e);
}
try {
AxisEngine engine = new AxisEngine(configurationContext);
if (msgContext != null) {
msgContext.setProperty(MessageContext.TRANSPORT_OUT, baos);
MessageContext faultContext = engine.createFaultMessageContext(msgContext, e);
response.setStatusLine(request.getRequestLine().getHttpVersion(), 500,
"Internal server error");
engine.sendFault(faultContext);
byte[] buf = baos.toByteArray();
response.setBody(new ByteArrayInputStream(buf));
setResponseHeaders(conn, request, response, buf.length, msgContext);
conn.writeResponse(response);
}
} catch (SocketException e1) {
log.debug(e1.getMessage(), e1);