Skip to content

Commit e1c39e4

Browse files
nirb-jfrogyahavi
authored andcommitted
Support adding HTTP request interceptors to the underlying HTTP client (#248)
1 parent e7443f4 commit e1c39e4

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

httpClient/src/main/java/org/jfrog/artifactory/client/httpClient/http/HttpBuilderBase.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.apache.commons.lang.StringUtils;
44
import org.apache.http.*;
5-
import org.apache.http.auth.*;
5+
import org.apache.http.auth.AuthScope;
6+
import org.apache.http.auth.Credentials;
7+
import org.apache.http.auth.UsernamePasswordCredentials;
68
import org.apache.http.client.config.AuthSchemes;
79
import org.apache.http.client.config.RequestConfig;
810
import org.apache.http.config.Registry;
@@ -15,7 +17,10 @@
1517
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
1618
import org.apache.http.conn.ssl.NoopHostnameVerifier;
1719
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
18-
import org.apache.http.impl.client.*;
20+
import org.apache.http.impl.client.BasicCredentialsProvider;
21+
import org.apache.http.impl.client.CloseableHttpClient;
22+
import org.apache.http.impl.client.HttpClientBuilder;
23+
import org.apache.http.impl.client.HttpClients;
1924
import org.apache.http.impl.conn.DefaultRoutePlanner;
2025
import org.apache.http.impl.conn.DefaultSchemePortResolver;
2126
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
@@ -27,7 +32,8 @@
2732
import org.jfrog.artifactory.client.httpClient.http.auth.ProxyPreemptiveAuthInterceptor;
2833

2934
import javax.net.ssl.SSLContext;
30-
import java.net.*;
35+
import java.net.MalformedURLException;
36+
import java.net.URL;
3137
import java.util.Arrays;
3238
import java.util.List;
3339
import java.util.concurrent.TimeUnit;
@@ -118,6 +124,11 @@ public T socketTimeout(int soTimeout) {
118124
return self();
119125
}
120126

127+
public T addInterceptorLast(HttpRequestInterceptor httpRequestInterceptor) {
128+
builder.addInterceptorLast(httpRequestInterceptor);
129+
return self();
130+
}
131+
121132
/**
122133
* How long to keep connections alive for reuse purposes before ditching them
123134
*

services/src/main/groovy/org/jfrog/artifactory/client/ArtifactoryClientBuilder.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.jfrog.artifactory.client;
22

33
import org.apache.commons.lang.StringUtils;
4+
import org.apache.http.HttpRequestInterceptor;
45
import org.apache.http.client.utils.URIBuilder;
56
import org.apache.http.impl.client.CloseableHttpClient;
67
import org.apache.http.ssl.SSLContextBuilder;
7-
88
import org.jfrog.artifactory.client.httpClient.http.HttpBuilderBase;
99
import org.jfrog.artifactory.client.impl.ArtifactoryImpl;
1010
import org.jfrog.artifactory.client.impl.util.ArtifactoryHttpClient;
@@ -14,6 +14,8 @@
1414
import java.net.URI;
1515
import java.net.URISyntaxException;
1616
import java.net.URL;
17+
import java.util.ArrayList;
18+
import java.util.List;
1719
import java.util.Properties;
1820

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

3740
protected ArtifactoryClientBuilder() {
3841
super();
@@ -95,6 +98,19 @@ public ArtifactoryClientBuilder setAccessToken(String accessToken) {
9598
return this;
9699
}
97100

101+
/**
102+
* Add an Http request interceptor to the underlying Http client builder used by the artifactory client
103+
* <br>
104+
* For further details see
105+
* {@link org.apache.http.impl.client.HttpClientBuilder#addInterceptorLast(org.apache.http.HttpRequestInterceptor)}
106+
* @param httpRequestInterceptor request interceptor that allows manipulating and examining of outgoing requests
107+
* @return ArtifactoryClientBuilder
108+
*/
109+
public ArtifactoryClientBuilder addInterceptorLast(HttpRequestInterceptor httpRequestInterceptor) {
110+
this.requestInterceptorList.add(httpRequestInterceptor);
111+
return this;
112+
}
113+
98114
private CloseableHttpClient createClientBuilder(URI uri) {
99115
ArtifactoryHttpClient artifactoryHttpClient = new ArtifactoryHttpClient();
100116
artifactoryHttpClient.hostFromUrl(uri.toString());
@@ -126,6 +142,9 @@ private CloseableHttpClient createClientBuilder(URI uri) {
126142
else {
127143
artifactoryHttpClient.trustSelfSignCert(!ignoreSSLIssues);
128144
}
145+
for (HttpRequestInterceptor httpRequestInterceptor : requestInterceptorList) {
146+
artifactoryHttpClient.addInterceptorLast(httpRequestInterceptor);
147+
}
129148
return artifactoryHttpClient.build();
130149
}
131150

services/src/test/java/org/jfrog/artifactory/client/ArtifactoryTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.jfrog.artifactory.client;
22

3+
import org.jfrog.artifactory.client.impl.ArtifactoryRequestImpl;
34
import org.testng.annotations.Test;
45

56
import java.io.IOException;
7+
import java.util.concurrent.atomic.AtomicInteger;
68

79
import static junit.framework.Assert.assertEquals;
810

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

8284
artifactory = ArtifactoryClientBuilder.create()
83-
.setUrl("http://abc.com:80/ab/artifactory/webapp/webapp").build();
85+
.setUrl("http://abc.com:80/ab/artifactory/webapp/webapp").build();
8486
assertEquals("http://abc.com:80", artifactory.getUri());
8587
assertEquals("ab/artifactory/webapp/webapp", artifactory.getContextName());
8688

@@ -124,4 +126,32 @@ public void socketTimeoutBuilderTest() {
124126
assertEquals(builder.getSocketTimeout(), new Integer(100));
125127
builder.build();
126128
}
129+
130+
@Test
131+
public void addInterceptorTest() {
132+
AtomicInteger interceptor1Visits = new AtomicInteger(0);
133+
AtomicInteger interceptor2Visits = new AtomicInteger(0);
134+
ArtifactoryClientBuilder builder = ArtifactoryClientBuilder.create();
135+
builder.setUrl("http://localhost:7/");
136+
builder.addInterceptorLast((request, httpContext) -> {
137+
interceptor1Visits.incrementAndGet();
138+
});
139+
builder.addInterceptorLast((request, httpContext) -> {
140+
// Verify interceptor1 was called before interceptor2
141+
assertEquals(interceptor1Visits.intValue(), 1);
142+
interceptor2Visits.incrementAndGet();
143+
});
144+
145+
ArtifactoryRequest req = new ArtifactoryRequestImpl()
146+
.method(ArtifactoryRequest.Method.GET)
147+
.apiUrl("api/security/permissions")
148+
.responseType(ArtifactoryRequest.ContentType.JSON);
149+
150+
try {
151+
builder.build().restCall(req);
152+
} catch (IOException ignore) {
153+
}
154+
assertEquals(interceptor1Visits.intValue(), 1);
155+
assertEquals(interceptor2Visits.intValue(), 1);
156+
}
127157
}

0 commit comments

Comments
 (0)