Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions source/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,12 @@
</scm>

<properties>
<httpcomponents-httpclient.version>4.5.14</httpcomponents-httpclient.version>

<sonar.projectKey>aerius_taskmanager</sonar.projectKey>

<aggregate.report.dir>taskmanager-report/target/site/jacoco-aggregate/jacoco.xml</aggregate.report.dir>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../${aggregate.report.dir}</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<dependencyManagement>
<dependencies>
<!-- Httpclient (should be replaced by httpclient5 at some point) -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpcomponents-httpclient.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<repositories>
<repository>
<id>aerius-nexus-public</id>
Expand Down
6 changes: 0 additions & 6 deletions source/taskmanager-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
<artifactId>amqp-client</artifactId>
</dependency>

<!-- Commons -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

<!-- Logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down
6 changes: 6 additions & 0 deletions source/taskmanager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>

<!-- Commons -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>

<!-- json -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,23 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -60,7 +55,7 @@ class RabbitMQQueueMonitor {

private static final Logger LOG = LoggerFactory.getLogger(RabbitMQQueueMonitor.class);

private static final int TIMEOUT = (int) TimeUnit.SECONDS.toMillis(3);
private static final Timeout TIMEOUT = Timeout.ofSeconds(3);

private final ObjectMapper objectMapper = new ObjectMapper();
private final ConnectionConfiguration configuration;
Expand All @@ -75,21 +70,17 @@ class RabbitMQQueueMonitor {
*/
public RabbitMQQueueMonitor(final ConnectionConfiguration configuration) {
this.configuration = configuration;
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(getDefaultRequestConfig()).build();
targetHost = new HttpHost(configuration.getBrokerHost(), configuration.getBrokerManagementPort(), "http");
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(configuration.getBrokerHost(), configuration.getBrokerManagementPort()),
new UsernamePasswordCredentials(configuration.getBrokerUsername(), configuration.getBrokerPassword()));
// Create AuthCache instance
final AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
httpClient = HttpClientBuilder.create()
.setConnectionManager(getDefaultConnectionManager())
.setDefaultRequestConfig(getDefaultRequestConfig())
.build();
targetHost = new HttpHost("http", configuration.getBrokerHost(), configuration.getBrokerManagementPort());
// Generate BASIC scheme object and pre-emptively set the credentials (saves a roundtrip to figure out it should do so)
final BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
basicAuth.initPreemptive(new UsernamePasswordCredentials(configuration.getBrokerUsername(), configuration.getBrokerPassword().toCharArray()));

context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
context.resetAuthExchange(targetHost, basicAuth);
}

/**
Expand Down Expand Up @@ -125,7 +116,7 @@ public Map<String, RabbitMQQueueStatus> getQueueStates() {
} else {
LOG.error("Queue configuration from RabbitMQ admin json returned an unexpected value.");
}
} catch (final URISyntaxException | IOException e) {
} catch (final IOException e) {
LOG.info("Error getting RabbitMQ status from admin api: {}", e.getMessage());
}
return queueStates;
Expand All @@ -147,27 +138,34 @@ private static int getJsonIntPrimitive(final JsonNode jsonObject, final String k
return jsonObject == null || !jsonObject.has(key) ? 0 : jsonObject.get(key).intValue();
}

protected JsonNode getJsonResultFromApi(final String apiPath) throws URISyntaxException, IOException {
// Not using new URI constructor because it will escape the %2f in apiPath to %252f.
final URI uri = new URL("http", configuration.getBrokerHost(), configuration.getBrokerManagementPort(), apiPath).toURI();
protected JsonNode getJsonResultFromApi(final String apiPath) throws IOException {
final URI uri = URI.create("http://" + configuration.getBrokerHost() + ":" + configuration.getBrokerManagementPort() + apiPath);

try (final CloseableHttpResponse response = httpClient.execute(targetHost, new HttpGet(uri), context)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return httpClient.execute(targetHost, new HttpGet(uri), context, response -> {
if (response.getCode() == HttpStatus.SC_OK) {
try (final InputStreamReader is = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)) {
return objectMapper.readTree(is);
}
} else {
throw new IOException(String.format("Status code wasn't 200 when retrieving json result. Status was: %d, %s",
response.getStatusLine().getStatusCode(), response.getStatusLine()));
response.getCode(), response.getReasonPhrase()));
}
}
});
}

private static HttpClientConnectionManager getDefaultConnectionManager() {
return PoolingHttpClientConnectionManagerBuilder.create()
.setDefaultConnectionConfig(ConnectionConfig.custom()
.setConnectTimeout(TIMEOUT)
.setSocketTimeout(TIMEOUT)
.build())
.build();
}

private static RequestConfig getDefaultRequestConfig() {
return RequestConfig.custom()
.setConnectTimeout(TIMEOUT)
.setConnectionRequestTimeout(TIMEOUT)
.setSocketTimeout(TIMEOUT)
.setResponseTimeout(TIMEOUT)
.build();
}
}
Loading