|
|
|
@ -0,0 +1,185 @@
|
|
|
|
|
package org.pavlik.helpers;
|
|
|
|
|
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
|
import org.apache.http.client.config.CookieSpecs;
|
|
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
|
import org.apache.http.ssl.PrivateKeyStrategy;
|
|
|
|
|
import org.apache.http.ssl.SSLContexts;
|
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.security.*;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RestHelper by Pavel Belyaev
|
|
|
|
|
*/
|
|
|
|
|
public class RestHelper {
|
|
|
|
|
|
|
|
|
|
public String codepage="UTF-8";
|
|
|
|
|
protected HttpClient httpClient = null;
|
|
|
|
|
|
|
|
|
|
public RestHelper () {
|
|
|
|
|
this(null,"", "", "", null, "", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RestHelper (String keypath,String keytype,String keypass, String keyAlias, String trustpath, String trusttype, String trustpass) {
|
|
|
|
|
boolean withTrust = false;
|
|
|
|
|
KeyStore truststore_material = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KeyStore keystore_material = (keypath !=null) ? this.readKeyStore(keypath, keytype, keypass) : null;
|
|
|
|
|
PrivateKeyStrategy privateKeyStrategy = keyAlias == null ? null : (aliases, socket) -> keyAlias;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (trustpath !=null) {
|
|
|
|
|
truststore_material = this.readKeyStore(trustpath, trusttype, trustpass);
|
|
|
|
|
withTrust = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SSLContext sslContext = (withTrust) ?
|
|
|
|
|
genSSLContext(privateKeyStrategy,keystore_material,truststore_material,keypass) :
|
|
|
|
|
genSSLContextAllTrust(privateKeyStrategy,keystore_material,keypass);
|
|
|
|
|
|
|
|
|
|
int timeout=61;
|
|
|
|
|
RequestConfig reqConf = RequestConfig.custom()
|
|
|
|
|
.setCookieSpec(CookieSpecs.STANDARD)
|
|
|
|
|
.setConnectTimeout(timeout * 1000)
|
|
|
|
|
.setConnectionRequestTimeout(timeout * 1000)
|
|
|
|
|
.setSocketTimeout(timeout * 1000)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
this.httpClient = HttpClients.custom().setSSLContext(sslContext).setDefaultRequestConfig(reqConf).build();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only keystore init
|
|
|
|
|
* @param keypath
|
|
|
|
|
* @param keytype
|
|
|
|
|
* @param keypass
|
|
|
|
|
* @param keyAlias
|
|
|
|
|
*/
|
|
|
|
|
public RestHelper (String keypath,String keytype,String keypass, String keyAlias) {
|
|
|
|
|
this(keypath,keytype,keypass,keyAlias, null, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Подгружает файл JKS или PKCS12
|
|
|
|
|
* @param path - путь до jks/p12
|
|
|
|
|
* @param type - JKS или PKCS12
|
|
|
|
|
* @param pwd - пароль к хранилищу
|
|
|
|
|
* @return - хранилище для SSLContext
|
|
|
|
|
*/
|
|
|
|
|
protected KeyStore readKeyStore(String path, String type, String pwd) {
|
|
|
|
|
try {
|
|
|
|
|
FileInputStream KeyStoreFile = new FileInputStream(new File(path));
|
|
|
|
|
KeyStore keyStore = KeyStore.getInstance(type);
|
|
|
|
|
keyStore.load(KeyStoreFile, pwd.toCharArray());
|
|
|
|
|
return keyStore;
|
|
|
|
|
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* mtls - взаимная аутентификация
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
SSLContext genSSLContext (PrivateKeyStrategy privateKeyStrategy,KeyStore keyStore, KeyStore trustStore, String keypass) {
|
|
|
|
|
try {
|
|
|
|
|
return SSLContexts.custom()
|
|
|
|
|
.loadTrustMaterial(trustStore,null)
|
|
|
|
|
.loadKeyMaterial(keyStore, keypass.toCharArray(),privateKeyStrategy)
|
|
|
|
|
.build();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Аутентификация только клиента, серверный сертификат не проверяется
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
SSLContext genSSLContextAllTrust (PrivateKeyStrategy privateKeyStrategy,KeyStore keyStore, String keypass) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return SSLContexts.custom()
|
|
|
|
|
.loadTrustMaterial(null, (x509CertChain, authType) -> true) //вариант принимающий всё
|
|
|
|
|
.loadKeyMaterial(keyStore, keypass.toCharArray(),privateKeyStrategy) // use null as second param if you don't have a separate key password
|
|
|
|
|
.build();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void map2headers(HttpPost query,HashMap<String, String> HeaderMap) {
|
|
|
|
|
for (String key : HeaderMap.keySet()) {
|
|
|
|
|
query.setHeader(key,HeaderMap.get(key));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getRespBody(HttpResponse resp){
|
|
|
|
|
try {
|
|
|
|
|
HttpEntity entity = resp.getEntity();
|
|
|
|
|
String body = EntityUtils.toString(entity, this.codepage);
|
|
|
|
|
EntityUtils.consume(entity);
|
|
|
|
|
return body;
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Отправляет запрос "как есть" строкой
|
|
|
|
|
* @param queryUrl - url
|
|
|
|
|
* @param queryBody - тело запроса
|
|
|
|
|
* @param headerMap - заголовки запроса
|
|
|
|
|
*/
|
|
|
|
|
public HttpResponse queryPostRaw(String queryUrl, String queryBody, HashMap<String, String> headerMap) {
|
|
|
|
|
HttpPost query = new HttpPost(queryUrl);
|
|
|
|
|
if (headerMap != null) this.map2headers(query, headerMap);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
query.setEntity(new StringEntity(queryBody,this.codepage)); //тело
|
|
|
|
|
return this.httpClient.execute(query);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HttpResponse queryGet(String queryUrl) {
|
|
|
|
|
HttpGet httpGet = new HttpGet(queryUrl);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
//query.setEntity(new StringEntity(queryBody,this.codepage)); //тело
|
|
|
|
|
return this.httpClient.execute(httpGet);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
//throw new RuntimeException(e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String queryGetBody(String queryUrl) {
|
|
|
|
|
System.out.println("Query:"+queryUrl);
|
|
|
|
|
HttpResponse res = queryGet(queryUrl);
|
|
|
|
|
if (res==null) return null;
|
|
|
|
|
|
|
|
|
|
return getRespBody(res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|