Search code examples
javarestlet

How to attach a restlet to a partiular URL path scheme (HTTP or HTTPS)?


I'm using Restlet 2.3.2

I'd like to run a Restlet server with both HTTP and HTTPS protocols, but I'd like to attach a different restlet to each one. For exemple, restletUnsecure to HTTP but restletSecure to HTTPS. Actualy, attaching only one restlet to the path /test works properly.

I tried putting the scheme in the URI while attaching, like this, but this does not work (I get a page not found with my browser) :

Server server = component.getServers().add(Protocol.HTTPS, 8443);
component.getServers().add(Protocol.HTTP, 8080);
[...]
component.getDefaultHost().attach("https://localhost:8443/test", restletSecure);
component.getDefaultHost().attach("http://localhost:8080/test", restletUnsecure);

How can I achieve this ?


Solution

  • I think that you should define specific virtual hosts HTTP and HTTPS requests, as described below:

    Component component = (...)
    
    // HTTPS
    VirtualHost hostHttps
          = new VirtualHost(component.getContext());
    component.getHosts().add(hostHttps);
    hostHttps.setHostScheme("https");
    
    Restlet restletSecure = (...)
    hostHttps.attachDefault(restletSecure);
    
    // HTTP
    VirtualHost hostHttp
          = new VirtualHost(component.getContext());
    component.getHosts().add(hostHttp);
    hostHttp.setHostScheme("http");
    
    Restlet restletUnsecure = (...)
    hostHttp.attachDefault(restletUnsecure);
    

    Hope it helps you, Thierry