/** * Used to log the "x-amzn-RequestId" header at DEBUG level, if any, from the response. This * method assumes the apache httpClientSettings request/response has just been successfully * executed. The request id is logged using the "com.amazonaws.requestId" logger if it was * enabled at DEBUG level; otherwise, it is logged at DEBUG level using the * "com.amazonaws.request" logger. * * @return true if the AWS request id is available from the httpClientSettings header; false * otherwise. */ private boolean logHeaderRequestId(final HttpResponse response) { final String reqIdHeader = response.getHeaders() .get(HttpResponseHandler.X_AMZN_REQUEST_ID_HEADER); final boolean isHeaderReqIdAvail = reqIdHeader != null; if (requestIdLog.isDebugEnabled() || requestLog.isDebugEnabled()) { final String msg = HttpResponseHandler.X_AMZN_REQUEST_ID_HEADER + ": " + (isHeaderReqIdAvail ? reqIdHeader : "not available"); if (requestIdLog.isDebugEnabled()) { requestIdLog.debug(msg); } else { requestLog.debug(msg); } } return isHeaderReqIdAvail; }
public static <T> HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler(final FunctionWithException<HttpResponse, T> handle) { return new HttpResponseHandler<AmazonWebServiceResponse<T>>() { @Override public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception { AmazonWebServiceResponse<T> resp = new AmazonWebServiceResponse<T>(); resp.setResult(handle.apply(response)); return resp; } @Override public boolean needsConnectionLeftOpen() { return false; } }; }
/** * Ctor. * @param esEdp ElasticSearch URL. * @param uri REST path to the desired ElasticSearch endpoint. * @param respHandler Response handler. * @param errHandle Error handler. */ public EsHttpRequest( EsEndPoint esEdp, String uri, HttpResponseHandler<T> respHandler, HttpResponseHandler<AmazonServiceException> errHandler ){ this.request = new DefaultRequest<Void>("es"); String esEndpoint = esEdp.read(); if(esEndpoint == null || esEndpoint.isEmpty()) { throw new IllegalStateException("ElasticSearch endpoint needs to be specified!"); } if(esEndpoint.endsWith("/")) { esEndpoint += uri; } else { esEndpoint += "/" + uri; } this.request.setEndpoint(URI.create(esEndpoint)); this.respHandler = respHandler; this.errHandler = errHandler; }
private <T> T invoke(HttpMethodName httpMethodName, String resourcePath, Object representation, HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler) throws AmazonClientException { ExecutionContext executionContext = createExecutionContext(); AWSRequestMetrics awsRequestMetrics = executionContext.getAwsRequestMetrics(); awsRequestMetrics.startEvent(AWSRequestMetrics.Field.RequestMarshallTime.name()); Request request = buildRequest(httpMethodName, resourcePath, representation); awsRequestMetrics.endEvent(AWSRequestMetrics.Field.RequestMarshallTime.name()); awsRequestMetrics.startEvent(AWSRequestMetrics.Field.CredentialsRequestTime.name()); AWSCredentials credentials = awsCredentialsProvider.getCredentials(); awsRequestMetrics.endEvent(AWSRequestMetrics.Field.CredentialsRequestTime.name()); executionContext.setCredentials(credentials); awsRequestMetrics.startEvent(AWSRequestMetrics.Field.ClientExecuteTime.name()); Response<T> response = client.execute(request, responseHandler, errorResponseHandler, executionContext); awsRequestMetrics.endEvent(AWSRequestMetrics.Field.ClientExecuteTime.name()); awsRequestMetrics.log(); return response.getAwsResponse(); }
/** * @param delegate Response handler to delegate to and unwrap * @param request Marshalled request * @param awsRequestMetrics Request metrics */ public AwsResponseHandlerAdapter(HttpResponseHandler<AmazonWebServiceResponse<T>> delegate, Request<?> request, AWSRequestMetrics awsRequestMetrics, MetadataCache responseMetadataCache) { this.delegate = delegate; this.request = request; this.awsRequestMetrics = awsRequestMetrics; this.responseMetadataCache = responseMetadataCache; }
/** * Normal invoke with authentication. Credentials are required and may be overriden at the * request level. **/ private <Output, Input> Response<Output> invoke(Request<Input> request, RequestConfig requestConfig, ExecutionContext executionContext, HttpResponseHandler<Output> responseHandler, HttpResponseHandler<? extends SdkBaseException> errorResponseHandler) { executionContext.setCredentialsProvider(CredentialUtils.getCredentialsProvider( requestConfig, awsCredentialsProvider)); return doInvoke(request, requestConfig, executionContext, responseHandler, errorResponseHandler); }
/** * Invoke the request using the http client. Assumes credentials (or lack thereof) have been * configured in the ExecutionContext beforehand. **/ private <Output, Input> Response<Output> doInvoke(Request<Input> request, RequestConfig requestConfig, ExecutionContext executionContext, HttpResponseHandler<Output> responseHandler, HttpResponseHandler<? extends SdkBaseException> errorResponseHandler) { request.setEndpoint(endpoint); return client.requestExecutionBuilder() .request(request) .requestConfig(requestConfig) .executionContext(executionContext) .errorResponseHandler(errorResponseHandler) .execute(responseHandler); }
public static HttpResponseHandler<AmazonWebServiceResponse<String>> stringResponseHandler() { return responseHandler(new FunctionWithException<HttpResponse, String>() { @Override public String apply(HttpResponse in) throws Exception { return IOUtils.toString(in.getContent()); } }); }
public HalService(String endpoint, String serviceName, String regionId, Class<T> rootClass, String rootPath, HttpResponseHandler<AmazonServiceException> errorResponseHandler) { this.endpoint = endpoint; this.serviceName = serviceName; this.regionId = regionId; this.rootClass = rootClass; this.rootPath = rootPath; this.errorResponseHandler = errorResponseHandler; }
public HalClient(ClientConfiguration clientConfiguration, String endpoint, String serviceName, AWSCredentialsProvider awsCredentialsProvider, Map<String, Object> resourceCache, HttpResponseHandler<AmazonServiceException> errorResponseHandler) { super(clientConfiguration); this.setServiceNameIntern(serviceName); this.setEndpoint(endpoint); this.awsCredentialsProvider = awsCredentialsProvider; this.resourceCache = resourceCache; this.errorResponseHandler = errorResponseHandler; this.addRequestHandler(new AcceptHalJsonRequestHandler()); }
/** * Returns the response handler to be used for handling a successful response. * * @param operationMetadata Additional context information about an operation to create the appropriate response handler. */ public <T> HttpResponseHandler<AmazonWebServiceResponse<T>> createResponseHandler( JsonOperationMetadata operationMetadata, Unmarshaller<T, JsonUnmarshallerContext> responseUnmarshaller) { return getSdkFactory().createResponseHandler(operationMetadata, responseUnmarshaller); }
/** * Creates a response handler for handling a error response (non 2xx response). */ public HttpResponseHandler<AmazonServiceException> createErrorResponseHandler( JsonErrorResponseMetadata errorResponsMetadata) { return getSdkFactory().createErrorResponseHandler(errorUnmarshallers, errorResponsMetadata .getCustomErrorCodeFieldName()); }
public HttpResponseHandler<Output> getResponseHandler() { return responseHandler; }
public ClientExecutionParams<Input, Output> withResponseHandler( HttpResponseHandler<Output> responseHandler) { this.responseHandler = responseHandler; return this; }
public HttpResponseHandler<? extends SdkBaseException> getErrorResponseHandler() { return errorResponseHandler; }
public ClientExecutionParams<Input, Output> withErrorResponseHandler( HttpResponseHandler<? extends SdkBaseException> errorResponseHandler) { this.errorResponseHandler = errorResponseHandler; return this; }
protected HttpResponseHandler<AmazonServiceException> stubErrorHandler() throws Exception { HttpResponseHandler<AmazonServiceException> errorHandler = mock(JsonErrorResponseHandler.class); when(errorHandler.handle(any(HttpResponse.class))).thenReturn(mockException()); return errorHandler; }
private <X, Y extends AmazonWebServiceRequest> X invoke(Request<Y> request, HttpResponseHandler<AmazonWebServiceResponse<X>> responseHandler, String bucket, String key) { return invoke(request, responseHandler, bucket, key, false); }
private <X, Y extends AmazonWebServiceRequest> Response<X> invoke( Request<Y> request, HttpResponseHandler<AmazonWebServiceResponse<X>> responseHandler, ExecutionContext executionContext) { JsonErrorResponseHandler errorResponseHandler = new JsonErrorResponseHandler(exceptionUnmarshallers); Response<X> result = client.execute( prepareRequest(request, executionContext), responseHandler, errorResponseHandler, executionContext); return result; }
public HalService(String endpoint, String serviceName, String regionId, Class<T> rootClass, HttpResponseHandler<AmazonServiceException> errorResponseHandler) { this(endpoint, serviceName, regionId, rootClass, DEFAULT_ROOT_PATH, errorResponseHandler); }