98b0fe18e5ea4689373062984b13dc8cba6b2769.svn-base 6.88 KB
package com.thinkgem.jeesite.modules.title.buss;
 
import java.io.IOException;
import java.net.ConnectException;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.exception.BussException;
import com.thinkgem.jeesite.common.utils.HttpUtil;
import com.thinkgem.jeesite.common.utils.JsonUtil;
import com.thinkgem.jeesite.common.utils.StringUtil;
import com.thinkgem.jeesite.common.utils.Sysutil;


@Component
public class RemoteBuss {
	
	private static final Logger logger = LoggerFactory.getLogger(RemoteBuss.class);	  
	 
	public void sendEvent(Map data) throws Exception {   
		String url = Global.getTitleUrl() + "/add/event";
		HttpUtil httpUtil = new HttpUtil();
		Map params = Maps.newHashMap();
		params.put("data",JsonUtil.toJsonString(data));
		String resmsg =  httpUtil.getReturnResByUrl(Global.getTitleUrl(), url,params,null);
		Map resmap = JsonUtil.jsonToMap(resmsg);
		String errcode = StringUtil.getValueByMap(resmap, "resultCode", "0");
		if("0".equals(errcode)) { 
			logger.info("事件转发完成");
		}else {
			String errmsg = resmap.get("errmsg")== null?"": resmap.get("errmsg").toString();
			throw new Exception("事件转发失败,错误信息:"+errmsg);
		} 
		
	} 
	

	public Map getZdxx() throws Exception {
		HttpUtil httpUtil = new HttpUtil();
		String url = "/get/zdxx";
		Map map = Maps.newHashMap();
		map.put("qxdm", Global.getAreaCode());
		String resmsg =  httpUtil.getReturnResByUrl(Global.getTitleUrl(), url,map,null);
		logger.info(resmsg);
		Map resmap = JsonUtil.jsonToMap(resmsg);
		String errcode = StringUtil.getValueByMap(resmap, "resultCode", "0");
		if("0".equals(errcode)) {
			 logger.info("完成");
			 return resmap;
		}else {
			String errmsg = resmap.get("resultMsg")== null?"": resmap.get("resultMsg").toString();
			logger.error("失败,错误信息:"+errmsg);
			throw new Exception(errmsg);
		} 
	}


	public Map getZxx() throws Exception {
		HttpUtil httpUtil = new HttpUtil();
		String url = "/get/zxx";
		Map map = Maps.newHashMap();
		map.put("qxdm", Global.getAreaCode());
		String resmsg =  httpUtil.getReturnResByUrl(Global.getTitleUrl(), url,map,null);
		Map resmap = JsonUtil.jsonToMap(resmsg);
		String errcode = StringUtil.getValueByMap(resmap, "resultCode", "0");
		if("0".equals(errcode)) {
			 logger.info("完成");
			 return resmap;
		}else {
			String errmsg = resmap.get("resultMsg")== null?"": resmap.get("resultMsg").toString();
			logger.error("失败,错误信息:"+errmsg);
			throw new Exception(errmsg);
		} 
	}

	// post方式向权籍端传递数据
			public String posturl(Map params, String url) throws IOException {
	            logger.debug("222"+params);
				Integer URLOUTTIME = 1000*60*5;
				String res = "";
				HttpPost httppost = null;
				CloseableHttpClient client = null;
				CloseableHttpResponse response = null;
				Exception exception = null;
				try {
					client = HttpClientBuilder.create().build();
					httppost = new HttpPost(url);
					RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(URLOUTTIME)
							.setConnectTimeout(URLOUTTIME).setSocketTimeout(URLOUTTIME).build();
					httppost.setConfig(requestConfig);

					List<NameValuePair> urlParameters = Lists.newArrayList();
					
					if(params != null && params.size()>0) {
						Iterator<String> it = params.keySet().iterator();
						while (it.hasNext()) {
							String key = it.next(); 
							urlParameters.add(new BasicNameValuePair(key,params.get(key).toString())); 
						}
						httppost.setEntity(new UrlEncodedFormEntity(urlParameters, "utf-8"));
					} 
					httppost.setHeader("Connection", "Keep-Alive");
					httppost.setHeader("Accept", "*/*");
					httppost.setHeader("Cookie","yikikata=12ea5adf-6c4a9902f1d8a258fa573024717cb369; brmidyrvj=both");
					httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
					
					response = client.execute(httppost);
					int code = response.getStatusLine().getStatusCode();
					if (code == 200) {
						res = EntityUtils.toString(response.getEntity(), "utf-8");
					}else if (code == 307) {
						Header header_location = response.getFirstHeader("location"); // 跳转的目标地址是在 HTTP-HEAD上
			            String newuri = header_location.getValue(); // 这就是跳转后的地址,再向这个地址发出新申请
			            logger.info("307 url:"+url); 
						 
						Header header_cookie = response.getFirstHeader("Set-Cookie");
						String cookie = header_cookie+ "; brmidyrvj=both";
						 
						logger.info("307 cookie:"+cookie); 
						httppost.setURI(new URI(url));
						httppost.setHeader("Cookie",cookie);
						response = client.execute(httppost);
			        	code = response.getStatusLine().getStatusCode();
		              if (code == 200) {
							res = EntityUtils.toString(response.getEntity(), "utf-8");
			        	}else {
							throw new BussException("返回状态代码" + code);
						}	
					} else {
						throw new BussException("返回状态代码" + code);
					}
				}catch (ConnectException s) {
					logger.info("http请求发生错误",s);
					throw new BussException("网络连接错误");
				} catch (Exception s) {
					logger.info("http请求发生错误",s);
					throw new BussException(" http请求发生错误," + s.getMessage());
				} finally {
					if (httppost != null) {
						httppost.releaseConnection();
					}
					if (response != null) {
						response.close();
					}
					if (client != null) {
						client.close();
					} 
				}
				return res;
			}


			public Map getZbxx(String bdcdyh) throws Exception {
				HttpUtil httpUtil = new HttpUtil();
				String url = "/get/zbxx";
				Map map = Maps.newHashMap();
				map.put("bdcdyh",bdcdyh);
				String resmsg =  httpUtil.getReturnResByUrl(Global.getTitleUrl(), url,map,null);
				logger.info(resmsg);
				Map resmap = JsonUtil.jsonToMap(resmsg);
				String errcode = StringUtil.getValueByMap(resmap, "resultCode", "0");
				if("0".equals(errcode)) {
					 logger.info("完成");
					 return resmap;
				}else {
					String errmsg = resmap.get("resultMsg")== null?"": resmap.get("resultMsg").toString();
					logger.error("失败,错误信息:"+errmsg);
					throw new Exception(errmsg);
				} 
			}
	
}