Skip to content

Commit

Permalink
Improve native support for org.apache.http.impl.client.BasicAuthCache
Browse files Browse the repository at this point in the history
Fixes #3079
  • Loading branch information
jamesnetherton authored and ppalaga committed Jan 12, 2022
1 parent 266460d commit 425fdfa
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
5 changes: 5 additions & 0 deletions extensions-support/httpclient/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>svm</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.couchdb;
package org.apache.camel.quarkus.support.httpclient.graalvm;

import java.util.Map;

Expand Down
5 changes: 0 additions & 5 deletions extensions/couchdb/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-couchdb</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>svm</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
import javax.inject.Named;

import org.apache.camel.component.netty.ClientInitializerFactory;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
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.protocol.HttpContext;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.DefaultAsyncHttpClient;
Expand Down Expand Up @@ -58,4 +67,23 @@ public AsyncHttpClient asyncHttpClientWithProxy() {

return new DefaultAsyncHttpClient(config);
}

@Named
HttpContext basicAuthContext() {
Integer port = ConfigProvider.getConfig().getValue("quarkus.http.test-port", Integer.class);

UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(USER_ADMIN, USER_ADMIN_PASSWORD);
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY, credentials);

BasicAuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(new HttpHost("localhost", port), basicAuth);

HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
context.setCredentialsProvider(provider);

return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ public Response httpBasicAuth(
return Response.status(status).entity(body).build();
}

@Path("/http/auth/basic/cache")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response httpBasicAuthCache(@QueryParam("test-port") int port) {

Exchange result = producerTemplate
.withHeader(Exchange.HTTP_QUERY, "component=http")
.toF("http://localhost:%d/test/client/auth/basic"
+ "?throwExceptionOnFailure=false"
+ "&httpContext=#basicAuthContext", port)
.send();

Integer status = result.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
String body = result.getMessage().getBody(String.class);
return Response.status(status).entity(body).build();
}

@Path("/http/proxy")
@GET
@Produces(MediaType.APPLICATION_XML)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ public void basicAuth(String component) {
.body(is("Component " + component + " is using basic auth"));
}

@Test
public void basicAuthCache() {
RestAssured
.given()
.queryParam("test-port", RestAssured.port)
.when()
.get("/test/client/http/auth/basic/cache")
.then()
.statusCode(200)
.body(is("Component http is using basic auth"));
}

@ParameterizedTest
@MethodSource("getHttpComponentNames")
public void proxyServer(String component) {
Expand Down

0 comments on commit 425fdfa

Please sign in to comment.