com.fleetpin.graphql.aws.lambda.LambdaSubscriptionControl Maven / Gradle / Ivy
/*
* Licensed 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 com.fleetpin.graphql.aws.lambda;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyResponseEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fleetpin.graphql.aws.lambda.subscription.SubscriptionConnectionInit;
import com.fleetpin.graphql.aws.lambda.subscription.SubscriptionResponseAccept;
import com.fleetpin.graphql.aws.lambda.subscription.SubscriptionResponseConnectionError;
import com.fleetpin.graphql.aws.lambda.subscription.SubscriptionResponseError;
import com.fleetpin.graphql.aws.lambda.subscription.SubscriptionStart;
import com.fleetpin.graphql.aws.lambda.subscription.SubscriptionStop;
import com.fleetpin.graphql.aws.lambda.subscription.SubscriptionTerminate;
import com.fleetpin.graphql.aws.lambda.subscription.WebsocketMessage;
import com.fleetpin.graphql.dynamodb.manager.DynamoDbManager;
import com.google.common.annotations.VisibleForTesting;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.GraphQL.Builder;
import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.apigatewaymanagementapi.ApiGatewayManagementApiClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
public abstract class LambdaSubscriptionControl implements RequestHandler{
private final ApiGatewayManagementApiClient gatewayApi;
private final GraphQL graph;
private final String subscriptionTable;
private final DynamoDbManager manager;
public LambdaSubscriptionControl(String subscriptionTable, String gatewayUri) throws Exception {
prepare();
this.manager = builderManager();
this.graph = buildGraphQL().subscriptionExecutionStrategy(new InterceptExecutionStrategy()).build();
this.subscriptionTable = subscriptionTable;
if(gatewayUri == null) {
gatewayApi = null;
}else {
URI endpoint = new URI(gatewayUri);
this.gatewayApi = ApiGatewayManagementApiClient.builder().endpointOverride(endpoint).build();
}
}
@Override
public APIGatewayV2ProxyResponseEvent handleRequest(APIGatewayV2ProxyRequestEvent input, Context context) {
try {
switch(input.getRequestContext().getEventType()) {
case "CONNECT" : {
break;
}
case "DISCONNECT" :
Map keyConditions = new HashMap<>();
keyConditions.put(":connectionId", AttributeValue.builder().s(input.getRequestContext().getConnectionId()).build());
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy