日期:2014-05-16  浏览次数:20694 次

Ajax 跨域代理

使用了httpclient做代理请求。

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import jef.database.Condition.Operator;
import jef.database.QB;
import jef.database.query.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.ailk.easyframe.web.common.exception.BusinessException;
import com.ailk.so.redo.MessageCode;
import com.ailk.so.redo.constants.Constants;
import com.ailk.so.redo.persistence.paramconf.entity.SoRedoParam;
import com.ailk.so.redo.service.paramconf.SoRedoParamService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
@Transactional
public class AjaxProxyServiceImpl extends AjaxProxyServiceSkeleton implements AjaxProxyService{


	private Log log = LogFactory.getLog(AjaxProxyServiceImpl.class);

	@Autowired
	protected SoRedoParamService paramconf_soRedoParamService;

	public void setParamconf_soRedoParamService(SoRedoParamService obj){
		this.paramconf_soRedoParamService = obj;
	}

	public Map<String, Object> doRequest(Map<String,String> requestParams) {
		Map<String, String> dbParamMap = getDBParamMap(requestParams);
		/** 得到url参数*/
		String requestUrl = dbParamMap.get(requestParams.get(Constants.REQUEST_URL_KEY));
		if(requestUrl == null) {
			BusinessException ex =  new BusinessException(MessageCode.E1013);
			log.error(ex.getFormattedMessage());
			throw ex;
		}
		log.info("requestUrl:" + requestUrl);
		/** 建立一个NameValuePair数组,用于存储欲传送的参数*/
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		HttpClient hc = new DefaultHttpClient();
		/** 给数组加入参数*/
		if(requestParams != null && !requestParams.isEmpty()) {
			Set<Entry<String, String>> pSet = requestParams.entrySet();
			for(Entry<String, String> entry : pSet) {
				String key = entry.getKey();
				String value = entry.getValue();
				if(log.isDebugEnabled()) {
					log.debug("request param " + key + ":" + value);
				}
				if(Constants.REQUEST_URL_KEY.equals(key)) {
					continue;
				}
				//优先查询参数表,如果请求的code在参数表有配置,则用参数表的配置,否则直接使用页面传过来的值
				if(Constants.REQUEST_CODE_KEY.equals(key)) {
					String codeValue = dbParamMap.get(value);
					if(codeValue == null || codeValue.trim().length() == 0) {
						log.info("can not found job code in db by key:" + key);
						codeValue = value;
					}
					qparams.add(new BasicNameValuePair(key, codeValue));
					continue;
				}
				qparams.add(new BasicNameValuePair(key, value));
			}
		}
		
		HttpResponse response;
		try {
			/** 创建url对象并设置编码等属性*/
			URL url = new URL(requestUrl);
			URI uri = URIUtils.createURI(url.getProtocol(), url.getHost(),
					url.getPort(), url.getPath(),
					URLEncodedUtils.format(qparams, "UTF-8"), null);
			/** 创建一个get请求对象*/
			HttpGet httpget = new HttpGet(uri);
			/** 发送get请求,并返回一个HttpResponse对象*/
			response = hc.execute(httpget);
			HttpEntity entity = response.getEntity();
			StringBuffer responseTxtSb = new StringBuffer();
			/** 将返回值得到并放入stringBuffer中*/
			if (entity !=