use of org.ballerinalang.net.grpc.config.EndpointConfiguration in project ballerina by ballerina-lang.
the class InitEndpoint method execute.
@Override
public void execute(Context context) {
try {
Struct serviceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
Struct serviceEndpointConfig = serviceEndpoint.getStructField(EndpointConstants.ENDPOINT_CONFIG);
EndpointConfiguration configuration = EndpointUtils.getEndpointConfiguration(serviceEndpointConfig);
io.grpc.ServerBuilder serverBuilder;
if (configuration.getSslConfig() != null) {
SslContext sslCtx = new SSLHandlerFactory(configuration.getSslConfig()).createHttp2TLSContextForServer();
serverBuilder = GrpcServicesBuilder.initService(configuration, sslCtx);
} else {
serverBuilder = GrpcServicesBuilder.initService(configuration, null);
}
serviceEndpoint.addNativeData(SERVICE_BUILDER, serverBuilder);
context.setReturnValues();
} catch (Throwable throwable) {
BStruct err = getConnectorError(context, throwable);
context.setError(err);
}
}
use of org.ballerinalang.net.grpc.config.EndpointConfiguration in project ballerina by ballerina-lang.
the class InitEndpoint method execute.
@Override
public void execute(Context context) {
try {
Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
// Creating client endpoint with channel as native data.
Struct endpointConfig = clientEndpoint.getStructField(EndpointConstants.ENDPOINT_CONFIG);
EndpointConfiguration configuration = EndpointUtils.getEndpointConfiguration(endpointConfig);
ManagedChannel channel;
if (configuration.getSslConfig() == null) {
channel = ManagedChannelBuilder.forAddress(configuration.getHost(), configuration.getPort()).usePlaintext(true).build();
} else {
SslContext sslContext = new SSLHandlerFactory(configuration.getSslConfig()).createHttp2TLSContextForClient();
channel = NettyChannelBuilder.forAddress(generateSocketAddress(configuration.getHost(), configuration.getPort())).flowControlWindow(65 * 1024).maxInboundMessageSize(MAX_MESSAGE_SIZE).sslContext(sslContext).build();
}
clientEndpoint.addNativeData(CHANNEL_KEY, channel);
} catch (Throwable throwable) {
BStruct errorStruct = MessageUtils.getConnectorError(context, throwable);
context.setError(errorStruct);
}
}
use of org.ballerinalang.net.grpc.config.EndpointConfiguration in project ballerina by ballerina-lang.
the class EndpointUtils method getEndpointConfiguration.
/**
* Generate server endpoint object from service endpoint configuration struct.
*
* @param endpointConfig service endpoint configuration struct.
* @return server endpoint object
*/
public static EndpointConfiguration getEndpointConfiguration(Struct endpointConfig) {
String host = endpointConfig.getStringField(EndpointConstants.ENDPOINT_CONFIG_HOST);
long port = endpointConfig.getIntField(EndpointConstants.ENDPOINT_CONFIG_PORT);
Struct sslConfig = endpointConfig.getStructField(EndpointConstants.ENDPOINT_CONFIG_SSL);
EndpointConfiguration endpointConfiguration = new EndpointConfiguration();
if (host == null || host.isEmpty()) {
endpointConfiguration.setHost(EndpointConstants.HTTP_DEFAULT_HOST);
} else {
endpointConfiguration.setHost(host);
}
endpointConfiguration.setPort(Math.toIntExact(port));
if (sslConfig != null && (!EMPTY_STRING.equals(sslConfig.getStringField(TRUST_FILE)) || !EMPTY_STRING.equals(sslConfig.getStringField(KEY_FILE)))) {
endpointConfiguration.setScheme(EndpointConstants.PROTOCOL_HTTPS);
endpointConfiguration.setSslConfig(getSslConfig(sslConfig));
return endpointConfiguration;
}
return endpointConfiguration;
}
Aggregations