Dynamic headers with REST dispatch

319 views
Skip to first unread message

Butl Nor

unread,
Mar 5, 2015, 11:12:34 AM3/5/15
to gwt-pl...@googlegroups.com
Hello!

In my project, I am using GWTP rest dispatch API. I have successfully defined REST method on the server, and asynchronously call that method from GWT client.

However, I am using a session header, that is not constant, and is so global, that I will not add it as a parameter to _every_ REST method. This header is dynamic, as it's changed whenever the user logs in or logs out. But GWTP doesn't allow me to specify such dynamic headers. It only allows me to specify static global headers. Which is unusable for my case.

I have tried to use RestDispatchAsyncModule.Builder.dispatchHooks to intercept the calls. This worked. And I was able to get the action's parameters. But this method doesn't allow me to add my own parameters, as getParameters() makes a copy of the parameter list.

Do I have any option to do what I want? To specify the headers for every REST method, that are resolved before each call?

Regards,
Butl

Riccardo Camanzi

unread,
Mar 23, 2015, 4:53:21 AM3/23/15
to gwt-pl...@googlegroups.com
Hi Buti,
I have your same problem and question!
The dispatchHooks method is great but if can't modify header/query/form parameter, how it should be used?

Do you have any news?

Riccardo

Butl Nor

unread,
Mar 26, 2015, 10:57:00 AM3/26/15
to gwt-pl...@googlegroups.com
I gave up with GWTP and wrote my own lightweight REST client framework, which gives me flexibility, works with GWT superdev incremental mode and doesn't have millions of .jar dependencies. My code has around 500 lines of code, and does exactly the same as GWT REST dispatch.

So, Riccardo, I cannot help you, unfortunately.

Christopher Viel

unread,
Mar 26, 2015, 6:45:08 PM3/26/15
to gwt-pl...@googlegroups.com
You can use Interceptors for that purpose. Actions are immutable, but using the interceptor, you can create a new one with your extra parameters and continue the execution with that action. Interceptors were introduced by an external contributor and we didn't have time to document the feature yet. In the meanwhile can have a look at the javadoc and the carstore for an example on how to configure interceptors. I will gladly answer any further questions.

-----

Sorry to hear that you had to write another tool. We are very receptive to feature requests on github and take the time to discuss them both internally and with the community.

I could go over each dependencies and explain why each one is being used. I assure you the bare minimum is being used in order to offer a smooth experience. I am not a fan of repackaging and strongly encourage the use of a dependency management tool. Rest-Dispatch is a framework based on JSR-311, it uses DI (which we believe is a good development practice) and it relies on a great library to perform serialization. The process of eliminating Guava has started in 1.4 and should be complete in 1.5.

"Works with superdev incremental": There are know issues with the latest GWT and targeted improvements, but it is compatible with superdev. All those things are going to improve with further releases.

On Thu, Mar 26, 2015 at 10:57 AM Butl Nor <budalon...@gmail.com> wrote:
I gave up with GWTP and wrote my own lightweight REST client framework, which gives me flexibility, works with GWT superdev incremental mode and doesn't have millions of .jar dependencies. My code has around 500 lines of code, and does exactly the same as GWT REST dispatch.

So, Riccardo, I cannot help you, unfortunately.

--
You received this message because you are subscribed to the Google Groups "GWTP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gwt-platform+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Riccardo Camanzi

unread,
Mar 27, 2015, 10:09:01 AM3/27/15
to gwt-pl...@googlegroups.com
Hi Christopher,
thanks for your suggestions.
I've successfully configured Interceptor for each call but now I don't know how add a extra header on every RestAction from client to server.

From javadoc I've found this 

Rest Interceptors provide a number of flexible options:
 * <ul>
 * <li>The action can be modified before sending the action to the server.</li>
 * <li>A result can be returned without contacting the server.</li>
 * <li>The result can be modified or processed after it is returned from the server.</li>
 * <li>The {@link Interceptor} can take over and communicate directly with the server, possibly using a
 * different mechanism.</li>
 
 I make 'clone' the original RestAction and call "return executeCommand.execute(clone, resultCallback);"
 at the end of method "public DispatchRequest execute(final RestAction action, AsyncCallback<Object> resultCallback, ExecuteCommand<RestAction, Object> executeCommand)"
 but the gwtp send every time the original RestAction.
 
 Where wrong?
 
 Thank in advance.
 
 Riccardo


Sorry for my not perfect english!
To unsubscribe from this group and stop receiving emails from it, send an email to gwt-platform...@googlegroups.com.

Butl Nor

unread,
Mar 28, 2015, 3:42:33 AM3/28/15
to gwt-pl...@googlegroups.com
Yes, guava is the one that brings too many dependencies. I am glad you will get rid of it. I completely understand that all those jars were needed.
However, it's too late for me, as we had a deadline and had to deploy the application. I wasn't able to fix that GIN issue which prevented me using superdev incremental, so I had to restart the gwt each time and wait for several minutes instead of just seconds. That's the reason I had to write my own implementation. I couldn't afford to wait minutes to develop for mobile. Our project is very complex, several hundred thousands lines of code. So incremental mode is a must.

Christopher Viel

unread,
Mar 30, 2015, 10:22:43 AM3/30/15
to gwt-pl...@googlegroups.com
I need to do some tests. I'll answer back it's done.

Steven Prather

unread,
Oct 28, 2015, 6:46:01 AM10/28/15
to GWTP
Did anyone figure out how to add headers dynamically? I don't see how.

Thanks,

Steve
Message has been deleted

Eman

unread,
Oct 28, 2015, 9:56:52 AM10/28/15
to GWTP
Simple solution I found was to use the csrf-token.  You can rename it and set it by setting the cookie.

Steven Prather

unread,
Oct 28, 2015, 11:13:09 AM10/28/15
to GWTP
Thanks for answering, Eman.

For a strange reason, I need to be able to take the value from the csrf token and set it as a header on the request. In GWT RPC, we were able to do this:
public class CustomRequestBuilder extends RpcRequestBuilder{
@Override
protected void doFinish(RequestBuilder rb) {
super.doFinish(rb);
String token = Cookies.getCookie("XSRF-TOKEN");
if(token != null){
rb.setHeader("X-XSRF-TOKEN", token);
}
}
}

Does anyone have a suggestion of how to accomplish this? It's possible I'm missing something obvious.

Eman

unread,
Oct 28, 2015, 2:17:10 PM10/28/15
to GWTP
EX.
install(new RestDispatchAsyncModuleBuilder() .xsrfTokenHeaderName("Protection-Token") .build()); bindConstant().annotatedWith(SecurityCookie.class).to("JSESSIONID");

xsrfTokenHeaderName is the header name
annotatedWith(SecurityCookie.class).to("JSESSIONID") is the name of the cookie to get the value from

Eman

unread,
Oct 28, 2015, 2:19:05 PM10/28/15
to GWTP
This is all handled by GWTP if you set the SecurityCookie.

Asier

unread,
Oct 30, 2015, 2:10:55 PM10/30/15
to GWTP
Hi

The karaka application by Harald Pehl is a bit old, but has some code about adding cookies to REST calls.

See https://github.com/hpehl/karaka

Regards
Reply all
Reply to author
Forward
0 new messages