@Override public SdkHttpFullRequest.Builder execute(SdkHttpFullRequest input, RequestExecutionContext context) throws Exception { return input.toBuilder(); } }
@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); }
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(); }
private boolean shouldSetContentLength(SdkHttpFullRequest request, SdkHttpContentPublisher requestProvider) { return requestProvider != null && !request.firstMatchingHeader("Content-Length").isPresent() && requestProvider.contentLength().isPresent() // Can cause issues with signing if content length is present for these method && request.method() != SdkHttpMethod.GET && request.method() != SdkHttpMethod.HEAD; }
protected String getCanonicalizedEndpoint(SdkHttpFullRequest request) { String endpointForStringToSign = StringUtils.lowerCase(request.host()); // Omit the port from the endpoint if we're using the default port for the protocol. Some HTTP clients (ie. Apache) don't // allow you to specify it in the request, so we're standardizing around not including it. See SdkHttpRequest#port(). if (!SdkHttpUtils.isUsingStandardPort(request.protocol(), request.port())) { endpointForStringToSign += ":" + request.port(); } return endpointForStringToSign; }
/** * Modifies the given {@link SdkHttpFullRequest} with new host if host prefix is enabled and set. */ private static SdkHttpFullRequest modifyEndpointHostIfNeeded(SdkHttpFullRequest originalRequest, SdkClientConfiguration clientConfiguration, ClientExecutionParams executionParams) { if (executionParams.discoveredEndpoint() != null) { URI discoveredEndpoint = executionParams.discoveredEndpoint(); return originalRequest.toBuilder().host(discoveredEndpoint.getHost()).port(discoveredEndpoint.getPort()).build(); } Boolean disableHostPrefixInjection = clientConfiguration.option(SdkAdvancedClientOption.DISABLE_HOST_PREFIX_INJECTION); if ((disableHostPrefixInjection != null && disableHostPrefixInjection.equals(Boolean.TRUE)) || StringUtils.isEmpty(executionParams.hostPrefixExpression())) { return originalRequest; } return originalRequest.toBuilder() .host(executionParams.hostPrefixExpression() + originalRequest.host()) .build(); }
public SimpleHttpContentPublisher(SdkHttpFullRequest request) { this.content = request.contentStreamProvider().map(p -> invokeSafely(() -> IoUtils.toByteArray(p.newStream()))) .orElseGet(() -> new byte[0]); this.length = content.length; }
public InterceptorContext modifyHttpRequest(InterceptorContext context, ExecutionAttributes executionAttributes) { InterceptorContext result = context; for (ExecutionInterceptor interceptor : interceptors) { AsyncRequestBody asyncRequestBody = interceptor.modifyAsyncHttpContent(result, executionAttributes).orElse(null); SdkHttpFullRequest sdkHttpFullRequest = (SdkHttpFullRequest) context.httpRequest(); if (!result.requestBody().isPresent() && sdkHttpFullRequest.contentStreamProvider().isPresent()) { int contentLength = Integer.parseInt(sdkHttpFullRequest.firstMatchingHeader("Content-Length").orElse("0")); String contentType = sdkHttpFullRequest.firstMatchingHeader("Content-Type").orElse(""); RequestBody requestBody = RequestBody.fromContentProvider(sdkHttpFullRequest.contentStreamProvider().get(), contentLength, contentType); result = result.toBuilder().requestBody(requestBody).build(); } RequestBody requestBody = interceptor.modifyHttpContent(result, executionAttributes).orElse(null); SdkHttpRequest interceptorResult = interceptor.modifyHttpRequest(result, executionAttributes); validateInterceptorResult(result.httpRequest(), interceptorResult, interceptor, "modifyHttpRequest"); result = result.copy(b -> b.httpRequest(interceptorResult) .asyncRequestBody(asyncRequestBody) .requestBody(requestBody)); } return result; }
/** * Encodes the request into a flow message and then returns bytebuffer from the message. * * @param request The request to encode * @return A bytebuffer representing the given request */ public static ByteBuffer encodeEventStreamRequestToByteBuffer(SdkHttpFullRequest request) { Map<String, HeaderValue> headers = request.headers() .entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> HeaderValue.fromString( firstIfPresent(e.getValue())))); byte[] payload = null; if (request.contentStreamProvider().isPresent()) { try { payload = IoUtils.toByteArray(request.contentStreamProvider().get().newStream()); } catch (IOException e) { throw new UncheckedIOException(e); } } return new Message(headers, payload).toByteBuffer(); }
/** * Extract signature from Authentication header * * @param request signed request with Authentication header * @return signature (Hex) string */ private String getHeaderSignature(SdkHttpFullRequest request) { Optional<String> authHeader = request.firstMatchingHeader(SignerConstant.AUTHORIZATION); if (authHeader.isPresent()) { Matcher matcher = AUTHENTICATION_HEADER_PATTERN.matcher(authHeader.get()); if (matcher.matches()) { String headerSignature = matcher.group(3); return headerSignature; } } // Without header signature, signer can not proceed. Thus throw out exception throw SdkClientException.builder().message("Signature is missing in AUTHORIZATION header!").build(); } }
private static byte[] toUtf8ByteArray(Map<String, AttributeValue> item) { SdkHttpFullRequest marshalled = putItemRequestMarshaller().marshall(PutItemRequest.builder().item(item).build()); InputStream content = marshalled.contentStreamProvider().get().newStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[8192]; int read; try { while ((read = content.read(buff)) != -1) { baos.write(buff, 0, read); } } catch (IOException ioe) { throw new UncheckedIOException(ioe); } return baos.toByteArray(); }
protected String getCanonicalizedEndpoint(SdkHttpFullRequest request) { String endpointForStringToSign = StringUtils.lowerCase(request.host()); // Omit the port from the endpoint if we're using the default port for the protocol. Some HTTP clients (ie. Apache) don't // allow you to specify it in the request, so we're standardizing around not including it. See SdkHttpRequest#port(). if (!SdkHttpUtils.isUsingStandardPort(request.protocol(), request.port())) { endpointForStringToSign += ":" + request.port(); } return endpointForStringToSign; }
/** * Extract signature from Authentication header * * @param request signed request with Authentication header * @return signature (Hex) string */ private String getHeaderSignature(SdkHttpFullRequest request) { Optional<String> authHeader = request.firstMatchingHeader(SignerConstant.AUTHORIZATION); if (authHeader.isPresent()) { Matcher matcher = AUTHENTICATION_HEADER_PATTERN.matcher(authHeader.get()); if (matcher.matches()) { String headerSignature = matcher.group(3); return headerSignature; } } // Without header signature, signer can not proceed. Thus throw out exception throw SdkClientException.builder().message("Signature is missing in AUTHORIZATION header!").build(); } }
@Override public SdkHttpFullRequest marshall(AudioEvent audioEvent) { Validate.paramNotNull(audioEvent, "audioEvent"); try { ProtocolMarshaller<SdkHttpFullRequest> protocolMarshaller = protocolFactory .createProtocolMarshaller(SDK_OPERATION_BINDING); return protocolMarshaller.marshall(audioEvent).toBuilder().putHeader(":message-type", "event") .putHeader(":event-type", "AudioEvent").putHeader(":content-type", "application/octet-stream").build(); } catch (Exception e) { throw SdkClientException.builder().message("Unable to marshall request to JSON: " + e.getMessage()).cause(e).build(); } } }
protected void testForResponseCodeUsingHttps(SdkHttpClient client, int returnCode) throws Exception { SdkHttpMethod sdkHttpMethod = SdkHttpMethod.POST; stubForMockRequest(returnCode); SdkHttpFullRequest req = mockSdkRequest("https://localhost:" + mockServer.httpsPort(), sdkHttpMethod); HttpExecuteResponse rsp = client.prepareRequest(HttpExecuteRequest.builder() .request(req) .contentStreamProvider(req.contentStreamProvider() .orElse(null)) .build()) .call(); validateResponse(rsp, returnCode, sdkHttpMethod); }
/** * Creates the basic {@link SdkHttpFullRequest} with information from the {@link OperationInfo} and the endpoint. * * @param operationInfo Metadata about operation, contains HTTP method and request URI. * @param endpoint Endpoint of request. * @return Mutable {@link SdkHttpFullRequest.Builder} with HTTP method, URI, and static query parameters set. */ public static SdkHttpFullRequest.Builder createSdkHttpRequest(OperationInfo operationInfo, URI endpoint) { SdkHttpFullRequest.Builder request = SdkHttpFullRequest .builder() .method(operationInfo.httpMethod()) .uri(endpoint); return request.encodedPath(SdkHttpUtils.appendUri(request.encodedPath(), addStaticQueryParametersToRequest(request, operationInfo.requestUri()))); }
@Override public SdkHttpFullRequest marshall(AudioEvent audioEvent) { Validate.paramNotNull(audioEvent, "audioEvent"); try { ProtocolMarshaller<SdkHttpFullRequest> protocolMarshaller = protocolFactory .createProtocolMarshaller(SDK_OPERATION_BINDING); return protocolMarshaller.marshall(audioEvent).toBuilder().putHeader(":message-type", "event") .putHeader(":event-type", "AudioEvent").putHeader(":content-type", "application/octet-stream").build(); } catch (Exception e) { throw SdkClientException.builder().message("Unable to marshall request to JSON: " + e.getMessage()).cause(e).build(); } } }