Search in sources :

Example 6 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 serviceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
        // Creating server connector
        Struct serviceEndpointConfig = serviceEndpoint.getStructField(HttpConstants.SERVICE_ENDPOINT_CONFIG);
        ListenerConfiguration listenerConfiguration = getListerConfig(serviceEndpointConfig);
        ServerConnector httpServerConnector = HttpConnectionManager.getInstance().createHttpServerConnector(listenerConfiguration);
        serviceEndpoint.addNativeData(HttpConstants.HTTP_SERVER_CONNECTOR, httpServerConnector);
        // Adding service registries to native data
        WebSocketServicesRegistry webSocketServicesRegistry = new WebSocketServicesRegistry();
        HTTPServicesRegistry httpServicesRegistry = new HTTPServicesRegistry(webSocketServicesRegistry);
        serviceEndpoint.addNativeData(HttpConstants.HTTP_SERVICE_REGISTRY, httpServicesRegistry);
        serviceEndpoint.addNativeData(HttpConstants.WS_SERVICE_REGISTRY, webSocketServicesRegistry);
        // set filters
        setFilters(serviceEndpointConfig, serviceEndpoint);
        context.setReturnValues((BValue) null);
    } catch (Throwable throwable) {
        BStruct errorStruct = HttpUtil.getHttpConnectorError(context, throwable);
        context.setReturnValues(errorStruct);
    }
}
Also used : ServerConnector(org.wso2.transport.http.netty.contract.ServerConnector) BStruct(org.ballerinalang.model.values.BStruct) HTTPServicesRegistry(org.ballerinalang.net.http.HTTPServicesRegistry) WebSocketServicesRegistry(org.ballerinalang.net.http.WebSocketServicesRegistry) ListenerConfiguration(org.wso2.transport.http.netty.config.ListenerConfiguration) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Example 7 with Struct

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

the class Stop method execute.

@Override
public void execute(Context context) {
    Struct serverEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
    getServerConnector(serverEndpoint).stop();
    context.setReturnValues();
}
Also used : Struct(org.ballerinalang.connector.api.Struct)

Example 8 with Struct

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

the class HttpResource method buildHttpResource.

public static HttpResource buildHttpResource(Resource resource, HttpService httpService) {
    HttpResource httpResource = new HttpResource(resource, httpService);
    Annotation resourceConfigAnnotation = getResourceConfigAnnotation(resource);
    if (resourceConfigAnnotation == null) {
        if (log.isDebugEnabled()) {
            log.debug("resourceConfig not specified in the Resource instance, using default sub path");
        }
        httpResource.setPath(resource.getName());
        httpResource.prepareAndValidateSignatureParams();
        return httpResource;
    }
    Struct resourceConfig = resourceConfigAnnotation.getValue();
    httpResource.setPath(resourceConfig.getStringField(PATH_FIELD));
    httpResource.setMethods(getAsStringList(resourceConfig.getArrayField(METHODS_FIELD)));
    httpResource.setConsumes(getAsStringList(resourceConfig.getArrayField(CONSUMES_FIELD)));
    httpResource.setProduces(getAsStringList(resourceConfig.getArrayField(PRODUCES_FIELD)));
    httpResource.setEntityBodyAttributeValue(resourceConfig.getStringField(BODY_FIELD));
    httpResource.setCorsHeaders(CorsHeaders.buildCorsHeaders(resourceConfig.getStructField(CORS_FIELD)));
    httpResource.setTransactionInfectable(resourceConfig.getBooleanField(TRANSACTION_INFECTABLE_FIELD));
    processResourceCors(httpResource, httpService);
    httpResource.prepareAndValidateSignatureParams();
    return httpResource;
}
Also used : Annotation(org.ballerinalang.connector.api.Annotation) Struct(org.ballerinalang.connector.api.Struct)

Example 9 with Struct

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

the class HttpService method buildHttpService.

public static HttpService buildHttpService(Service service) {
    HttpService httpService = new HttpService(service);
    Annotation serviceConfigAnnotation = getHttpServiceConfigAnnotation(service);
    if (serviceConfigAnnotation == null) {
        log.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()));
    } else {
        Struct serviceConfig = serviceConfigAnnotation.getValue();
        httpService.setBasePath(serviceConfig.getStringField(BASE_PATH_FIELD));
        httpService.setCompression(serviceConfig.getEnumField(COMPRESSION_FIELD));
        httpService.setCorsHeaders(CorsHeaders.buildCorsHeaders(serviceConfig.getStructField(CORS_FIELD)));
        httpService.setWebSocketUpgradeConfig(serviceConfig.getStructField(WEBSOCKET_UPGRADE_FIELD));
    }
    List<HttpResource> resources = new ArrayList<>();
    for (Resource resource : httpService.getBallerinaService().getResources()) {
        HttpResource httpResource = HttpResource.buildHttpResource(resource, httpService);
        try {
            httpService.getUriTemplate().parse(httpResource.getPath(), httpResource, new HttpResourceElementFactory());
        } catch (URITemplateException | UnsupportedEncodingException e) {
            throw new BallerinaConnectorException(e.getMessage());
        }
        resources.add(httpResource);
    }
    httpService.setResources(resources);
    httpService.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(httpService));
    return httpService;
}
Also used : URITemplateException(org.ballerinalang.net.uri.URITemplateException) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) ArrayList(java.util.ArrayList) Resource(org.ballerinalang.connector.api.Resource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Annotation(org.ballerinalang.connector.api.Annotation) Struct(org.ballerinalang.connector.api.Struct)

Example 10 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 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);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) 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)

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