menglu 3 лет назад
Родитель
Сommit
e6ab4911ca

+ 7 - 66
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/rest/RedirectApi.java

@@ -1,83 +1,24 @@
 package com.persagy.ibms.data.sdk.service.rest;
 package com.persagy.ibms.data.sdk.service.rest;
 
 
-import java.net.URLEncoder;
-
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.bind.annotation.RestController;
 
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
-import com.alibaba.fastjson.serializer.SerializerFeature;
-import com.persagy.ibms.core.util.LogUtil;
-import com.persagy.ibms.data.sdk.util.ConfigRedirect;
-import com.persagy.ibms.data.sdk.util.Constant;
-import com.persagy.ibms.data.sdk.util.HttpClientUtil;
-
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiOperation;
-import lombok.extern.slf4j.Slf4j;
 
 
 @ApiOperation(value = "重定向接口")
 @ApiOperation(value = "重定向接口")
 @RestController
 @RestController
-@Slf4j
 public class RedirectApi {
 public class RedirectApi {
 
 
 	@PostMapping(path = { "/redirect", "/zkt-sdk/redirect" }, produces = "application/json;charset=UTF-8")
 	@PostMapping(path = { "/redirect", "/zkt-sdk/redirect" }, produces = "application/json;charset=UTF-8")
 	public String redirect(@RequestBody String param) {
 	public String redirect(@RequestBody String param) {
-		String result;
-		JSONObject sql_json = JSON.parseObject(param);
-		String code = sql_json.getString("code");
-		ConfigRedirect ConfigRedirect = Constant.ConfigRedirectMap.get(code);
-		if (ConfigRedirect == null) {
-			JSONObject resultJSON = new JSONObject();
-			resultJSON.put("Result", "failure");
-			resultJSON.put("ResultMsg", "code: " + code + " not exist");
-			resultJSON.put("ResultCode", 250);
-			return JSONObject.toJSONString(resultJSON, SerializerFeature.WriteMapNullValue);
-		}
-		try {
-			String url = ConfigRedirect.url;
-			JSONObject urlParams = sql_json.getJSONObject("urlParams");
-			if (urlParams != null) {
-				StringBuffer sb = new StringBuffer();
-				for (String key : urlParams.keySet()) {
-					if (sb.length() > 0) {
-						sb.append("&");
-					}
-					sb.append(key + "=" + URLEncoder.encode(urlParams.get(key).toString(), "UTF-8"));
-				}
-				if (sb.length() > 0) {
-					url = url + "?" + sb.toString();
-				}
-			}
-			String urlSuffix = sql_json.getString("urlSuffix");
-			if (urlSuffix != null) {
-				url = url + urlSuffix;
-			}
-			if (ConfigRedirect.http_request_type.equals("get")) {
-				result = HttpClientUtil.get(url, null, ConfigRedirect.headerMap);
-			} else {
-				Object postBody = sql_json.get("postBody");
-				String postString;
-				if (postBody instanceof JSONObject) {
-					postString = JSONObject.toJSONString((JSONObject) postBody, SerializerFeature.WriteMapNullValue);
-				} else if (postBody instanceof JSONArray) {
-					postString = JSONArray.toJSONString((JSONArray) postBody, SerializerFeature.WriteMapNullValue);
-				} else {
-					postString = postBody.toString();
-				}
-				result = HttpClientUtil.post(url, postString, null, ConfigRedirect.headerMap);
-			}
-		} catch (Exception e) {
-			log.error(e.getMessage(), e);
-			JSONObject resultJSON = new JSONObject();
-			resultJSON.put("Result", "failure");
-			String message = LogUtil.GetExceptionStackTrace(e);
-			resultJSON.put("ResultMsg", message);
-			resultJSON.put("ResultCode", 250);
-			result = JSONObject.toJSONString(resultJSON, SerializerFeature.WriteMapNullValue);
-		}
+		String result = RedirectUtil.redirect(param, false);
+		return result;
+	}
+
+	@PostMapping(path = { "/redirect_wrapper", "/zkt-sdk/redirect_wrapper" }, produces = "application/json;charset=UTF-8")
+	public String redirect_wrapper(@RequestBody String param) {
+		String result = RedirectUtil.redirect(param, true);
 		return result;
 		return result;
 	}
 	}
 }
 }

+ 114 - 0
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/service/rest/RedirectUtil.java

@@ -0,0 +1,114 @@
+package com.persagy.ibms.data.sdk.service.rest;
+
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.persagy.ibms.core.util.LogUtil;
+import com.persagy.ibms.data.sdk.util.ConfigRedirect;
+import com.persagy.ibms.data.sdk.util.Constant;
+import com.persagy.ibms.data.sdk.util.HttpClientUtil;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class RedirectUtil {
+	public static String redirect(String param, boolean wrapper) {
+		JSONObject sql_json = JSON.parseObject(param);
+		String code = sql_json.getString("code");
+		ConfigRedirect ConfigRedirect = Constant.ConfigRedirectMap.get(code);
+		if (ConfigRedirect == null) {
+			JSONObject resultJSON = new JSONObject();
+			resultJSON.put("Result", "failure");
+			resultJSON.put("ResultMsg", "code: " + code + " not exist");
+			resultJSON.put("ResultCode", 250);
+			return JSONObject.toJSONString(resultJSON, SerializerFeature.WriteMapNullValue);
+		}
+		String resultString;
+		try {
+			Map<String, String> otherMap = new HashMap<String, String>();
+			Object result = RedirectUtil.redirectInner(ConfigRedirect, sql_json, otherMap);
+			if (wrapper) {
+				JSONObject resultJSON = new JSONObject();
+				resultJSON.put("Result", "success");
+				resultJSON.put("Content", result);
+				resultJSON.put("Url", otherMap.get("Url"));
+				resultString = JSONObject.toJSONString(resultJSON, SerializerFeature.WriteMapNullValue);
+			} else {
+				if (result instanceof JSONObject) {
+					JSONObject resultJSON = (JSONObject) result;
+					resultString = JSONObject.toJSONString(resultJSON, SerializerFeature.WriteMapNullValue);
+				} else if (result instanceof JSONArray) {
+					JSONArray resultJSON = (JSONArray) result;
+					resultString = JSONArray.toJSONString(resultJSON, SerializerFeature.WriteMapNullValue);
+				} else {
+					resultString = result.toString();
+				}
+			}
+		} catch (Exception e) {
+			log.error(e.getMessage(), e);
+			JSONObject resultJSON = new JSONObject();
+			resultJSON.put("Result", "failure");
+			String message = LogUtil.GetExceptionStackTrace(e);
+			resultJSON.put("ResultMsg", message);
+			resultJSON.put("ResultCode", 250);
+			resultString = JSONObject.toJSONString(resultJSON, SerializerFeature.WriteMapNullValue);
+		}
+		return resultString;
+	}
+
+	public static Object redirectInner(ConfigRedirect ConfigRedirect, JSONObject sql_json, Map<String, String> otherMap) throws Exception {
+		String resultString;
+		String url = ConfigRedirect.url;
+		String url_ori = url;
+		JSONObject urlParams = sql_json.getJSONObject("urlParams");
+		if (urlParams != null) {
+			StringBuffer sb = new StringBuffer();
+			StringBuffer sb_ori = new StringBuffer();
+			for (String key : urlParams.keySet()) {
+				if (sb.length() > 0) {
+					sb.append("&");
+					sb_ori.append("&");
+				}
+				sb.append(key + "=" + URLEncoder.encode(urlParams.get(key).toString(), "UTF-8"));
+				sb_ori.append(key + "=" + urlParams.get(key));
+			}
+			if (sb.length() > 0) {
+				url = url + "?" + sb.toString();
+				url_ori = url_ori + "?" + sb_ori.toString();
+			}
+		}
+		String urlSuffix = sql_json.getString("urlSuffix");
+		if (urlSuffix != null) {
+			url = url + urlSuffix;
+			url_ori = url_ori + "?" + urlSuffix;
+		}
+		otherMap.put("Url", url_ori);
+		log.warn(url_ori);
+		if (ConfigRedirect.http_request_type.equals("get")) {
+			resultString = HttpClientUtil.get(url, null, ConfigRedirect.headerMap);
+		} else {
+			Object postBody = sql_json.get("postBody");
+			String postString;
+			if (postBody instanceof JSONObject) {
+				postString = JSONObject.toJSONString((JSONObject) postBody, SerializerFeature.WriteMapNullValue);
+			} else if (postBody instanceof JSONArray) {
+				postString = JSONArray.toJSONString((JSONArray) postBody, SerializerFeature.WriteMapNullValue);
+			} else {
+				postString = postBody.toString();
+			}
+			resultString = HttpClientUtil.post(url, postString, null, ConfigRedirect.headerMap);
+		}
+		Object result;
+		try {
+			result = JSON.parse(resultString);
+		} catch (Exception e) {
+			result = resultString;
+		}
+		return result;
+	}
+}

+ 26 - 0
ibms-data-sdk/src/main/resources/config.xml

@@ -45,6 +45,32 @@
 		<!-- 中转服务的header,有几项就写几行 -->
 		<!-- 中转服务的header,有几项就写几行 -->
 		<header key="Content-Type" value="application/json" />
 		<header key="Content-Type" value="application/json" />
 	</Redirect>
 	</Redirect>
+	
+	<Redirect code="香港置地最近24小时功率" http_request_type="post" url="http://39.102.43.179:9982/energy/24data/list">
+		<!-- 中转服务的header,有几项就写几行 -->
+		<header key="Content-Type" value="application/json" />
+	</Redirect>
+	<Redirect code="香港置地首页能耗数据" http_request_type="post" url="http://39.102.43.179:9982/energy/query">
+		<!-- 中转服务的header,有几项就写几行 -->
+		<header key="Content-Type" value="application/json" />
+	</Redirect>
+	<Redirect code="查询能源系统数据" http_request_type="post" url="http://39.102.43.179:9982/energySystem/query">
+		<!-- 中转服务的header,有几项就写几行 -->
+		<header key="Content-Type" value="application/json" />
+	</Redirect>
+	<Redirect code="查询楼层平均温度" http_request_type="get" url="http://39.102.43.179:9982/floorAvgTemperature/query">
+		<!-- 中转服务的header,有几项就写几行 -->
+		<header key="Content-Type" value="application/json" />
+	</Redirect>
+	<Redirect code="查询详细信息列表" http_request_type="get" url="http://39.102.43.179:9982/itemDataList/query">
+		<!-- 中转服务的header,有几项就写几行 -->
+		<header key="Content-Type" value="application/json" />
+	</Redirect>
+	<Redirect code="查询详细信息月份列表" http_request_type="get" url="http://39.102.43.179:9982/itemMonthDataList/query">
+		<!-- 中转服务的header,有几项就写几行 -->
+		<header key="Content-Type" value="application/json" />
+	</Redirect>
+	
 	<Redirect code="查询项目概括" http_request_type="get" url="http://39.102.43.179:9982/projectSummary/query">
 	<Redirect code="查询项目概括" http_request_type="get" url="http://39.102.43.179:9982/projectSummary/query">
 		<header key="Content-Type" value="application/json" />
 		<header key="Content-Type" value="application/json" />
 	</Redirect>
 	</Redirect>