Httpg protocol

Although httpg protocol is now obsolete, there are still some services which use it. It is possible to access such a service from Vine Toolkit modules, however one should not forget about some tricks. To see the complete and working example, look at srm project, paying special attention to SrmFileManager.java class, implementing file system plugin for gLite Storage Resource Management.

All hints, presented below, are snippets of codes taken from SrmFileManager.java class.

1) First problem, which can be encountered is class loading and jar conflicts. To avoid this problem, store current classloader and switch to the one appropriate for the current class.

    ClassLoader savedClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

2) Register types of communication transport, especially the one for the httpg protocol.

        SimpleProvider provider = new SimpleProvider();
        provider.deployTransport(
                "httpg", new SimpleTargetedChain(new GSIHTTPSender()));
        provider.deployTransport(
                "https", new SimpleTargetedChain(new HTTPSSender()));
        provider.deployTransport(
                "http", new SimpleTargetedChain(new HTTPSender()));
        AxisClient ac = new AxisClient(provider);
        SRMServiceLocator locator = new SRMServiceLocator(ac.getClientEngine().getConfig());
        locator.setEngine(ac);

3) Create an httpg url.

        HttpgURLStreamHandlerFactory httpgfac = new HttpgURLStreamHandlerFactory();
        ISRM isrm = null;
        URL serviceURL = null;
        try {
            SrmResource rs = (SrmResource) getResource();        	
        	String rootContext = rs.getRootContext();
            serviceURL = new URL("httpg", fileResourceLocation.getHost(), fileResourceLocation.getPort(), fileResourceLocation.getPath() + rootContext, httpgfac.createURLStreamHandler("httpg"));
            isrm = locator.getsrm(serviceURL);
            log.debug("isrm" + isrm);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

4) HttpgURLStreamHandlerFactory is a custom Vine Toolkit class.

import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import org.globus.net.protocol.httpg.Handler;
public class HttpgURLStreamHandlerFactory implements URLStreamHandlerFactory {
    public URLStreamHandler createURLStreamHandler(String protocol) {
        if(protocol.equalsIgnoreCase("httpg")){
            return new Handler();
        }
        return null;
    }
}

5) Register and initialize transport for httpg protocol.

        Util.reregisterTransport();

        if (MessageContext.getCurrentContext()!= null)
            MessageContext.getCurrentContext().setClassLoader(this.getClass().getClassLoader());
        if (MessageContext.getCurrentContext()!= null)
            MessageContext.getCurrentContext().setTransportName("httpg");

        Call.initialize();
        Call.addTransportPackage("org.globus.axis.transport");
        Call.setTransportForProtocol("httpg", org.globus.axis.transport.GSIHTTPTransport.class);

6) Switch the classloader to the previously stored one.

        Thread.currentThread().setContextClassLoader(savedClassLoader);

7) Axis class 'HTTPSender' was reimplemented a little, recompiled and served instead of original one by Vine classloader engine:

org.apache.axis.transport.http.HTTPSender of the axis 1.4 distribution.

Method invoke was changed:

public void invoke(MessageContext msgContext) throws AxisFault;

after the line:

String someUrl = msgContext.getStrProp(MessageContext.TRANS_URL);

the block of code was added to reflect any 'httpg' urls:

          log.debug("HTTPSender::httpg url retrieved: "+someUrl);
            if (someUrl.startsWith("httpg")) {
                boolean isPort = false;
                int indexA = someUrl.indexOf("://");
                indexA +=3;
                int indexB = someUrl.indexOf(":",indexA);
                if (indexB==-1)
                    indexB = someUrl.indexOf("/",indexA);
                else
                    isPort = true;
                String host = someUrl.substring(indexA,indexB);
                String port = null;
                String path = null;
                if (isPort) {
                    int indexNext = someUrl.indexOf("/",indexB);
                    if (indexNext>0) {
                        port = someUrl.substring(indexB+1,indexNext);
                        path = someUrl.substring(indexNext);
                    } else {
                        port = someUrl.substring(indexB+1);
                        path = "/";
                    }
                } else {
                    path = someUrl.substring(indexB);
                }
                log.debug("HTTPSender::httpg url host: "+host);
                log.debug("HTTPSender::httpg url port: "+port);
                log.debug("HTTPSender::httpg url path: "+path);
                int portVal;
                if (port!=null)
                    portVal = Integer.valueOf(port);
                else
                    portVal = 0;
                HttpgURLStreamHandlerFactory httpgfac = new HttpgURLStreamHandlerFactory();
                targetURL = new URL("httpg",host,portVal,path,httpgfac.createURLStreamHandler("httpg"));
            }
            else
                targetURL = new URL(msgContext.getStrProp(MessageContext.TRANS_URL));

The class source could be located here.