View Javadoc

1   package org.appfuse.webapp.client.application.base.request;
2   
3   import java.util.logging.Level;
4   import java.util.logging.Logger;
5   
6   import org.appfuse.webapp.client.application.base.security.AuthRequiredEvent;
7   import org.appfuse.webapp.client.application.base.security.RequestForbidenEvent;
8   
9   import com.google.gwt.http.client.Request;
10  import com.google.gwt.http.client.RequestCallback;
11  import com.google.gwt.http.client.Response;
12  import com.google.gwt.user.client.History;
13  import com.google.web.bindery.event.shared.EventBus;
14  import com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport;
15  import com.google.web.bindery.requestfactory.shared.ServerFailure;
16  
17  class CustomDefaultRequestTransport extends DefaultRequestTransport {
18  
19      private static final String SERVER_ERROR = "Server Error";
20      private static final Logger wireLogger = Logger.getLogger("WireActivityLogger");
21  
22      private final EventBus eventBus;
23  
24      /**
25       * @param eventBus
26       */
27      public CustomDefaultRequestTransport(EventBus eventBus) {
28          super();
29          this.eventBus = eventBus;
30      }
31  
32      @Override
33      protected RequestCallback createRequestCallback(final TransportReceiver receiver) {
34          return new RequestCallback() {
35  
36              public void onError(Request request, Throwable exception) {
37                  wireLogger.log(Level.SEVERE, SERVER_ERROR, exception);
38                  receiver.onTransportFailure(new ServerFailure(exception.getMessage(), getExceptionName(exception), null, true));
39              }
40  
41              public void onResponseReceived(Request request, Response response) {
42                  wireLogger.finest("Response received");
43                  if (Response.SC_UNAUTHORIZED == response.getStatusCode()) {
44                      eventBus.fireEvent(new AuthRequiredEvent());
45                  } else if (Response.SC_FORBIDDEN == response.getStatusCode()) {
46                      eventBus.fireEvent(new RequestForbidenEvent());
47                  } else if (Response.SC_OK == response.getStatusCode()) {
48                      String text = response.getText();
49                      receiver.onTransportSuccess(text);
50                  } else {
51                      String message = response.getStatusCode() + " " + response.getText();
52                      wireLogger.severe(SERVER_ERROR + " " + message);
53                      receiver.onTransportFailure(new ServerFailure(message, null, null, true));
54                  }
55              }
56          };
57      }
58  
59      private String getExceptionName(Throwable exception) {
60          Class exceptionType = getMostSpecificCause(exception).getClass();
61          String simpleName = exceptionType.getName();
62          // simpleName = simpleName.substring(simpleName.lastIndexOf('.'));
63          // simpleName = simpleName.replace("Exception", "");
64          return simpleName;
65      }
66  
67      private Throwable getMostSpecificCause(Throwable exception) {
68          if (exception.getCause() != null) {
69              return getMostSpecificCause(exception);
70          } else {
71              return exception;
72          }
73      }
74  
75  }