|
@@ -15,13 +15,44 @@ import kd.bos.servicehelper.operation.SaveServiceHelper;
|
|
import kd.bos.util.HttpClientUtils;
|
|
import kd.bos.util.HttpClientUtils;
|
|
import nckd.jimin.jyyy.hr.wtc.wtis.util.DingTalkSyncUtil;
|
|
import nckd.jimin.jyyy.hr.wtc.wtis.util.DingTalkSyncUtil;
|
|
import org.apache.commons.net.util.Base64;
|
|
import org.apache.commons.net.util.Base64;
|
|
|
|
+import org.apache.http.Header;
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
|
+import org.apache.http.client.entity.GzipDecompressingEntity;
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
+import org.apache.http.config.Registry;
|
|
|
|
+import org.apache.http.config.RegistryBuilder;
|
|
|
|
+import org.apache.http.conn.socket.ConnectionSocketFactory;
|
|
|
|
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
|
|
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
+import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
|
+import org.apache.http.params.HttpConnectionParams;
|
|
|
|
+import org.apache.http.params.HttpParams;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
|
|
+import javax.net.ssl.KeyManager;
|
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
|
+import javax.net.ssl.TrustManager;
|
|
|
|
+import javax.net.ssl.X509TrustManager;
|
|
|
|
+import javax.security.cert.CertificateException;
|
|
|
|
+import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.InputStreamReader;
|
|
|
|
+import java.net.URI;
|
|
|
|
+import java.security.SecureRandom;
|
|
|
|
+import java.security.cert.X509Certificate;
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
-import java.util.Date;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Moka工具类
|
|
* Moka工具类
|
|
@@ -30,6 +61,7 @@ import java.util.Map;
|
|
*/
|
|
*/
|
|
public class MokaApiUtil {
|
|
public class MokaApiUtil {
|
|
private static Log logger = LogFactory.getLog(MokaApiUtil.class);
|
|
private static Log logger = LogFactory.getLog(MokaApiUtil.class);
|
|
|
|
+ private static String SCHEME_HTTPS = "https";
|
|
/**
|
|
/**
|
|
* 新增招聘需求
|
|
* 新增招聘需求
|
|
* @param recruitTypeFlag 招聘模式可选值:1:社招 2:校招
|
|
* @param recruitTypeFlag 招聘模式可选值:1:社招 2:校招
|
|
@@ -46,7 +78,7 @@ public class MokaApiUtil {
|
|
headers.put("Authorization", "Basic " + Base64.encodeBase64String((getParamValue("moka_apikey") + ":").getBytes()));
|
|
headers.put("Authorization", "Basic " + Base64.encodeBase64String((getParamValue("moka_apikey") + ":").getBytes()));
|
|
logger.info(String.format("url[%s],data[%s]", url, bodyData.toJSONString()));
|
|
logger.info(String.format("url[%s],data[%s]", url, bodyData.toJSONString()));
|
|
System.out.println(String.format("url[%s],data[%s]", url, bodyData.toJSONString()));
|
|
System.out.println(String.format("url[%s],data[%s]", url, bodyData.toJSONString()));
|
|
- String responseEntify = HttpClientUtils.postjson(url, headers, bodyData.toJSONString(), 10000, 10000);
|
|
|
|
|
|
+ String responseEntify = postjson(url, headers, bodyData.toJSONString(), 10000, 10000);
|
|
|
|
|
|
logger.info(responseEntify);
|
|
logger.info(responseEntify);
|
|
JSONObject result = (JSONObject)JSONObject.parse(responseEntify);
|
|
JSONObject result = (JSONObject)JSONObject.parse(responseEntify);
|
|
@@ -86,6 +118,122 @@ public class MokaApiUtil {
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static String postjson(String url, Map<String, String> header, String json, int connectionTimeout, int readTimeout) throws IOException {
|
|
|
|
+ String CONTENT_TYPE_TEXT_JSON = "text/json;";
|
|
|
|
+ String data = "";
|
|
|
|
+ HttpClient client = null;
|
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
|
+ URI uri = post.getURI();
|
|
|
|
+ if (SCHEME_HTTPS.equals(uri.getScheme())) {
|
|
|
|
+ client = wrapperHttpClient(connectionTimeout, readTimeout);
|
|
|
|
+ if (client == null) {
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ client = createHttpClient(connectionTimeout, readTimeout);
|
|
|
|
+ HttpParams httpParams = client.getParams();
|
|
|
|
+ HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout);
|
|
|
|
+ HttpConnectionParams.setSoTimeout(httpParams, readTimeout);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ if (header != null && header.size() != 0) {
|
|
|
|
+ Iterator<String> iterator = header.keySet().iterator();
|
|
|
|
+
|
|
|
|
+ while(iterator.hasNext()) {
|
|
|
|
+ String key = (String)iterator.next();
|
|
|
|
+ post.setHeader(key, (String)header.get(key));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringEntity se = new StringEntity(json, ContentType.APPLICATION_JSON);
|
|
|
|
+ se.setContentType("text/json;");
|
|
|
|
+ post.setEntity(se);
|
|
|
|
+ HttpResponse response = client.execute(post);
|
|
|
|
+ //Moka调用失败 statusCode = 403
|
|
|
|
+ //if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
|
+ HttpEntity resEntity = response.getEntity();
|
|
|
|
+ Header respHeader = resEntity.getContentEncoding();
|
|
|
|
+ if (respHeader == null || !"gzip".equalsIgnoreCase(respHeader.getValue()) && !"x-gzip".equalsIgnoreCase(respHeader.getValue())) {
|
|
|
|
+ data = EntityUtils.toString(resEntity, "UTF-8");
|
|
|
|
+ } else {
|
|
|
|
+ GzipDecompressingEntity gzipEntity = new GzipDecompressingEntity(resEntity);
|
|
|
|
+ InputStream in = gzipEntity.getContent();
|
|
|
|
+ data = getHTMLContent(in);
|
|
|
|
+ }
|
|
|
|
+ //}
|
|
|
|
+ } catch (IOException var19) {
|
|
|
|
+ logger.warn("Http postjson error", var19);
|
|
|
|
+ throw var19;
|
|
|
|
+ } catch (Exception e){
|
|
|
|
+ logger.warn("Http postjson error", e);
|
|
|
|
+ throw e;
|
|
|
|
+ } finally {
|
|
|
|
+ post.releaseConnection();
|
|
|
|
+ }
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static String getHTMLContent(InputStream in) {
|
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
|
+ BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
|
|
|
+ try {
|
|
|
|
+ String line = null;
|
|
|
|
+ while((line = br.readLine()) != null) {
|
|
|
|
+ sb.append(line);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException var12) {
|
|
|
|
+ logger.error("getHTMLContent error", var12);
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ br.close();
|
|
|
|
+ } catch (IOException var11) {
|
|
|
|
+ logger.error("getHTMLContent error", var11);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static HttpClient createHttpClient(int connectionTimeout, int readTimeout) {
|
|
|
|
+ HttpClient client = new DefaultHttpClient();
|
|
|
|
+ HttpParams httpParams = client.getParams();
|
|
|
|
+ HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout);
|
|
|
|
+ HttpConnectionParams.setSoTimeout(httpParams, readTimeout);
|
|
|
|
+ return client;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static HttpClient wrapperHttpClient(int connectionTimeout, int readTimeout) {
|
|
|
|
+ try {
|
|
|
|
+ final X509TrustManager trustManager = new X509TrustManager() {
|
|
|
|
+ @Override
|
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ public void checkClientTrusted(final javax.security.cert.X509Certificate[] arg0, final String arg1) throws CertificateException {
|
|
|
|
+ }
|
|
|
|
+ public void checkServerTrusted(final javax.security.cert.X509Certificate[] arg0, final String arg1) throws CertificateException {
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public void checkClientTrusted(final X509Certificate[] chain, final String authType) throws java.security.cert.CertificateException {
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws java.security.cert.CertificateException {
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ SSLContext ctx = SSLContext.getInstance("TLS");
|
|
|
|
+ ctx.init((KeyManager[])null, new TrustManager[]{trustManager}, (SecureRandom)null);
|
|
|
|
+ SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
|
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setCookieSpec("standard-strict").setConnectTimeout(connectionTimeout).setConnectionRequestTimeout(connectionTimeout).setSocketTimeout(readTimeout).setExpectContinueEnabled(Boolean.TRUE).setTargetPreferredAuthSchemes(Arrays.asList("NTLM", "Digest")).setProxyPreferredAuthSchemes(Arrays.asList("Basic")).build();
|
|
|
|
+ Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
|
|
|
|
+ PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
|
|
+ CloseableHttpClient closeableHttpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
|
|
|
|
+ return closeableHttpClient;
|
|
|
|
+ } catch (Exception var9) {
|
|
|
|
+ logger.error("包装无证书校验客户端失败:" + var9.getMessage());
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
/**
|
|
/**
|
|
* 新增调用日志
|
|
* 新增调用日志
|