|
30 | 30 | import software.amazon.awssdk.annotations.SdkInternalApi;
|
31 | 31 | import software.amazon.awssdk.http.apache5.ProxyConfiguration;
|
32 | 32 | import software.amazon.awssdk.utils.Logger;
|
| 33 | +import software.amazon.awssdk.utils.ReflectionMethodInvoker; |
33 | 34 |
|
34 | 35 | @SdkInternalApi
|
35 | 36 | public final class Apache5Utils {
|
36 | 37 | private static final Logger logger = Logger.loggerFor(Apache5Utils.class);
|
| 38 | + private static final ReflectionMethodInvoker<RequestConfig.Builder, RequestConfig.Builder> NORMALIZE_URI_INVOKER; |
| 39 | + |
| 40 | + static { |
| 41 | + // Attempt to initialize the invoker once on class-load. If it fails, it will not be attempted again, but we'll |
| 42 | + // use that opportunity to log a warning. |
| 43 | + NORMALIZE_URI_INVOKER = |
| 44 | + new ReflectionMethodInvoker<>(RequestConfig.Builder.class, |
| 45 | + RequestConfig.Builder.class, |
| 46 | + "setNormalizeUri", |
| 47 | + boolean.class); |
| 48 | + |
| 49 | + try { |
| 50 | + NORMALIZE_URI_INVOKER.initialize(); |
| 51 | + } catch (NoSuchMethodException ignored) { |
| 52 | + noSuchMethodThrownByNormalizeUriInvoker(); |
| 53 | + } |
| 54 | + } |
37 | 55 |
|
38 | 56 | private Apache5Utils() {
|
39 | 57 | }
|
@@ -61,11 +79,36 @@ public static HttpClientContext newClientContext(ProxyConfiguration proxyConfigu
|
61 | 79 | addPreemptiveAuthenticationProxy(clientContext, proxyConfiguration);
|
62 | 80 |
|
63 | 81 | RequestConfig.Builder builder = RequestConfig.custom();
|
| 82 | + disableNormalizeUri(builder); |
| 83 | + |
64 | 84 | clientContext.setRequestConfig(builder.build());
|
65 | 85 | return clientContext;
|
66 | 86 |
|
67 | 87 | }
|
68 | 88 |
|
| 89 | + /** |
| 90 | + * From Apache v4.5.8, normalization should be disabled or AWS requests with special characters in URI path will fail |
| 91 | + * with Signature Errors. |
| 92 | + * <p> |
| 93 | + * setNormalizeUri is added only in 4.5.8, so customers using the latest version of SDK with old versions (4.5.6 or less) |
| 94 | + * of Apache httpclient will see NoSuchMethodError. Hence this method will suppress the error. |
| 95 | + * |
| 96 | + * Do not use Apache version 4.5.7 as it breaks URI paths with special characters and there is no option |
| 97 | + * to disable normalization. |
| 98 | + * </p> |
| 99 | + * |
| 100 | + * For more information, See https://github.com/aws/aws-sdk-java/issues/1919 |
| 101 | + */ |
| 102 | + public static void disableNormalizeUri(RequestConfig.Builder requestConfigBuilder) { |
| 103 | + // For efficiency, do not attempt to call the invoker again if it failed to initialize on class-load |
| 104 | + if (NORMALIZE_URI_INVOKER.isInitialized()) { |
| 105 | + try { |
| 106 | + NORMALIZE_URI_INVOKER.invoke(requestConfigBuilder, false); |
| 107 | + } catch (NoSuchMethodException ignored) { |
| 108 | + noSuchMethodThrownByNormalizeUriInvoker(); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
69 | 112 |
|
70 | 113 | /**
|
71 | 114 | * Returns a new Credentials Provider for use with proxy authentication.
|
@@ -111,4 +154,13 @@ private static void addPreemptiveAuthenticationProxy(HttpClientContext clientCon
|
111 | 154 | }
|
112 | 155 | }
|
113 | 156 |
|
| 157 | + // Just log and then swallow the exception |
| 158 | + private static void noSuchMethodThrownByNormalizeUriInvoker() { |
| 159 | + // setNormalizeUri method was added in httpclient 4.5.8 |
| 160 | + logger.warn(() -> "NoSuchMethodException was thrown when disabling normalizeUri. This indicates you are using " |
| 161 | + + "an old version (< 4.5.8) of Apache http client. It is recommended to use http client " |
| 162 | + + "version >= 4.5.9 to avoid the breaking change introduced in apache client 4.5.7 and " |
| 163 | + + "the latency in exception handling. See https://github.com/aws/aws-sdk-java/issues/1919" |
| 164 | + + " for more information"); |
| 165 | + } |
114 | 166 | }
|
0 commit comments