com.amazonaws.http.AmazonHttpClient Java Examples

The following examples show how to use com.amazonaws.http.AmazonHttpClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GenericApiGatewayClientTest.java    From apigateway-generic-java-sdk with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws IOException {
    AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider(new BasicAWSCredentials("foo", "bar"));

    mockClient = Mockito.mock(SdkHttpClient.class);
    HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream("test payload".getBytes()));
    resp.setEntity(entity);
    Mockito.doReturn(resp).when(mockClient).execute(any(HttpUriRequest.class), any(HttpContext.class));

    ClientConfiguration clientConfig = new ClientConfiguration();

    client = new GenericApiGatewayClientBuilder()
            .withClientConfiguration(clientConfig)
            .withCredentials(credentials)
            .withEndpoint("https://foobar.execute-api.us-east-1.amazonaws.com")
            .withRegion(Region.getRegion(Regions.fromName("us-east-1")))
            .withApiKey("12345")
            .withHttpClient(new AmazonHttpClient(clientConfig, mockClient, null))
            .build();
}
 
Example #2
Source File: GenericApiGatewayClientTest.java    From apigateway-generic-java-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute_noApiKey_noCreds() throws IOException {
    client = new GenericApiGatewayClientBuilder()
            .withEndpoint("https://foobar.execute-api.us-east-1.amazonaws.com")
            .withRegion(Region.getRegion(Regions.fromName("us-east-1")))
            .withClientConfiguration(new ClientConfiguration())
            .withHttpClient(new AmazonHttpClient(new ClientConfiguration(), mockClient, null))
            .build();

    GenericApiGatewayResponse response = client.execute(
            new GenericApiGatewayRequestBuilder()
                    .withBody(new ByteArrayInputStream("test request".getBytes()))
                    .withHttpMethod(HttpMethodName.POST)
                    .withResourcePath("/test/orders").build());

    assertEquals("Wrong response body", "test payload", response.getBody());
    assertEquals("Wrong response status", 200, response.getHttpResponse().getStatusCode());

    Mockito.verify(mockClient, times(1)).execute(argThat(new LambdaMatcher<>(
                    x -> (x.getMethod().equals("POST")
                            && x.getFirstHeader("x-api-key") == null
                            && x.getFirstHeader("Authorization") == null
                            && x.getURI().toString().equals("https://foobar.execute-api.us-east-1.amazonaws.com/test/orders")))),
            any(HttpContext.class));
}
 
Example #3
Source File: TestInvokeAmazonGatewayApiMock.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    mockSdkClient = Mockito.mock(SdkHttpClient.class);
    ClientConfiguration clientConfig = new ClientConfiguration();

    mockGetApi = new InvokeAWSGatewayApi(
        new AmazonHttpClient(clientConfig, mockSdkClient, null));
    runner = TestRunners.newTestRunner(mockGetApi);
    runner.setValidateExpressionUsage(false);

    final AWSCredentialsProviderControllerService serviceImpl = new AWSCredentialsProviderControllerService();
    runner.addControllerService("awsCredentialsProvider", serviceImpl);
    runner.setProperty(serviceImpl, InvokeAWSGatewayApi.ACCESS_KEY, "awsAccessKey");
    runner.setProperty(serviceImpl, InvokeAWSGatewayApi.SECRET_KEY, "awsSecretKey");
    runner.enableControllerService(serviceImpl);

    runner.setProperty(InvokeAWSGatewayApi.AWS_CREDENTIALS_PROVIDER_SERVICE,
                       "awsCredentialsProvider");
    runner.setProperty(InvokeAWSGatewayApi.PROP_AWS_GATEWAY_API_REGION, "us-east-1");
    runner.setProperty(InvokeAWSGatewayApi.PROP_AWS_API_KEY, "abcd");
    runner.setProperty(InvokeAWSGatewayApi.PROP_RESOURCE_NAME, "/TEST");
    runner.setProperty(InvokeAWSGatewayApi.PROP_AWS_GATEWAY_API_ENDPOINT,
                       "https://foobar.execute-api.us-east-1.amazonaws.com");
}
 
Example #4
Source File: GettingStarted.java    From getting-started with MIT License 5 votes vote down vote up
private static RequestExecutionBuilder buildRequest(final Request request) {
  try {
    return new AmazonHttpClient(new ClientConfiguration()).requestExecutionBuilder()
        .executionContext(new ExecutionContext(true)).request(request)
        .errorResponseHandler(new ErrorResponseHandler(false));
  } catch (AmazonServiceException exception) {
    System.out.println("Unexpected status code in response: " + exception.getStatusCode());
    System.out.println("Content: " + exception.getRawResponseContent());
    throw new RuntimeException("Failed request. Aborting.");
  }
}
 
Example #5
Source File: GenericApiGatewayClient.java    From apigateway-generic-java-sdk with Apache License 2.0 5 votes vote down vote up
GenericApiGatewayClient(ClientConfiguration clientConfiguration, String endpoint, Region region,
                        AWSCredentialsProvider credentials, String apiKey, AmazonHttpClient httpClient) {
    super(clientConfiguration);
    setRegion(region);
    setEndpoint(endpoint);
    this.credentials = credentials;
    this.apiKey = apiKey;
    this.signer = new AWS4Signer();
    this.signer.setServiceName(API_GATEWAY_SERVICE_NAME);
    this.signer.setRegionName(region.getName());

    final JsonOperationMetadata metadata = new JsonOperationMetadata().withHasStreamingSuccessResponse(false).withPayloadJson(false);
    final Unmarshaller<GenericApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller = in -> new GenericApiGatewayResponse(in.getHttpResponse());
    this.responseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createResponseHandler(metadata, responseUnmarshaller);
    JsonErrorUnmarshaller defaultErrorUnmarshaller = new JsonErrorUnmarshaller(GenericApiGatewayException.class, null) {
        @Override
        public AmazonServiceException unmarshall(JsonNode jsonContent) throws Exception {
            return new GenericApiGatewayException(jsonContent.toString());
        }
    };
    this.errorResponseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createErrorResponseHandler(
            Collections.singletonList(defaultErrorUnmarshaller), null);

    if (httpClient != null) {
        super.client = httpClient;
    }
}
 
Example #6
Source File: EsHttpRequest.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Perform this request.	
 */
@Override
public T perform() {
    final Response<T> rsp = new AmazonHttpClient(new ClientConfiguration())
        .requestExecutionBuilder()
        .executionContext(new ExecutionContext(true))
        .request(this.request)
        .errorResponseHandler(this.errHandler)
        .execute(this.respHandler);
    return rsp.getAwsResponse();
}
 
Example #7
Source File: GenericApiGatewayClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
GenericApiGatewayClient(ClientConfiguration clientConfiguration, String endpoint, Region region,
                        AWSCredentialsProvider credentials, String apiKey, AmazonHttpClient httpClient) {
    super(clientConfiguration);
    setRegion(region);
    setEndpoint(endpoint);
    this.credentials = credentials;
    this.apiKey = apiKey;
    this.signer = new AWS4Signer();
    this.signer.setServiceName(API_GATEWAY_SERVICE_NAME);
    this.signer.setRegionName(region.getName());

    final JsonOperationMetadata metadata = new JsonOperationMetadata().withHasStreamingSuccessResponse(false).withPayloadJson(false);
    final Unmarshaller<GenericApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller = in -> new GenericApiGatewayResponse(in.getHttpResponse());
    this.responseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createResponseHandler(metadata, responseUnmarshaller);
    JsonErrorUnmarshaller defaultErrorUnmarshaller = new JsonErrorUnmarshaller(GenericApiGatewayException.class, null) {
        @Override
        public AmazonServiceException unmarshall(JsonNode jsonContent) throws Exception {
            return new GenericApiGatewayException(jsonContent.toString());
        }
    };
    this.errorResponseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createErrorResponseHandler(
            Collections.singletonList(defaultErrorUnmarshaller), null);

    if (httpClient != null) {
        super.client = httpClient;
    }
}
 
Example #8
Source File: GenericApiGatewayClientBuilder.java    From apigateway-generic-java-sdk with Apache License 2.0 4 votes vote down vote up
public GenericApiGatewayClientBuilder withHttpClient(AmazonHttpClient client) {
    this.httpClient = client;
    return this;
}
 
Example #9
Source File: GenericApiGatewayClientBuilder.java    From apigateway-generic-java-sdk with Apache License 2.0 4 votes vote down vote up
public AmazonHttpClient getHttpClient() {
    return httpClient;
}
 
Example #10
Source File: InvokeAWSGatewayApi.java    From nifi with Apache License 2.0 4 votes vote down vote up
public InvokeAWSGatewayApi(AmazonHttpClient client) {
    super(client);
}
 
Example #11
Source File: GenericApiGatewayClientBuilder.java    From nifi with Apache License 2.0 4 votes vote down vote up
public GenericApiGatewayClientBuilder withHttpClient(AmazonHttpClient client) {
    this.httpClient = client;
    return this;
}
 
Example #12
Source File: GenericApiGatewayClientBuilder.java    From nifi with Apache License 2.0 4 votes vote down vote up
public AmazonHttpClient getHttpClient() {
    return httpClient;
}
 
Example #13
Source File: AbstractAWSGatewayApiProcessor.java    From nifi with Apache License 2.0 4 votes vote down vote up
public AbstractAWSGatewayApiProcessor(AmazonHttpClient client) {
    providedClient = client;
}