de.kp.ames.web.client.core.http.ConnectionManager.java Source code

Java tutorial

Introduction

Here is the source code for de.kp.ames.web.client.core.http.ConnectionManager.java

Source

package de.kp.ames.web.client.core.http;
/**
 * This Java module is part of the
 *  Application Developer Framework
 *
 *  Project: AMES-Web-GUI
 *  Package: de.kp.ames.web.client.core.http
 *  Module: ConnectionManager
 *  @author krusche@dr-kruscheundpartner.de
 *
 * Add your semantic annotations within the SemanticAssist tags and
 * mark them with a leading hashtag #:
 *
 * <SemanticAssist>
 *     #client #connection #core #http #manager #web
 * </SemanticAssist>
 *
 */

/**
 *   Copyright 2012 Dr. Krusche & Partner PartG
 *
 *   AMES-Web-GUI is free software: you can redistribute it and/or 
 *   modify it under the terms of the GNU General Public License 
 *   as published by the Free Software Foundation, either version 3 of 
 *   the License, or (at your option) any later version.
 *
 *   AMES- Web-GUI is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * 
 *  See the GNU General Public License for more details. 
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this software. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.util.HashMap;
import java.util.Set;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.RequestTimeoutException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import de.kp.ames.web.client.core.globals.GuiConstants;
import de.kp.ames.web.client.core.method.RequestMethodImpl;

/**
 * @author Stefan Krusche (krusche@dr-kruscheundpartner.de)
 *
 */
public class ConnectionManager {

    private static final int STATUS_CODE_OK = 200;

    private static ConnectionManager instance = new ConnectionManager();

    public static ConnectionManager getInstance() {
        if (instance == null)
            instance = new ConnectionManager();
        return instance;
    }

    /**
     * @param baseUrl
     * @param method
     * @param callback
     */
    public void sendGetRequest(final String baseUrl, final RequestMethodImpl method,
            final HashMap<String, String> headers, final ConnectionCallback callback) {
        String url = baseUrl + method.toQuery();
        sendGetRequest(url, headers, callback);
    }

    /**
     * @param url
     * @param callback
     */
    public void sendGetRequest(final String url, final HashMap<String, String> headers,
            final ConnectionCallback callback) {

        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
        builder.setTimeoutMillis(GuiConstants.CONNECTION_TIMEOUT);

        /*
         * Set header parameters
         */
        if (headers.isEmpty() == false) {
            Set<String> keys = headers.keySet();
            for (String key : keys) {
                builder.setHeader(key, headers.get(key));
            }
        }

        /*
          * Set request callback
          */
        builder.setCallback(new RequestCallback() {

            public void onResponseReceived(Request request, Response response) {

                if (STATUS_CODE_OK == response.getStatusCode()) {
                    handleSuccess(response, callback);

                } else {
                    handleFailure(response, callback);
                }

            }

            public void onError(Request request, Throwable exception) {

                if (exception instanceof RequestTimeoutException) {
                    handleTimeout(exception, callback);

                } else {
                    handleError(exception, callback);
                }

            }

        });

        try {
            builder.send();

        } catch (RequestException e) {
            handleError(e, callback);

        }

    }

    /**
     * @param baseUrl
     * @param method
     * @param requestData
     * @param callback
     */
    public void sendPostRequest(final String baseUrl, final RequestMethodImpl method,
            final HashMap<String, String> headers, final String requestData, final ConnectionCallback callback) {
        String url = baseUrl + method.toQuery();
        sendPostRequest(url, headers, requestData, callback);
    }

    /**
     * @param url
     * @param requestData
     * @param callback
     */
    public void sendPostRequest(final String url, final HashMap<String, String> headers, final String requestData,
            final ConnectionCallback callback) {

        RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
        builder.setTimeoutMillis(GuiConstants.CONNECTION_TIMEOUT);

        /*
         * Set header parameters
         */
        if (headers.isEmpty() == false) {
            Set<String> keys = headers.keySet();
            for (String key : keys) {
                builder.setHeader(key, headers.get(key));
            }
        }

        /*
         * Set request data
         */
        if (requestData != null)
            builder.setRequestData(requestData);

        /*
         * Set request callback
         */
        builder.setCallback(new RequestCallback() {

            public void onResponseReceived(Request request, Response response) {

                if (STATUS_CODE_OK == response.getStatusCode()) {
                    handleSuccess(response, callback);

                } else {
                    handleFailure(response, callback);
                }

            }

            public void onError(Request request, Throwable exception) {

                if (exception instanceof RequestTimeoutException) {
                    handleTimeout(exception, callback);

                } else {
                    handleError(exception, callback);
                }

            }

        });

        try {
            builder.send();

        } catch (RequestException e) {
            handleError(e, callback);

        }

    }

    /**
     * @param response
     * @param callback
     */
    private void handleSuccess(Response response, ConnectionCallback callback) {

        String responseText = response.getText();
        if (callback != null)
            callback.onSuccess(responseText);

    }

    /**
     * @param response
     * @param callback
     */
    private void handleFailure(Response response, ConnectionCallback callback) {

        String responseText = response.getText();
        if (callback != null)
            callback.onFailure(responseText);

    }

    /**
     * @param throwable
     * @param callback
     */
    private void handleError(Throwable throwable, ConnectionCallback callback) {
        if (callback != null)
            callback.onError(throwable);
    }

    /**
     * @param exception
     * @param callback
     */
    private void handleError(RequestException exception, ConnectionCallback callback) {
        if (callback != null)
            callback.onError(exception);
    }

    /**
     * @param throwable
     * @param callback
     */
    private void handleTimeout(Throwable throwable, ConnectionCallback callback) {

        RequestTimeoutException exception = (RequestTimeoutException) throwable;
        if (callback != null)
            callback.onTimeout(exception.getMessage());

    }

}