Patch Axis 1.3 to Use java.net.Authenticator

 

Axis 1.3 based web services client does not support java.net.Authenticator. Follow the instruction below to enable java.net.Authenticator support.

Override org.apache.axis.transport.http.HTTPSender

package com.my.patch.axis;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;
import org.apache.axis.AxisFault;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.transport.http.HTTPSender;
 
/**
 * Based on com.liferay.util.axis.SimpleHTTPSender.
 * @see http://docs.liferay.com/portal/5.1/javadocs/util-java/com/liferay/util/axis/SimpleHTTPSender.html
 */
public class SimpleHTTPSender extends HTTPSender {
	public static final String CONTENT_TYPE = "Content-Type";
 
	public static final String CONTENT_LOCATION = "Content-Location";
 
	public static final String SOAP_ACTION = "SOAPAction";
 
    final static Logger logger =
            Logger.getLogger(SimpleHTTPSender.class.getName());
 
    @Override
    public void invoke(MessageContext ctx) throws AxisFault {
        String url = ctx.getStrProp(MessageContext.TRANS_URL);
        _invoke(ctx, url);
    }
 
    private void _invoke(MessageContext ctx, String url) throws AxisFault {
        try {
            URL urlObj = new URL(url);
 
            URLConnection urlc = urlObj.openConnection();
 
            _writeToConnection(urlc, ctx);
            _readFromConnection(urlc, ctx);
        } catch (Exception e) {
            throw AxisFault.makeFault(e);
        }
    }
 
    private void _writeToConnection(URLConnection urlc, MessageContext ctx)
            throws Exception {
        urlc.setDoOutput(true);
 
        Message request = ctx.getRequestMessage();
 
        String contentType = request.getContentType(ctx.getSOAPConstants());
 
        urlc.setRequestProperty(CONTENT_TYPE, contentType);
 
        if (ctx.useSOAPAction()) {
            urlc.setRequestProperty(SOAP_ACTION, ctx.getSOAPActionURI());
        }
 
        OutputStream out = new BufferedOutputStream(urlc.getOutputStream(),
                8192);
 
        request.writeTo(out);
 
        out.flush();
    }
 
    private void _readFromConnection(URLConnection urlc, MessageContext ctx)
            throws Exception {
 
        String contentType = urlc.getContentType();
        String contentLocation = urlc.getHeaderField(CONTENT_LOCATION);
 
        InputStream in = ((HttpURLConnection) urlc).getErrorStream();
 
        if (in == null) {
            in = urlc.getInputStream();
        }
 
        in = new BufferedInputStream(in, 8192);
 
        Message response = new Message(in, false, contentType, contentLocation);
        response.setMessageType(Message.RESPONSE);
 
        ctx.setResponseMessage(response);
    }
}

Point to New SimpleHTTPSender

* Create a new file named client-config.wsdd and place it in classpath Root. For example, for Maven file structure, place it in src/main/resources directory.

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig" xmlns="http://xml.apache.org/axis/wsdd/" 
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<transport name="http" pivot="java:com.my.patch.axis.SimpleHTTPSender" />
<transport name="https" pivot="java:com.my.patch.axis.SimpleHTTPSender"/>
<transport name="local" pivot="java:com.my.patch.axis.SimpleHTTPSender"/>
</deployment>

Test

* Axis 1.3 now should support java.net.Authenticator
* See Windows Kerberos Authentication with java.net.Authenticator on how to setup Kerberos for testing.

References

* com.liferay.util.axis.SimpleHTTPSender

This entry was posted in java, security and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.