|
@@ -19,149 +19,155 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
@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 ConcurrentHashMap<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 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 ConcurrentHashMap<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.instance("redirect").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.instance("redirect").post(url, postString, null, ConfigRedirect.headerMap);
|
|
|
- }
|
|
|
- Object result;
|
|
|
- try {
|
|
|
- result = JSON.parse(resultString);
|
|
|
- } catch (Exception e) {
|
|
|
- result = resultString;
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
+ 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.instance("redirect").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();
|
|
|
+ }
|
|
|
+ //这里添加日志,重试手动,并设置成短链接
|
|
|
+ try {
|
|
|
+ resultString = HttpClientUtil.instance("redirect").post(url, postString, null, ConfigRedirect.headerMap);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("redirect异常", e);
|
|
|
+ resultString = HttpClientUtil.instance("redirect").post(url, postString, null, ConfigRedirect.headerMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Object result;
|
|
|
+ try {
|
|
|
+ result = JSON.parse(resultString);
|
|
|
+ } catch (Exception e) {
|
|
|
+ result = resultString;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
- public static void redirect_export(String param,HttpServletResponse response) {
|
|
|
- JSONObject sql_json = JSON.parseObject(param);
|
|
|
- String code = sql_json.getString("code");
|
|
|
- ConfigRedirect ConfigRedirect = Constant.ConfigRedirectMap.get(code);
|
|
|
- try {
|
|
|
- Map<String, String> otherMap = new ConcurrentHashMap<String, String>();
|
|
|
- RedirectUtil.redirect_export_Inner(ConfigRedirect, sql_json, otherMap,response);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- }
|
|
|
- }
|
|
|
+ public static void redirect_export(String param, HttpServletResponse response) {
|
|
|
+ JSONObject sql_json = JSON.parseObject(param);
|
|
|
+ String code = sql_json.getString("code");
|
|
|
+ ConfigRedirect ConfigRedirect = Constant.ConfigRedirectMap.get(code);
|
|
|
+ try {
|
|
|
+ Map<String, String> otherMap = new ConcurrentHashMap<String, String>();
|
|
|
+ RedirectUtil.redirect_export_Inner(ConfigRedirect, sql_json, otherMap, response);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- public static void redirect_export_Inner(ConfigRedirect ConfigRedirect, JSONObject sql_json, Map<String, String> otherMap, HttpServletResponse response) throws Exception {
|
|
|
- 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);
|
|
|
- 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();
|
|
|
- }
|
|
|
- HttpClientUtil.instance("redirect").post_export(url, postString, null, ConfigRedirect.headerMap,response);
|
|
|
- }
|
|
|
+ public static void redirect_export_Inner(ConfigRedirect ConfigRedirect, JSONObject sql_json, Map<String, String> otherMap, HttpServletResponse response) throws Exception {
|
|
|
+ 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);
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ HttpClientUtil.instance("redirect").post_export(url, postString, null, ConfigRedirect.headerMap, response);
|
|
|
+ }
|
|
|
}
|