15
15
16
16
package software .amazon .awssdk .services .cloudsearchdomain .internal ;
17
17
18
- import static java .util .Collections .singletonList ;
19
18
import static software .amazon .awssdk .utils .StringUtils .lowerCase ;
20
19
21
20
import java .io .ByteArrayInputStream ;
@@ -39,30 +38,28 @@ public final class SwitchToPostInterceptor implements ExecutionInterceptor {
39
38
40
39
@ Override
41
40
public SdkHttpRequest modifyHttpRequest (Context .ModifyHttpRequest context , ExecutionAttributes executionAttributes ) {
42
- byte [] params = SdkHttpUtils .encodeAndFlattenFormData (context .httpRequest ().rawQueryParameters ()).orElse ("" )
43
- .getBytes (StandardCharsets .UTF_8 );
44
-
45
- return context .httpRequest ()
46
- .toBuilder ()
47
- .method (SdkHttpMethod .POST )
48
- .putHeader ("Content-Length" , singletonList (String .valueOf (params .length )))
49
- .putHeader ("Content-Type" , singletonList ("application/x-www-form-urlencoded; charset=" +
50
- lowerCase (StandardCharsets .UTF_8 .toString ()))).build ();
41
+ SdkHttpRequest httpRequest = context .httpRequest ();
42
+ if (context .request () instanceof SearchRequest ) {
43
+ return httpRequest .toBuilder ()
44
+ .clearQueryParameters ()
45
+ .method (SdkHttpMethod .POST )
46
+ .build ();
47
+ }
48
+ return context .httpRequest ();
51
49
}
52
50
53
51
@ Override
54
52
public Optional <RequestBody > modifyHttpContent (Context .ModifyHttpRequest context , ExecutionAttributes executionAttributes ) {
55
- SdkHttpRequest request = context .httpRequest ();
56
- Object originalRequest = context .request ();
57
- if (originalRequest instanceof SearchRequest && request .method () == SdkHttpMethod .GET ) {
58
- byte [] params = SdkHttpUtils .encodeAndFlattenFormData (request .rawQueryParameters ()).orElse ("" )
59
- .getBytes (StandardCharsets .UTF_8 );
53
+ if (context .request () instanceof SearchRequest ) {
54
+ byte [] params = SdkHttpUtils .encodeAndFlattenFormData (context .httpRequest ().rawQueryParameters ()).orElse ("" )
55
+ .getBytes (StandardCharsets .UTF_8 );
60
56
return Optional .of (RequestBody .fromContentProvider (() -> new ByteArrayInputStream (params ),
61
- params .length ,
62
- "application/x-www-form-urlencoded; charset=" +
63
- lowerCase (StandardCharsets .UTF_8 .toString ())));
57
+ params .length ,
58
+ "application/x-www-form-urlencoded; charset=" +
59
+ lowerCase (StandardCharsets .UTF_8 .toString ())));
64
60
}
65
61
66
62
return context .requestBody ();
67
63
}
64
+
68
65
}
0 commit comments