All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jpattern.gwt.client.communication.rest.ProxyRequestCallback Maven / Gradle / Ivy

package com.jpattern.gwt.client.communication.rest;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.jpattern.gwt.client.IApplicationProvider;
import com.jpattern.gwt.client.communication.ICallbackAction;
import com.jpattern.gwt.client.logger.ILogger;
import com.jpattern.gwt.client.serializer.IObjectSerializer;
import com.jpattern.shared.result.facade.ICommandFacadeResult;

/**
 * 
 * @author Francesco Cina'
 *
 */
public class ProxyRequestCallback> implements RequestCallback {

	private final ICallbackAction callbackAction;
	private final IObjectSerializer jsonBuilder;
	private final ILogger logger;

	public ProxyRequestCallback(ICallbackAction callbackAction, IObjectSerializer jsonBuilder, IApplicationProvider provider) {
		this.callbackAction = callbackAction;
		this.jsonBuilder = jsonBuilder;
		logger = provider.getLoggerService().getLogger(this.getClass());
	}
	
	@Override
	public void onError(Request request, Throwable exception) {
		logger.error("onError", "RequestCallback error", exception);
		callbackAction.onError(exception, 0);
	}

	@Override
	public void onResponseReceived(Request request, Response response) {
		int responseCode = response.getStatusCode();
		logger.debug("onResponseReceived", "RequestCallBack OK. StatusCode: [" + responseCode + "] StatusText: [" + response.getStatusText() + "]");
		logger.debug("onResponseReceived", "Code received:");
		logger.debug("onResponseReceived", response.getText());
		T result;
		try {
			result = jsonBuilder.deserialize( response.getText());
			if (result!=null) {
				callbackAction.onSuccess( result , responseCode ); 
			} else {
				callbackAction.onError(new Exception("Server call result is null") , responseCode);
			}
		} catch (Exception e) {
			callbackAction.onError(e , responseCode);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy