Hi Yuri,
> Looking at Peter's code I had a question about the best way
> to use a client
> connector from within a Restlet application. Peter's code is
> instantiating a new HTTP client connector and using.
All applications hosted within a given component should share the same
server and client connectors. This is possible because those connectors are
mostly stateless in nature.
> My impression from the design is that Client connectors,
> along with Server
> connectors should be added to the Component and automatically
> selected based on
> the url when dispatching calls using getDispatcher(). Is that correct?
Yes, this is the intended behavior.
> Specifically when using Restlet embedded in a servlet
> container, how do we get
> access to the underlying Component created by the
> ServerServlet? And when should
> one add the client connectors to it?
This is a tricky case here that will require you to extend the default
ServerServlet class. You can override the createServer() method and do
something like:
@Override public HttpServerHelper createServer(HttpServletRequest
request) {
HttpServerHelper result = super.createServer(request);
if(firstCall) {
Component comp = getComponent();
comp.getClients().add(new Client(comp.getContext(),
Protocol.HTTP);
firstCall = false;
}
}
You should of course declare a "firstCall" member variable.
> Last question, related to dispatching calls to client
> connectors is about
> request, response and request headers: what if I want to
> dispatch N number of
> times before returning a response to the browser and for
> every dispatch there
> are, for instance, different headers I want to submit (maybe
> authentication
> headers, for instance, or a X-JSON header. Should I save the
> current state of
> the request, response and modify them for each dispatch,
> reverting to the saved
> one before returning the response?
You should create a new set of Request/Response instances for your client
calls, separate from your current server call. You can try reusing/modifying
the client Request instances as long as you ensure that any input
entity/representation has fresh content to submit if necessary (PUT/POST).
Best regards,
Jerome