mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Initial Commit
This commit is contained in:
70
util/lantern-util-http/pom.xml
Normal file
70
util/lantern-util-http/pom.xml
Normal file
@@ -0,0 +1,70 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.lanternsoftware.util</groupId>
|
||||
<artifactId>lantern-util-http</artifactId>
|
||||
<name>lantern-util-http</name>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>lantern-util-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>4.4.8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>4.5.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
<phase>compile</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<optimize>true</optimize>
|
||||
<showDeprecation>true</showDeprecation>
|
||||
<encoding>UTF-8</encoding>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.lanternsoftware.util.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.client.config.CookieSpecs;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.conn.ConnectionKeepAliveStrategy;
|
||||
import org.apache.http.impl.client.BasicCookieStore;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
|
||||
public class HttpPool {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(HttpPool.class);
|
||||
|
||||
private final RequestConfig requestConfig;
|
||||
private final CookieStore cookieStore = new BasicCookieStore();
|
||||
private final ConnectionKeepAliveStrategy keepAliveStrategy;
|
||||
private final PoolingHttpClientConnectionManager connectionManager;
|
||||
|
||||
public HttpPool(int _maxTotalConnections, int _maxPerRoute) {
|
||||
this(_maxTotalConnections, _maxPerRoute, 10000, 5000, 10000);
|
||||
}
|
||||
|
||||
public HttpPool(int _maxTotalConnections, int _maxPerRoute, int _socketTimeoutMs, int _connectTimeoutMs, int _connectionRequestTimeoutMs) {
|
||||
requestConfig = RequestConfig.custom().setSocketTimeout(_socketTimeoutMs).setConnectTimeout(_connectTimeoutMs).setConnectionRequestTimeout(_connectionRequestTimeoutMs).setCookieSpec(CookieSpecs.STANDARD).build();
|
||||
keepAliveStrategy = (HttpResponse response, HttpContext context) -> 0;
|
||||
connectionManager = new PoolingHttpClientConnectionManager();
|
||||
connectionManager.setMaxTotal(_maxTotalConnections);
|
||||
connectionManager.setDefaultMaxPerRoute(_maxPerRoute);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
connectionManager.shutdown();
|
||||
}
|
||||
|
||||
public CloseableHttpResponse execute(HttpUriRequest _request) {
|
||||
try {
|
||||
return getClient().execute(_request);
|
||||
}
|
||||
catch (IOException _e) {
|
||||
LOG.error("Failed to make http request to " + _request.getURI().toString(), _e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public CloseableHttpResponse execute(HttpUriRequest _request, HttpContext _context) {
|
||||
try {
|
||||
return getClient().execute(_request, _context);
|
||||
}
|
||||
catch (IOException _e) {
|
||||
LOG.error("Failed to make http request to " + _request.getURI().toString(), _e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private CloseableHttpClient getClient() {
|
||||
return HttpClients.custom().setConnectionManager(connectionManager).setDefaultCookieStore(cookieStore).setDefaultRequestConfig(requestConfig).setKeepAliveStrategy(keepAliveStrategy).build();
|
||||
}
|
||||
|
||||
public String executeToString(HttpUriRequest _request) {
|
||||
return NullUtils.toString(executeToByteArray(_request));
|
||||
}
|
||||
|
||||
public byte[] executeToByteArray(HttpUriRequest _request) {
|
||||
InputStream is = null;
|
||||
CloseableHttpResponse resp = null;
|
||||
try {
|
||||
resp = execute(_request);
|
||||
if ((resp.getStatusLine().getStatusCode() < 200) || (resp.getStatusLine().getStatusCode() >= 300)) {
|
||||
LOG.error("Failed to make http request to " + _request.getURI().toString() + ". Status code: " + resp.getStatusLine().getStatusCode());
|
||||
return null;
|
||||
}
|
||||
HttpEntity entity = resp.getEntity();
|
||||
if (entity != null) {
|
||||
is = entity.getContent();
|
||||
return IOUtils.toByteArray(is);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch (IOException _e) {
|
||||
LOG.error("Failed to make http request to " + _request.getURI().toString(), _e);
|
||||
return null;
|
||||
}
|
||||
finally {
|
||||
IOUtils.closeQuietly(is);
|
||||
IOUtils.closeQuietly(resp);
|
||||
}
|
||||
}
|
||||
|
||||
public static void addBasicAuthHeader(HttpUriRequest _request, String _username, String _password) {
|
||||
_request.addHeader("Authorization", "Basic " + Base64.encodeBase64String(NullUtils.toByteArray(_username + ":" + _password)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user