d909fd4410551a824157041d8d4e0424cc3c3f79.svn-base 11.4 KB
package com.thinkgem.jeesite.modules.reg.web.httprest.httpRestService;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.JsonUtil;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.reg.service.qjxt.InterfacelogService;
import com.thinkgem.jeesite.modules.reg.utils.Base64;
import com.thinkgem.jeesite.modules.ycsl.entity.token.YcslToken;
import com.thinkgem.jeesite.modules.ycsl.service.token.YcslTokenService;

@Service
public class HttpactivityService {
	
	@Autowired
	private YcslTokenService ycslTokenService;

	@Autowired
	private InterfacelogService interfacelogService;
	Logger logger = LoggerFactory.getLogger(getClass());

	public Map postRequest(Map data,String bj) throws ClientProtocolException, IOException {
		String resjson = "";
		 
		logger.info("*******请求数据信息:"+JsonUtil.toJsonString(data)); 
		
		if(StringUtils.isNotBlank(bj)){
			if("sendDb".equals(bj)){
				//2.1.6	不动产登簿信息接收接口 登薄结果推送
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_FZ,null);
			}else if("bhSqxx".equals(bj)){
				//退件删除请求
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_XTTJ,null);
			}else if("updateYwzt".equals(bj)){
				//业务办结状态请求
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_YWZT,null);
			}else if("downTradeFiles".equals(bj)){
				//2.1.5	房产交易附件下载
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_downTradeFiles,null);
			}else if("queryTradeWj".equals(bj)){
				//2.1.4	获取交易业务办结信息
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_queryTradeWj,null);
			}else if("insertSL".equals(bj)){
				//2.1.1	新增受理信息到中间库
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_insertSL,null);
			}else if("updateSL".equals(bj)){
				//2.1.2	修改受理信息到中间库
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_updateSL,null);
			}else if("deleteSL".equals(bj)){
				//2.1.3	删除受理信息到中间库
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_deleteSL,null);
			}else if("queryTradeYw".equals(bj)){
				//2.1.8	不动产获取交易业务办结信息
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_queryTradeYw,null);
			}else if("queryTradeEstate".equals(bj)){
				//2.1.9	查询房产交易信息
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_queryTradeEstate,null);
			}else if("saveHookRelation".equals(bj)){
				//2.1.10	不动产新增挂接关系
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_saveHookRelation,null);
			}else if("removeHookRelation".equals(bj)){
				//2.1.11	不动产删除挂接关系
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_removeHookRelation,null);
			}else if("saveHouseStatus".equals(bj)){
				//2.1.12	新增房屋状态
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_saveHouseStatus,null);
			}else if("modifyHouseStatus".equals(bj)){
				//2.1.13	修改房屋状态
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_modifyHouseStatus,null);
			}else if("terminateInfo".equals(bj)){
				//2.1.14	不动产办结信息接收接口
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_terminateInfo,null);
			}else if("findLicenseInfo".equals(bj)){
				//2.1.15	是否挂牌信息查询接口
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_findLicenseInfo,null);
			}else if("wsqk".equals(bj)){
				//4.3	完税接口
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_wsqk,null);
			}else if("downDealFiles".equals(bj)){
				//2.1.17	房产合同下载
				resjson = getReturnResByUrl(JsonMapper.toJsonString(data),"一窗受理平台",Global.POST_YCSL_URL_downDealFiles,null);
			}
		}
		if(!StringUtils.isBlank(resjson)) {
			ObjectMapper mapper = new ObjectMapper();
			mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
			mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
			mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
			mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
			mapper.configure(Feature.INTERN_FIELD_NAMES, true);
			mapper.configure(Feature.CANONICALIZE_FIELD_NAMES, true);
			Map json = mapper.readValue(resjson, Map.class); 
			return json;
		}else {
			return null;
		}
		
	}

	/**
	 * @param url
	 *            post方式请求
	 * @return url返回的数据,以字符串显示
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private String getReturnResByUrl(String in_json,String servername,String urls,Map<String ,String> header) throws IOException {
		logger.info("url:{}",urls); 
		logger.info("http request header:" + JsonUtil.toJsonString(header));
		logger.info("http request data:" +  in_json);
		
		// URL访问超时时间					   
		String url = urls;
		Integer URLOUTTIME = 1000 * 60;
		String json = Base64.encryptBASE64(in_json);
		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);
			StringEntity entity = new StringEntity(in_json, "utf-8");
			httppost.setEntity(entity);
			if(header != null && header.size() >0) {
				Iterator<String> it = header.keySet().iterator();
				while(it.hasNext()){
					String key = it.next();
					httppost.setHeader(key,header.get(key));	
				}
			}
			httppost.setHeader("ContentType", "application/json;charset=UTF-8");
			response = client.execute(httppost);
			int code = response.getStatusLine().getStatusCode();
			if (code == 200) {
				res = EntityUtils.toString(response.getEntity(), "utf-8");
			} else {
				throw new ServiceException("返回状态代码" + code);
			}
		} catch (Exception s) {
			exception = s;
			throw new ServiceException(" http请求发生错误," + s.getMessage());
		} finally {
			if (httppost != null) {
				httppost.releaseConnection();
			}
			if (response != null) {
				response.close();
			}
			if (client != null) {
				client.close();
			} 
			interfacelogService.saveAskByProperty(url, servername,in_json,res, exception);
		}
		logger.info("http post back:" + res);
		return res;
	}

	
	/**
	 * @param url
	 *            post方式请求
	 * @return url返回的数据,以字符串显示
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public String getrzsbByUrl(String rzsbxml) throws IOException {
		// URL访问超时时间					   
		String url = Global.getCommonLogUrl(); 
		Integer URLOUTTIME = 1000 * 60;
		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);
			StringEntity entity = new StringEntity(rzsbxml, "utf-8");
			httppost.setEntity(entity);
			httppost.setHeader("ContentType", "application/json;charset=UTF-8");
			response = client.execute(httppost);
			int code = response.getStatusLine().getStatusCode();
			if (code == 200) {
				res = EntityUtils.toString(response.getEntity(), "utf-8");
			} else {
				throw new ServiceException("返回状态代码" + code);
			}
		} catch (Exception s) {
			exception = s;
			throw new ServiceException(" http请求发生错误," + s.getMessage());
		} finally {
			if (httppost != null) {
				httppost.releaseConnection();
			}
			if (response != null) {
				response.close();
			}
			if (client != null) {
				client.close();
			} 
		}
		logger.info("http post back:" + res);
		return res; 
	}

	public Map hzpostRequest(Map data) throws ClientProtocolException, IOException {
		Map resmap = Maps.newHashMap();
		Map head = Maps.newHashMap();
		head.put("regionCode", "0000");
		head.put("orgid", "");
		resmap.put("head", head);
		resmap.put("data", data);
		System.out.println("*******返回登记结果信息:"+resmap);
		String resjson = getReturnResByUrl(JsonMapper.toJsonString(resmap),"数据汇总复核",Global.POST_SJHZ_URL_FH,null);
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
		mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		mapper.configure(Feature.INTERN_FIELD_NAMES, true);
		mapper.configure(Feature.CANONICALIZE_FIELD_NAMES, true);
		Map json = mapper.readValue(resjson, Map.class);
		String success = json.get("success").toString();
		if("false".equals(success)){
			 throw new ServiceException("响应失败,错误信息:"+(json.get("resultMsg")) == null?"错误信息未返回":json.get("resultMsg").toString().toString());
		}
		return json;
	}
	
	
//	public static void main(String[] args) {
//		HttpactivityService service = new HttpactivityService();
//		Map data = Maps.newHashMap();
//		data.put("proid", "Adfsdgwew");
//		data.put("bdcdyh", "Adfsdgwew");
//		data.put("cqzh", "陕(2017)汉中市不动产权第0000010号");
//		data.put("xml", "此处放xml报文");
//
//		try {
//			Map map = service.postRequest(data);
//			System.out.println("返回信息head" + map.get("head"));
//		} catch (ClientProtocolException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}