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();
}
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);
}
}
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());
}
}
}
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));
}
}
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");
}
Aggregations