重写gwt的RemoteServiceServlet,将GWT所有RPC的请求拦截下来交给SPRING来处理

package com.jme.help.gwt;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.gwt.user.server.rpc.UnexpectedException;
import com.jme.exception.HOMSException;

/**
 * 重写gwt的RemoteServiceServlet,将GWT所有RPC的请求拦截下来交给SPRING来处理
 * @author zhangguangyu
 *
 */
public class SpringGwtRemoteServiceServlet extends RemoteServiceServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = Logger
			.getLogger(SpringGwtRemoteServiceServlet.class);

	public void init() {
		if (LOG.isDebugEnabled())
			LOG.debug("Spring GWT service exporter deployed");
	}

	public String processCall(String payload) throws SerializationException {
		
		try {
			Object handler = getBean(getThreadLocalRequest());
			RPCRequest rpcRequest = RPC.decodeRequest(payload,
					handler.getClass(), this);
			onAfterRequestDeserialized(rpcRequest);
			if (LOG.isDebugEnabled()) {
				LOG.debug("Invoking " + handler.getClass().getName() + "."
						+ rpcRequest.getMethod().getName());
			}
			return RPC.invokeAndEncodeResponse(handler, rpcRequest.getMethod(),
					rpcRequest.getParameters(),
					rpcRequest.getSerializationPolicy());
		}catch(HOMSException e){
			return RPC.encodeResponseForFailure(null, e);
		} catch (IncompatibleRemoteServiceException ex) {
			log("An IncompatibleRemoteServiceException was thrown while processing this call.",
					ex);
			return RPC.encodeResponseForFailure(null, ex);
		}
		
	}

	protected Object getBean(HttpServletRequest request) {
		String service = getService(request);
		Object bean = getBean(service);
		
		BaseRemoteService base=(BaseRemoteService)bean;
		
		base.setThreadLocalRequest(this.getThreadLocalRequest());
		
		base.setThreadLocalResponse(this.getThreadLocalResponse());
		
		base.setServletContext(this.getServletContext());
		
		if (!(bean instanceof RemoteService)) {
			throw new IllegalArgumentException(
					"Spring bean is not a GWT RemoteService: " + service + " ("
							+ bean + ")");
		}
		if (LOG.isDebugEnabled()) {
			LOG.debug("Bean for service " + service + " is " + bean);
		}
		return bean;
	}

	protected String getService(HttpServletRequest request) {
		String url = request.getRequestURI();
		String service = url.substring(url.lastIndexOf("/") + 1);
		if (LOG.isDebugEnabled()) {
			LOG.debug("Service for URL " + url + " is " + service);
		}
		return service;
	}

	protected Object getBean(String name) {
		WebApplicationContext applicationContext = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		
		if (applicationContext == null) {
			throw new IllegalStateException(
					"No Spring web application context found");
		}
		if (!applicationContext.containsBean(name)) {
			throw new IllegalArgumentException("Spring bean not found: " + name);
		}

		return applicationContext.getBean(name);
	}
}
 

猜你喜欢

转载自iweisi.iteye.com/blog/1112956
GWT