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"));

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"));



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

SOAPEnvelope response = inMsgtCtx.getEnvelope();


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:

auth = new HttpTransportProperties.Authenticator();

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

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

Ananth said...


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

Thanks in advance,

Amila Suriarachchi said...

Amit Roy 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.