-
Notifications
You must be signed in to change notification settings - Fork 916
adds an interceptor to replace params-to-body stage and applies it to… #1566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2010-2020 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.protocols.query.interceptor; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static software.amazon.awssdk.utils.StringUtils.lowerCase; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import software.amazon.awssdk.annotations.SdkProtectedApi; | ||
import software.amazon.awssdk.core.interceptor.Context; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; | ||
import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
import software.amazon.awssdk.http.SdkHttpMethod; | ||
import software.amazon.awssdk.http.SdkHttpRequest; | ||
import software.amazon.awssdk.utils.CollectionUtils; | ||
import software.amazon.awssdk.utils.http.SdkHttpUtils; | ||
|
||
/** | ||
* Modifies an HTTP request by moving query parameters to the body under the following conditions: | ||
* - It is a POST request | ||
* - There is no content stream provider | ||
* - There are query parameters to transfer | ||
* <p> | ||
* This interceptor is automatically inserted by codegen for services using Query Protocol | ||
*/ | ||
@SdkProtectedApi | ||
public final class QueryParametersToBodyInterceptor implements ExecutionInterceptor { | ||
|
||
private static final String DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded; charset=" + | ||
lowerCase(StandardCharsets.UTF_8.toString()); | ||
|
||
@Override | ||
public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, | ||
ExecutionAttributes executionAttributes) { | ||
|
||
SdkHttpRequest httpRequest = context.httpRequest(); | ||
|
||
if (!(httpRequest instanceof SdkHttpFullRequest)) { | ||
return httpRequest; | ||
} | ||
Comment on lines
+54
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are the situations under which this legitimately happens? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know :-). I put it in more defensively, you could say. |
||
|
||
SdkHttpFullRequest httpFullRequest = (SdkHttpFullRequest) httpRequest; | ||
if (shouldPutParamsInBody(httpFullRequest)) { | ||
return changeQueryParametersToFormData(httpFullRequest); | ||
} | ||
return httpFullRequest; | ||
} | ||
|
||
private boolean shouldPutParamsInBody(SdkHttpFullRequest input) { | ||
return input.method() == SdkHttpMethod.POST && | ||
!input.contentStreamProvider().isPresent() && | ||
!CollectionUtils.isNullOrEmpty(input.rawQueryParameters()); | ||
} | ||
|
||
private SdkHttpRequest changeQueryParametersToFormData(SdkHttpFullRequest input) { | ||
byte[] params = SdkHttpUtils.encodeAndFlattenFormData(input.rawQueryParameters()).orElse("") | ||
.getBytes(StandardCharsets.UTF_8); | ||
|
||
return input.toBuilder().clearQueryParameters() | ||
.contentStreamProvider(() -> new ByteArrayInputStream(params)) | ||
.putHeader("Content-Length", singletonList(String.valueOf(params.length))) | ||
.putHeader("Content-Type", singletonList(DEFAULT_CONTENT_TYPE)) | ||
.build(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright 2010-2020 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.protocols.query.interceptor; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import software.amazon.awssdk.core.Protocol; | ||
import software.amazon.awssdk.core.SdkRequest; | ||
import software.amazon.awssdk.core.async.AsyncRequestBody; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.http.ContentStreamProvider; | ||
import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
import software.amazon.awssdk.http.SdkHttpMethod; | ||
import software.amazon.awssdk.http.SdkHttpRequest; | ||
import software.amazon.awssdk.utils.IoUtils; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class QueryParametersToBodyInterceptorTest { | ||
|
||
public static final URI HTTP_LOCALHOST = URI.create("http://localhost:8080"); | ||
|
||
private QueryParametersToBodyInterceptor interceptor; | ||
private ExecutionAttributes executionAttributes; | ||
|
||
private SdkHttpFullRequest.Builder requestBuilder; | ||
|
||
@Before | ||
public void setup() { | ||
|
||
interceptor = new QueryParametersToBodyInterceptor(); | ||
executionAttributes = new ExecutionAttributes(); | ||
|
||
requestBuilder = SdkHttpFullRequest.builder() | ||
.protocol(Protocol.HTTPS.toString()) | ||
.method(SdkHttpMethod.POST) | ||
.putRawQueryParameter("key", singletonList("value")) | ||
.uri(HTTP_LOCALHOST); | ||
} | ||
|
||
@Test | ||
public void postRequestsWithNoBodyHaveTheirParametersMovedToTheBody() throws Exception { | ||
|
||
SdkHttpFullRequest request = requestBuilder.build(); | ||
|
||
SdkHttpFullRequest output = (SdkHttpFullRequest) interceptor.modifyHttpRequest( | ||
new HttpRequestOnlyContext(request, null), executionAttributes); | ||
|
||
assertThat(output.rawQueryParameters()).hasSize(0); | ||
assertThat(output.headers()) | ||
.containsKey("Content-Length") | ||
.containsEntry("Content-Type", singletonList("application/x-www-form-urlencoded; charset=utf-8")); | ||
assertThat(output.contentStreamProvider()).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
public void nonPostRequestsWithNoBodyAreUnaltered() throws Exception { | ||
Stream.of(SdkHttpMethod.values()) | ||
.filter(m -> !m.equals(SdkHttpMethod.POST)) | ||
.forEach(this::nonPostRequestsUnaltered); | ||
} | ||
|
||
@Test | ||
public void postWithContentIsUnaltered() throws Exception { | ||
byte[] contentBytes = "hello".getBytes(StandardCharsets.UTF_8); | ||
ContentStreamProvider contentProvider = () -> new ByteArrayInputStream(contentBytes); | ||
|
||
SdkHttpFullRequest request = requestBuilder.contentStreamProvider(contentProvider).build(); | ||
|
||
SdkHttpFullRequest output = (SdkHttpFullRequest) interceptor.modifyHttpRequest( | ||
new HttpRequestOnlyContext(request, null), executionAttributes); | ||
|
||
assertThat(output.rawQueryParameters()).hasSize(1); | ||
assertThat(output.headers()).hasSize(0); | ||
assertThat(IoUtils.toByteArray(output.contentStreamProvider().get().newStream())).isEqualTo(contentBytes); | ||
} | ||
|
||
@Test | ||
public void onlyAlterRequestsIfParamsArePresent() throws Exception { | ||
SdkHttpFullRequest request = requestBuilder.clearQueryParameters().build(); | ||
|
||
SdkHttpFullRequest output = (SdkHttpFullRequest) interceptor.modifyHttpRequest( | ||
new HttpRequestOnlyContext(request, null), executionAttributes); | ||
|
||
assertThat(output.rawQueryParameters()).hasSize(0); | ||
assertThat(output.headers()).hasSize(0); | ||
assertThat(output.contentStreamProvider()).isEmpty(); | ||
} | ||
|
||
private void nonPostRequestsUnaltered(SdkHttpMethod method) { | ||
|
||
SdkHttpFullRequest request = requestBuilder.method(method).build(); | ||
|
||
SdkHttpFullRequest output = (SdkHttpFullRequest) interceptor.modifyHttpRequest( | ||
new HttpRequestOnlyContext(request, null), executionAttributes); | ||
|
||
assertThat(output.rawQueryParameters()).hasSize(1); | ||
assertThat(output.headers()).hasSize(0); | ||
assertThat(output.contentStreamProvider()).isEmpty(); | ||
} | ||
|
||
public final class HttpRequestOnlyContext implements software.amazon.awssdk.core.interceptor.Context.ModifyHttpRequest { | ||
|
||
private final SdkHttpRequest request; | ||
private final RequestBody requestBody; | ||
|
||
public HttpRequestOnlyContext(SdkHttpRequest request, | ||
RequestBody requestBody) { | ||
this.request = request; | ||
this.requestBody = requestBody; | ||
} | ||
|
||
@Override | ||
public SdkRequest request() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public SdkHttpRequest httpRequest() { | ||
return request; | ||
} | ||
|
||
@Override | ||
public Optional<RequestBody> requestBody() { | ||
return Optional.ofNullable(requestBody); | ||
} | ||
|
||
@Override | ||
public Optional<AsyncRequestBody> asyncRequestBody() { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.