Search in sources :

Example 11 with Struct

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);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) ManagedChannel(io.grpc.ManagedChannel) EndpointConfiguration(org.ballerinalang.net.grpc.config.EndpointConfiguration) SSLHandlerFactory(org.ballerinalang.net.grpc.ssl.SSLHandlerFactory) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct) SslContext(io.netty.handler.ssl.SslContext)

Example 12 with Struct

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());
    }
}
Also used : BLogManager(org.ballerinalang.logging.BLogManager) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) IOException(java.io.IOException) Struct(org.ballerinalang.connector.api.Struct)

Example 13 with Struct

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;
}
Also used : HttpResource(org.ballerinalang.net.http.HttpResource) ArrayList(java.util.ArrayList) HttpResource(org.ballerinalang.net.http.HttpResource) Resource(org.ballerinalang.connector.api.Resource) Annotation(org.ballerinalang.connector.api.Annotation) Struct(org.ballerinalang.connector.api.Struct)

Example 14 with Struct

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();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) WebSocketServicesRegistry(org.ballerinalang.net.http.WebSocketServicesRegistry) WebSubServicesRegistry(org.ballerinalang.net.websub.WebSubServicesRegistry) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Example 15 with Struct

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();
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) WsClientConnectorConfig(org.wso2.transport.http.netty.contract.websocket.WsClientConnectorConfig) HandshakeFuture(org.wso2.transport.http.netty.contract.websocket.HandshakeFuture) WebSocketClientConnector(org.wso2.transport.http.netty.contract.websocket.WebSocketClientConnector) WebSocketClientConnectorListener(org.ballerinalang.net.http.WebSocketClientConnectorListener) WebSocketService(org.ballerinalang.net.http.WebSocketService) HttpWsConnectorFactory(org.wso2.transport.http.netty.contract.HttpWsConnectorFactory) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Aggregations

Struct (org.ballerinalang.connector.api.Struct)26 BStruct (org.ballerinalang.model.values.BStruct)16 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)9 ArrayList (java.util.ArrayList)4 Annotation (org.ballerinalang.connector.api.Annotation)4 Service (org.ballerinalang.connector.api.Service)4 WebSocketService (org.ballerinalang.net.http.WebSocketService)4 WebSocketServicesRegistry (org.ballerinalang.net.http.WebSocketServicesRegistry)4 IOException (java.io.IOException)3 EndpointConfiguration (org.ballerinalang.net.grpc.config.EndpointConfiguration)3 HTTPServicesRegistry (org.ballerinalang.net.http.HTTPServicesRegistry)3 ResponseCacheControlStruct (org.ballerinalang.net.http.caching.ResponseCacheControlStruct)3 WebSubServicesRegistry (org.ballerinalang.net.websub.WebSubServicesRegistry)3 SslContext (io.netty.handler.ssl.SslContext)2 Resource (org.ballerinalang.connector.api.Resource)2 Value (org.ballerinalang.connector.api.Value)2 BLogManager (org.ballerinalang.logging.BLogManager)2 SSLHandlerFactory (org.ballerinalang.net.grpc.ssl.SSLHandlerFactory)2 RetryConfig (org.ballerinalang.net.http.RetryConfig)2 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)2