Skip to content

Instantly share code, notes, and snippets.

@jnmronquillo
Created March 30, 2013 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnmronquillo/5275288 to your computer and use it in GitHub Desktop.
Save jnmronquillo/5275288 to your computer and use it in GitHub Desktop.
package com.img.client.util;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.sencha.gxt.core.client.Style;
import com.sencha.gxt.core.client.Style.AnchorAlignment;
//http://stackoverflow.com/questions/7608235/intercepting-gwt-requestfactory-requests
public class MyDefaultRequestTransport extends DefaultRequestTransport {
@Inject
private BusyIndicator indicator;
@Override
public void send(final String payload, final TransportReceiver receiver) {
onIn();
final TransportReceiver proxy = new TransportReceiver() {
public void onTransportFailure(final ServerFailure failure) {
onOut();
receiver.onTransportFailure(failure);
}
public void onTransportSuccess(final String payload) {
onOut();
receiver.onTransportSuccess(payload);
}
};
super.send(payload, proxy);
}
private void onOut() {
indicator.hide();
}
private void onIn() {
//muestra gif
indicator.show(RootPanel.get().getElement(), new AnchorAlignment(Style.Anchor.TOP_LEFT, Style.Anchor.TOP_LEFT));
}
//http://stackoverflow.com/questions/6508238/gwt-authentication-for-some-part-of-application-using-gwt-login-page
@Override
protected RequestCallback createRequestCallback(
final TransportReceiver receiver) {
final RequestCallback superCallback = super.createRequestCallback(receiver);
return new RequestCallback() {
public void onResponseReceived(Request request, Response response) {
/*
* The HTTPStatusCodeAuthenticationFilter filter responds with Response.SC_UNAUTHORIZED
* if the user is not logged in.
*/
if (Response.SC_UNAUTHORIZED == response.getStatusCode()) {
/*
* Hand the receiver a non-fatal callback, so that
* com.google.web.bindery.requestfactory.shared.Receiver will not post a
* runtime exception.
*/
receiver.onTransportFailure(new ServerFailure(
"Unauthenticated user", null, null, false /* not fatal */));
Window.Location.reload();
return;
}
superCallback.onResponseReceived(request, response);
}
public void onError(Request request, Throwable exception) {
superCallback.onError(request, exception);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment