
prerna.security.HttpHelperUtility Maven / Gradle / Ivy
The newest version!
package prerna.security;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.net.ssl.HostnameVerifier;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import io.burt.jmespath.Expression;
import io.burt.jmespath.JmesPath;
import io.burt.jmespath.jackson.JacksonRuntime;
import jodd.util.URLDecoder;
import prerna.auth.AccessToken;
import prerna.io.connector.antivirus.VirusScannerUtils;
import prerna.util.Constants;
import prerna.util.Utility;
public final class HttpHelperUtility {
private static final Logger classLogger = LogManager.getLogger(HttpHelperUtility.class.getName());
private static ObjectMapper mapper = new ObjectMapper();
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
/**
* Get a custom client using the info passed in
* @param cookieStore
* @param keyStore the keystore location
* @param keyStorePass the password for the keystore
* @param keyPass the password for the certificate if different from the keystore password
* @return
*/
public static CloseableHttpClient getCustomClient(CookieStore cookieStore, String keyStore, String keyStorePass, String keyPass) {
HttpClientBuilder builder = HttpClients.custom();
if(cookieStore != null) {
builder.setDefaultCookieStore(cookieStore);
}
TrustStrategy trustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
};
HostnameVerifier verifier = new NoopHostnameVerifier();
SSLConnectionSocketFactory connFactory = null;
try {
SSLContextBuilder sslContextBuilder = SSLContextBuilder.create().loadTrustMaterial(trustStrategy);
// add the cert if required
if(keyStore != null && !keyStore.isEmpty() && keyStorePass != null && !keyStorePass.isEmpty()) {
File keyStoreF = new File(keyStore);
if(!keyStoreF.exists() && !keyStoreF.isFile()) {
classLogger.warn("Defined a keystore to use in the request but the file " + keyStoreF.getAbsolutePath() + " does not exist");
} else {
if(keyPass == null || keyPass.isEmpty()) {
sslContextBuilder.loadKeyMaterial(keyStoreF, keyStorePass.toCharArray(), keyStorePass.toCharArray());
} else {
sslContextBuilder.loadKeyMaterial(keyStoreF, keyStorePass.toCharArray(), keyPass.toCharArray());
}
}
}
connFactory = new SSLConnectionSocketFactory(
sslContextBuilder.build()
, new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"}
, null
, verifier)
// {
// @Override
// protected void prepareSocket(SSLSocket socket) {
// socket.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" });
// }
// }
;
} catch (KeyManagementException e) {
classLogger.error(Constants.STACKTRACE, e);
} catch (NoSuchAlgorithmException e) {
classLogger.error(Constants.STACKTRACE, e);
} catch (KeyStoreException e) {
classLogger.error(Constants.STACKTRACE, e);
} catch (UnrecoverableKeyException e) {
classLogger.error(Constants.STACKTRACE, e);
} catch (CertificateException e) {
classLogger.error(Constants.STACKTRACE, e);
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
}
builder.setSSLSocketFactory(connFactory);
return builder.build();
}
/**
*
* @param url
* @param headerMap
* @param keyStore
* @param keyStorePass
* @param keyPass
* @return
*/
public static String getRequest(String url, Map headerMap, String keyStore, String keyStorePass, String keyPass) {
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = null;
HttpEntity entity = null;
String responseData = null;
try {
httpClient = HttpHelperUtility.getCustomClient(null, keyStore, keyStorePass, keyPass);
HttpGet httpGet = new HttpGet(url);
if(headerMap != null && !headerMap.isEmpty()) {
for(String key : headerMap.keySet()) {
httpGet.addHeader(key, headerMap.get(key));
}
}
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
entity = response.getEntity();
if (statusCode >= 200 && statusCode < 300) {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : null;
} else {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : "";
throw new IllegalArgumentException("Connected to " + url + " but received error = " + responseData);
}
return responseData;
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
throw new IllegalArgumentException("Could not connect to URL at " + url);
} finally {
if(entity != null) {
try {
EntityUtils.consume(entity);
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
}
}
if(response != null) {
try {
response.close();
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
}
}
if(httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
}
}
}
}
/**
*
* @param url
* @param headerMap
* @param keyStore
* @param keyStorePass
* @param keyPass
* @param saveFilePath
* @param saveFileName
* @return
*/
public static File getRequestFileDownload(String url, Map headerMap, String keyStore, String keyStorePass, String keyPass, String saveFilePath, String saveFileName) {
String fileName = saveFileName;
if(fileName == null) {
// if not passed in, see if we can grab it from the URL
String[] pathSeparated = url.split("/");
fileName = pathSeparated[pathSeparated.length - 1];
if (fileName == null) {
throw new IllegalArgumentException("Url path does not end in a file name");
}
}
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = null;
InputStream is = null;
// used if virus scanning
ByteArrayOutputStream baos = null;
ByteArrayInputStream bais = null;
HttpEntity entity = null;
try {
httpClient = HttpHelperUtility.getCustomClient(null, keyStore, keyStorePass, keyPass);
HttpGet httpGet = new HttpGet(url);
if(headerMap != null && !headerMap.isEmpty()) {
for(String key : headerMap.keySet()) {
httpGet.addHeader(key, headerMap.get(key));
}
}
response = httpClient.execute(httpGet);
File fileDir = new File(saveFilePath);
if (!fileDir.exists()) {
Boolean success = fileDir.mkdirs();
if(!success) {
classLogger.warn("Unable to make the directory to save the file at location: " + Utility.cleanLogString(saveFilePath));
throw new IllegalArgumentException("Directory to save the file download does not exist and could not be created");
}
}
String fileLocation = Utility.getUniqueFilePath(saveFilePath, fileName);
File savedFile = new File(fileLocation);
entity = response.getEntity();
is = entity.getContent();
if (Utility.isVirusScanningEnabled()) {
try {
baos = new ByteArrayOutputStream();
IOUtils.copy(is, baos);
bais = new ByteArrayInputStream(baos.toByteArray());
Map> viruses = VirusScannerUtils.getViruses(fileName, bais);
if (!viruses.isEmpty()) {
String error = "File contained " + viruses.size() + " virus";
if (viruses.size() > 1) {
error = error + "es";
}
throw new IllegalArgumentException(error);
}
bais.reset();
FileUtils.copyInputStreamToFile(bais, savedFile);
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
throw new IllegalArgumentException("Could not read file item.");
}
} else {
FileUtils.copyInputStreamToFile(is, savedFile);
}
return savedFile;
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
throw new IllegalArgumentException("Could not connect to URL at " + url);
} finally {
if(is != null) {
IOUtils.closeQuietly(is);
}
if(bais != null) {
IOUtils.closeQuietly(bais);
}
if(baos != null) {
IOUtils.closeQuietly(baos);
}
if(entity != null) {
try {
EntityUtils.consume(entity);
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
}
}
if(response != null) {
try {
response.close();
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
}
}
if(httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
}
}
}
}
/**
*
* @param url
* @param headersMap
* @param bodyMap
* @param keyStore
* @param keyStorePass
* @param keyPass
* @return
*/
public static String postRequestUrlEncodedBody(String url, Map headersMap, Map bodyMap, String keyStore, String keyStorePass, String keyPass) {
String responseData = null;
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
httpClient = HttpHelperUtility.getCustomClient(null, keyStore, keyStorePass, keyPass);
HttpPost httpPost = new HttpPost(url);
if(headersMap != null && !headersMap.isEmpty()) {
for(String key : headersMap.keySet()) {
httpPost.addHeader(key, headersMap.get(key));
}
}
if(bodyMap != null && !bodyMap.isEmpty()) {
List params = new ArrayList();
for(String key : bodyMap.keySet()) {
params.add(new BasicNameValuePair(key, bodyMap.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
}
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
entity = response.getEntity();
if (statusCode >= 200 && statusCode < 300) {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : null;
} else {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : "";
throw new IllegalArgumentException("Connected to " + url + " but received error = " + responseData);
}
return responseData;
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
throw new IllegalArgumentException("Could not connect to URL at " + url + " and received error = " + e.getMessage());
}
}
/**
*
* @param url
* @param headersMap
* @param body
* @param contentType
* @param keyStore
* @param keyStorePass
* @param keyPass
* @return
*/
public static String postRequestStringBody(String url, Map headersMap, String body, ContentType contentType, String keyStore, String keyStorePass, String keyPass) {
String responseData = null;
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
httpClient = HttpHelperUtility.getCustomClient(null, keyStore, keyStorePass, keyPass);
HttpPost httpPost = new HttpPost(url);
if(headersMap != null && !headersMap.isEmpty()) {
for(String key : headersMap.keySet()) {
httpPost.addHeader(key, headersMap.get(key));
}
}
if(body != null && !body.isEmpty()) {
httpPost.setEntity(new StringEntity(body, contentType));
}
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
entity = response.getEntity();
if (statusCode >= 200 && statusCode < 300) {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : null;
} else {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : "";
throw new IllegalArgumentException("Connected to " + url + " but received error = " + responseData);
}
return responseData;
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
throw new IllegalArgumentException("Could not connect to URL at " + url);
}
}
/**
*
* @param url
* @param headersMap
* @param body
* @param contentType
* @param keyStore
* @param keyStorePass
* @param keyPass
* @return
*/
public static String putRequestStringBody(String url, Map headersMap, String body, ContentType contentType, String keyStore, String keyStorePass, String keyPass) {
String responseData = null;
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
httpClient = HttpHelperUtility.getCustomClient(null, keyStore, keyStorePass, keyPass);
HttpPut httpPut = new HttpPut(url);
if(headersMap != null && !headersMap.isEmpty()) {
for(String key : headersMap.keySet()) {
httpPut.addHeader(key, headersMap.get(key));
}
}
if(body != null && !body.isEmpty()) {
httpPut.setEntity(new StringEntity(body, contentType));
}
response = httpClient.execute(httpPut);
int statusCode = response.getStatusLine().getStatusCode();
entity = response.getEntity();
if (statusCode >= 200 && statusCode < 300) {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : null;
} else {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : "";
throw new IllegalArgumentException("Connected to " + url + " but received error = " + responseData);
}
return responseData;
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
throw new IllegalArgumentException("Could not connect to URL at " + url);
}
}
/**
*
* @param url
* @param headersMap
* @param bodyMap
* @param keyStore
* @param keyStorePass
* @param keyPass
* @return
*/
public static String putRequestUrlEncodedBody(String url, Map headersMap, Map bodyMap, String keyStore, String keyStorePass, String keyPass) {
String responseData = null;
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
httpClient = HttpHelperUtility.getCustomClient(null, keyStore, keyStorePass, keyPass);
HttpPut httpPost = new HttpPut(url);
if(headersMap != null && !headersMap.isEmpty()) {
for(String key : headersMap.keySet()) {
httpPost.addHeader(key, headersMap.get(key));
}
}
if(bodyMap != null && !bodyMap.isEmpty()) {
List params = new ArrayList();
for(String key : bodyMap.keySet()) {
params.add(new BasicNameValuePair(key, bodyMap.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
}
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
entity = response.getEntity();
if (statusCode >= 200 && statusCode < 300) {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : null;
} else {
responseData = entity != null ? EntityUtils.toString(entity, "UTF-8") : "";
throw new IllegalArgumentException("Connected to " + url + " but received error = " + responseData);
}
return responseData;
} catch (IOException e) {
classLogger.error(Constants.STACKTRACE, e);
throw new IllegalArgumentException("Could not connect to URL at " + url + " and received error = " + e.getMessage());
}
}
/**
* Return the headers from the request only
*
* @param url
* @param headersMap
* @param keyStore
* @param keyStorePass
* @param keyPass
* @return
*/
public static String headRequest(String url, Map headersMap, String keyStore, String keyStorePass, String keyPass) {
String responseData = null;
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
try {
httpClient = HttpHelperUtility.getCustomClient(null, keyStore, keyStorePass, keyPass);
HttpHead httpHead = new HttpHead(url);
if(headersMap != null && !headersMap.isEmpty()) {
for(String key : headersMap.keySet()) {
httpHead.addHeader(key, headersMap.get(key));
}
}
response = httpClient.execute(httpHead);
int statusCode = response.getStatusLine().getStatusCode();
entity = response.getEntity();
if (statusCode >= 200 && statusCode < 300) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy