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

com.github.hi_fi.httprequestlibrary.utils.RestClient Maven / Gradle / Ivy

package com.github.hi_fi.httprequestlibrary.utils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
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.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
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.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import com.github.hi_fi.httprequestlibrary.domain.Authentication;
import com.github.hi_fi.httprequestlibrary.domain.Session;

public class RestClient {

	RobotLogger logger = new RobotLogger("RestClient");
	private static Map sessions = new HashMap();

	public Session getSession(String alias) {
		return sessions.get(alias);
	}

	public void createSession(String alias, String url, Map headers, Authentication auth, String verify,
			Boolean debug) {
		if (debug) {
			System.setProperty("org.apache.commons.logging.Log",
					"com.github.hi_fi.httprequestlibrary.utils.RobotLogger");
			System.setProperty("org.apache.commons.logging.robotlogger.log.org.apache.http", "DEBUG");
		}
		HttpHost target;
		try {
			target = URIUtils.extractHost(new URI(url));
		} catch (URISyntaxException e) {
			throw new RuntimeException("Parsing of URL failed. Error message: " + e.getMessage());
		}
		Session session = new Session();
		session.setContext(this.createContext(auth, target));
		session.setClient(this.createHttpClient(auth, verify, target, false));
		session.setUrl(url);
		session.setHeaders(headers);
		session.setHttpHost(target);
		session.setVerify(verify);
		session.setAuthentication(auth);
		sessions.put(alias, session);
	}

	public void makeGetRequest(String alias, String uri, Map headers, Map parameters,
			boolean allowRedirects) {
		logger.debug("Making GET request");
		HttpGet getRequest = new HttpGet(this.buildUrl(alias, uri, parameters));
		getRequest = this.setHeaders(getRequest, headers);
		getRequest.setConfig(RequestConfig.custom().setRedirectsEnabled(allowRedirects).build());
		Session session = this.getSession(alias);
		this.makeRequest(getRequest, session);
	}

	public void makeHeadRequest(String alias, String uri, Map headers, Boolean allowRedirects) {
		logger.debug("Making HEAD request");
		HttpHead headRequest = new HttpHead(this.buildUrl(alias, uri));
		headRequest = this.setHeaders(headRequest, headers);
		headRequest.setConfig(RequestConfig.custom().setRedirectsEnabled(allowRedirects).build());
		Session session = this.getSession(alias);
		this.makeRequest(headRequest, session);
	}

	public void makeOptionsRequest(String alias, String uri, Map headers, Boolean allowRedirects) {
		logger.debug("Making OPTIONS request");
		HttpOptions patchRequest = new HttpOptions(this.buildUrl(alias, uri));
		patchRequest = this.setHeaders(patchRequest, headers);

		if (allowRedirects) {
			Session session = this.getSession(alias);
			session.setClient(this.createHttpClient(session.getAuthentication(), session.getVerify(),
					session.getHttpHost(), true));
		}
		Session session = this.getSession(alias);
		this.makeRequest(patchRequest, session);
	}

	public void makePatchRequest(String alias, String uri, Object data, Map headers,
			Map files, Boolean allowRedirects) {
		logger.debug("Making PATCH request");
		HttpPatch patchRequest = new HttpPatch(this.buildUrl(alias, uri));
		patchRequest = this.setHeaders(patchRequest, headers);
		if (data.toString().length() > 0) {
			logger.debug(data);
			patchRequest.setEntity(this.createDataEntity(data));
		}
		if (files.entrySet().size() > 0) {
			logger.debug(files);
			patchRequest.setEntity(this.createFileEntity(files));
		}
		if (allowRedirects) {
			Session session = this.getSession(alias);
			session.setClient(this.createHttpClient(session.getAuthentication(), session.getVerify(),
					session.getHttpHost(), true));
		}
		Session session = this.getSession(alias);
		this.makeRequest(patchRequest, session);
	}

	public void makePostRequest(String alias, String uri, Object data, Map parameters,
			Map headers, Map files, Boolean allowRedirects) {
		logger.debug("Making POST request");
		HttpPost postRequest = new HttpPost(this.buildUrl(alias, uri, parameters));
		postRequest = this.setHeaders(postRequest, headers);
		if (data.toString().length() > 0) {
			logger.debug(data);
			postRequest.setEntity(this.createDataEntity(data));
		}
		if (files.entrySet().size() > 0) {
			logger.debug(files);
			postRequest.setEntity(this.createFileEntity(files));
		}
		if (allowRedirects) {
			Session session = this.getSession(alias);
			session.setClient(this.createHttpClient(session.getAuthentication(), session.getVerify(),
					session.getHttpHost(), true));
		}
		Session session = this.getSession(alias);
		this.makeRequest(postRequest, session);
	}

	public void makePutRequest(String alias, String uri, Object data, Map parameters,
			Map headers, Map files, Boolean allowRedirects) {
		logger.debug("Making PUT request");
		HttpPut putRequest = new HttpPut(this.buildUrl(alias, uri, parameters));
		putRequest = this.setHeaders(putRequest, headers);
		if (data.toString().length() > 0) {
			logger.debug(data);
			putRequest.setEntity(this.createDataEntity(data));
		}
		if (files.entrySet().size() > 0) {
			logger.debug(files);
			putRequest.setEntity(this.createFileEntity(files));
		}
		if (allowRedirects) {
			Session session = this.getSession(alias);
			session.setClient(this.createHttpClient(session.getAuthentication(), session.getVerify(),
					session.getHttpHost(), true));
		}
		Session session = this.getSession(alias);
		this.makeRequest(putRequest, session);
	}

	public void makeDeleteRequest(String alias, String uri, Object data, Map parameters,
			Map headers, Boolean allowRedirects) {
		logger.debug("Making DELETE request");
		HttpDeleteWithBody deleteRequest = new HttpDeleteWithBody(this.buildUrl(alias, uri, parameters));
		deleteRequest = this.setHeaders(deleteRequest, headers);
		if (data.toString().length() > 0) {
			logger.debug(data);
			deleteRequest.setEntity(this.createDataEntity(data));
		}

		if (allowRedirects) {
			Session session = this.getSession(alias);
			session.setClient(this.createHttpClient(session.getAuthentication(), session.getVerify(),
					session.getHttpHost(), true));
		}
		Session session = this.getSession(alias);
		this.makeRequest(deleteRequest, session);
	}

	@SuppressWarnings("unchecked")
	private HttpEntity createDataEntity(Object data) {
		try {
			if (data instanceof Map) {
				List params = new ArrayList(0);
				for (Entry entry : ((Map) data).entrySet()) {
					params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
				}
				return new UrlEncodedFormEntity(params, "UTF-8");
			} else {
				return new StringEntity(data.toString());
			}
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("Unsupported encoding noticed. Error message: " + e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	private HttpEntity createFileEntity(Object files) {
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		for (Entry entry : ((Map) files).entrySet()) {
			if (new File(entry.getValue().toString()).exists()) {
				builder.addPart(entry.getKey(),
						new FileBody(new File(entry.getValue().toString()), ContentType.DEFAULT_BINARY));
			} else {
				builder.addPart(entry.getKey(), new StringBody(entry.getValue().toString(), ContentType.DEFAULT_TEXT));
			}
		}
		return builder.build();
	}

	private void makeRequest(HttpUriRequest request, Session session) {
		request = this.setHeaders(request, session.getHeaders());
		try {
			session.setResponse(session.getClient().execute(request, session.getContext()));
		} catch (ClientProtocolException e) {
			throw new RuntimeException("Client protocol Exception. Message: " + e.getMessage());
		} catch (IOException e) {
			throw new RuntimeException("IO exception. Message: " + e.getMessage());
		}
	}

	private HttpClientContext createContext(Authentication auth, HttpHost target) {
		HttpClientContext httpClientContext = HttpClientContext.create();
		CookieStore cookieStore = new BasicCookieStore();
		httpClientContext.setCookieStore(cookieStore);
		if (auth.usePreemptiveAuthentication()) {
			httpClientContext.setAuthCache(new Security().getAuthCache(auth, target));
		}
		return httpClientContext;
	}

	private HttpClient createHttpClient(Authentication auth, String verify, HttpHost target, Boolean postRedirects) {
		Security security = new Security();
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

		logger.debug("Verify value: " + verify);
		logger.debug((new File(verify).getAbsolutePath()));

		if (new File(verify).exists()) {
			logger.debug("Loading custom keystore");
			httpClientBuilder.setSSLSocketFactory(
					security.allowAllCertificates(security.createCustomKeyStore(verify.toString())));
		} else if (!Boolean.parseBoolean(verify.toString())) {
			logger.debug("Allowing all certificates");
			httpClientBuilder.setSSLSocketFactory(security.allowAllCertificates(null));
		}

		if (auth.isAuthenticable()) {
			httpClientBuilder.setDefaultCredentialsProvider(security.getCredentialsProvider(auth, target));
		}

		if (postRedirects) {
			httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());
		}
		RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build();
		httpClientBuilder.setDefaultRequestConfig(requestConfig);

		return httpClientBuilder.build();
	}

	private String buildUrl(String alias, String uri) {
		return this.buildUrl(alias, uri, new HashMap());
	}

	private String buildUrl(String alias, String uri, Map parameters) {
		String url = this.getSession(alias).getUrl();
		if (uri.length() > 0) {
			String separator = uri.startsWith("/") ? "" : "/";
			url = url + separator + uri;
		}
		String parameterString = "";
		for (String key : parameters.keySet()) {
			try {
				parameterString += key + "=" + URLEncoder.encode(parameters.get(key), "UTF-8") + "&";
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException("Unsupported encoding noticed. Error message: " + e.getMessage());
			}
		}
		url += parameterString.length() > 1 ? "?" + parameterString.substring(0, parameterString.length() - 1) : "";
		return url;
	}

	private  T setHeaders(T request, Map headers) {
		for (Entry entry : headers.entrySet()) {
			((HttpRequest) request).setHeader(entry.getKey(), entry.getValue());
		}
		return request;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy