You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.9 KiB

/*
* Copyright (C), 2018-2020
* Project: starter
* FileName: PostSubmitJob
* Author: xx
* Date: 2020/8/28 17:01
*/
package com.fr.plugin.xx.submitjob.job;
import com.fanruan.api.i18n.I18nKit;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.net.http.rs.HttpRequest;
import com.fr.base.TemplateUtils;
import com.fr.general.ComparatorUtils;
import com.fr.json.JSONObject;
import com.fr.plugin.xx.submitjob.kit.HttpKit;
import com.fr.script.Calculator;
import com.fr.stable.StringUtils;
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse;
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier;
import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import com.fr.third.org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import com.fr.third.org.apache.http.cookie.Cookie;
import com.fr.third.org.apache.http.entity.StringEntity;
import com.fr.third.org.apache.http.impl.client.BasicCookieStore;
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.third.org.apache.http.impl.client.HttpClients;
import com.fr.third.org.apache.http.ssl.SSLContexts;
import com.fr.third.org.apache.http.util.EntityUtils;
import com.fr.third.springframework.web.util.HtmlUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <Function Description><br>
* <PostSubmitJob>
*
* @author xx
* @since 1.0.0
*/
public class PostJsonSubmitJob extends AbstractDataSubmitJob {
public static final String KEY = "PostJsonSubmitJob";
private static final long serialVersionUID = -2439535244039859157L;
@Override
public void checkHeader(Map<String, String> headers) throws Exception {
if (!headers.containsKey("Content-Type")) {
headers.put("Content-Type", "application/json");
}
if (!ComparatorUtils.equalsIgnoreCase(headers.get("Content-Type"), "application/json")) {
throw new Exception(I18nKit.getLocText("Plugin-submitjob_Header_Content-Type_Valid"));
}
}
/**
* 提交属性处理操作
*
* @param params
* @throws Exception
*/
@Override
public void operation(Map<String, String> params, Map<String, String> headers, Calculator calculator) throws Exception {
setCookies(headers);
LogKit.info("submitjob-PostJsonSubmitJob-operation-params:{}, headers:{}", params, headers);
String bodyContent;
if (StringUtils.isNotBlank(this.jobBean.getBodyContent())) {
bodyContent = TemplateUtils.render(this.jobBean.getBodyContent(), params, calculator);
} else {
bodyContent = JSONObject.mapFrom(params).encode();
}
StringEntity stringEntity = new StringEntity(bodyContent, "UTF-8");
String response = HttpKit.executeAndParse(com.fanruan.api.net.http.rs.HttpRequest.custom()
.url(this.jobBean.getUrl()).post(stringEntity).headers(headers).build());
LogKit.info("submitjob-PostJsonSubmitJob-operation-response:{}", response);
throwErrorMessage(HtmlUtils.htmlUnescape(response), PostJsonSubmitJob.KEY, this.jobBean);
}
private void setCookies(Map<String, String> headers) {
if (!headers.containsKey("loginUrl") || !headers.containsKey("loginParams")) {
return;
}
List<Cookie> cookieList = getLoginCookies(headers.remove("loginUrl"), headers.remove("loginParams")).getCookies();
StringBuilder cookieBuilder = new StringBuilder();
for (Cookie cookie : cookieList) {
cookieBuilder.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
}
headers.put("Cookie", cookieBuilder.toString());
}
private BasicCookieStore getLoginCookies(String loginUrl, String bodyContent) {
BasicCookieStore loginCookie = new BasicCookieStore();
try {
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(scsf).setDefaultCookieStore(loginCookie).build();
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
StringEntity stringEntity = new StringEntity(bodyContent, "UTF-8");
HttpRequest httpRequest = HttpRequest.custom()
.url(loginUrl).post(stringEntity).headers(headers).build();
CloseableHttpResponse response = HttpKit.execute(httpClient, httpRequest);
String result = EntityUtils.toString(response.getEntity());
LogKit.info("submitjob-PostJsonSubmitJob-getLoginCookies-result:{}", result);
} catch (Exception e) {
LogKit.error(e.getMessage(), e);
}
return loginCookie;
}
}