Search code examples
httpgwtdatehttp-request

GWT RequestBuilder set client Date to HTTP Date POST header


I'm using GWT RPC to communicate between client and server. I want to be able to read the browser's date on the server side, and for that I'm using setRpcRequestBuilder from the class ServiceDefTarget to costumize my request, and add the header I want.

On the client side I'm using:

private static final RpcRequestBuilder rpcReqBuilder = new RpcRequestBuilder() {

    @Override
    protected RequestBuilder doCreate(String serviceEntryPoint) {

        RequestBuilder builder = super.doCreate(serviceEntryPoint);
        builder.setHeader("Date1", new Date().toString()); 
        return builder;

    }
};

 ......

((ServiceDefTarget) greetingService).setRpcRequestBuilder(rpcReqBuilder); 

//rpc call
greetingService.greetServer(.........)

On the server side I do:

HttpServletRequest request = this.getThreadLocalRequest();
    Enumeration<?> enumeration = request.getHeaderNames();
    while (enumeration.hasMoreElements()) {
        String name = (String) enumeration.nextElement();
        String value = request.getHeader(name);
        System.out.println(name + ": " + value);
    }

which among all the default headers prints

Date1: Tue Apr 10 12:19:28 BST 2012

Ok, this works fine, but when I try to set the "Date" header, then it doesn't show up on the server side. Why is that? Anybody can help. I'll be very helpfull. :)


Solution

  • Date is a predefined header of HTTP, and by definition, XMLHttpRequest (the thing behind GWT's RequestBuilder) cannot let you set it to an arbitrary value.

    Anyway, when crafting your own headers, you should add a prefix to avoid conflicts with other things on the network adding headers, something like MyApp-Date or X-MyApp-Date (like GWT does it with X-GWT-Permutation and X-GWT-Module-Base in GWT-RPC and RequestFactory)