-
Notifications
You must be signed in to change notification settings - Fork 996
Expand file tree
/
Copy pathHttpSettingsToHttpClient.java
More file actions
514 lines (422 loc) · 22.8 KB
/
HttpSettingsToHttpClient.java
File metadata and controls
514 lines (422 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.v2migration;
import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.fullyQualified;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JContainer;
import org.openrewrite.java.tree.JRightPadded;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.marker.Markers;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
import software.amazon.awssdk.utils.CollectionUtils;
import software.amazon.awssdk.utils.Logger;
import software.amazon.awssdk.utils.Pair;
import software.amazon.awssdk.v2migration.internal.utils.IdentifierUtils;
import software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils;
/**
* This recipe moves the HTTP settings such as maxConnections configured on v1 ClientConfiguration to v2 ApacheHttpClient builder
* for sync SDK client or to v2 NettyNioAsyncHttpClient builder for async SDK client.
* <p>
* {@snippet :
* ClientConfiguration clientConfiguration = new ClientConfiguration()
* .withMaxConnections(100)
* .withTcpKeepAlive(true);
*
* AmazonSQS sqs = AmazonSQSClient.builder()
* .withClientConfiguration(clientConfiguration)
* .build();
*}
* <p>
* to v2:
* <p>
* {@snippet :
* ClientOverrideConfiguration clientConfiguration = ClientOverrideConfiguration.builder().build();
*
* SqsClient sqs = SqsClient.builder()
* .overrideConfiguration(clientConfiguration)
* .httpClientBuilder(ApacheHttpClient.builder()
* .maxConnections(100)
* .tcpKeepAlive(true))
* .build();
*}
*/
@SdkInternalApi
public class HttpSettingsToHttpClient extends Recipe {
private static final Logger log = Logger.loggerFor(HttpSettingsToHttpClient.class);
private static final String CONFIG_VAR_TO_HTTP_SETTINGS_KEY = "software.amazon.awssdk.migration.variableReference";
private static final String CONFIG_METHOD_TO_HTTP_SETTINGS_KEY = "software.amazon.awssdk.migration.configReference";
private static final String OVERRIDE_CONFIG_BUILDER_HTTP_SETTINGS_CURSOR = "clientOverrideConfigBuilderHttpSettingsCursor";
private static final Set<String> HTTP_SETTING_METHOD_NAMES = new HashSet<>(Arrays.asList("connectionTimeout",
"connectionTimeToLive",
"connectionMaxIdleTime",
"tcpKeepAlive",
"connectionTtl",
"socketTimeout",
"maxConnections"));
@Override
public String getDisplayName() {
return "Move HTTP settings from the ClientOverrideConfiguration to ApacheHttpClient for sync and "
+ "NettyNioAsyncHttpClient for async";
}
@Override
public String getDescription() {
return "Move HTTP settings from the ClientOverrideConfiguration to ApacheHttpClient for sync SDK client and "
+ "NettyNioAsyncHttpClient for async SDK client.";
}
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MoveHttpSettingsVisitor();
}
private static final class MoveHttpSettingsVisitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable,
ExecutionContext executionContext) {
variable = super.visitVariable(variable, executionContext);
JavaType type = variable.getType();
if (!isClientOverrideConfigurationType(type)) {
return variable;
}
Expression initializer = variable.getInitializer();
if (initializer instanceof J.MethodInvocation) {
saveHttpSettingsInExecutionContextIfNeeded(executionContext, CONFIG_VAR_TO_HTTP_SETTINGS_KEY,
variable.getSimpleName());
}
return variable;
}
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
method = super.visitMethodDeclaration(method, executionContext);
JavaType.Method methodType = method.getMethodType();
if (methodType == null) {
return method;
}
if (!returnClientOverrideConfiguration(methodType)) {
return method;
}
saveHttpSettingsInExecutionContextIfNeeded(executionContext, CONFIG_METHOD_TO_HTTP_SETTINGS_KEY,
method.getSimpleName());
return method;
}
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation originalMethod, ExecutionContext executionContext) {
J.MethodInvocation method = super.visitMethodInvocation(originalMethod, executionContext);
if (isClientOverrideConfigurationBuilder(method)) {
return handleClientOverrideConfiguration(originalMethod, method, executionContext);
}
if (isSdkClientBuilder(method)) {
return configureHttpClientBuilder(originalMethod, method, executionContext);
}
return method;
}
private void saveHttpSettingsInExecutionContextIfNeeded(ExecutionContext executionContext,
String configVarToHttpSettingsKey,
String variable) {
Map<String, Expression> httpSettings = getCursor()
.getMessage(OVERRIDE_CONFIG_BUILDER_HTTP_SETTINGS_CURSOR, new HashMap<>());
Map<String, Map<String, Expression>> variableToHttpSettings =
executionContext.getMessage(configVarToHttpSettingsKey, new HashMap<>());
variableToHttpSettings.put(variable, httpSettings);
executionContext.putMessage(configVarToHttpSettingsKey,
variableToHttpSettings);
}
private static boolean isClientOverrideConfigurationType(JavaType type) {
return Optional.ofNullable(TypeUtils.asFullyQualified(type))
.filter(t -> t.isAssignableTo(ClientOverrideConfiguration.class.getCanonicalName()))
.isPresent();
}
private static boolean isSdkClientBuilder(J.MethodInvocation method) {
return Optional.ofNullable(method.getMethodType()).map(mt -> mt.getDeclaringType())
.filter(t -> SdkTypeUtils.isV2ClientClass(t))
.isPresent();
}
private static boolean returnClientOverrideConfiguration(JavaType.Method methodType) {
if (methodType == null) {
return false;
}
return isClientOverrideConfigurationType(methodType.getReturnType());
}
private static boolean isClientOverrideConfigurationBuilder(J.MethodInvocation method) {
return Optional.ofNullable(method.getMethodType()).map(mt -> mt.getDeclaringType())
.filter(t -> t.isAssignableTo(ClientOverrideConfiguration.Builder.class.getCanonicalName()))
.isPresent();
}
private J.MethodInvocation configureHttpClientBuilder(
J.MethodInvocation originalMethod, J.MethodInvocation method, ExecutionContext executionContext) {
JavaType.Method methodType = method.getMethodType();
if (methodType == null) {
return method;
}
if (method.getSimpleName().equals("overrideConfiguration")) {
return addHttpClientBuilderIfNeeded(Pair.of(originalMethod, method), executionContext);
}
return method;
}
/**
* This method adds ".httpClientBuilder(ApacheHttpClient.builder().xxx)" right
* before ".overrideConfiguration(xxx)" if there are HTTP settings stored in execution context
*/
private J.MethodInvocation addHttpClientBuilderIfNeeded(
Pair<J.MethodInvocation, J.MethodInvocation> methods, ExecutionContext executionContext) {
J.MethodInvocation method = methods.right();
Expression expression = method.getArguments().get(0);
// If a variable is passed to overrideConfiguration, i.e., clientBuilder.overrideConfiguration(config)
if (expression instanceof J.Identifier) {
J.Identifier id = (J.Identifier) expression;
Map<String, Map<String, Expression>> configToHttpSettings =
executionContext.getMessage(CONFIG_VAR_TO_HTTP_SETTINGS_KEY);
return addHttpClientBuilderIfNeeded(methods, configToHttpSettings, id.getSimpleName(), executionContext);
}
// If a method invocation is passed to overrideConfiguration, i.e., clientBuilder.overrideConfiguration(getConfig())
if (expression instanceof J.MethodInvocation) {
J.MethodInvocation methodInvocation = (J.MethodInvocation) expression;
String methodInvocationName = methodInvocation.getName().getSimpleName();
Map<String, Map<String, Expression>> configToHttpSettings =
executionContext.getMessage(CONFIG_METHOD_TO_HTTP_SETTINGS_KEY);
return addHttpClientBuilderIfNeeded(methods, configToHttpSettings, methodInvocationName, executionContext);
}
return method;
}
private J.MethodInvocation addHttpClientBuilderIfNeeded(
Pair<J.MethodInvocation, J.MethodInvocation> methods,
Map<String, Map<String, Expression>> configToHttpSettings,
String configKey,
ExecutionContext executionContext) {
J.MethodInvocation method = methods.right();
if (CollectionUtils.isNullOrEmpty(configToHttpSettings) ||
!configToHttpSettings.containsKey(configKey)) {
return method;
}
Map<String, Expression> httpSettings = configToHttpSettings.get(configKey);
JavaType.FullyQualified classType =
Optional.ofNullable(method.getMethodType())
.map(JavaType.Method::getDeclaringType)
.orElse(null);
if (classType == null) {
return method;
}
JavaType.FullyQualified builderType = SdkTypeUtils.v2Builder(classType);
Expression expressionBeforeOverrideConfiguration = method.getSelect();
if (!(expressionBeforeOverrideConfiguration instanceof J.MethodInvocation ||
expressionBeforeOverrideConfiguration instanceof J.Identifier)) {
return method;
}
if (expressionBeforeOverrideConfiguration instanceof J.MethodInvocation) {
J.MethodInvocation selectInvoke = (J.MethodInvocation) expressionBeforeOverrideConfiguration;
// If we've already added httpClientBuilder, skip
if (selectInvoke.getSimpleName().equals("httpClientBuilder")) {
return method;
}
}
Space space = (expressionBeforeOverrideConfiguration instanceof J.MethodInvocation) ? Space.format("\n") :
expressionBeforeOverrideConfiguration.getPrefix();
Pair<Class, Class> httpClientClassNamePair = httpClientClassNamePair(classType);
List<JavaType> parametersTypes = new ArrayList<>();
parametersTypes.add(JavaType.buildType(httpClientClassNamePair.right().getCanonicalName()));
JavaType.Method httpClientBuilderMethod = new JavaType.Method(
null,
0L,
builderType,
"httpClientBuilder",
builderType,
Collections.emptyList(),
parametersTypes,
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);
J.Identifier httpClientBuilderName =
IdentifierUtils.makeId("httpClientBuilder", httpClientBuilderMethod);
J.MethodInvocation httpClientBuilderMethodInvoke = new J.MethodInvocation(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
new JRightPadded(expressionBeforeOverrideConfiguration, space, Markers.EMPTY),
null,
httpClientBuilderName,
httpClientBuilderInvoke(httpSettings, httpClientClassNamePair),
httpClientBuilderMethod
);
method = method.withSelect(httpClientBuilderMethodInvoke);
maybeAddImport(httpClientClassNamePair.left().getCanonicalName());
return maybeAutoFormat(methods.left(), method, executionContext);
}
private Pair<Class, Class> httpClientClassNamePair(JavaType.FullyQualified classType) {
Pair<Class, Class> httpClientClassNamePair;
if (!classType.getClassName().contains("Async")) {
httpClientClassNamePair = Pair.of(ApacheHttpClient.class,
ApacheHttpClient.Builder.class);
} else {
httpClientClassNamePair = Pair.of(NettyNioAsyncHttpClient.class,
NettyNioAsyncHttpClient.Builder.class);
}
return httpClientClassNamePair;
}
private JContainer<Expression> httpClientBuilderInvoke(Map<String, Expression> httpSettings,
Pair<Class, Class> httpClientClassNamePair) {
Class httpClientClassName = httpClientClassNamePair.left();
Class httpClientBuilderClassName = httpClientClassNamePair.right();
JavaType.FullyQualified httpClientType = fullyQualified(httpClientClassName.getCanonicalName());
JavaType.FullyQualified httpClientBuilderType = fullyQualified(httpClientBuilderClassName.getCanonicalName());
JavaType.Method httpClientBuilderMethodType = new JavaType.Method(
null,
0L,
httpClientType,
"builder",
httpClientBuilderType,
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);
J.Identifier httpClient =
IdentifierUtils.makeId(httpClientClassName.getSimpleName(), httpClientType);
J.Identifier httpClientBuilderName =
IdentifierUtils.makeId("builder", httpClientBuilderMethodType);
J.MethodInvocation httpClientBuilderMethodInvoke = new J.MethodInvocation(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
JRightPadded.build(httpClient),
null,
httpClientBuilderName,
JContainer.empty(),
httpClientBuilderMethodType
);
for (Map.Entry<String, Expression> entry : httpSettings.entrySet()) {
httpClientBuilderMethodInvoke = invokeHttpSetting(entry, httpClientBuilderType, httpClientBuilderMethodInvoke);
}
return JContainer.build(Arrays.asList(JRightPadded.build(httpClientBuilderMethodInvoke)));
}
private static J.MethodInvocation invokeHttpSetting(Map.Entry<String, Expression> entry,
JavaType.FullyQualified httpClientBuilderType,
J.MethodInvocation httpClientBuilderMethodInvoke) {
String settingName = entry.getKey();
Expression value = entry.getValue();
List<JavaType> parametersTypes = Collections.singletonList(value.getType());
JavaType.Method settingMethod = new JavaType.Method(
null,
0L,
httpClientBuilderType,
settingName,
httpClientBuilderType,
Collections.emptyList(),
parametersTypes,
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
);
J.Identifier settingBuilderName =
IdentifierUtils.makeId(settingName, settingMethod);
JContainer argument = JContainer.build(Arrays.asList(JRightPadded.build(value)));
J.MethodInvocation settingsInvoke = new J.MethodInvocation(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
new JRightPadded(httpClientBuilderMethodInvoke, Space.format("\n"), Markers.EMPTY),
null,
settingBuilderName,
argument,
settingMethod
);
return settingsInvoke;
}
private J.MethodInvocation handleClientOverrideConfiguration(
J.MethodInvocation originalMethod,
J.MethodInvocation method,
ExecutionContext executionContext) {
Expression methodSelectExpression = method.getSelect();
if (methodSelectExpression == null || !(methodSelectExpression instanceof J.MethodInvocation)) {
return method;
}
// method: ClientOverrideConfiguration.builder().maxConnection(xx).aNonHttpSetting(xx)
// method.getSelect: ClientOverrideConfiguration.builder().maxConnection(xx)
J.MethodInvocation selectInvoke = (J.MethodInvocation) methodSelectExpression;
String selectMethodName = selectInvoke.getSimpleName();
if (!HTTP_SETTING_METHOD_NAMES.contains(selectMethodName)) {
return method;
}
if (!(selectInvoke.getSelect() instanceof J.MethodInvocation)) {
return method;
}
addHttpSettingsToClientOverrideConfigCursor(selectMethodName, selectInvoke);
// select.getSelect: ClientOverrideConfiguration.builder()
J.MethodInvocation selectInvokeSelect = (J.MethodInvocation) selectInvoke.getSelect();
// new method: ClientOverrideConfiguration.builder().aNonHttpSetting(xx)
method = method.withSelect(selectInvokeSelect);
if (method.getSimpleName().equals("build")) {
method = method.withPrefix(Space.SINGLE_SPACE);
}
return maybeAutoFormat(originalMethod, method, executionContext);
}
/**
* Add HTTP settings to clientOverrideConfiguration parent cursor so that it can be popped out later to pass to SDK client
* builder.
* <p>
* The location of parent cursor depends on where the clientOverrideConfiguration is defined. If it is a variable, the
* parent cursor is the NamedVariable; if it is a method, then the parent cursor is the MethodDeclaration.
*/
private void addHttpSettingsToClientOverrideConfigCursor(String selectMethodName,
J.MethodInvocation selectInvoke) {
Cursor parentCursor = getCursor();
try {
parentCursor = getCursor().dropParentUntil(parent -> isClientOverrideConfigurationNamedVariableType(parent));
} catch (Exception e) {
// Ignore if it's not a variable
log.debug(() -> "Cannot find named variable type", e);
}
try {
parentCursor = getCursor()
.dropParentUntil(parent -> (parent instanceof J.MethodDeclaration) &&
returnClientOverrideConfiguration(((J.MethodDeclaration) parent).getMethodType()));
} catch (Exception e) {
// Ignore if it's not a method declaration
log.debug(() -> "Cannot find method declaration type", e);
}
Map<String, Expression> httpSettings =
parentCursor.getMessage(OVERRIDE_CONFIG_BUILDER_HTTP_SETTINGS_CURSOR, new HashMap<>());
httpSettings.put(selectMethodName, selectInvoke.getArguments().get(0));
parentCursor.putMessage(OVERRIDE_CONFIG_BUILDER_HTTP_SETTINGS_CURSOR, httpSettings);
}
private static boolean isClientOverrideConfigurationNamedVariableType(Object parent) {
return (parent instanceof J.VariableDeclarations.NamedVariable) &&
isClientOverrideConfigurationType(((J.VariableDeclarations.NamedVariable) parent).getType());
}
}
}