taikai2312/ic/src/private/nccloud/pubift/commen/impl/utils/HttpPostOtherSysImpl.java

158 lines
6.2 KiB
Java
Raw Normal View History

package nccloud.pubift.commen.impl.utils;
import com.alibaba.fastjson.JSONObject;
import nc.bs.logging.Log;
import nc.vo.pub.BusinessException;
import nccloud.pubift.commen.itf.utils.IHttpPostOtherSys;
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;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
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";
private static final String LOG_INFO_NAME = "OALOG";
private static final Log obmlog = Log.getInstance(LOG_INFO_NAME);
@Override
public String callMes(String url, JSONObject json) {
String mesip = "http://192.168.29.32";
String baseurl = mesip + url;
String tokenValue = this.getMESToken(mesip);
Map<String, String> headers = new HashMap<>();
if (tokenValue != null) {
headers.put("Cookie", ".ASPXAUTH=" + tokenValue);
}
return doPost(baseurl, headers, json);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD>ⲿϵͳ
*/
public void sendToExternalSystem(String apiPath, Map<String, Object> requestData) throws BusinessException {
try {
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.";
}
throw new BusinessException("ͬ<EFBFBD><EFBFBD>mesϵͳʧ<EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>" + errorMessage);
}
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
throw new BusinessException("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD>ӿ<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>" + e.getMessage(), e);
}
}
/**
* ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>post<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
private String doPost(String baseurl, Map<String, String> headers, JSONObject jsonPayload) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(baseurl);
// Set standard headers
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
// Set custom headers from the map
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
if (jsonPayload != null) {
StringEntity stringEntity = new StringEntity(jsonPayload.toJSONString(), "UTF-8");
httpPost.setEntity(stringEntity);
}
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
return responseString;
}
} 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);
}
}
// <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>ֵ
}
}
return null; // û<><C3BB><EFBFBD>ҵ<EFBFBD> .ASPXAUTH
}
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
}
} catch (IOException e) {
throw new RuntimeException("Failed to get MESToken (cookie) from " + loginUrl + " due to IO issue: " + e.getMessage(), e);
}
}
}