Search in sources :

Example 1 with ServerConnectionContinuationHandler

use of software.amazon.awssdk.crt.eventstream.ServerConnectionContinuationHandler in project aws-iot-device-sdk-java-v2 by aws.

the class TestIpcServiceHandler method getOperationHandler.

@Override
public Function<OperationContinuationHandlerContext, ? extends ServerConnectionContinuationHandler> getOperationHandler(final String operationName) {
    final EventStreamRPCServiceModel svcModel = this.serviceModel;
    System.out.println("Operation handler retrieved for operation name: " + operationName);
    return (context) -> {
        final TestOperationContinuationHandler handler = new TestOperationContinuationHandler(context) {

            @Override
            public OperationModelContext getOperationModelContext() {
                return svcModel.getOperationModelContext(operationName);
            }

            @Override
            protected void onStreamClosed() {
                streamClosed = true;
            }

            @Override
            public EventStreamJsonMessage handleRequest(EventStreamJsonMessage request) {
                System.out.println("Handling request...");
                return requestHandler.apply(request);
            }

            @Override
            public void handleStreamEvent(EventStreamJsonMessage streamRequestEvent) {
                System.out.println("Handling request stream event...");
            }
        };
        continuationHandlerQueue.offer(new AbstractMap.SimpleImmutableEntry<>(operationName, handler));
        return handler;
    };
}
Also used : OperationModelContext(software.amazon.awssdk.eventstreamrpc.OperationModelContext) EventStreamRPCServiceModel(software.amazon.awssdk.eventstreamrpc.EventStreamRPCServiceModel) EventStreamRPCServiceHandler(software.amazon.awssdk.eventstreamrpc.EventStreamRPCServiceHandler) OperationContinuationHandlerContext(software.amazon.awssdk.eventstreamrpc.OperationContinuationHandlerContext) Collection(java.util.Collection) Set(java.util.Set) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) Function(java.util.function.Function) HashSet(java.util.HashSet) AbstractMap(java.util.AbstractMap) ServerConnectionContinuationHandler(software.amazon.awssdk.crt.eventstream.ServerConnectionContinuationHandler) Map(java.util.Map) OperationContinuationHandler(software.amazon.awssdk.eventstreamrpc.OperationContinuationHandler) Optional(java.util.Optional) Queue(java.util.Queue) LinkedList(java.util.LinkedList) EventStreamRPCServiceModel(software.amazon.awssdk.eventstreamrpc.EventStreamRPCServiceModel) OperationModelContext(software.amazon.awssdk.eventstreamrpc.OperationModelContext) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage)

Example 2 with ServerConnectionContinuationHandler

use of software.amazon.awssdk.crt.eventstream.ServerConnectionContinuationHandler in project aws-iot-device-sdk-java-v2 by aws.

the class ServiceOperationMappingContinuationHandler method onIncomingStream.

@Override
protected ServerConnectionContinuationHandler onIncomingStream(ServerConnectionContinuation continuation, String operationName) {
    final OperationContinuationHandlerContext operationContext = new OperationContinuationHandlerContext(connection, continuation, authenticationData);
    final Function<OperationContinuationHandlerContext, ? extends ServerConnectionContinuationHandler> registeredOperationHandlerFn = serviceHandler.getOperationHandler(operationName);
    if (registeredOperationHandlerFn != null) {
        return registeredOperationHandlerFn.apply(operationContext);
    } else {
        return new ServerConnectionContinuationHandler(continuation) {

            @Override
            protected void onContinuationClosed() {
                close();
            }

            @Override
            protected void onContinuationMessage(List<Header> headers, byte[] payload, MessageType messageType, int messageFlags) {
                int responseMessageFlag = MessageFlags.TerminateStream.getByteValue();
                MessageType responseMessageType = MessageType.ApplicationError;
                String responsePayload = "{ \"error\": \"Unsupported Operation\", " + "\"message\": \"" + operationName + " is an unsupported operation.\" }";
                Header contentTypeHeader = Header.createHeader(":content-type", "application/json");
                List<Header> responseHeaders = new ArrayList<>();
                responseHeaders.add(contentTypeHeader);
                continuation.sendMessage(responseHeaders, responsePayload.getBytes(StandardCharsets.UTF_8), responseMessageType, responseMessageFlag);
            }
        };
    }
}
Also used : ServerConnectionContinuationHandler(software.amazon.awssdk.crt.eventstream.ServerConnectionContinuationHandler) Header(software.amazon.awssdk.crt.eventstream.Header) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MessageType(software.amazon.awssdk.crt.eventstream.MessageType)

Aggregations

ServerConnectionContinuationHandler (software.amazon.awssdk.crt.eventstream.ServerConnectionContinuationHandler)2 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Queue (java.util.Queue)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Header (software.amazon.awssdk.crt.eventstream.Header)1 MessageType (software.amazon.awssdk.crt.eventstream.MessageType)1 EventStreamRPCServiceHandler (software.amazon.awssdk.eventstreamrpc.EventStreamRPCServiceHandler)1 EventStreamRPCServiceModel (software.amazon.awssdk.eventstreamrpc.EventStreamRPCServiceModel)1 OperationContinuationHandler (software.amazon.awssdk.eventstreamrpc.OperationContinuationHandler)1 OperationContinuationHandlerContext (software.amazon.awssdk.eventstreamrpc.OperationContinuationHandlerContext)1 OperationModelContext (software.amazon.awssdk.eventstreamrpc.OperationModelContext)1 EventStreamJsonMessage (software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage)1