views:

1084

answers:

1

Hi,

I'd like to setup RMI to pass through port 80 on a java servlet.. (To pass through the firewall) I have a Tomcat server and i'm looking for the module that will catch the RMI http request and pass it to the RMI layer on the server machine. I've read that RMI automatically handles this on it's side.

Is there a step-by-step guide to this? Or just a war file to install on the server?

Thanks.

+2  A: 

We've done this on a production server, but it was a long time ago. There's no war file to install as this is a pretty unusual thing to do, especially these days with AJAX and web-enabled apps and what-not. I wouldn't recommend deploying RMI over the Internet for reasons of scalability and reliability. It's great across LANs or even JVMs but on a wider-scale I'd consider a more conventional web service if possible.

That aside, there's a so-so guide here (I've linked to a google cache because I got some phishing warning from McAfee). It's for Tomcat 3 but the concepts are still the same, and it mentions an integration with Apache which is quite redundant for Servlet use, so there's chunks of it you can ignore if you're already quite au fait with Tomcat.

It will guide you through installing a servlet in Tomcat which will unpack an RMI request that's piggy-backing on an HTTP Post, locate the local registry and complete the remote method call for you. It also provides a little demo server and client application to test your set up with.


Edit in response to comments:

I've found a servlet that will mimic /cgi-bin/java-rmi.cgi but to use it I have to direct all requests to /RMIServlet without letting the client know that it is being redirected. I googled a lot for this and tried numerous solution. All without success. What's the best way to do this?

I think this is done by providing a SocketFactory to RMI that forgoes the usual port 1099 route and piggybacks the request on an HTTP/Post instead.

The socket factory itself is something like this: You can make it work with HTTPS just by changing the URL object.

public class RMIHttpToCgiSocketFactory extends RMISocketFactory implements Serializable {
    public static final long serialVersionUID = 1L;

    public RMIHttpToCgiSocketFactory(){}

    public Socket createSocket( String s, int i ) throws IOException {
        // Whacked out indentation for StackOverflow! Hope you can read this.
        return new HttpsSendSocket( 
            s, 
            i,
            new URL(
                "http", 
                s, 
                "/cgi-bin/java-rmi.cgi?forward="+i
            )
        );
    }

    public ServerSocket createServerSocket( int i ) {
        return null;
    }
}

Now you just need to tell your application to use this socket factory in its RMI dealings. In our codebase this is done on both the server and the client. On the server side when your RMI service is binding itself to the registry you can specify the client socket factory when export the object to get the Remote stub.

RMIClientSocketFactory clientSocketFactory = new RMIHttpToCgiSocketFactory();
Remote stub = UnicastRemoteObject.exportObject( 
    server, 
    port, 
    clientSocketFactory, 
    RMISocketFactory.getDefaultSocketFactory() 
);

And on the client you can specify the socket factory when finding the registry.

Registry registry = LocateRegistry.getRegistry(
    loginHostName, 
    portNumber, 
    new RMIHttpToCgiSocketFactory()
);
banjollity
I've found a servlet that will mimic /cgi-bin/java-rmi.cgi but to use it I have to direct all requests to /RMIServlet without letting the client know that it is being redirected.I googled a lot for this and tried numerous solution. All without success. Most of my attempts combined apache httpd + tomcat as connector.No success there. (In a nutshell, I tried - jkmount, proxypass and rewrite)What's the best way to do this?
Ben