org.plantview.client.request.PlantViewRequestTransport.java Source code

Java tutorial

Introduction

Here is the source code for org.plantview.client.request.PlantViewRequestTransport.java

Source

/*
 * Copyright 2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.plantview.client.request;

import org.plantview.client.event.AuthenticationFailureEvent;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
//import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

/**
 * We can do the pre and post processing for all requests and handle the authentication failures
 * reported by {@link org.plantview.server.AuthenticationFilter}
 * 
 */
public class PlantViewRequestTransport extends DefaultRequestTransport {

    private final EventBus eventBus;

    //   @Inject
    public PlantViewRequestTransport(EventBus eventBus) {
        this.eventBus = eventBus;
    }

    @Override
    protected RequestCallback createRequestCallback(final TransportReceiver delegate) {

        final RequestCallback superCallback = super.createRequestCallback(delegate);
        doBeforeCallback();
        return new RequestCallback() {
            public void onResponseReceived(Request request, Response response) {
                doOnResponseReceived();
                /*
                 * The GaeAuthFailure filter responds with Response.SC_UNAUTHORIZED and
                 * adds a "login" url header if the user is not logged in. When we
                 * receive that combo, post an event so that the app can handle things
                 * as it sees fit.
                 */

                if (Response.SC_UNAUTHORIZED == response.getStatusCode()) {
                    String loginUrl = response.getHeader("login");
                    if (loginUrl != null) {
                        /*
                         * Hand the receiver a non-fatal callback, so that
                         * com.google.web.bindery.requestfactory.shared.Receiver will not post a
                         * runtime exception.
                         */
                        delegate.onTransportFailure(
                                new ServerFailure("Unauthenticated user", null, null, false /* not fatal */));
                        eventBus.fireEvent(new AuthenticationFailureEvent(loginUrl));
                        return;
                    }
                } else if (Response.SC_BAD_REQUEST == response.getStatusCode()) {
                    String unsupportedUrl = response.getHeader("unsupported");
                    if (unsupportedUrl != null) {
                        /*
                         * Hand the receiver a non-fatal callback, so that
                         * com.google.web.bindery.requestfactory.shared.Receiver will not post a
                         * runtime exception.
                         */
                        delegate.onTransportFailure(new ServerFailure("Unsupported user agent user", null, null,
                                false /* not fatal */));
                        eventBus.fireEvent(new AuthenticationFailureEvent(unsupportedUrl));
                        return;
                    }
                }
                superCallback.onResponseReceived(request, response);
            }

            public void onError(Request request, Throwable exception) {
                doOnError(exception);
                superCallback.onError(request, exception);
            }
        };
    }

    //   
    // We can replace those methods with fire events, it's better and elegant way 
    //
    protected void doBeforeCallback() {
        // Some processing before the request is send
        Document.get().getBody().getStyle().setCursor(Cursor.DEFAULT);
    }

    protected void doOnResponseReceived() {
        // Some processing on success
        Document.get().getBody().getStyle().setCursor(Cursor.DEFAULT);
    }

    protected void doOnError(Throwable exception) {
        // Some processing on failure
        Document.get().getBody().getStyle().setCursor(Cursor.DEFAULT);
    }
}