io.logspace.jvm.agent.hq.HqClient Maven / Gradle / Ivy
The newest version!
/**
* Logspace
* Copyright (c) 2015 Indoqa Software Design und Beratung GmbH. All rights reserved.
* This program and the accompanying materials are made available under the terms of
* the Eclipse Public License Version 1.0, which accompanies this distribution and
* is available at http://www.eclipse.org/legal/epl-v10.html.
*/
package io.logspace.jvm.agent.hq;
import static io.logspace.jvm.agent.shaded.apache.http.entity.ContentType.APPLICATION_JSON;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import io.logspace.jvm.agent.shaded.apache.http.Header;
import io.logspace.jvm.agent.shaded.apache.http.client.config.RequestConfig;
import io.logspace.jvm.agent.shaded.apache.http.client.methods.HttpGet;
import io.logspace.jvm.agent.shaded.apache.http.client.methods.HttpPut;
import io.logspace.jvm.agent.shaded.apache.http.entity.StringEntity;
import io.logspace.jvm.agent.shaded.apache.http.impl.client.CloseableHttpClient;
import io.logspace.jvm.agent.shaded.apache.http.impl.client.HttpClients;
import io.logspace.jvm.agent.shaded.apache.http.message.BasicHeader;
import io.logspace.jvm.agent.shaded.slf4j.Logger;
import io.logspace.jvm.agent.shaded.slf4j.LoggerFactory;
import io.logspace.jvm.agent.api.AgentControllerInitializationException;
import io.logspace.jvm.agent.api.event.Event;
import io.logspace.jvm.agent.api.json.AgentControllerCapabilitiesJsonSerializer;
import io.logspace.jvm.agent.api.json.EventJsonSerializer;
import io.logspace.jvm.agent.api.order.AgentControllerCapabilities;
import io.logspace.jvm.agent.api.order.AgentControllerOrder;
public class HqClient {
private static final int TIMEOUT = 3000;
private CloseableHttpClient httpClient;
private String baseUrl;
private String agentControllerId;
private String spaceToken;
private final AgentControllerOrderResponseHandler agentControllerOrderResponseHandler = new AgentControllerOrderResponseHandler();
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public HqClient(String baseUrl, String agentControllerId, String spaceToken) {
super();
this.baseUrl = baseUrl;
if (this.baseUrl == null || this.baseUrl.trim().length() == 0) {
throw new AgentControllerInitializationException("The base URL must not be empty!");
}
this.agentControllerId = agentControllerId;
this.spaceToken = spaceToken;
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(TIMEOUT)
.setConnectTimeout(TIMEOUT)
.setSocketTimeout(TIMEOUT)
.build();
Set defaultHeaders = Collections.singleton(new BasicHeader("Accept", APPLICATION_JSON.getMimeType()));
this.httpClient = HttpClients.custom()
.disableAutomaticRetries()
.setDefaultRequestConfig(requestConfig)
.setDefaultHeaders(defaultHeaders)
.build();
}
private static StringEntity toJsonEntity(Collection event) throws IOException {
return new StringEntity(EventJsonSerializer.toJson(event), APPLICATION_JSON);
}
private static StringEntity toJSonEntity(AgentControllerCapabilities capabilities) throws IOException {
return new StringEntity(AgentControllerCapabilitiesJsonSerializer.toJson(capabilities), APPLICATION_JSON);
}
public void close() throws IOException {
this.logger.info("Closing now.");
this.httpClient.close();
}
public AgentControllerOrder downloadOrder() throws IOException {
HttpGet httpGet = new HttpGet(this.baseUrl + "/orders/" + this.agentControllerId);
httpGet.addHeader("If-Modified-Since", this.agentControllerOrderResponseHandler.getLastModified());
httpGet.addHeader("logspace.space-token", this.spaceToken);
return this.httpClient.execute(httpGet, this.agentControllerOrderResponseHandler);
}
public void uploadCapabilities(AgentControllerCapabilities capabilities) throws IOException {
HttpPut httpPut = new HttpPut(this.baseUrl + "/capabilities/" + this.agentControllerId);
httpPut.setEntity(toJSonEntity(capabilities));
httpPut.addHeader("logspace.space-token", this.spaceToken);
this.httpClient.execute(httpPut, new UploadCapabilitiesResponseHandler());
}
public void uploadEvents(Collection events) throws IOException {
String eventsUrl = this.baseUrl + "/events";
HttpPut httpPut = new HttpPut(eventsUrl);
httpPut.setEntity(toJsonEntity(events));
httpPut.addHeader("logspace.space-token", this.spaceToken);
this.logger.info("Uploading {} event(s) using space-token '{}' to {}", events.size(), this.spaceToken, eventsUrl);
this.httpClient.execute(httpPut, new UploadEventsResponseHandler());
}
}