Thursday, September 24, 2009

Sending an arbitrary soap message with Axis2

Axis2 code generation strictly follows the WS-I profile which prohibits sending soap messages with two child elements in the soap body. Even normal Service client API allows only one omElement to be send at the soap body.
One work around to send two elements is to use an operation client which allows a user to set whole SoapEnvelope hence sending any arbitrary one.

ServiceClient serviceClient = new ServiceClient();
OperationClient opClient = serviceClient.createClient(ServiceClient.ANON_OUT_IN_OP);
//creating message context
MessageContext outMsgCtx = new MessageContext();
Options opts = outMsgCtx.getOptions();
//setting properties into option
opts.setTo(new EndpointReference("http://localhost:8088/axis2/services/TestInOutService"));
opts.setAction("urn:TestInOutService");

SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();

OMElement omElement1 = soapFactory.createOMElement(new QName("http://axis2.test","firstElement"));
OMElement omElement2 = soapFactory.createOMElement(new QName("http://axis2.test","secondElement"));

soapEnvelope.getBody().addChild(omElement1);
soapEnvelope.getBody().addChild(omElement2);

outMsgCtx.setEnvelope(soapEnvelope);
opClient.addMessageContext(outMsgCtx);
opClient.execute(true);

MessageContext inMsgtCtx = opClient.getMessageContext("In");

SOAPEnvelope response = inMsgtCtx.getEnvelope();
System.out.println(response);

5 comments:

meisert said...

Great writeup. This helped me get a SOAP client written up quickly in Axis2.

I'll add that I also needed Basic Authentication which is common. I used this code:

HttpTransportProperties.Authenticator
auth = new HttpTransportProperties.Authenticator();


auth.setUsername("superadmin"); auth.setPassword("labsipx");

options.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth);

Ananth said...

Hi,

Good posting ! Can you please tell how do we read soap message with attachment in a service method in Axis2.

Thanks in advance,
Ananth

Amila Suriarachchi said...

http://ws.apache.org/axis2/1_5_1/mtom-guide.html

Unknown said...

Hi i am following your post to create my client that will invoke my secure esb's proxy service. I have used usernametoken as my authentication scenario. I was wondering what is ANON_OUT_IN_OP that you mentioned in your blog? Waiting for your response

merryloct said...

Mesmerized article written on this blog with other relevant information. It is straight to the point that how we can improve our skills as well as how we can be represented to a new stream of professionalism.
โรงงานสบู่