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

com.newrelic.insights.publish.InsightsClient Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
package com.newrelic.insights.publish;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public abstract class InsightsClient {

	private static final Logger logger = LoggerFactory.getLogger(InsightsClient.class);
	protected static final String PROPERTY_VALUE_HEADER_CONTENT_TYPE = "application/json";
	protected static final String PROPERTY_NAME_HEADER_X_INSERT_KEY = "X-Insert-Key";
	protected CloseableHttpClient httpclient = null;
	protected String insightsAPIUrl = null;
	protected String insightsAPIInsertKey = null; 

	// This should be threadsafe but I will doublecheck later
    ObjectMapper mapper = new ObjectMapper();
	
	public InsightsClient(String insightsAPIUrl, String insightsAPIInsertKey) {
		this.insightsAPIUrl = insightsAPIUrl;
		this.insightsAPIInsertKey = insightsAPIInsertKey;
	}
	
	public abstract void init(ClientConnectionConfiguration httpConfig);
	
	public void init() {
		this.init(new ClientConnectionConfiguration());
	}
	
	public boolean post(String insightsEventType, Map stringProperties) {
		return post(insightsEventType, stringProperties, null);
	}
	
	public boolean post(String insightsEventType, Map stringProperties, Map numericProperties) {
		ArrayList eventList = new ArrayList();
		Event thisEvent = new Event(insightsEventType);
						
		for (String key : stringProperties.keySet()) {
			thisEvent.put(key, stringProperties.get(key));
		}
			
		if (numericProperties != null) {
			for (String key : numericProperties.keySet()) {
				thisEvent.put(key, numericProperties.get(key));
			}	
		}
		return post((Event[]) eventList.toArray());
	}
	
	public boolean post(Event[] events) {
		boolean result = false;
		HttpPost postrequest = new HttpPost(insightsAPIUrl );
		try {
			ArrayNode arrayNode = mapper.createArrayNode();
	        	for (int i = 0; i < events.length; i++) {
	        		ObjectNode objectNode = events[i].getObjectNode();
	        		arrayNode.add(objectNode);
			}
			String jsonRequest = mapper.writeValueAsString(arrayNode); 
			logger.debug("JSON being sent is " + jsonRequest);
			postrequest.setEntity(new StringEntity(jsonRequest));
			CloseableHttpResponse response = httpclient.execute(postrequest);
			int status = response.getStatusLine().getStatusCode();
			HttpEntity entity = response.getEntity();
			logger.debug("Response is " + response.getStatusLine());
			if (status >= 200 && status < 300) {
				try {
					if (entity != null) {
						InputStream input = entity.getContent();
						BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));				
						InsightsResponse insightsStatus = mapper.readValue(bufferedReader, InsightsResponse.class);
						if (insightsStatus.isSuccess()) {
							result = true;
						} 
					}
				} finally {
					EntityUtils.consumeQuietly(entity);
					response.close();
				}
			} else {
				String responseBody = EntityUtils.toString(response.getEntity());
				logger.error("Response error is " + responseBody);
				EntityUtils.consumeQuietly(entity);
				response.close();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return result;
	}
	
	public void destroyClient() {
		try {
			httpclient.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy