|
@@ -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;
|
|
|
|
+ }
|
|
|
|
+}
|