Skip to content

Commit 7ea51de

Browse files
committed
GH-2933: Fix deprecation in the RestTemplateNodeLocator
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
1 parent d139c98 commit 7ea51de

File tree

2 files changed

+29
-72
lines changed

2 files changed

+29
-72
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/RestTemplateHolder.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/RestTemplateNodeLocator.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,18 +17,17 @@
1717
package org.springframework.amqp.rabbit.connection;
1818

1919
import java.net.URI;
20-
import java.net.URISyntaxException;
2120
import java.nio.charset.StandardCharsets;
2221
import java.util.Map;
22+
import java.util.concurrent.atomic.AtomicBoolean;
2323

2424
import org.apache.hc.client5.http.auth.AuthCache;
2525
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
2626
import org.apache.hc.client5.http.impl.auth.BasicScheme;
2727
import org.apache.hc.client5.http.protocol.HttpClientContext;
2828
import org.apache.hc.core5.http.HttpHost;
29-
import org.apache.hc.core5.http.protocol.BasicHttpContext;
30-
import org.apache.hc.core5.http.protocol.HttpContext;
3129

30+
import org.springframework.core.ParameterizedTypeReference;
3231
import org.springframework.http.HttpMethod;
3332
import org.springframework.http.HttpStatus;
3433
import org.springframework.http.ResponseEntity;
@@ -42,44 +41,43 @@
4241
* A {@link NodeLocator} using the {@link RestTemplate}.
4342
*
4443
* @author Gary Russell
44+
* @author Artem Bilan
45+
*
4546
* @since 3.0
4647
*
4748
*/
48-
public class RestTemplateNodeLocator implements NodeLocator<RestTemplateHolder> {
49+
public class RestTemplateNodeLocator implements NodeLocator<RestTemplate> {
50+
51+
private final AuthCache authCache = new BasicAuthCache();
52+
53+
private final AtomicBoolean authSchemeIsSetToCache = new AtomicBoolean(false);
4954

5055
@Override
51-
public RestTemplateHolder createClient(String userName, String password) {
52-
return new RestTemplateHolder(userName, password);
56+
public RestTemplate createClient(String userName, String password) {
57+
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
58+
requestFactory.setHttpContextFactory((httpMethod, uri) -> {
59+
HttpClientContext context = HttpClientContext.create();
60+
context.setAuthCache(this.authCache);
61+
return context;
62+
});
63+
RestTemplate template = new RestTemplate(requestFactory);
64+
template.getInterceptors().add(new BasicAuthenticationInterceptor(userName, password));
65+
return template;
5366
}
5467

55-
@SuppressWarnings({ "unchecked", "rawtypes" })
5668
@Override
5769
@Nullable
58-
public Map<String, Object> restCall(RestTemplateHolder client, String baseUri, String vhost, String queue)
59-
throws URISyntaxException {
60-
61-
if (client.template == null) {
62-
URI uri = new URI(baseUri);
63-
HttpHost host = new HttpHost(uri.getHost(), uri.getPort());
64-
client.template = new RestTemplate(new HttpComponentsClientHttpRequestFactory() {
65-
66-
@Override
67-
@Nullable
68-
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
69-
AuthCache cache = new BasicAuthCache();
70-
BasicScheme scheme = new BasicScheme();
71-
cache.put(host, scheme);
72-
BasicHttpContext context = new BasicHttpContext();
73-
context.setAttribute(HttpClientContext.AUTH_CACHE, cache);
74-
return context;
75-
}
76-
77-
});
78-
client.template.getInterceptors().add(new BasicAuthenticationInterceptor(client.userName, client.password));
70+
public Map<String, Object> restCall(RestTemplate client, String baseUri, String vhost, String queue) {
71+
URI theBaseUri = URI.create(baseUri);
72+
if (!this.authSchemeIsSetToCache.getAndSet(true)) {
73+
this.authCache.put(HttpHost.create(theBaseUri), new BasicScheme());
7974
}
80-
URI uri = new URI(baseUri)
75+
URI uri = theBaseUri
8176
.resolve("/api/queues/" + UriUtils.encodePathSegment(vhost, StandardCharsets.UTF_8) + "/" + queue);
82-
ResponseEntity<Map> response = client.template.exchange(uri, HttpMethod.GET, null, Map.class);
77+
ResponseEntity<Map<String, Object>> response =
78+
client.exchange(uri, HttpMethod.GET, null, new ParameterizedTypeReference<>() {
79+
80+
});
8381
return response.getStatusCode().equals(HttpStatus.OK) ? response.getBody() : null;
8482
}
8583

0 commit comments

Comments
 (0)