Wednesday, December 06, 2006

a simple jsp SOAP proxy

Some time ago, I wrote a SOAP proxy in jsp using axis.
Please don't ask why we needed it. But maybe it would be useful to you. If the code below is exciting for you, maybe I'll send you a version that includes the dynamic fetching of the wsdl, and the handling of soap faults.

Incidentally, since this jsp might ignore the http headers from a SOAP fault, this type of proxy can help when you are encountering errors like this, or maybe even these:

So you could either, buy flash remoting, or use the following jsp proxy. Or you might be like us, we have already purchased Flex and flash remoting, and still insist on doing things the hard way :-)
<%@ page import = "
java.util.*,
java.io.*,
java.net.*,
org.apache.axis.*,
org.apache.axis.message.*,
org.apache.axis.message.SOAPFault,
org.apache.axis.server.*,
org.apache.axis.soap.SOAPConstants,
org.w3c.dom.Document,
org.xml.sax.InputSource,
java.io.StringReader,
org.apache.axis.utils.*,
org.apache.axis.client.*,
org.apache.axis.encoding.*
" %><%

response.setContentType("text/xml;charset=UTF-8");

String wsdl = (String) request.getParameter("wsdl");
if (wsdl != null) {

%>Your wsdl xml goes here<%

} else {

String endPoint = "http://localhost:8080/axis/services/Version";
int timeout = 60000;

String method = (String) request.getMethod();

if (method.equalsIgnoreCase("POST")) {

String soapAction = (String) request.getHeader("soapaction");
int contentLength = request.getContentLength();

byte ba[] = new byte[contentLength];
request.getInputStream().read(ba);
ByteArrayInputStream inputStream = new ByteArrayInputStream(ba);
StringBuffer sb = new StringBuffer();
sb.append(new String(ba));

System.out.println("==== soapRequest ====");
System.out.println(sb.toString());

SOAPEnvelope soapRequest = new SOAPEnvelope();
SOAPEnvelope soapResponse = new SOAPEnvelope();

try {
soapRequest = new SOAPEnvelope(inputStream);
Service service = new Service();
Call call = (Call) service.createCall();
call.setMaintainSession(false);
call.setTargetEndpointAddress(new URL(endPoint));
call.setTimeout(new Integer(timeout));
if (soapAction == null) {
call.setUseSOAPAction(false);
} else {
if (soapAction.equals("")) {
call.setUseSOAPAction(false);
} else {
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapAction);
}
}
soapResponse = (SOAPEnvelope) call.invoke(soapRequest);
} catch (Exception e) {
e.printStackTrace();
} finally {
inputStream.close();
}

System.out.println("==== soapResponse ====");
System.out.println(soapResponse.toString());
%><%=soapResponse.toString()%><%
}

if (method.equalsIgnoreCase("GET")) {
%>method="GET" is not implimented here<%
}
}
%>
You can test this jsp pretty easily with something like http://www.soapui.org/ We also have a version of soapui written in flash, so you can put that swf in those hard to get to places on your network for trouble shooting, or when soapui will not authenticate easily in the "https only" areas of your network.

Have fun.

No comments: