Skip to content

Commit

Permalink
GH-2933: Fix deprecation in the RestTemplateNodeLocator
Browse files Browse the repository at this point in the history
Fixes: #2933

* Remove bogus `RestTemplateHolder` class which is not `public`
and exposed accidentally by the public `RestTemplateNodeLocator`
* Rework `RestTemplateNodeLocator` logic to expose `RestTemplate` directly.
* Populated `AuthCache` for `HttpHost` based on the `baseUri` on-demand
  • Loading branch information
artembilan committed Dec 19, 2024
1 parent d139c98 commit 7ea51de
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 72 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,18 +17,17 @@
package org.springframework.amqp.rabbit.connection;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.hc.client5.http.auth.AuthCache;
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.protocol.BasicHttpContext;
import org.apache.hc.core5.http.protocol.HttpContext;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -42,44 +41,43 @@
* A {@link NodeLocator} using the {@link RestTemplate}.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 3.0
*
*/
public class RestTemplateNodeLocator implements NodeLocator<RestTemplateHolder> {
public class RestTemplateNodeLocator implements NodeLocator<RestTemplate> {

private final AuthCache authCache = new BasicAuthCache();

private final AtomicBoolean authSchemeIsSetToCache = new AtomicBoolean(false);

@Override
public RestTemplateHolder createClient(String userName, String password) {
return new RestTemplateHolder(userName, password);
public RestTemplate createClient(String userName, String password) {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpContextFactory((httpMethod, uri) -> {
HttpClientContext context = HttpClientContext.create();
context.setAuthCache(this.authCache);
return context;
});
RestTemplate template = new RestTemplate(requestFactory);
template.getInterceptors().add(new BasicAuthenticationInterceptor(userName, password));
return template;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
@Nullable
public Map<String, Object> restCall(RestTemplateHolder client, String baseUri, String vhost, String queue)
throws URISyntaxException {

if (client.template == null) {
URI uri = new URI(baseUri);
HttpHost host = new HttpHost(uri.getHost(), uri.getPort());
client.template = new RestTemplate(new HttpComponentsClientHttpRequestFactory() {

@Override
@Nullable
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
AuthCache cache = new BasicAuthCache();
BasicScheme scheme = new BasicScheme();
cache.put(host, scheme);
BasicHttpContext context = new BasicHttpContext();
context.setAttribute(HttpClientContext.AUTH_CACHE, cache);
return context;
}

});
client.template.getInterceptors().add(new BasicAuthenticationInterceptor(client.userName, client.password));
public Map<String, Object> restCall(RestTemplate client, String baseUri, String vhost, String queue) {
URI theBaseUri = URI.create(baseUri);
if (!this.authSchemeIsSetToCache.getAndSet(true)) {
this.authCache.put(HttpHost.create(theBaseUri), new BasicScheme());
}
URI uri = new URI(baseUri)
URI uri = theBaseUri
.resolve("/api/queues/" + UriUtils.encodePathSegment(vhost, StandardCharsets.UTF_8) + "/" + queue);
ResponseEntity<Map> response = client.template.exchange(uri, HttpMethod.GET, null, Map.class);
ResponseEntity<Map<String, Object>> response =
client.exchange(uri, HttpMethod.GET, null, new ParameterizedTypeReference<>() {

});
return response.getStatusCode().equals(HttpStatus.OK) ? response.getBody() : null;
}

Expand Down

0 comments on commit 7ea51de

Please sign in to comment.