All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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> headersArray = new ArrayList<>();
            	Header[] allHeaders = response.getAllHeaders();
            	for(Header h : allHeaders) {
            		Map headerMap = new HashMap<>();
            		headerMap.put(h.getName(), h.getValue());
            		headersArray.add(headerMap);
            	}
                responseData = new Gson().toJson(headersArray);
            } 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 contentType
	 * @param keyStore
	 * @param keyStorePass
	 * @param keyPass
	 * @return
	 */
	public static String deleteRequestStringBody(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);
			HttpDelete httpHead = new HttpDelete(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) {
                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);
		}
	}
	
	/////////////////////////////////////////////////////
	/////////////////////////////////////////////////////
	/////////////////////////////////////////////////////
	
	/*
	 * Methods for generating an access token
	 */
	
	/**
	 * Make a request to get an access token
	 * Uses hashtable for list of params
	 * @param url
	 * @param params
	 * @param json
	 * @param extract
	 * @return
	 */
	public static AccessToken getAccessToken(String url, Map params, boolean json, boolean extract) {
		AccessToken tok = null;
		CloseableHttpClient httpclient = null;
		String result = null;
		try {
			// default client
			httpclient = HttpClients.createDefault();
			// this is a post
			HttpPost httppost = new HttpPost(url);
			// loop through all keys and add as basic name value pair 
			List paramList = new ArrayList();
			params.keySet().stream().forEach(param -> paramList.add(new BasicNameValuePair(param, params.get(param))));
			// set within post
			httppost.setEntity(new UrlEncodedFormEntity(paramList));

			CloseableHttpResponse response = httpclient.execute(httppost);
			int status = response.getStatusLine().getStatusCode();
			classLogger.info("Request for access token at " + url + " returned status code = " + status);

			result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
			classLogger.info("Request response = " + Utility.cleanLogString(result));
			
			// this will set the token to use
			if(status == 200 && extract) {
				if(json) {
					tok = getJAccessToken(result);
				} else {
					tok = getAccessToken(result);
				}
			}
		} catch (UnsupportedEncodingException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} catch (ClientProtocolException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} catch (UnsupportedOperationException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} catch (IOException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} finally {
			if(httpclient != null) {
				try {
					httpclient.close();
				} catch(IOException e) {
					classLogger.error(Constants.STACKTRACE, e);
				}
			}
		}

		if(tok != null && tok.getAccess_token() == null) {
			classLogger.warn("Error occurred grabbing the access token: " + Utility.cleanLogString(result));
		}
		
		// send back the token
		return tok;
	}
	
	
	/**
	 * Make a request to get an id token
	 * Uses hashtable for list of params
	 * @param url
	 * @param params
	 * @param json
	 * @param extract
	 * @return
	 */
	public static AccessToken getIdToken(String url, Map params, boolean json, boolean extract) {
		//still using accessToken object
		AccessToken tok = null;
		CloseableHttpClient httpclient = null;
		String result = null;
		try {
			// default client
			httpclient = HttpClients.createDefault();
			// this is a post
			HttpPost httppost = new HttpPost(url);
			// loop through all keys and add as basic name value pair 
			List paramList = new ArrayList();
			params.keySet().stream().forEach(param -> paramList.add(new BasicNameValuePair(param, params.get(param))));
			// set within post
			httppost.setEntity(new UrlEncodedFormEntity(paramList));

			CloseableHttpResponse response = httpclient.execute(httppost);
			int status = response.getStatusLine().getStatusCode();
			classLogger.info("Request for access token at " + url + " returned status code = " + status);

			result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
			classLogger.info("Request response = " + Utility.cleanLogString(result));
			
			// this will set the token to use
			if(status == 200 && extract) {
				if(json) {
					tok = getJIDToken(result);
				} else {
					tok = getIDToken(result);
				}
			}
		} catch (UnsupportedEncodingException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} catch (ClientProtocolException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} catch (UnsupportedOperationException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} catch (IOException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} finally {
			if(httpclient != null) {
				try {
					httpclient.close();
				} catch(IOException e) {
					classLogger.error(Constants.STACKTRACE, e);
				}
			}
		}

		if(tok != null && tok.getAccess_token() == null) {
			classLogger.warn("Error occurred grabbing the id token: " + Utility.cleanLogString(result));
		}
		
		// send back the token
		return tok;
	}
	
	/**
	 * Get access token from a basic string
	 * @param input
	 * @return
	 */
	public static AccessToken getAccessToken(String input) {
		return getAccessToken(input, "access_token");
	}
	
	/**
	 * Get id token from a basic string
	 * @param input
	 * @return
	 */
	public static AccessToken getIDToken(String input) {
		return getAccessToken(input, "id_token");
	}
	
	/**
	 * Get access token from a basic string
	 * Example: access_token=2577b7a6ef68c2a736bf0648ea024b0f4d10e32d&scope=public_repo%2Cuser&token_type=bearer
	 * @param input
	 * @param nameOfToken
	 * @return
	 */
	public static AccessToken getAccessToken(String input, String nameOfToken) {
		String accessToken = null;
		String [] tokens = input.split("&");
		for(int tokenIndex = 0;tokenIndex < tokens.length;tokenIndex++) {
			String thisToken = tokens[tokenIndex];
			if(thisToken.startsWith(nameOfToken)) {
				accessToken = thisToken.replaceAll(nameOfToken + "=", "");
				break;
			}
		}
		AccessToken tok = new AccessToken();
		tok.setAccess_token(accessToken);
		tok.init();
		
		return tok;
	}

	/**
	 * Get the access token from a json
	 * @param input
	 * @return
	 */
	public static AccessToken getJAccessToken(String input) {
		return getJAccessToken(input, "[access_token, token_type, expires_in]");
	}
	
	/**
	 * Get the id token from a json
	 * @param input
	 * @return
	 */
	public static AccessToken getJIDToken(String input) {
		return getJAccessToken(input, "[id_token, token_type, expires_in]");
	}
	
	/**
	 * Get the access token from a json
	 * @param json
	 * @param nameOfToken
	 * @return
	 */
	public static AccessToken getJAccessToken(String json, String nameOfToken) {
		AccessToken tok = new AccessToken();
		try {
			JmesPath jmespath = new JacksonRuntime();
			// Expressions need to be compiled before you can search. Compiled expressions
			// are reusable and thread safe
			// Compile your expressions once, just like database prepared statements.
			Expression expression = jmespath.compile(nameOfToken);

			JsonNode input = mapper.readTree(json);
			JsonNode result = expression.search(input);
			if(result.size() >= 0) {
				tok.setAccess_token(result.get(0).asText());
			}
			if(result.size() >= 1) {
				tok.setToken_type(result.get(1).asText());
			}
			if(result.size() >= 2) {
				tok.setExpires_in(result.get(2).asInt());
			}
			tok.init();
		} catch (IOException e) {
			classLogger.error(Constants.STACKTRACE, e);
		}
		return tok;
	}
	
	
	
	/////////////////////////////////////////////////////
	/////////////////////////////////////////////////////
	/////////////////////////////////////////////////////

	/*
	 * Methods for making requests using the access token
	 */

	/**
	 * Makes the call to every resource going forward with the specified keys as get
	 * @param urlStr
	 * @param accessToken
	 * @return
	 */
	public static String makeGetCall(String urlStr, String accessToken) {
		return makeGetCall(urlStr, accessToken, null, true);
	}

	/**
	 * Makes the call to every resource going forward with the specified keys as get
	 * @param urlStr
	 * @param accessToken
	 * @param params
	 * @param auth
	 * @return
	 */
	public static String makeGetCall(String urlStr, String accessToken, Map params, boolean auth) {
		if(urlStr == null) {
			throw new NullPointerException("Must provide the URL");
		}
		// fill the params on the get since it is not null
		if(params != null) {
			StringBuffer urlBuf = new StringBuffer(urlStr);
			urlBuf.append("?");
			boolean first = true;
			Set keys = params.keySet();
			for(String key : keys) {
				Object value = params.get(key);
				if(!first) {
					urlBuf.append("&");
				}
				
				try {
					urlBuf.append(key).append("=").append(URLEncoder.encode(value+"", "UTF-8"));
				} catch (UnsupportedEncodingException e) {
					classLogger.error(Constants.STACKTRACE, e);
				}
				first = false;
			}
			urlStr = urlBuf.toString();
		}
		
		String retString = null;
		String responseCode = null;
		BufferedReader br = null;
		InputStreamReader isr = null;
		try {
			HttpURLConnection con = null;
			URL url = new URL(urlStr);
		    con = ( HttpURLConnection )url.openConnection();
		    con.setDoInput(true);
		    con.setDoOutput(true);
		    con.setUseCaches(false);
		    con.setRequestMethod("GET");
		    con.setRequestProperty("User-Agent", "SEMOSS");
		    if(auth) {
		    	con.setRequestProperty("Authorization","Bearer " + accessToken);
		    }
		    con.setRequestProperty("Accept","application/json"); // I added this line.
		    con.connect();
		   
		    isr = new InputStreamReader(con.getInputStream(), "UTF-8");
		    br = new BufferedReader(isr);
		    StringBuilder str = new StringBuilder();
		    String line;
		    while((line = br.readLine()) != null){
		        str.append(line);
		    }
		    retString = str.toString();
		    
		    responseCode = String.valueOf(con.getResponseCode());
		} catch (MalformedURLException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} catch (IOException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} finally {
			if(br != null) {
				try {
					br.close();
				} catch (IOException e) {
					classLogger.error(Constants.STACKTRACE, e);
				}
			}
			if(isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					classLogger.error(Constants.STACKTRACE, e);
				}
			}
		}
		
		classLogger.info("Return from " + urlStr + " with response " + responseCode + " = " + retString);
	    if (responseCode.startsWith("4") || responseCode.startsWith("5")) {
	    	throw new IllegalArgumentException(retString);
	    }
		
		return retString;
	}

	// makes the call to every resource going forward with the specified keys as get
	public static BufferedReader getHttpStream(String urlStr, String accessToken, Map params, boolean auth) {
		// fill the params on the get since it is not null
		// fill the params on the get since it is not null
		if(params != null) {
			StringBuffer urlBuf = new StringBuffer(urlStr);
			urlBuf.append("?");
			boolean first = true;
			Set keys = params.keySet();
			for(String key : keys) {
				Object value = params.get(key);
				if(!first) {
					urlBuf.append("&");
				}
				
				try {
					urlBuf.append(key).append("=").append(URLEncoder.encode(value+"", "UTF-8"));
				} catch (UnsupportedEncodingException e) {
					classLogger.error(Constants.STACKTRACE, e);
				}
				first = false;
			}
			urlStr = urlBuf.toString();
		}
		
		try {
			HttpURLConnection con = null;
			URL url = new URL(urlStr);
		    con = ( HttpURLConnection )url.openConnection();
		    con.setDoInput(true);
		    con.setDoOutput(true);
		    con.setUseCaches(false);
		    con.setRequestMethod("GET");
		    con.setRequestProperty("User-Agent", "SEMOSS");
		    if(auth) {
		    	con.setRequestProperty("Authorization","Bearer " + accessToken);
		    }
		    con.setRequestProperty("Accept","application/json"); // I added this line.
		    con.connect();

		    BufferedReader br = new BufferedReader(new InputStreamReader( con.getInputStream(), "UTF-8" ));
		    return br;
		} catch (MalformedURLException e) {
			classLogger.error(Constants.STACKTRACE, e);
		} catch (IOException e) {
			classLogger.error(Constants.STACKTRACE, e);
		}
		
		return null;
	}

	// make a post call
	
	public static String makePostCall(String url, String accessToken, Object input,  boolean json) {
		CloseableHttpClient httpclient = null;
		try {
			httpclient = HttpClients.createDefault();
			HttpPost httppost = new HttpPost(url);
			httppost.addHeader("Authorization", "Bearer " + accessToken);
			httppost.addHeader("Content-Type","application/json; charset=utf-8");
			Hashtable params = null;
			List paramList = new ArrayList();
			if(!json) {
				params = (Hashtable)input;
				Enumeration keys = params.keys();
				while (keys.hasMoreElements()) {
					String key = keys.nextElement();
					String value = (String) params.get(key);
					paramList.add(new BasicNameValuePair(key, value));
				}
				httppost.setEntity(new UrlEncodedFormEntity(paramList));
			}
			// this is a json input
			else {
				String inputJson = mapper.writeValueAsString(input);
				httppost.setEntity(new StringEntity(inputJson));
			}
			
			ResponseHandler handler = new BasicResponseHandler();
			CloseableHttpResponse response = httpclient.execute(httppost);
			
			System.out.println("Response Code " + response.getStatusLine().getStatusCode());
			
			BufferedReader rd = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8"));
			StringBuffer result = new StringBuffer();
			String line = "";
			while ((line = rd.readLine()) != null) {
				result.append(line);
			}
			return result.toString();
		} catch(Exception ex) {
			classLogger.error(Constants.STACKTRACE, ex);
		} finally {
			if(httpclient != null) {
		          try {
		            httpclient.close();
		          } catch(IOException e) {
		              classLogger.error(Constants.STACKTRACE, e);
		          }
		        }
		}
		
		return null;

	}
	
	public static String makeBinaryFilePutCall(String url, String accessToken, String fileName,  String localPath){
		CloseableHttpClient httpclient = null;
			try {
				httpclient = HttpClients.createDefault();
				HttpPut httpput = new HttpPut(url);
				httpput.addHeader("Authorization", "Bearer " + accessToken);
				httpput.addHeader("Content-Type","application/json; charset=utf-8");
				File fileupload = new File(localPath);
				httpput.setEntity(new FileEntity(fileupload));
				ResponseHandler handler = new BasicResponseHandler();
				CloseableHttpResponse response = httpclient.execute(httpput);
				
				System.out.println("Response Code " + response.getStatusLine().getStatusCode());
				
				int status = response.getStatusLine().getStatusCode();
				
				BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

				StringBuffer result = new StringBuffer();
				String line = "";
				while ((line = rd.readLine()) != null) {
					result.append(line);
				}
				return result.toString();
			}catch(Exception ex)
			{
				classLogger.error(Constants.STACKTRACE, ex);
			} finally {
				if(httpclient != null) {
			          try {
			            httpclient.close();
			          } catch(IOException e) {
			              classLogger.error(Constants.STACKTRACE, e);
			          }
			        }
			}
			
			return null;

		}
	
	public static String makeBinaryFilePostCall(String url, String accessToken, String filename, String filepath)
	{
		CloseableHttpClient httpclient = null;
		try {
			httpclient = HttpClients.createDefault();
			HttpPost httppost = new HttpPost(url);
			httppost.addHeader("Authorization", "Bearer " + accessToken);
			httppost.addHeader("Content-Type","application/octet-stream");
			httppost.addHeader("Dropbox-API-Arg","{\"path\": \"/"+filename+"\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}");
			File fileupload = new File(filepath);
			httppost.setEntity(new FileEntity(fileupload));
			ResponseHandler handler = new BasicResponseHandler();
			CloseableHttpResponse response = httpclient.execute(httppost);
			
			System.out.println("Response Code " + response.getStatusLine().getStatusCode());
			
			int status = response.getStatusLine().getStatusCode();
			
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
			StringBuffer result = new StringBuffer();
			String line = "";
			while ((line = rd.readLine()) != null) {
				result.append(line);
			}
			return result.toString();
		}catch(Exception ex)
		{
			classLogger.error(Constants.STACKTRACE, ex);
		} finally {
			if(httpclient != null) {
		          try {
		            httpclient.close();
		          } catch(IOException e) {
		              classLogger.error(Constants.STACKTRACE, e);
		          }
		        }
		}
		return null;

	}
	
	public static String makeBinaryFilePatchCall(String url, String accessToken, String filepath)
	{
		CloseableHttpClient httpclient = null;
		try {
			 httpclient = HttpClients.createDefault();
			HttpPatch httppatch = new HttpPatch(url);
			httppatch.addHeader("Authorization", "Bearer " + accessToken);

			File fileupload = new File(filepath);
			httppatch.setEntity(new FileEntity(fileupload));
			ResponseHandler handler = new BasicResponseHandler();
			CloseableHttpResponse response = httpclient.execute(httppatch);
			
			System.out.println("Response Code " + response.getStatusLine().getStatusCode());
			
			int status = response.getStatusLine().getStatusCode();
			
			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

			StringBuffer result = new StringBuffer();
			String line = "";
			while ((line = rd.readLine()) != null) {
				result.append(line);
			}
			return result.toString();
		}catch(Exception ex)
		{
			classLogger.error(Constants.STACKTRACE, ex);
		} finally {
			if(httpclient != null) {
		          try {
		            httpclient.close();
		          } catch(IOException e) {
		              classLogger.error(Constants.STACKTRACE, e);
		          }
		        }
		}
		return null;

	}
	
	
	// makes the call to every resource going forward with the specified keys as post
	
	public static String [] getCodes(String queryStr) {
	 	String [] retString = new String[2];
		String[] inputCodes = URLDecoder.decode(queryStr).split("&");
		for(int inputIndex = 0;inputIndex < inputCodes.length;inputIndex++) {
			String thisToken = Utility.inputSQLSanitizer(inputCodes[inputIndex]);
			if(thisToken.startsWith("state")) {
				retString[1] = thisToken.replaceAll("state=", "");
			}
			if(thisToken.startsWith("code")) {
				retString[0] = thisToken.replaceAll("code=", "");
			}
		}
		
		return retString;
	}
	
	//////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////
	
	private HttpHelperUtility() {
		
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy