Hello John,
I send you a sample code that works for me (with current trunk of
Restlet and HttpClient 3.1). Could you run it, and maybe send a
reproductible test?
Best regards,
Thierry Boileau
> I am trying to run a Component that provides 2 different HTTP
> services, both using the same domain, but on different ports. The
> specific ports are configurable, but in my testing, I'm using 8081 and
> 8443 for one service, and 9999 for the other. So my goal is to route
> to 2 different apps when I receive requests: go to app 1 on
> "myhost.com:8081" and "myhost.com:8443", and go to app 2 on
> "myhost.com:9999".
>
> My initial solution is to create 2 different VirtualHost objects, call
> setHostPort() on each of them, and let the routing work that way.
> When I'm using a web browser, this seems to work fine.
>
> I have an issue that I can't seem to solve, though. One of my test
> clients connects using apache.commons.HttpClient, and my server can't
> route its requests - the routing fails and I get 404s. I suspect it's
> because the "Host" header isn't being populated properly, but in all
> my fiddling with the client program, I haven't been able to get it to
> work.
>
> I can continue trying to fix the test client, but I wonder if my
> approach is the best way to handle this. I also wonder if I'm going
> to have further problems when I try to run the server with app 1 bound
> to port 80 and 443. Is the routing going to continue working if the
> requests come with the default ports? I thought the solution to that
> might be to make app 1 the components default host, but that seems to
> break all routing to app 2; it never receives any calls, even when its
> port number is specified in the request (such as with a web browser as
> the client.)
>
> Thanks for any thoughts!
>
> --------------------------------
> John Wismar
> mailto:[email protected]
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2393547
package virtualHosts;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.restlet.Component;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.routing.VirtualHost;
public class TestVirtualHostPort {
public static void main(String[] args) throws Exception {
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
component.getServers().add(Protocol.HTTP, 8183);
// Application A
Restlet restletA = new Restlet() {
@Override
public void handle(Request request, Response response) {
System.out.println("host A " + request.getResourceRef());
}
};
// Application B
Restlet restletB = new Restlet() {
@Override
public void handle(Request request, Response response) {
System.out.println("host B " + request.getResourceRef());
}
};
// Virtual hosts
VirtualHost hostA = new VirtualHost();
hostA.setHostPort("8182");
component.getHosts().add(hostA);
VirtualHost hostB = new VirtualHost();
hostB.setHostPort("8183");
component.getHosts().add(hostB);
hostA.attachDefault(restletA);
hostB.attachDefault(restletB);
component.start();
HttpClient client = new HttpClient();
HttpMethod method8182 = new GetMethod("http://localhost:8182/");
HttpMethod method8183 = new GetMethod("http://localhost:8183/");
// Prints on the console : host A http://localhost:8182/
client.executeMethod(method8182);
// Prints on the console : host B http://localhost:8183/
client.executeMethod(method8183);
component.stop();
}
}