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

158 lines
6.2 KiB
Java

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);
}
/**
* ·¢ËÍÊý¾Ýµ½Íⲿϵͳ
*/
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("Èý·½½Ó¿Ú·µ»Ø:£º" + 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("ͬ²½mesϵͳʧ°Ü,´íÎóÏûÏ¢£º" + errorMessage);
}
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
throw new BusinessException("µ÷ÓÃÍⲿ½Ó¿Úʧ°Ü£º" + e.getMessage(), e);
}
}
/**
* ÒµÎñÇëÇópost·½·¨
*/
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);
}
}
// ÌáÈ¡ .ASPXAUTH µÄÖµ - This method remains unchanged
private String extractAspxAuth(String setCookieHeaderLine) {
// setCookieHeaderLine ÊÇÒ»ÐÐÍêÕûµÄ Set-Cookie ÏìӦͷ£¬ÀýÈç:
// ".ASPXAUTH=TOKEN_VALUE; path=/; HttpOnly"
// »ò ".ASPXAUTH=TOKEN_VALUE;"
String[] cookieAttributes = setCookieHeaderLine.split(";"); // °´·ÖºÅ·Ö¸î
for (String attribute : cookieAttributes) {
String trimmedAttribute = attribute.trim(); // È¥³ýǰºó¿Õ¸ñ
if (trimmedAttribute.startsWith(".ASPXAUTH=")) {
return trimmedAttribute.substring(".ASPXAUTH=".length()); // ÌáÈ¡µÈºÅºóµÄÖµ
}
}
return null; // ûÓÐÕÒµ½ .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);
}
}
}