• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.services.cloudsearchdomain.internal;
17 
18 import static java.util.Collections.singletonList;
19 import static software.amazon.awssdk.utils.StringUtils.lowerCase;
20 
21 import java.io.ByteArrayInputStream;
22 import java.nio.charset.StandardCharsets;
23 import java.util.Optional;
24 import software.amazon.awssdk.annotations.SdkInternalApi;
25 import software.amazon.awssdk.core.interceptor.Context;
26 import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
27 import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
28 import software.amazon.awssdk.core.sync.RequestBody;
29 import software.amazon.awssdk.http.SdkHttpMethod;
30 import software.amazon.awssdk.http.SdkHttpRequest;
31 import software.amazon.awssdk.services.cloudsearchdomain.model.SearchRequest;
32 
33 /**
34  * Ensures that all SearchRequests use <code>POST</code> instead of <code>GET</code>, moving the query parameters to be form data.
35  */
36 @SdkInternalApi
37 public final class SwitchToPostInterceptor implements ExecutionInterceptor {
38 
39     @Override
modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes)40     public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) {
41         SdkHttpRequest httpRequest = context.httpRequest();
42         if (context.request() instanceof SearchRequest) {
43             return httpRequest.toBuilder()
44                               .clearQueryParameters()
45                               .method(SdkHttpMethod.POST)
46                               .putHeader("Content-Type", singletonList("application/x-www-form-urlencoded"))
47                               .build();
48         }
49         return context.httpRequest();
50     }
51 
52     @Override
modifyHttpContent(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes)53     public Optional<RequestBody> modifyHttpContent(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) {
54         if (context.request() instanceof SearchRequest) {
55             byte[] params = context.httpRequest().encodedQueryParametersAsFormData().orElse("")
56                                    .getBytes(StandardCharsets.UTF_8);
57             return Optional.of(RequestBody.fromContentProvider(() -> new ByteArrayInputStream(params),
58                                                                params.length,
59                                                                "application/x-www-form-urlencoded; charset=" +
60                                                                lowerCase(StandardCharsets.UTF_8.toString())));
61         }
62         return context.requestBody();
63     }
64 
65 }
66