Question

In Java, there is ThreadLocal, which can be used to carry some data from one object to another without explicit passing as method argument.

I need to intercept GWT request and extract custom HTTP header from it, then I need to store the header value somehow to be processed later.

The problem is that the place to extract the header belongs to RequestBuilder, and there is no way (?) to pass the variable from within RequestBuilder to the custom code actually handling the request/response from server. And it is not possible to pass some variable from client code to that request builder.

ThreadLocal could be the solution, however it is not available in GWT. Is there something I can use?

Was it helpful?

Solution

You can use RequestBuilder.setHeader to set header values for your HTTP request.
On the backend you can use HttpServletRequest of your servlet to retrieve the header values from your HTTP request.

Update:

Some class with a static instance variable:

public class SomeClass {
    public static String myVar;
}

And in the RequestBuilder code you can do following:

RequestBuilder request = new RequestBuilder(url);
request.setCallback(new RequestCallback() {
    @Override
    public void onResponseReceived(Request request, Response response) {
        SomeClass.myVar = response.getHeader("someheader");
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top