-
Notifications
You must be signed in to change notification settings - Fork 3k
NIFI-5022 InvokeAWSGatewayApi processor #2588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cb07b4c
NIFI-5022 InvokeAWSGatewayApi processor
ottobackwards 57dc6f8
Merge branch 'master' of https://github.com/apache/nifi into aws-web-api
ottobackwards 65639bb
per review, simplify header building and remove new map
ottobackwards b51bfc3
Per review:
ottobackwards File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
675 changes: 675 additions & 0 deletions
675
...sors/src/main/java/org/apache/nifi/processors/aws/wag/AbstractAWSGatewayApiProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
134 changes: 134 additions & 0 deletions
134
...sors/src/main/java/org/apache/nifi/processors/aws/wag/client/GenericApiGatewayClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.nifi.processors.aws.wag.client; | ||
|
|
||
| import com.amazonaws.AmazonServiceException; | ||
| import com.amazonaws.AmazonWebServiceClient; | ||
| import com.amazonaws.ClientConfiguration; | ||
| import com.amazonaws.DefaultRequest; | ||
| import com.amazonaws.auth.AWS4Signer; | ||
| import com.amazonaws.auth.AWSCredentialsProvider; | ||
| import com.amazonaws.http.AmazonHttpClient; | ||
| import com.amazonaws.http.ExecutionContext; | ||
| import com.amazonaws.http.HttpMethodName; | ||
| import com.amazonaws.http.HttpResponseHandler; | ||
| import com.amazonaws.http.JsonResponseHandler; | ||
| import com.amazonaws.internal.auth.DefaultSignerProvider; | ||
| import com.amazonaws.protocol.json.JsonOperationMetadata; | ||
| import com.amazonaws.protocol.json.SdkStructuredPlainJsonFactory; | ||
| import com.amazonaws.regions.Region; | ||
| import com.amazonaws.transform.JsonErrorUnmarshaller; | ||
| import com.amazonaws.transform.JsonUnmarshallerContext; | ||
| import com.amazonaws.transform.Unmarshaller; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import java.io.InputStream; | ||
| import java.net.URI; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class GenericApiGatewayClient extends AmazonWebServiceClient { | ||
| private static final String API_GATEWAY_SERVICE_NAME = "execute-api"; | ||
| private static final String API_KEY_HEADER = "x-api-key"; | ||
|
|
||
| private final JsonResponseHandler<GenericApiGatewayResponse> responseHandler; | ||
| private final HttpResponseHandler<AmazonServiceException> errorResponseHandler; | ||
| private final AWSCredentialsProvider credentials; | ||
| private String apiKey; | ||
| private final AWS4Signer signer; | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
|
|
||
| public GenericApiGatewayResponse execute(GenericApiGatewayRequest request) { | ||
| return execute(request.getHttpMethod(), request.getResourcePath(), request.getHeaders(), request.getParameters(), request.getBody()); | ||
| } | ||
|
|
||
| private GenericApiGatewayResponse execute(HttpMethodName method, String resourcePath, Map<String, String> headers, Map<String,List<String>> parameters, InputStream content) { | ||
| final ExecutionContext executionContext = buildExecutionContext(); | ||
|
|
||
| DefaultRequest request = new DefaultRequest(API_GATEWAY_SERVICE_NAME); | ||
| request.setHttpMethod(method); | ||
| request.setContent(content); | ||
| request.setEndpoint(this.endpoint); | ||
| request.setResourcePath(resourcePath); | ||
| request.setHeaders(buildRequestHeaders(headers, apiKey)); | ||
| if (parameters != null) { | ||
| request.setParameters(parameters); | ||
| } | ||
| return this.client.execute(request, responseHandler, errorResponseHandler, executionContext).getAwsResponse(); | ||
| } | ||
|
|
||
| private ExecutionContext buildExecutionContext() { | ||
| final ExecutionContext executionContext = ExecutionContext.builder().withSignerProvider( | ||
| new DefaultSignerProvider(this, signer)).build(); | ||
| executionContext.setCredentialsProvider(credentials); | ||
| executionContext.setSigner(signer); | ||
| return executionContext; | ||
| } | ||
|
|
||
| private Map<String, String> buildRequestHeaders(Map<String, String> headers, String apiKey) { | ||
| if (headers == null) { | ||
| headers = new HashMap<>(); | ||
| } | ||
| if (apiKey != null) { | ||
| final Map<String, String> headersWithApiKey = new HashMap<>(); | ||
| headers.forEach(headersWithApiKey::put); | ||
| headersWithApiKey.put(API_KEY_HEADER, apiKey); | ||
| return headersWithApiKey; | ||
| } else { | ||
| return headers; | ||
| } | ||
| } | ||
|
|
||
| public URI getEndpoint() { | ||
| return this.endpoint; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getServiceNameIntern() { | ||
| return API_GATEWAY_SERVICE_NAME; | ||
| } | ||
| } | ||
|
|
||
|
|
||
92 changes: 92 additions & 0 deletions
92
...c/main/java/org/apache/nifi/processors/aws/wag/client/GenericApiGatewayClientBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.nifi.processors.aws.wag.client; | ||
|
|
||
| import com.amazonaws.ClientConfiguration; | ||
| import com.amazonaws.auth.AWSCredentialsProvider; | ||
| import com.amazonaws.http.AmazonHttpClient; | ||
| import com.amazonaws.regions.Region; | ||
|
|
||
| public class GenericApiGatewayClientBuilder { | ||
| private String endpoint; | ||
| private Region region; | ||
| private AWSCredentialsProvider credentials; | ||
| private ClientConfiguration clientConfiguration; | ||
| private String apiKey; | ||
| private AmazonHttpClient httpClient; | ||
|
|
||
| public GenericApiGatewayClientBuilder withEndpoint(String endpoint) { | ||
| this.endpoint = endpoint; | ||
| return this; | ||
| } | ||
|
|
||
| public GenericApiGatewayClientBuilder withRegion(Region region) { | ||
| this.region = region; | ||
| return this; | ||
| } | ||
|
|
||
| public GenericApiGatewayClientBuilder withClientConfiguration(ClientConfiguration clientConfiguration) { | ||
| this.clientConfiguration = clientConfiguration; | ||
| return this; | ||
| } | ||
|
|
||
| public GenericApiGatewayClientBuilder withCredentials(AWSCredentialsProvider credentials) { | ||
| this.credentials = credentials; | ||
| return this; | ||
| } | ||
|
|
||
| public GenericApiGatewayClientBuilder withApiKey(String apiKey) { | ||
| this.apiKey = apiKey; | ||
| return this; | ||
| } | ||
|
|
||
| public GenericApiGatewayClientBuilder withHttpClient(AmazonHttpClient client) { | ||
| this.httpClient = client; | ||
| return this; | ||
| } | ||
|
|
||
| public AWSCredentialsProvider getCredentials() { | ||
| return credentials; | ||
| } | ||
|
|
||
| public String getApiKey() { | ||
| return apiKey; | ||
| } | ||
|
|
||
| public AmazonHttpClient getHttpClient() { | ||
| return httpClient; | ||
| } | ||
|
|
||
| public String getEndpoint() { | ||
| return endpoint; | ||
| } | ||
|
|
||
| public Region getRegion() { | ||
| return region; | ||
| } | ||
|
|
||
| public ClientConfiguration getClientConfiguration() { | ||
| return clientConfiguration; | ||
| } | ||
|
|
||
| public GenericApiGatewayClient build() { | ||
| Validate.notEmpty(endpoint, "Endpoint"); | ||
| Validate.notNull(region, "Region"); | ||
| return new GenericApiGatewayClient(clientConfiguration, endpoint, region, credentials, apiKey, httpClient); | ||
| } | ||
|
|
||
| } |
29 changes: 29 additions & 0 deletions
29
...s/src/main/java/org/apache/nifi/processors/aws/wag/client/GenericApiGatewayException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.nifi.processors.aws.wag.client; | ||
|
|
||
| import com.amazonaws.AmazonServiceException; | ||
|
|
||
| public class GenericApiGatewayException extends AmazonServiceException { | ||
| public GenericApiGatewayException(String errorMessage) { | ||
| super(errorMessage); | ||
| } | ||
|
|
||
| public GenericApiGatewayException(String errorMessage, Exception cause) { | ||
| super(errorMessage, cause); | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
...ors/src/main/java/org/apache/nifi/processors/aws/wag/client/GenericApiGatewayRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.nifi.processors.aws.wag.client; | ||
|
|
||
| import com.amazonaws.http.HttpMethodName; | ||
| import java.io.InputStream; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class GenericApiGatewayRequest { | ||
|
|
||
| private final HttpMethodName httpMethod; | ||
| private final String resourcePath; | ||
| private final InputStream body; | ||
| private final Map<String, String> headers; | ||
| private final Map<String, List<String>> parameters; | ||
|
|
||
| public GenericApiGatewayRequest(HttpMethodName httpMethod, String resourcePath, | ||
| InputStream body, Map<String, String> headers, | ||
| Map<String, List<String>> parameters) { | ||
| this.httpMethod = httpMethod; | ||
| this.resourcePath = resourcePath; | ||
| this.body = body; | ||
| this.headers = headers; | ||
| this.parameters = parameters; | ||
| } | ||
|
|
||
| public HttpMethodName getHttpMethod() { | ||
| return httpMethod; | ||
| } | ||
|
|
||
| public String getResourcePath() { | ||
| return resourcePath; | ||
| } | ||
|
|
||
| public InputStream getBody() { | ||
| return body; | ||
| } | ||
|
|
||
| public Map<String, String> getHeaders() { | ||
| return headers; | ||
| } | ||
|
|
||
| public Map<String, List<String>> getParameters() { | ||
| return parameters; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to just add the API key to
headerswithout making a new map and then alwaysreturn headers;?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done