|
1 | 1 | /* |
2 | | - * Copyright 2022 the original author or authors. |
| 2 | + * Copyright 2022-2024 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
17 | 17 | package org.springframework.amqp.rabbit.connection; |
18 | 18 |
|
19 | 19 | import java.net.URI; |
20 | | -import java.net.URISyntaxException; |
21 | 20 | import java.nio.charset.StandardCharsets; |
22 | 21 | import java.util.Map; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
23 | 23 |
|
24 | 24 | import org.apache.hc.client5.http.auth.AuthCache; |
25 | 25 | import org.apache.hc.client5.http.impl.auth.BasicAuthCache; |
26 | 26 | import org.apache.hc.client5.http.impl.auth.BasicScheme; |
27 | 27 | import org.apache.hc.client5.http.protocol.HttpClientContext; |
28 | 28 | 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; |
31 | 29 |
|
| 30 | +import org.springframework.core.ParameterizedTypeReference; |
32 | 31 | import org.springframework.http.HttpMethod; |
33 | 32 | import org.springframework.http.HttpStatus; |
34 | 33 | import org.springframework.http.ResponseEntity; |
|
42 | 41 | * A {@link NodeLocator} using the {@link RestTemplate}. |
43 | 42 | * |
44 | 43 | * @author Gary Russell |
| 44 | + * @author Artem Bilan |
| 45 | + * |
45 | 46 | * @since 3.0 |
46 | 47 | * |
47 | 48 | */ |
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); |
49 | 54 |
|
50 | 55 | @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; |
53 | 66 | } |
54 | 67 |
|
55 | | - @SuppressWarnings({ "unchecked", "rawtypes" }) |
56 | 68 | @Override |
57 | 69 | @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()); |
79 | 74 | } |
80 | | - URI uri = new URI(baseUri) |
| 75 | + URI uri = theBaseUri |
81 | 76 | .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 | + }); |
83 | 81 | return response.getStatusCode().equals(HttpStatus.OK) ? response.getBody() : null; |
84 | 82 | } |
85 | 83 |
|
|
0 commit comments