private static SdkHttpFullRequest.Builder changeQueryParametersToFormData(SdkHttpFullRequest.Builder input) { byte[] params = SdkHttpUtils.encodeAndFlattenFormData(input.rawQueryParameters()).orElse("") .getBytes(StandardCharsets.UTF_8); return input.clearQueryParameters() .contentStreamProvider(() -> new ByteArrayInputStream(params)) .putHeader("Content-Length", singletonList(String.valueOf(params.length))) .putHeader("Content-Type", singletonList("application/x-www-form-urlencoded; charset=" + lowerCase(StandardCharsets.UTF_8.toString()))); } }
private SdkHttpFullRequest mockSdkRequest(String uriString, SdkHttpMethod method) { URI uri = URI.create(uriString); SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder() .uri(uri) .method(method) .putHeader("Host", uri.getHost()) .putHeader("User-Agent", "hello-world!"); if (method != SdkHttpMethod.HEAD) { requestBuilder.contentStreamProvider(() -> new ByteArrayInputStream("Body".getBytes(StandardCharsets.UTF_8))); } return requestBuilder.build(); }
/** * Read the content of the request to get the length of the stream. */ private static long getContentLength(SdkHttpFullRequest.Builder requestBuilder) throws IOException { InputStream content = requestBuilder.contentStreamProvider().newStream(); long contentLength = 0; byte[] tmp = new byte[4096]; int read; while ((read = content.read(tmp)) != -1) { contentLength += read; } return contentLength; } }
private SdkHttpFullRequest finishMarshalling() { // Content may already be set if the payload is binary data. if (request.contentStreamProvider() == null) { // End the implicit request object if needed. if (!hasExplicitPayloadMember) { jsonGenerator.writeEndObject(); } byte[] content = jsonGenerator.getBytes(); request.contentStreamProvider(() -> new ByteArrayInputStream(content)); if (content.length > 0) { request.putHeader(CONTENT_LENGTH, Integer.toString(content.length)); } } // We skip setting the default content type if the request is streaming as // content-type is determined based on the body of the stream if (!request.headers().containsKey(CONTENT_TYPE) && contentType != null && !hasStreamingInput) { request.putHeader(CONTENT_TYPE, contentType); } return request.build(); }
/** * If necessary, creates a chunk-encoding wrapper on the request payload. */ @Override protected void processRequestPayload(SdkHttpFullRequest.Builder mutableRequest, byte[] signature, byte[] signingKey, Aws4SignerRequestParams signerRequestParams, AwsS3V4SignerParams signerParams) { if (useChunkEncoding(mutableRequest, signerParams)) { if (mutableRequest.contentStreamProvider() != null) { ContentStreamProvider streamProvider = mutableRequest.contentStreamProvider(); mutableRequest.contentStreamProvider(() -> AwsS3V4Signer.this.asChunkEncodedStream( streamProvider.newStream(), signature, signingKey, signerRequestParams )); } } }
private SdkHttpFullRequest finishMarshalling() { // Content may already be set if the payload is binary data. if (request.contentStreamProvider() == null) { // End the implicit request object if needed. if (!hasExplicitPayloadMember) { jsonGenerator.writeEndObject(); } byte[] content = jsonGenerator.getBytes(); request.contentStreamProvider(() -> new ByteArrayInputStream(content)); if (content.length > 0) { request.putHeader(CONTENT_LENGTH, Integer.toString(content.length)); } } // We skip setting the default content type if the request is streaming as // content-type is determined based on the body of the stream if (!request.headers().containsKey(CONTENT_TYPE) && contentType != null && !hasStreamingInput) { request.putHeader(CONTENT_TYPE, contentType); } return request.build(); }
void doMarshall(SdkPojo pojo) { for (SdkField<?> field : pojo.sdkFields()) { Object val = field.getValueOrDefault(pojo); if (isBinary(field, val)) { request.contentStreamProvider(((SdkBytes) val)::asInputStream); setContentTypeHeaderIfNeeded("binary/octet-stream"); } else if (isExplicitPayloadMember(field) && val instanceof String) { byte[] content = ((String) val).getBytes(StandardCharsets.UTF_8); request.contentStreamProvider(() -> new ByteArrayInputStream(content)); request.putHeader(CONTENT_LENGTH, Integer.toString(content.length)); } else { MARSHALLER_REGISTRY.getMarshaller(field.location(), field.marshallingType(), val) .marshall(val, marshallerContext, field.locationName(), (SdkField<Object>) field); } } }
void doMarshall(SdkPojo pojo) { for (SdkField<?> field : pojo.sdkFields()) { Object val = field.getValueOrDefault(pojo); if (isBinary(field, val)) { request.contentStreamProvider(((SdkBytes) val)::asInputStream); } else { if (val != null && field.containsTrait(PayloadTrait.class)) { jsonGenerator.writeStartObject(); doMarshall((SdkPojo) val); jsonGenerator.writeEndObject(); } else { MARSHALLER_REGISTRY.getMarshaller(field.location(), field.marshallingType(), val) .marshall(val, marshallerContext, field.locationName(), (SdkField<Object>) field); } } } }
void doMarshall(SdkPojo pojo) { for (SdkField<?> field : pojo.sdkFields()) { Object val = field.getValueOrDefault(pojo); if (isBinary(field, val)) { request.contentStreamProvider(((SdkBytes) val)::asInputStream); } else { if (val != null && field.containsTrait(PayloadTrait.class)) { jsonGenerator.writeStartObject(); doMarshall((SdkPojo) val); jsonGenerator.writeEndObject(); } else { MARSHALLER_REGISTRY.getMarshaller(field.location(), field.marshallingType(), val) .marshall(val, marshallerContext, field.locationName(), (SdkField<Object>) field); } } } }
void doMarshall(SdkPojo pojo) { for (SdkField<?> field : pojo.sdkFields()) { Object val = field.getValueOrDefault(pojo); if (isBinary(field, val)) { request.contentStreamProvider(((SdkBytes) val)::asInputStream); setContentTypeHeaderIfNeeded("binary/octet-stream"); } else if (isExplicitPayloadMember(field) && val instanceof String) { byte[] content = ((String) val).getBytes(StandardCharsets.UTF_8); request.contentStreamProvider(() -> new ByteArrayInputStream(content)); request.putHeader(CONTENT_LENGTH, Integer.toString(content.length)); } else { MARSHALLER_REGISTRY.getMarshaller(field.location(), field.marshallingType(), val) .marshall(val, marshallerContext, field.locationName(), (SdkField<Object>) field); } } }
/** * Read the content of the request to get the length of the stream. */ private static long getContentLength(SdkHttpFullRequest.Builder requestBuilder) throws IOException { InputStream content = requestBuilder.contentStreamProvider().newStream(); long contentLength = 0; byte[] tmp = new byte[4096]; int read; while ((read = content.read(tmp)) != -1) { contentLength += read; } return contentLength; } }
private SdkHttpFullRequest finishMarshalling(SdkPojo pojo) { // Content may already be set if the payload is binary data. if (hasPayloadMembers(pojo) && request.contentStreamProvider() == null && marshallerContext.xmlGenerator() != null) { String content = marshallerContext.xmlGenerator().stringWriter().getBuffer().toString(); if (!content.isEmpty()) { request.contentStreamProvider(() -> new StringInputStream(content)); request.putHeader("Content-Length", Integer.toString(content.getBytes(StandardCharsets.UTF_8).length)); setContentTypeHeaderIfNeeded("application/xml"); } } return request.build(); }
/** * Calculate the hash of the request's payload. Subclass could override this * method to provide different values for "x-amz-content-sha256" header or * do any other necessary set-ups on the request headers. (e.g. aws-chunked * uses a pre-defined header value, and needs to change some headers * relating to content-encoding and content-length.) */ protected String calculateContentHash(SdkHttpFullRequest.Builder mutableRequest, T signerParams) { InputStream payloadStream = getBinaryRequestPayloadStream(mutableRequest.contentStreamProvider()); return BinaryUtils.toHex(hash(payloadStream)); }
private SdkHttpFullRequest finishMarshalling(SdkPojo pojo) { // Content may already be set if the payload is binary data. if (hasPayloadMembers(pojo) && request.contentStreamProvider() == null && marshallerContext.xmlGenerator() != null) { String content = marshallerContext.xmlGenerator().stringWriter().getBuffer().toString(); if (!content.isEmpty()) { request.contentStreamProvider(() -> new StringInputStream(content)); request.putHeader("Content-Length", Integer.toString(content.getBytes(StandardCharsets.UTF_8).length)); setContentTypeHeaderIfNeeded("application/xml"); } } return request.build(); }
@Override public Response<OutputT> execute(SdkHttpFullRequest request, RequestExecutionContext context) throws Exception { if (request.contentStreamProvider().isPresent()) { request = request.toBuilder() .contentStreamProvider(createManagedStream(request.contentStreamProvider().get())).build(); } InterruptMonitor.checkInterrupted(); return wrapped.execute(request, context); }
/** * @return True if payload signing is explicitly enabled. */ private boolean isPayloadSigningEnabled(SdkHttpFullRequest.Builder request, AwsS3V4SignerParams signerParams) { /** * If we aren't using https we should always sign the payload unless there is no payload */ if (!request.protocol().equals("https") && request.contentStreamProvider() != null) { return true; } Boolean isPayloadSigningEnabled = signerParams.enablePayloadSigning(); return isPayloadSigningEnabled != null && isPayloadSigningEnabled; }
@Override public SdkHttpFullRequest marshall(T in) { SdkHttpFullRequest.Builder marshalled = delegate.marshall(in).toBuilder(); marshalled.contentStreamProvider(requestBody.contentStreamProvider()); String contentType = marshalled.firstMatchingHeader(CONTENT_TYPE) .orElse(null); if (StringUtils.isEmpty(contentType)) { marshalled.putHeader(CONTENT_TYPE, requestBody.contentType()); } marshalled.putHeader(CONTENT_LENGTH, String.valueOf(requestBody.contentLength())); return marshalled.build(); } }
/** * @return True if payload signing is explicitly enabled. */ private boolean isPayloadSigningEnabled(SdkHttpFullRequest.Builder request, AwsS3V4SignerParams signerParams) { /** * If we aren't using https we should always sign the payload unless there is no payload */ if (!request.protocol().equals("https") && request.contentStreamProvider() != null) { return true; } Boolean isPayloadSigningEnabled = signerParams.enablePayloadSigning(); return isPayloadSigningEnabled != null && isPayloadSigningEnabled; }
/** * Calculate the hash of the request's payload. Subclass could override this * method to provide different values for "x-amz-content-sha256" header or * do any other necessary set-ups on the request headers. (e.g. aws-chunked * uses a pre-defined header value, and needs to change some headers * relating to content-encoding and content-length.) */ protected String calculateContentHash(SdkHttpFullRequest.Builder mutableRequest, T signerParams) { InputStream payloadStream = getBinaryRequestPayloadStream(mutableRequest.contentStreamProvider()); return BinaryUtils.toHex(hash(payloadStream)); }
private boolean shouldPutParamsInBody(SdkHttpFullRequest.Builder input) { return input.method() == SdkHttpMethod.POST && input.contentStreamProvider() == null && !CollectionUtils.isNullOrEmpty(input.rawQueryParameters()); }