WSO2Con 2013 CFP Banner

How to Provide a Custom Socket Factory to be used in SSL Communication

Discuss this article on Stack Overflow
By Eran Chinthaka
  • 2 Jun, 2007
  • Level:  Introductory
  • Reads: 15294

Within Apache Axis2, Jakarta Commons HTTPClient is used to send messages with HTTP and HTTPS. HTTPClient uses JSSE (Java Secure Socket Extension) to provide the https functionality. Since JSSE is part of Java 1.4 (and in later versions) there is no special configuration required for this to work.

But in some situations the default socket factory implementation is not enough. So some users might want to provide their own socket factory implementation to work with SSL. In that case Axis2 can accept a socket factory from the user so that it will be used to send that message. Let's see how this can be done.

Eran Chinthaka
Software Engineer
WSO2 Inc.

First we need to create an instance of org.apache.commons.httpclient.protocol.Protocol.

Protocol myProtocolHandler = new Protocol("https", new MySSLSocketFactory(), 443);

MySSLSocketFactory should implement org.apache.commons.httpclient.protocol.ProtocolSocketFactory interface, which provides a method to get a new socket from a host name and port.

Then this needs to be passed to AxisEngine, through the message context.

messageContext.getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER, myProtocolHandler);

or via the Options in service client

serviceClient.getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER, myProtocolHandler);

That's it. When you try to send a message using HTTPS, with the above configured message context, Axis2 will use the provided socket factory implementation.

You can provide a virtual protocol name to be used with your new socket factory as well. In order to do this, you just have to register your new protocol with HTTPClient. Nothing else is required to be passed in to Axis engine.

Protocol.registerProtocol("myhttps", new Protocol("https", new MySSLSocketFactory(), 9443));

This will enable you to use URLs like myhttps://www.myendpoint.com with SSL support. This won't change anything if you use HTTPS. If you want to change the behaviour of HTTPS for the whole system, then use https instead of myhttps.

Protocol.registerProtocol("https", new Protocol("https", new MySSLSocketFactory(), 9443));

Applies To

Apache Axis2/Java, post 1.2 versions and nightly builds

JDK 1.4 or later

More Information

Apache Axis2/Java

Apache Jakarta Commons HTTPClient

The SSL Guide in Apache Commons HTTP Client - How to Provide a Custom Socket Factory to be used in SSL Communication only with HTTPClient