Search code examples
javalocalhostbindrestletrestlet-2.3.1

Restlet framework: how to bind to localhost only?


I need to build a (standalone Java) restlet-based service that only listens on localhost, i.e. no requests from network are allowed.

I was trying to do the obvious:

Server srv = new Server(Protocol.HTTPS, "localhost", httpsPort); 
component.getServers().add(srv);

But the service still listens on 0.0.0.0. :-(

I went into the code and found that HttpsServerHelper ignores the hostname when creating the service:

this.server = HttpsServer.create(new InetSocketAddress(getHelped().getPort()), 0);

Similar code exists in plain HTTP's HttpServerHelper, where it is even more clear.

My question then is this:

How can I configure Restlet component/service to only listen on localhost?


Solution

  • I don't know which server you use under the hood within your standalone Restlet application. You should use a server connector other than the default one and I recommend you to use the Jetty one.

    To do that, simply put the jar of the extension org.restlet.ext.jetty in your classpath.

    In this case, using the following code should correspond to your needs:

    component.getServers().add(Protocol.HTTP, "localhost", 8182);
    

    Here is the corresponding trace at application startup:

    2015-09-03 09:47:22.180:INFO::jetty-7.1.6.v20100715
    2015-09-03 09:47:22.211:INFO::Started SelectChannelConnector@localhost:8182
    

    In addition, here is the link in the Restlet documentation regarding Restlet connectors: http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/base/connectors.

    Hope it helps you, Thierry