Created
March 30, 2013 04:03
-
-
Save jnmronquillo/5275288 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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