Skip to content

Commit 15b856d

Browse files
committed
NIFI-5022 InvokeAWSGatewayApi processor
1 parent 5613bf4 commit 15b856d

21 files changed

+3828
-2
lines changed

NOTICE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,27 @@ This includes derived works from Apache Calcite available under Apache Software
5050
Copyright 2012-2017 The Apache Software Foundation
5151
The code can be found in nifi-nar-bundles/nifi-standard-nar/nifi-standard-processors/../FlowFileProjectTableScanRule
5252
and nifi-nar-bundles/nifi-standard-nar/nifi-standard-processors/../FlowFileTableScan
53+
54+
This includes derived works from apigateway-generic-java-sdk (ASLv2 licenced) project (https://github.com/rpgreen/apigateway-generic-java-sdk):
55+
The derived work is adapted from
56+
main/ca/ryangreen/apigateway/generic/
57+
GenericApiGatewayClient.java
58+
GenericApiGatewayClientBuilder.java
59+
GenericApiGatewayException.java
60+
GenericApiGatewayRequest.java
61+
GenericApiGatewayRequestBuilder.java
62+
test/ca/ryangreen/apigateway/generic/
63+
GenericApiGatewayClientTest.java
64+
LambdaMatcher.java
65+
and can be found in the directories:
66+
nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/src/main/../wag/client/
67+
GenericApiGatewayClient.java
68+
GenericApiGatewayClientBuilder.java
69+
GenericApiGatewayException.java
70+
GenericApiGatewayRequest.java
71+
GenericApiGatewayRequestBuilder.java
72+
Validate.java
73+
nifi-nar-bundles/nifi-aws-bundle/nifi-aws-processors/src/test/../wag/
74+
RequestMatcher.java
75+
GetAWSGatewayApiTest.java
76+

nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
<groupId>com.amazonaws</groupId>
5454
<artifactId>aws-java-sdk-sqs</artifactId>
5555
</dependency>
56+
<dependency>
57+
<groupId>commons-io</groupId>
58+
<artifactId>commons-io</artifactId>
59+
<version>2.6</version>
60+
</dependency>
5661
<dependency>
5762
<groupId>org.apache.commons</groupId>
5863
<artifactId>commons-lang3</artifactId>

nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/src/main/java/org/apache/nifi/processors/aws/AbstractAWSProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ public abstract class AbstractAWSProcessor<ClientType extends AmazonWebServiceCl
130130
protected static final Protocol DEFAULT_PROTOCOL = Protocol.HTTPS;
131131
protected static final String DEFAULT_USER_AGENT = "NiFi";
132132

133-
private static AllowableValue createAllowableValue(final Regions region) {
133+
protected static AllowableValue createAllowableValue(final Regions region) {
134134
return new AllowableValue(region.getName(), AWSRegions.getRegionDisplayName(region.getName()));
135135
}
136136

137-
private static AllowableValue[] getAvailableRegions() {
137+
protected static AllowableValue[] getAvailableRegions() {
138138
final List<AllowableValue> values = new ArrayList<>();
139139
for (final Regions region : Regions.values()) {
140140
values.add(createAllowableValue(region));

nifi-nar-bundles/nifi-aws-bundle/nifi-aws-abstract-processors/src/main/java/org/apache/nifi/processors/aws/wag/AbstractAWSGatewayApiProcessor.java

Lines changed: 717 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.processors.aws.wag.client;
18+
19+
import com.amazonaws.AmazonServiceException;
20+
import com.amazonaws.AmazonWebServiceClient;
21+
import com.amazonaws.ClientConfiguration;
22+
import com.amazonaws.DefaultRequest;
23+
import com.amazonaws.auth.AWS4Signer;
24+
import com.amazonaws.auth.AWSCredentialsProvider;
25+
import com.amazonaws.http.AmazonHttpClient;
26+
import com.amazonaws.http.ExecutionContext;
27+
import com.amazonaws.http.HttpMethodName;
28+
import com.amazonaws.http.HttpResponseHandler;
29+
import com.amazonaws.internal.auth.DefaultSignerProvider;
30+
import com.amazonaws.protocol.json.JsonOperationMetadata;
31+
import com.amazonaws.protocol.json.SdkStructuredPlainJsonFactory;
32+
import com.amazonaws.regions.Region;
33+
import com.amazonaws.transform.JsonErrorUnmarshaller;
34+
import com.amazonaws.transform.JsonUnmarshallerContext;
35+
import com.amazonaws.transform.Unmarshaller;
36+
import com.fasterxml.jackson.databind.JsonNode;
37+
import java.io.InputStream;
38+
import java.net.URI;
39+
import java.util.Collections;
40+
import java.util.HashMap;
41+
import java.util.List;
42+
import java.util.Map;
43+
44+
public class GenericApiGatewayClient extends AmazonWebServiceClient {
45+
private static final String API_GATEWAY_SERVICE_NAME = "execute-api";
46+
private static final String API_KEY_HEADER = "x-api-key";
47+
48+
private final JsonResponseHandler<GenericApiGatewayResponse> responseHandler;
49+
private final HttpResponseHandler<AmazonServiceException> errorResponseHandler;
50+
private final AWSCredentialsProvider credentials;
51+
private String apiKey;
52+
private final AWS4Signer signer;
53+
54+
GenericApiGatewayClient(ClientConfiguration clientConfiguration, String endpoint, Region region,
55+
AWSCredentialsProvider credentials, String apiKey, AmazonHttpClient httpClient) {
56+
super(clientConfiguration);
57+
setRegion(region);
58+
setEndpoint(endpoint);
59+
this.credentials = credentials;
60+
this.apiKey = apiKey;
61+
this.signer = new AWS4Signer();
62+
this.signer.setServiceName(API_GATEWAY_SERVICE_NAME);
63+
this.signer.setRegionName(region.getName());
64+
65+
final JsonOperationMetadata metadata = new JsonOperationMetadata().withHasStreamingSuccessResponse(false).withPayloadJson(false);
66+
final Unmarshaller<GenericApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller = in -> new GenericApiGatewayResponse(in.getHttpResponse());
67+
this.responseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createResponseHandler(metadata, responseUnmarshaller);
68+
JsonErrorUnmarshaller defaultErrorUnmarshaller = new JsonErrorUnmarshaller(GenericApiGatewayException.class, null) {
69+
@Override
70+
public AmazonServiceException unmarshall(JsonNode jsonContent) throws Exception {
71+
return new GenericApiGatewayException(jsonContent.toString());
72+
}
73+
};
74+
this.errorResponseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createErrorResponseHandler(
75+
Collections.singletonList(defaultErrorUnmarshaller), null);
76+
77+
if (httpClient != null) {
78+
super.client = httpClient;
79+
}
80+
}
81+
82+
public GenericApiGatewayResponse execute(GenericApiGatewayRequest request) {
83+
return execute(request.getHttpMethod(), request.getResourcePath(), request.getHeaders(), request.getParameters(), request.getBody());
84+
}
85+
86+
private GenericApiGatewayResponse execute(HttpMethodName method, String resourcePath, Map<String, String> headers, Map<String,List<String>> parameters, InputStream content) {
87+
final ExecutionContext executionContext = buildExecutionContext();
88+
89+
DefaultRequest request = new DefaultRequest(API_GATEWAY_SERVICE_NAME);
90+
request.setHttpMethod(method);
91+
request.setContent(content);
92+
request.setEndpoint(this.endpoint);
93+
request.setResourcePath(resourcePath);
94+
request.setHeaders(buildRequestHeaders(headers, apiKey));
95+
if (parameters != null) {
96+
request.setParameters(parameters);
97+
}
98+
return this.client.execute(request, responseHandler, errorResponseHandler, executionContext).getAwsResponse();
99+
}
100+
101+
private ExecutionContext buildExecutionContext() {
102+
final ExecutionContext executionContext = ExecutionContext.builder().withSignerProvider(
103+
new DefaultSignerProvider(this, signer)).build();
104+
executionContext.setCredentialsProvider(credentials);
105+
executionContext.setSigner(signer);
106+
return executionContext;
107+
}
108+
109+
private Map<String, String> buildRequestHeaders(Map<String, String> headers, String apiKey) {
110+
if (headers == null) {
111+
headers = new HashMap<>();
112+
}
113+
if (apiKey != null) {
114+
final Map<String, String> headersWithApiKey = new HashMap<>();
115+
headers.forEach(headersWithApiKey::put);
116+
headersWithApiKey.put(API_KEY_HEADER, apiKey);
117+
return headersWithApiKey;
118+
} else {
119+
return headers;
120+
}
121+
}
122+
123+
public URI getEndpoint() {
124+
return this.endpoint;
125+
}
126+
127+
@Override
128+
protected String getServiceNameIntern() {
129+
return API_GATEWAY_SERVICE_NAME;
130+
}
131+
}
132+
133+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.processors.aws.wag.client;
18+
19+
import com.amazonaws.ClientConfiguration;
20+
import com.amazonaws.auth.AWSCredentialsProvider;
21+
import com.amazonaws.http.AmazonHttpClient;
22+
import com.amazonaws.regions.Region;
23+
24+
public class GenericApiGatewayClientBuilder {
25+
private String endpoint;
26+
private Region region;
27+
private AWSCredentialsProvider credentials;
28+
private ClientConfiguration clientConfiguration;
29+
private String apiKey;
30+
private AmazonHttpClient httpClient;
31+
32+
public GenericApiGatewayClientBuilder withEndpoint(String endpoint) {
33+
this.endpoint = endpoint;
34+
return this;
35+
}
36+
37+
public GenericApiGatewayClientBuilder withRegion(Region region) {
38+
this.region = region;
39+
return this;
40+
}
41+
42+
public GenericApiGatewayClientBuilder withClientConfiguration(ClientConfiguration clientConfiguration) {
43+
this.clientConfiguration = clientConfiguration;
44+
return this;
45+
}
46+
47+
public GenericApiGatewayClientBuilder withCredentials(AWSCredentialsProvider credentials) {
48+
this.credentials = credentials;
49+
return this;
50+
}
51+
52+
public GenericApiGatewayClientBuilder withApiKey(String apiKey) {
53+
this.apiKey = apiKey;
54+
return this;
55+
}
56+
57+
public GenericApiGatewayClientBuilder withHttpClient(AmazonHttpClient client) {
58+
this.httpClient = client;
59+
return this;
60+
}
61+
62+
public AWSCredentialsProvider getCredentials() {
63+
return credentials;
64+
}
65+
66+
public String getApiKey() {
67+
return apiKey;
68+
}
69+
70+
public AmazonHttpClient getHttpClient() {
71+
return httpClient;
72+
}
73+
74+
public String getEndpoint() {
75+
return endpoint;
76+
}
77+
78+
public Region getRegion() {
79+
return region;
80+
}
81+
82+
public ClientConfiguration getClientConfiguration() {
83+
return clientConfiguration;
84+
}
85+
86+
public GenericApiGatewayClient build() {
87+
Validate.notEmpty(endpoint, "Endpoint");
88+
Validate.notNull(region, "Region");
89+
return new GenericApiGatewayClient(clientConfiguration, endpoint, region, credentials, apiKey, httpClient);
90+
}
91+
92+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.processors.aws.wag.client;
18+
19+
import com.amazonaws.AmazonServiceException;
20+
21+
public class GenericApiGatewayException extends AmazonServiceException {
22+
public GenericApiGatewayException(String errorMessage) {
23+
super(errorMessage);
24+
}
25+
26+
public GenericApiGatewayException(String errorMessage, Exception cause) {
27+
super(errorMessage, cause);
28+
}
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.nifi.processors.aws.wag.client;
18+
19+
import com.amazonaws.http.HttpMethodName;
20+
import java.io.InputStream;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
public class GenericApiGatewayRequest {
25+
26+
private final HttpMethodName httpMethod;
27+
private final String resourcePath;
28+
private final InputStream body;
29+
private final Map<String, String> headers;
30+
private final Map<String, List<String>> parameters;
31+
32+
public GenericApiGatewayRequest(HttpMethodName httpMethod, String resourcePath,
33+
InputStream body, Map<String, String> headers,
34+
Map<String, List<String>> parameters) {
35+
this.httpMethod = httpMethod;
36+
this.resourcePath = resourcePath;
37+
this.body = body;
38+
this.headers = headers;
39+
this.parameters = parameters;
40+
}
41+
42+
public HttpMethodName getHttpMethod() {
43+
return httpMethod;
44+
}
45+
46+
public String getResourcePath() {
47+
return resourcePath;
48+
}
49+
50+
public InputStream getBody() {
51+
return body;
52+
}
53+
54+
public Map<String, String> getHeaders() {
55+
return headers;
56+
}
57+
58+
public Map<String, List<String>> getParameters() {
59+
return parameters;
60+
}
61+
}

0 commit comments

Comments
 (0)