Search in sources :

Example 1 with Service

use of org.ballerinalang.connector.api.Service in project ballerina by ballerina-lang.

the class NonListeningRegister method execute.

@Override
public void execute(Context context) {
    Service service = BLangConnectorSPIUtil.getServiceRegistered(context);
    Struct serviceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
    HTTPServicesRegistry httpServicesRegistry = getHttpServicesRegistry(serviceEndpoint);
    WebSocketServicesRegistry webSocketServicesRegistry = getWebSocketServicesRegistry(serviceEndpoint);
    // TODO: In HTTP to WebSocket upgrade register WebSocket service in WebSocketServiceRegistry
    if (HttpConstants.HTTP_SERVICE_ENDPOINT_NAME.equals(service.getEndpointName())) {
        httpServicesRegistry.registerService(service);
    }
    if (WebSocketConstants.WEBSOCKET_ENDPOINT_NAME.equals(service.getEndpointName())) {
        WebSocketService webSocketService = new WebSocketService(service);
        webSocketServicesRegistry.registerService(webSocketService);
    }
    context.setReturnValues();
}
Also used : HTTPServicesRegistry(org.ballerinalang.net.http.HTTPServicesRegistry) WebSocketServicesRegistry(org.ballerinalang.net.http.WebSocketServicesRegistry) Service(org.ballerinalang.connector.api.Service) WebSocketService(org.ballerinalang.net.http.WebSocketService) WebSocketService(org.ballerinalang.net.http.WebSocketService) Struct(org.ballerinalang.connector.api.Struct)

Example 2 with Service

use of org.ballerinalang.connector.api.Service in project ballerina by ballerina-lang.

the class HttpUtil method setCompressionHeaders.

private static void setCompressionHeaders(Context context, HTTPCarbonMessage requestMsg, HTTPCarbonMessage outboundResponseMsg) {
    Service serviceInstance = BLangConnectorSPIUtil.getService(context.getProgramFile(), context.getServiceInfo().getType());
    Annotation configAnnot = getServiceConfigAnnotation(serviceInstance, PROTOCOL_PACKAGE_HTTP);
    if (configAnnot == null) {
        return;
    }
    String contentEncoding = outboundResponseMsg.getHeaders().get(HttpHeaderNames.CONTENT_ENCODING);
    if (contentEncoding != null) {
        return;
    }
    String compressionValue = configAnnot.getValue().getEnumField(ANN_CONFIG_ATTR_COMPRESSION);
    String acceptEncodingValue = requestMsg.getHeaders().get(HttpHeaderNames.ACCEPT_ENCODING);
    if (ALWAYS.equalsIgnoreCase(compressionValue)) {
        if (acceptEncodingValue == null || HTTP_TRANSFER_ENCODING_IDENTITY.equals(acceptEncodingValue)) {
            outboundResponseMsg.getHeaders().set(HttpHeaderNames.CONTENT_ENCODING, ENCODING_GZIP);
        }
    } else if (NEVER.equalsIgnoreCase(compressionValue)) {
        outboundResponseMsg.getHeaders().set(HttpHeaderNames.CONTENT_ENCODING, HTTP_TRANSFER_ENCODING_IDENTITY);
    }
}
Also used : Service(org.ballerinalang.connector.api.Service) BString(org.ballerinalang.model.values.BString) Annotation(org.ballerinalang.connector.api.Annotation)

Example 3 with Service

use of org.ballerinalang.connector.api.Service in project ballerina by ballerina-lang.

the class StreamingExecute method execute.

@Override
public void execute(Context context) {
    BStruct serviceStub = (BStruct) context.getRefArgument(SERVICE_STUB_REF_INDEX);
    if (serviceStub == null) {
        notifyErrorReply(context, "Error while getting connector. gRPC Client connector " + "is not initialized properly");
        return;
    }
    Object connectionStub = serviceStub.getNativeData(SERVICE_STUB);
    if (connectionStub == null) {
        notifyErrorReply(context, "Error while getting connection stub. gRPC Client connector " + "is not initialized properly");
        return;
    }
    String methodName = context.getStringArgument(0);
    if (methodName == null) {
        notifyErrorReply(context, "Error while processing the request. RPC endpoint doesn't " + "set properly");
        return;
    }
    com.google.protobuf.Descriptors.MethodDescriptor methodDescriptor = MessageRegistry.getInstance().getMethodDescriptor(methodName);
    if (methodDescriptor == null) {
        notifyErrorReply(context, "No registered method descriptor for '" + methodName + "'");
        return;
    }
    if (connectionStub instanceof GrpcNonBlockingStub) {
        GrpcNonBlockingStub grpcNonBlockingStub = (GrpcNonBlockingStub) connectionStub;
        BTypeDescValue serviceType = (BTypeDescValue) context.getRefArgument(1);
        Service callbackService = BLangConnectorSPIUtil.getServiceFromType(context.getProgramFile(), getTypeField(serviceType));
        try {
            MethodDescriptor.MethodType methodType = getMethodType(methodDescriptor);
            DefaultStreamObserver responseObserver = new DefaultStreamObserver(callbackService);
            StreamObserver<Message> requestSender;
            if (methodType.equals(MethodDescriptor.MethodType.CLIENT_STREAMING)) {
                requestSender = grpcNonBlockingStub.executeClientStreaming(responseObserver, methodName);
            } else if (methodType.equals(MethodDescriptor.MethodType.BIDI_STREAMING)) {
                requestSender = grpcNonBlockingStub.executeBidiStreaming(responseObserver, methodName);
            } else {
                notifyErrorReply(context, "Error while executing the client call. Method type " + methodType.name() + " not supported");
                return;
            }
            BStruct connStruct = createStruct(context, CLIENT_CONNECTION);
            connStruct.addNativeData(REQUEST_SENDER, requestSender);
            connStruct.addNativeData(REQUEST_MESSAGE_DEFINITION, methodDescriptor.getInputType());
            BStruct clientEndpoint = (BStruct) serviceStub.getNativeData(CLIENT_END_POINT);
            clientEndpoint.addNativeData(CLIENT_CONNECTION, connStruct);
            context.setReturnValues(clientEndpoint);
        } catch (RuntimeException | GrpcClientException e) {
            notifyErrorReply(context, "gRPC Client Connector Error :" + e.getMessage());
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Message(org.ballerinalang.net.grpc.Message) DefaultStreamObserver(org.ballerinalang.net.grpc.stubs.DefaultStreamObserver) Service(org.ballerinalang.connector.api.Service) MethodDescriptor(io.grpc.MethodDescriptor) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) GrpcClientException(org.ballerinalang.net.grpc.exception.GrpcClientException) GrpcNonBlockingStub(org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub)

Example 4 with Service

use of org.ballerinalang.connector.api.Service in project ballerina by ballerina-lang.

the class Register method execute.

@Override
public void execute(Context context) {
    BStruct serviceEndpoint = (BStruct) context.getRefArgument(SERVICE_ENDPOINT_INDEX);
    Service service = BLangConnectorSPIUtil.getServiceRegistered(context);
    io.grpc.ServerBuilder serverBuilder = getServiceBuilder(serviceEndpoint);
    try {
        registerService(serverBuilder, service);
        context.setReturnValues();
    } catch (GrpcServerException e) {
        context.setError(MessageUtils.getConnectorError(context, e));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Service(org.ballerinalang.connector.api.Service) GrpcServicesBuilder.registerService(org.ballerinalang.net.grpc.GrpcServicesBuilder.registerService) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 5 with Service

use of org.ballerinalang.connector.api.Service in project ballerina by ballerina-lang.

the class NonBlockingExecute method execute.

@Override
public void execute(Context context) {
    BStruct serviceStub = (BStruct) context.getRefArgument(SERVICE_STUB_REF_INDEX);
    if (serviceStub == null) {
        notifyErrorReply(context, "Error while getting connector. gRPC Client connector is " + "not initialized properly");
        return;
    }
    Object connectionStub = serviceStub.getNativeData(SERVICE_STUB);
    if (connectionStub == null) {
        notifyErrorReply(context, "Error while getting connection stub. gRPC Client connector " + "is not initialized properly");
        return;
    }
    String methodName = context.getStringArgument(0);
    if (methodName == null) {
        notifyErrorReply(context, "Error while processing the request. RPC endpoint doesn't " + "set properly");
        return;
    }
    com.google.protobuf.Descriptors.MethodDescriptor methodDescriptor = MessageRegistry.getInstance().getMethodDescriptor(methodName);
    if (methodDescriptor == null) {
        notifyErrorReply(context, "No registered method descriptor for '" + methodName + "'");
        return;
    }
    if (connectionStub instanceof GrpcNonBlockingStub) {
        BValue payloadBValue = context.getRefArgument(1);
        Message requestMsg = MessageUtils.generateProtoMessage(payloadBValue, methodDescriptor.getInputType());
        GrpcNonBlockingStub grpcNonBlockingStub = (GrpcNonBlockingStub) connectionStub;
        BTypeDescValue serviceType = (BTypeDescValue) context.getRefArgument(2);
        Service callbackService = BLangConnectorSPIUtil.getServiceFromType(context.getProgramFile(), getTypeField(serviceType));
        try {
            MethodDescriptor.MethodType methodType = getMethodType(methodDescriptor);
            if (methodType.equals(MethodDescriptor.MethodType.UNARY)) {
                grpcNonBlockingStub.executeUnary(requestMsg, new DefaultStreamObserver(callbackService), methodName);
            } else if (methodType.equals(MethodDescriptor.MethodType.SERVER_STREAMING)) {
                grpcNonBlockingStub.executeServerStreaming(requestMsg, new DefaultStreamObserver(callbackService), methodName);
            } else {
                notifyErrorReply(context, "Error while executing the client call. Method type " + methodType.name() + " not supported");
                return;
            }
            context.setReturnValues();
            return;
        } catch (RuntimeException | GrpcClientException e) {
            notifyErrorReply(context, "gRPC Client Connector Error :" + e.getMessage());
            return;
        }
    }
    notifyErrorReply(context, "Error while processing the request message. Connection Sub " + "type not supported");
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Message(org.ballerinalang.net.grpc.Message) DefaultStreamObserver(org.ballerinalang.net.grpc.stubs.DefaultStreamObserver) BValue(org.ballerinalang.model.values.BValue) Service(org.ballerinalang.connector.api.Service) MethodDescriptor(io.grpc.MethodDescriptor) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) GrpcClientException(org.ballerinalang.net.grpc.exception.GrpcClientException) GrpcNonBlockingStub(org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub)

Aggregations

Service (org.ballerinalang.connector.api.Service)9 Struct (org.ballerinalang.connector.api.Struct)4 BStruct (org.ballerinalang.model.values.BStruct)4 WebSocketService (org.ballerinalang.net.http.WebSocketService)3 MethodDescriptor (io.grpc.MethodDescriptor)2 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)2 Message (org.ballerinalang.net.grpc.Message)2 GrpcClientException (org.ballerinalang.net.grpc.exception.GrpcClientException)2 DefaultStreamObserver (org.ballerinalang.net.grpc.stubs.DefaultStreamObserver)2 GrpcNonBlockingStub (org.ballerinalang.net.grpc.stubs.GrpcNonBlockingStub)2 HTTPServicesRegistry (org.ballerinalang.net.http.HTTPServicesRegistry)2 WebSocketServicesRegistry (org.ballerinalang.net.http.WebSocketServicesRegistry)2 Annotation (org.ballerinalang.connector.api.Annotation)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 Value (org.ballerinalang.connector.api.Value)1 BString (org.ballerinalang.model.values.BString)1 BValue (org.ballerinalang.model.values.BValue)1 GrpcServicesBuilder.registerService (org.ballerinalang.net.grpc.GrpcServicesBuilder.registerService)1 GrpcServerException (org.ballerinalang.net.grpc.exception.GrpcServerException)1 WebSubServicesRegistry (org.ballerinalang.net.websub.WebSubServicesRegistry)1