2828import java .io .InputStream ;
2929import java .io .OutputStream ;
3030import java .net .URI ;
31- import java .nio .charset .Charset ;
31+ import java .nio .charset .StandardCharsets ;
3232import java .time .Instant ;
3333import java .util .Arrays ;
3434import java .util .Date ;
4444import org .json .JSONObject ;
4545import org .json .JSONTokener ;
4646
47+ import software .amazon .awssdk .http .SdkHttpClient ;
48+ import software .amazon .awssdk .http .apache .ApacheHttpClient ;
4749import software .amazon .awssdk .utils .StringUtils ;
4850import software .amazon .cloudformation .exceptions .BaseHandlerException ;
4951import software .amazon .cloudformation .exceptions .FileScrubberException ;
8082
8183public abstract class LambdaWrapper <ResourceT , CallbackT > implements RequestStreamHandler {
8284
85+ public static final SdkHttpClient HTTP_CLIENT = ApacheHttpClient .builder ().build ();
86+
8387 private static final List <Action > MUTATING_ACTIONS = Arrays .asList (Action .CREATE , Action .DELETE , Action .UPDATE );
8488 private static final int INVOCATION_TIMEOUT_MS = 60000 ;
8589
@@ -115,11 +119,11 @@ public abstract class LambdaWrapper<ResourceT, CallbackT> implements RequestStre
115119 protected LambdaWrapper () {
116120 this .platformCredentialsProvider = new SessionCredentialsProvider ();
117121 this .providerCredentialsProvider = new SessionCredentialsProvider ();
118- this .cloudFormationProvider = new CloudFormationProvider (this .platformCredentialsProvider );
119- this .platformCloudWatchProvider = new CloudWatchProvider (this .platformCredentialsProvider );
120- this .providerCloudWatchProvider = new CloudWatchProvider (this .providerCredentialsProvider );
121- this .platformCloudWatchEventsProvider = new CloudWatchEventsProvider (this .platformCredentialsProvider );
122- this .cloudWatchLogsProvider = new CloudWatchLogsProvider (this .providerCredentialsProvider );
122+ this .cloudFormationProvider = new CloudFormationProvider (this .platformCredentialsProvider , HTTP_CLIENT );
123+ this .platformCloudWatchProvider = new CloudWatchProvider (this .platformCredentialsProvider , HTTP_CLIENT );
124+ this .providerCloudWatchProvider = new CloudWatchProvider (this .providerCredentialsProvider , HTTP_CLIENT );
125+ this .platformCloudWatchEventsProvider = new CloudWatchEventsProvider (this .platformCredentialsProvider , HTTP_CLIENT );
126+ this .cloudWatchLogsProvider = new CloudWatchLogsProvider (this .providerCredentialsProvider , HTTP_CLIENT );
123127 this .serializer = new Serializer ();
124128 this .validator = new Validator ();
125129 this .typeReference = getTypeReference ();
@@ -137,16 +141,17 @@ public LambdaWrapper(final CallbackAdapter<ResourceT> callbackAdapter,
137141 final MetricsPublisher providerMetricsPublisher ,
138142 final CloudWatchScheduler scheduler ,
139143 final SchemaValidator validator ,
140- final Serializer serializer ) {
144+ final Serializer serializer ,
145+ final SdkHttpClient httpClient ) {
141146
142147 this .callbackAdapter = callbackAdapter ;
143148 this .platformCredentialsProvider = platformCredentialsProvider ;
144149 this .providerCredentialsProvider = providerCredentialsProvider ;
145- this .cloudFormationProvider = new CloudFormationProvider (this .platformCredentialsProvider );
146- this .platformCloudWatchProvider = new CloudWatchProvider (this .platformCredentialsProvider );
147- this .providerCloudWatchProvider = new CloudWatchProvider (this .providerCredentialsProvider );
148- this .platformCloudWatchEventsProvider = new CloudWatchEventsProvider (this .platformCredentialsProvider );
149- this .cloudWatchLogsProvider = new CloudWatchLogsProvider (this .providerCredentialsProvider );
150+ this .cloudFormationProvider = new CloudFormationProvider (this .platformCredentialsProvider , httpClient );
151+ this .platformCloudWatchProvider = new CloudWatchProvider (this .platformCredentialsProvider , httpClient );
152+ this .providerCloudWatchProvider = new CloudWatchProvider (this .providerCredentialsProvider , httpClient );
153+ this .platformCloudWatchEventsProvider = new CloudWatchEventsProvider (this .platformCredentialsProvider , httpClient );
154+ this .cloudWatchLogsProvider = new CloudWatchLogsProvider (this .providerCredentialsProvider , httpClient );
150155 this .providerEventsLogger = providerEventsLogger ;
151156 this .platformLambdaLogger = platformEventsLogger ;
152157 this .platformMetricsPublisher = platformMetricsPublisher ;
@@ -245,7 +250,7 @@ public void handleRequest(final InputStream inputStream, final OutputStream outp
245250 throw new TerminalException ("No request object received" );
246251 }
247252
248- String input = IOUtils .toString (inputStream , "UTF-8" );
253+ String input = IOUtils .toString (inputStream , StandardCharsets . UTF_8 );
249254 JSONObject rawInput = new JSONObject (new JSONTokener (input ));
250255
251256 // deserialize incoming payload to modelled request
@@ -483,7 +488,7 @@ private Response<ResourceT> createProgressResponse(final ProgressEvent<ResourceT
483488 private void writeResponse (final OutputStream outputStream , final Response <ResourceT > response ) throws IOException {
484489
485490 String output = this .serializer .serialize (response );
486- outputStream .write (output .getBytes (Charset . forName ( "UTF-8" ) ));
491+ outputStream .write (output .getBytes (StandardCharsets . UTF_8 ));
487492 outputStream .close ();
488493 }
489494
0 commit comments