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

nl.tno.bim.nmd.services.RestDataService Maven / Gradle / Ivy

The newest version!
package nl.tno.bim.nmd.services;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class RestDataService {

	protected ObjectMapper mapper = new ObjectMapper();
	PersistentHttpCache cache = new PersistentHttpCache();

	private String scheme;
	private String host;
	private Integer port;
	protected HttpClient httpClient = HttpClients.createDefault();
	HttpUriRequest request;

	protected HttpResponse performGetRequestWithParams(String path, Map params, Boolean checkCache) {
		HttpResponse response = null;
		request = this.createHttpGetRequest(path, params);

		// Execute and get the response.
		try {
			response = checkCache ? cache.get(request) : null; 
			if (response == null) {
				response = this.httpClient.execute(request);
				if (response.getStatusLine().getStatusCode() == 200 && checkCache) {
					response = cache.put(request, response);
				}
			}
		} catch (Exception e) {
			System.out.println("failed to perform get request: " + e.getMessage());
		}

		return response;
	}

	protected HttpResponse performPostRequestWithParams(String path, Map params,
			List headers, Object body) {
		// Try add a Post request to the base class.
		request = new HttpPost(createUri(path, params));

		if (headers != null) {
			for (NameValuePair header : headers) {
				request.addHeader(header.getName(), header.getValue().toString());
			}
		}

		// Execute and get the response.
		HttpResponse response = null;
		try {
			if (body != null) {
				String jsonBody = mapper.writeValueAsString(body);
				((HttpPost) request).setEntity(new StringEntity(jsonBody, "UTF-8"));
				request.setHeader("Accept", "application/json");
				request.setHeader("Content-type", "application/json;charset=UTF-8");
			}

			response = httpClient.execute(request);
		} catch (Exception e) {
			System.out.println("post request failed " + response.getStatusLine().toString());
		}
		return response;
	}

	protected HttpGet createHttpGetRequest(String path, Map params) {
		HttpGet request = new HttpGet(createUri(path, params));
		return request;
	}

	protected URI createUri(String path, Map params) {
		URIBuilder builder = new URIBuilder();
		builder.setScheme(this.getScheme()).setHost(this.host).setPort(this.port).setPath(path);
		if (params != null) {
			params.entrySet().forEach(p -> {
				builder.addParameter(p.getKey(), p.getValue().toString());	
			});
		}

		try {
			return builder.build();
		} catch (URISyntaxException e) {
			System.out.println("encountered error in creating URI: " + e.getMessage());
			return null;
		}
	}

	protected Integer TryParseJsonNode(JsonNode node, Integer defaultValue) {
		return (node == null) ? defaultValue : node.asInt(defaultValue);
	}

	protected Double TryParseJsonNode(JsonNode node, Double defaultValue) {
		return (node == null) ? defaultValue : node.asDouble(defaultValue);
	}

	protected String TryParseJsonNode(JsonNode node, String defaultValue) {
		return (node == null) ? defaultValue : node.asText(defaultValue);
	}

	protected Boolean TryParseJsonNode(JsonNode node, Boolean defaultValue) {
		return (node == null) ? defaultValue : node.asBoolean(defaultValue);
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public String getScheme() {
		return scheme;
	}

	public void setScheme(String scheme) {
		this.scheme = scheme;
	}

	public URI getConnectionString() {
		URIBuilder builder = new URIBuilder();
		builder.setScheme(this.getScheme()).setHost(this.host).setPort(this.port);
		try {
			return builder.build();
		} catch (URISyntaxException e) {
			return null;
		}
	}

	protected JsonNode responseToJson(HttpResponse response) {
		// do something with the entity to get the token
		JsonNode responseNode = null;
		try {
			String responseString = null;
			if (response instanceof CloseableHttpResponse) {
				responseString = new BasicResponseHandler().handleResponse(response);
			} else {
				responseString = EntityUtils.toString(response.getEntity());
			}
			responseNode = mapper.readTree(responseString);
		} catch (ParseException | IOException e) {
			e.printStackTrace();
		}

		return responseNode;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy