2025-05-15 15:01:29 +08:00
|
|
|
|
package nccloud.pubift.commen.impl.utils;
|
2025-05-15 11:00:51 +08:00
|
|
|
|
|
2025-05-16 14:44:12 +08:00
|
|
|
|
|
2025-05-15 11:00:51 +08:00
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
2025-05-16 14:44:12 +08:00
|
|
|
|
import nc.bs.logging.Log;
|
|
|
|
|
import nc.vo.pub.BusinessException;
|
2025-05-15 15:01:29 +08:00
|
|
|
|
import nccloud.pubift.commen.itf.utils.IHttpPostOtherSys;
|
2025-05-16 14:44:12 +08:00
|
|
|
|
import org.apache.http.Header;
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
|
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.HttpClients;
|
|
|
|
|
import org.apache.http.util.EntityUtils;
|
2025-05-15 11:00:51 +08:00
|
|
|
|
|
2025-05-15 12:17:42 +08:00
|
|
|
|
import java.io.IOException;
|
2025-05-15 11:00:51 +08:00
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
2025-05-16 14:44:12 +08:00
|
|
|
|
|
2025-05-15 11:00:51 +08:00
|
|
|
|
public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
|
|
|
|
|
|
2025-05-15 12:17:42 +08:00
|
|
|
|
private static final String USER_ID = "BIP";
|
|
|
|
|
private static final String PASSWORD = "BIP@2025bip";
|
|
|
|
|
private static final String CLIENT_TYPE = "S";
|
|
|
|
|
private static final String EP_ID = "";
|
|
|
|
|
private static final String LOGIN_URL = "/GTHINKING/AjaxService/N_MISPRO/100208057.ashx/Login";
|
|
|
|
|
|
2025-05-16 14:44:12 +08:00
|
|
|
|
private static final String LOG_INFO_NAME = "OALOG";
|
|
|
|
|
|
|
|
|
|
private static final Log obmlog = Log.getInstance(LOG_INFO_NAME);
|
2025-05-15 11:00:51 +08:00
|
|
|
|
@Override
|
2025-05-15 15:24:32 +08:00
|
|
|
|
public String callMes(String url, JSONObject json) {
|
2025-05-15 12:17:42 +08:00
|
|
|
|
String mesip = "http://192.168.29.32";
|
|
|
|
|
String baseurl = mesip + url;
|
2025-05-16 14:44:12 +08:00
|
|
|
|
String tokenValue = this.getMESToken(mesip);
|
2025-05-15 11:00:51 +08:00
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
2025-05-16 14:44:12 +08:00
|
|
|
|
if (tokenValue != null) {
|
|
|
|
|
headers.put("Cookie", ".ASPXAUTH=" + tokenValue);
|
|
|
|
|
}
|
2025-05-15 12:17:42 +08:00
|
|
|
|
return doPost(baseurl, headers, json);
|
2025-05-15 11:00:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-15 12:17:42 +08:00
|
|
|
|
/**
|
2025-05-16 14:44:12 +08:00
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD>ⲿϵͳ
|
2025-05-15 12:17:42 +08:00
|
|
|
|
*/
|
2025-05-16 14:44:12 +08:00
|
|
|
|
public void sendToExternalSystem(String apiPath, Map<String, Object> requestData) throws BusinessException {
|
2025-05-15 11:00:51 +08:00
|
|
|
|
try {
|
2025-05-16 14:44:12 +08:00
|
|
|
|
obmlog.debug("HttpPostOtherSys request :" + JSONObject.toJSONString(requestData));
|
|
|
|
|
JSONObject jsonRequest = new JSONObject(requestData);
|
|
|
|
|
String response = callMes(apiPath, jsonRequest);
|
|
|
|
|
JSONObject jsonResponse = JSONObject.parseObject(response);
|
|
|
|
|
|
|
|
|
|
obmlog.debug("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿڷ<EFBFBD><EFBFBD><EFBFBD>:<3A><>" + jsonResponse.toJSONString());
|
|
|
|
|
|
|
|
|
|
String success = jsonResponse.getString("Success");
|
|
|
|
|
if ("false".equals(success)) {
|
|
|
|
|
String errorMessage = jsonResponse.getString("ErrorMessage");
|
|
|
|
|
if (errorMessage == null) {
|
|
|
|
|
errorMessage = "No error message provided by the external system.";
|
2025-05-15 11:00:51 +08:00
|
|
|
|
}
|
2025-05-16 14:44:12 +08:00
|
|
|
|
throw new BusinessException("ͬ<EFBFBD><EFBFBD>mesϵͳʧ<EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>" + errorMessage);
|
2025-05-15 11:00:51 +08:00
|
|
|
|
}
|
2025-05-16 14:44:12 +08:00
|
|
|
|
|
|
|
|
|
} catch (BusinessException e) {
|
|
|
|
|
throw e;
|
2025-05-15 11:00:51 +08:00
|
|
|
|
} catch (Exception e) {
|
2025-05-16 14:44:12 +08:00
|
|
|
|
throw new BusinessException("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD>ӿ<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>" + e.getMessage(), e);
|
2025-05-15 11:00:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-15 12:17:42 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2025-05-16 14:44:12 +08:00
|
|
|
|
* ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>post<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2025-05-15 12:17:42 +08:00
|
|
|
|
*/
|
2025-05-16 14:44:12 +08:00
|
|
|
|
private String doPost(String baseurl, Map<String, String> headers, JSONObject jsonPayload) {
|
|
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
|
|
HttpPost httpPost = new HttpPost(baseurl);
|
2025-05-15 12:17:42 +08:00
|
|
|
|
|
2025-05-16 14:44:12 +08:00
|
|
|
|
// Set standard headers
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
|
2025-05-15 12:17:42 +08:00
|
|
|
|
|
2025-05-16 14:44:12 +08:00
|
|
|
|
// Set custom headers from the map
|
|
|
|
|
if (headers != null) {
|
|
|
|
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
|
|
httpPost.setHeader(entry.getKey(), entry.getValue());
|
|
|
|
|
}
|
2025-05-15 12:17:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 14:44:12 +08:00
|
|
|
|
if (jsonPayload != null) {
|
|
|
|
|
StringEntity stringEntity = new StringEntity(jsonPayload.toJSONString(), "UTF-8");
|
|
|
|
|
httpPost.setEntity(stringEntity);
|
2025-05-15 12:17:42 +08:00
|
|
|
|
}
|
2025-05-16 14:44:12 +08:00
|
|
|
|
|
|
|
|
|
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
|
|
String responseString = EntityUtils.toString(entity, "UTF-8");
|
|
|
|
|
return responseString;
|
2025-05-15 12:17:42 +08:00
|
|
|
|
}
|
2025-05-16 14:44:12 +08:00
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// In a real application, use a proper logging framework
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
throw new RuntimeException("HTTP POST request to " + baseurl + " failed: " + e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-15 12:17:42 +08:00
|
|
|
|
|
2025-05-16 14:44:12 +08:00
|
|
|
|
// <20><>ȡ .ASPXAUTH <20><>ֵ - This method remains unchanged
|
|
|
|
|
private String extractAspxAuth(String setCookieHeaderLine) {
|
|
|
|
|
// setCookieHeaderLine <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Set-Cookie <20><>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:
|
|
|
|
|
// ".ASPXAUTH=TOKEN_VALUE; path=/; HttpOnly"
|
|
|
|
|
// <20><> ".ASPXAUTH=TOKEN_VALUE;"
|
|
|
|
|
String[] cookieAttributes = setCookieHeaderLine.split(";"); // <20><><EFBFBD>ֺŷָ<C5B7>
|
|
|
|
|
for (String attribute : cookieAttributes) {
|
|
|
|
|
String trimmedAttribute = attribute.trim(); // ȥ<><C8A5>ǰ<EFBFBD><C7B0><EFBFBD>ո<EFBFBD>
|
|
|
|
|
if (trimmedAttribute.startsWith(".ASPXAUTH=")) {
|
|
|
|
|
return trimmedAttribute.substring(".ASPXAUTH=".length()); // <20><>ȡ<EFBFBD>Ⱥź<C8BA><C5BA><EFBFBD>ֵ
|
2025-05-15 12:17:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-16 14:44:12 +08:00
|
|
|
|
return null; // û<><C3BB><EFBFBD>ҵ<EFBFBD> .ASPXAUTH
|
2025-05-15 12:17:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 14:44:12 +08:00
|
|
|
|
private String getMESToken(String mesIpBase) {
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
jsonObject.put("userId", USER_ID);
|
|
|
|
|
jsonObject.put("password", PASSWORD);
|
|
|
|
|
jsonObject.put("clientType", CLIENT_TYPE);
|
|
|
|
|
jsonObject.put("epId", EP_ID);
|
|
|
|
|
|
|
|
|
|
String loginUrl = mesIpBase + LOGIN_URL;
|
|
|
|
|
|
|
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
|
|
HttpPost httpPost = new HttpPost(loginUrl);
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
|
|
|
|
|
|
|
StringEntity stringEntity = new StringEntity(jsonObject.toJSONString(), "UTF-8");
|
|
|
|
|
httpPost.setEntity(stringEntity);
|
|
|
|
|
|
|
|
|
|
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
|
|
Header[] setCookieHeaders = response.getHeaders("Set-Cookie");
|
|
|
|
|
if (setCookieHeaders != null) {
|
|
|
|
|
for (Header header : setCookieHeaders) {
|
|
|
|
|
String aspxAuthValue = extractAspxAuth(header.getValue());
|
|
|
|
|
if (aspxAuthValue != null) {
|
|
|
|
|
return aspxAuthValue; // Found and return .ASPXAUTH token value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null; // Return null if .ASPXAUTH cookie is not found, as per
|
2025-05-15 12:17:42 +08:00
|
|
|
|
}
|
2025-05-16 14:44:12 +08:00
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
|
|
throw new RuntimeException("Failed to get MESToken (cookie) from " + loginUrl + " due to IO issue: " + e.getMessage(), e);
|
2025-05-15 12:17:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-16 14:44:12 +08:00
|
|
|
|
|
2025-05-15 11:00:51 +08:00
|
|
|
|
}
|