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

com.jpattern.gwt.client.communication.direct.DirectServerCallService Maven / Gradle / Ivy

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

import java.util.HashMap;
import java.util.Map;

import com.jpattern.gwt.client.IServerCallService;
import com.jpattern.gwt.client.communication.AProxy;
import com.jpattern.gwt.client.communication.ICallbackAction;
import com.jpattern.gwt.client.communication.NullProxy;
import com.jpattern.gwt.client.serializer.IObjectSerializer;
import com.jpattern.shared.result.facade.ICommandFacadeResult;

/**
 * 
 * @author Francesco Cina'
 *
 * 08/mag/2011
 */
public class DirectServerCallService implements IServerCallService {

	Map postMap = new HashMap();
	Map getMap = new HashMap();
	Map putMap = new HashMap();
	Map deleteMap = new HashMap();

	@Override
	public > AProxy get(IObjectSerializer resultClassSerializer, ICallbackAction callbackAction, String url, Map keyValuesMap) {
		String urlWithoutQueryString = removeQueryString(url);
		String bestKey = getBestKey(urlWithoutQueryString, getMap);
		if (getMap.containsKey(bestKey)) {
			return new GetProxy(getMap.get(bestKey), callbackAction, resultClassSerializer.getSerializerClass(), urlWithoutQueryString, keyValuesMap);
		}
		return new NullProxy(callbackAction);
	}

	@Override
	public > AProxy delete(IObjectSerializer resultClassSerializer, ICallbackAction callbackAction, String url, Map keyValuesMap) {
		String urlWithoutQueryString = removeQueryString(url);
		String bestKey = getBestKey(urlWithoutQueryString, deleteMap);
		if (deleteMap.containsKey(bestKey)) {
			return new DeleteProxy(deleteMap.get(bestKey), callbackAction, resultClassSerializer.getSerializerClass(), urlWithoutQueryString, keyValuesMap);
		}
		return new NullProxy(callbackAction);
	}

	@Override
	public , Z> AProxy post(IObjectSerializer resultClassSerializer, IObjectSerializer dataClassSerializer, ICallbackAction callbackAction, String url, Z data) {
		String urlWithoutQueryString = removeQueryString(url);
		String bestKey = getBestKey(urlWithoutQueryString, postMap);
		if (postMap.containsKey(bestKey)) {
			return new PostProxy(postMap.get(bestKey), resultClassSerializer.getSerializerClass(), dataClassSerializer.getSerializerClass(), callbackAction, urlWithoutQueryString, data);
		}
		return new NullProxy(callbackAction);
	}

	@Override
	public , Z> AProxy put(IObjectSerializer resultClassSerializer, IObjectSerializer dataClassSerializer, ICallbackAction callbackAction, String url, Z data) {
		String urlWithoutQueryString = removeQueryString(url);
		String bestKey = getBestKey(urlWithoutQueryString, putMap);
		if (putMap.containsKey(bestKey)) {
			return new PutProxy(putMap.get(bestKey), resultClassSerializer.getSerializerClass(), dataClassSerializer.getSerializerClass(), callbackAction, urlWithoutQueryString, data);
		}
		return new NullProxy(callbackAction);
	}

	public void addPost(String path, IServerCallPostAction serverCallAction) {
		postMap.put(path, serverCallAction);
	}
	
	public void addPut(String path, IServerCallPutAction serverCallAction) {
		putMap.put(path, serverCallAction);
	}
	
	public void addGet(String path, IServerCallGetAction serverCallAction) {
		getMap.put(path, serverCallAction);
	}
	
	public void addDelete(String path, IServerCallDeleteAction serverCallAction) {
		deleteMap.put(path, serverCallAction);
	}
	
	protected String getBestKey(String url, Map map) {
		String temp = url;
		while (!temp.isEmpty()) {
			if ( map.containsKey(temp) ) {
				return temp;
			}
			int index = temp.lastIndexOf("/");
			if (index>=0) {
				temp = temp.substring(0, index);
			}
		}
		return temp;
	}
	
	private String removeQueryString(String url) {
		int questionMarkIndex = url.lastIndexOf("?");
		if (questionMarkIndex>0) {
			return url.substring(0, questionMarkIndex);
		}
		return url;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy