use of org.ballerinalang.connector.api.Struct 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.connector.api.Struct in project ballerina by ballerina-lang.
the class HTTPServicesRegistry method registerService.
/**
* Register a service into the map.
*
* @param service requested serviceInfo to be registered.
*/
public void registerService(Service service) {
String accessLogConfig = HttpConnectionManager.getInstance().getHttpAccessLoggerConfig();
if (accessLogConfig != null) {
try {
((BLogManager) BLogManager.getLogManager()).setHttpAccessLogHandler(accessLogConfig);
} catch (IOException e) {
throw new BallerinaConnectorException("Invalid file path: " + accessLogConfig, e);
}
}
HttpService httpService = HttpService.buildHttpService(service);
// TODO check with new method
// HttpUtil.populateKeepAliveAndCompressionStatus(service, annotation);
// TODO: Add websocket services to the service registry when service creation get available.
servicesInfoMap.put(httpService.getBasePath(), httpService);
logger.info("Service deployed : " + service.getName() + " with context " + httpService.getBasePath());
// basePath will get cached after registering service
sortedServiceURIs.add(httpService.getBasePath());
sortedServiceURIs.sort((basePath1, basePath2) -> basePath2.length() - basePath1.length());
// If WebSocket upgrade path is available, then register the name of the WebSocket service.
Struct websocketConfig = httpService.getWebSocketUpgradeConfig();
if (websocketConfig != null) {
registerWebSocketUpgradePath(WebSocketUtil.getProgramFile(httpService.getBallerinaService().getResources()[0]), websocketConfig, httpService.getBasePath());
}
}
use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class WebSubHttpService method buildWebSubSubscriberHttpService.
/**
* Builds the HTTP service representation of the service.
*
* @param service the service for which the HTTP representation is built
* @return the built HttpService representation
*/
static HttpService buildWebSubSubscriberHttpService(Service service) {
WebSubHttpService httpService = new WebSubHttpService(service);
Annotation serviceConfigAnnotation = getWebSubSubscriberServiceConfigAnnotation(service);
if (serviceConfigAnnotation == null) {
logger.debug("ServiceConfig not specified in the Service instance, using default base path");
// service name cannot start with / hence concat
httpService.setBasePath(HttpConstants.DEFAULT_BASE_PATH.concat(httpService.getName()));
return httpService;
}
Struct serviceConfig = serviceConfigAnnotation.getValue();
httpService.setBasePath(serviceConfig.getStringField(BASE_PATH_FIELD));
List<HttpResource> resources = new ArrayList<>();
for (Resource resource : httpService.getBallerinaService().getResources()) {
HttpResource httpResource = WebSubHttpResource.buildWebSubHttpResource(resource, httpService);
resources.add(httpResource);
}
httpService.setResources(resources);
httpService.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(httpService));
return httpService;
}
use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class InitWebSubSubscriberServiceEndpoint method execute.
@Override
public void execute(Context context) {
Struct subscriberServiceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
Struct serviceEndpoint = ConnectorSPIModelHelper.createStruct((BStruct) ((BStruct) (subscriberServiceEndpoint.getVMValue())).getRefField(1));
WebSubServicesRegistry webSubServicesRegistry = new WebSubServicesRegistry(new WebSocketServicesRegistry());
serviceEndpoint.addNativeData(WebSubSubscriberConstants.WEBSUB_SERVICE_REGISTRY, webSubServicesRegistry);
context.setReturnValues();
}
use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class Start method execute.
@Override
public void execute(Context context) {
HttpWsConnectorFactory connectorFactory = HttpUtil.createHttpWsConnectionFactory();
Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
Struct clientEndpointConfig = clientEndpoint.getStructField(HttpConstants.CLIENT_ENDPOINT_CONFIG);
Object configs = clientEndpointConfig.getNativeData(WebSocketConstants.CLIENT_CONNECTOR_CONFIGS);
if (configs == null || !(configs instanceof WsClientConnectorConfig)) {
throw new BallerinaConnectorException("Initialize the service before starting it");
}
WebSocketClientConnector clientConnector = connectorFactory.createWsClientConnector((WsClientConnectorConfig) configs);
WebSocketClientConnectorListener clientConnectorListener = new WebSocketClientConnectorListener();
Object serviceConfig = clientEndpointConfig.getNativeData(WebSocketConstants.CLIENT_SERVICE_CONFIG);
if (serviceConfig == null || !(serviceConfig instanceof WebSocketService)) {
throw new BallerinaConnectorException("Initialize the service before starting it");
}
WebSocketService wsService = (WebSocketService) serviceConfig;
HandshakeFuture handshakeFuture = clientConnector.connect(clientConnectorListener);
handshakeFuture.setHandshakeListener(new WsHandshakeListener(context, wsService, clientConnectorListener));
context.setReturnValues();
}
Aggregations