Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 5926495

Browse files
alessandro.gherardipavelbucek
authored andcommitted
Support request-specific CredentialsProvider
Change-Id: I222b4b797da8d8d5178900a6fc4b85b534022015
1 parent e9bc5ce commit 5926495

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnector.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,14 @@ public ClientResponse apply(final ClientRequest clientRequest) throws Processing
439439
authCache.put(getHost(request), basicScheme);
440440
context.setAuthCache(authCache);
441441
}
442+
443+
// If a request-specific CredentialsProvider exists, use it instead of the default one
444+
CredentialsProvider credentialsProvider =
445+
clientRequest.resolveProperty(ApacheClientProperties.CREDENTIALS_PROVIDER, CredentialsProvider.class);
446+
if (credentialsProvider != null) {
447+
context.setCredentialsProvider(credentialsProvider);
448+
}
449+
442450
response = client.execute(getHost(request), request, context);
443451
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());
444452

connectors/apache-connector/src/test/java/org/glassfish/jersey/apache/connector/AuthTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -248,6 +248,25 @@ public void testAuthGet() {
248248
assertEquals("GET", r.request().get(String.class));
249249
}
250250

251+
@Test
252+
public void testAuthGetWithRequestCredentialsProvider() {
253+
CredentialsProvider credentialsProvider = new org.apache.http.impl.client.BasicCredentialsProvider();
254+
credentialsProvider.setCredentials(
255+
AuthScope.ANY,
256+
new UsernamePasswordCredentials("name", "password")
257+
);
258+
259+
ClientConfig cc = new ClientConfig();
260+
cc.connectorProvider(new ApacheConnectorProvider());
261+
Client client = ClientBuilder.newClient(cc);
262+
WebTarget r = client.target(getBaseUri()).path("test");
263+
264+
assertEquals("GET",
265+
r.request()
266+
.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider)
267+
.get(String.class));
268+
}
269+
251270
@Test
252271
public void testAuthGetWithClientFilter() {
253272
ClientConfig cc = new ClientConfig();

0 commit comments

Comments
 (0)