Skip to content

Commit

Permalink
Support adding HTTP request interceptors to the underlying HTTP client (
Browse files Browse the repository at this point in the history
  • Loading branch information
nirb-jfrog authored and yahavi committed Jun 30, 2019
1 parent e7443f4 commit e1c39e4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.apache.commons.lang.StringUtils;
import org.apache.http.*;
import org.apache.http.auth.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
Expand All @@ -15,7 +17,10 @@
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.*;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultRoutePlanner;
import org.apache.http.impl.conn.DefaultSchemePortResolver;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
Expand All @@ -27,7 +32,8 @@
import org.jfrog.artifactory.client.httpClient.http.auth.ProxyPreemptiveAuthInterceptor;

import javax.net.ssl.SSLContext;
import java.net.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -118,6 +124,11 @@ public T socketTimeout(int soTimeout) {
return self();
}

public T addInterceptorLast(HttpRequestInterceptor httpRequestInterceptor) {
builder.addInterceptorLast(httpRequestInterceptor);
return self();
}

/**
* How long to keep connections alive for reuse purposes before ditching them
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.jfrog.artifactory.client;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.ssl.SSLContextBuilder;

import org.jfrog.artifactory.client.httpClient.http.HttpBuilderBase;
import org.jfrog.artifactory.client.impl.ArtifactoryImpl;
import org.jfrog.artifactory.client.impl.util.ArtifactoryHttpClient;
Expand All @@ -14,6 +14,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
Expand All @@ -33,6 +35,7 @@ public class ArtifactoryClientBuilder {
private boolean ignoreSSLIssues;
private SSLContextBuilder sslContextBuilder;
private String accessToken;
private List<HttpRequestInterceptor> requestInterceptorList = new ArrayList<>();

protected ArtifactoryClientBuilder() {
super();
Expand Down Expand Up @@ -95,6 +98,19 @@ public ArtifactoryClientBuilder setAccessToken(String accessToken) {
return this;
}

/**
* Add an Http request interceptor to the underlying Http client builder used by the artifactory client
* <br>
* For further details see
* {@link org.apache.http.impl.client.HttpClientBuilder#addInterceptorLast(org.apache.http.HttpRequestInterceptor)}
* @param httpRequestInterceptor request interceptor that allows manipulating and examining of outgoing requests
* @return ArtifactoryClientBuilder
*/
public ArtifactoryClientBuilder addInterceptorLast(HttpRequestInterceptor httpRequestInterceptor) {
this.requestInterceptorList.add(httpRequestInterceptor);
return this;
}

private CloseableHttpClient createClientBuilder(URI uri) {
ArtifactoryHttpClient artifactoryHttpClient = new ArtifactoryHttpClient();
artifactoryHttpClient.hostFromUrl(uri.toString());
Expand Down Expand Up @@ -126,6 +142,9 @@ private CloseableHttpClient createClientBuilder(URI uri) {
else {
artifactoryHttpClient.trustSelfSignCert(!ignoreSSLIssues);
}
for (HttpRequestInterceptor httpRequestInterceptor : requestInterceptorList) {
artifactoryHttpClient.addInterceptorLast(httpRequestInterceptor);
}
return artifactoryHttpClient.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.jfrog.artifactory.client;

import org.jfrog.artifactory.client.impl.ArtifactoryRequestImpl;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import static junit.framework.Assert.assertEquals;

Expand Down Expand Up @@ -80,7 +82,7 @@ public void urlsBuilderTest() throws IOException {
assertEquals("", artifactory.getContextName());

artifactory = ArtifactoryClientBuilder.create()
.setUrl("http://abc.com:80/ab/artifactory/webapp/webapp").build();
.setUrl("http://abc.com:80/ab/artifactory/webapp/webapp").build();
assertEquals("http://abc.com:80", artifactory.getUri());
assertEquals("ab/artifactory/webapp/webapp", artifactory.getContextName());

Expand Down Expand Up @@ -124,4 +126,32 @@ public void socketTimeoutBuilderTest() {
assertEquals(builder.getSocketTimeout(), new Integer(100));
builder.build();
}

@Test
public void addInterceptorTest() {
AtomicInteger interceptor1Visits = new AtomicInteger(0);
AtomicInteger interceptor2Visits = new AtomicInteger(0);
ArtifactoryClientBuilder builder = ArtifactoryClientBuilder.create();
builder.setUrl("http://localhost:7/");
builder.addInterceptorLast((request, httpContext) -> {
interceptor1Visits.incrementAndGet();
});
builder.addInterceptorLast((request, httpContext) -> {
// Verify interceptor1 was called before interceptor2
assertEquals(interceptor1Visits.intValue(), 1);
interceptor2Visits.incrementAndGet();
});

ArtifactoryRequest req = new ArtifactoryRequestImpl()
.method(ArtifactoryRequest.Method.GET)
.apiUrl("api/security/permissions")
.responseType(ArtifactoryRequest.ContentType.JSON);

try {
builder.build().restCall(req);
} catch (IOException ignore) {
}
assertEquals(interceptor1Visits.intValue(), 1);
assertEquals(interceptor2Visits.intValue(), 1);
}
}

0 comments on commit e1c39e4

Please sign in to comment.