Search in sources :

Example 1 with ServerConnector

use of org.wso2.transport.http.netty.contract.ServerConnector 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 2 with ServerConnector

use of org.wso2.transport.http.netty.contract.ServerConnector in project ballerina by ballerina-lang.

the class Register method startServerConnector.

private void startServerConnector(Struct serviceEndpoint, HTTPServicesRegistry httpServicesRegistry, WebSocketServicesRegistry webSocketServicesRegistry) {
    ServerConnector serverConnector = getServerConnector(serviceEndpoint);
    ServerConnectorFuture serverConnectorFuture = serverConnector.start();
    HashSet<FilterHolder> filterHolder = getFilters(serviceEndpoint);
    serverConnectorFuture.setHttpConnectorListener(new BallerinaHTTPConnectorListener(httpServicesRegistry, filterHolder));
    serverConnectorFuture.setWSConnectorListener(new WebSocketServerConnectorListener(webSocketServicesRegistry));
    serverConnectorFuture.setPortBindingEventListener(new HttpConnectorPortBindingListener());
    try {
        serverConnectorFuture.sync();
    } catch (Throwable ex) {
        throw new BallerinaException("failed to start server connector '" + serverConnector.getConnectorID() + "': " + ex.getMessage(), ex);
    }
    serviceEndpoint.addNativeData(HttpConstants.CONNECTOR_STARTED, true);
}
Also used : ServerConnector(org.wso2.transport.http.netty.contract.ServerConnector) WebSocketServerConnectorListener(org.ballerinalang.net.http.WebSocketServerConnectorListener) HttpConnectorPortBindingListener(org.ballerinalang.net.http.HttpConnectorPortBindingListener) ServerConnectorFuture(org.wso2.transport.http.netty.contract.ServerConnectorFuture) BallerinaHTTPConnectorListener(org.ballerinalang.net.http.BallerinaHTTPConnectorListener) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 3 with ServerConnector

use of org.wso2.transport.http.netty.contract.ServerConnector in project ballerina by ballerina-lang.

the class WebSocketServerConnectorListener method onMessage.

@Override
public void onMessage(WebSocketInitMessage webSocketInitMessage) {
    HTTPCarbonMessage msg = new HTTPCarbonMessage(((DefaultWebSocketInitMessage) webSocketInitMessage).getHttpRequest());
    Map<String, String> pathParams = new HashMap<>();
    WebSocketService wsService = WebSocketDispatcher.findService(servicesRegistry, pathParams, webSocketInitMessage, msg);
    BStruct serviceEndpoint = BLangConnectorSPIUtil.createBStruct(wsService.getResources()[0].getResourceInfo().getServiceInfo().getPackageInfo().getProgramFile(), PROTOCOL_PACKAGE_HTTP, WEBSOCKET_ENDPOINT);
    BStruct serverConnector = WebSocketUtil.createAndGetBStruct(wsService.getResources()[0]);
    serverConnector.addNativeData(WebSocketConstants.WEBSOCKET_MESSAGE, webSocketInitMessage);
    serverConnector.addNativeData(WebSocketConstants.WEBSOCKET_SERVICE, wsService);
    serviceEndpoint.setRefField(SERVICE_ENDPOINT_CONNECTION_INDEX, serverConnector);
    serviceEndpoint.setRefField(3, new BMap());
    serverConnector.addNativeData(WEBSOCKET_ENDPOINT, serviceEndpoint);
    Map<String, String> upgradeHeaders = webSocketInitMessage.getHeaders();
    BMap<String, BString> bUpgradeHeaders = new BMap<>();
    upgradeHeaders.forEach((key, value) -> bUpgradeHeaders.put(key, new BString(value)));
    serviceEndpoint.setRefField(4, bUpgradeHeaders);
    Resource onUpgradeResource = wsService.getResourceByName(WebSocketConstants.RESOURCE_NAME_ON_UPGRADE);
    if (onUpgradeResource != null) {
        Semaphore semaphore = new Semaphore(0);
        AtomicBoolean isResourceExeSuccessful = new AtomicBoolean(false);
        BStruct inRequest = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), PROTOCOL_PACKAGE_HTTP, HttpConstants.REQUEST);
        BStruct inRequestEntity = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.ENTITY);
        BStruct mediaType = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), org.ballerinalang.mime.util.Constants.PROTOCOL_PACKAGE_MIME, Constants.MEDIA_TYPE);
        BStruct cacheControlStruct = BLangConnectorSPIUtil.createBStruct(WebSocketUtil.getProgramFile(wsService.getResources()[0]), PROTOCOL_PACKAGE_HTTP, REQUEST_CACHE_CONTROL);
        RequestCacheControlStruct requestCacheControl = new RequestCacheControlStruct(cacheControlStruct);
        HttpUtil.populateInboundRequest(inRequest, inRequestEntity, mediaType, msg, requestCacheControl);
        List<ParamDetail> paramDetails = onUpgradeResource.getParamDetails();
        BValue[] bValues = new BValue[paramDetails.size()];
        bValues[0] = serviceEndpoint;
        bValues[1] = inRequest;
        WebSocketDispatcher.setPathParams(bValues, paramDetails, pathParams, 2);
        Tracer tracer = TraceManagerWrapper.newTracer(null, false);
        upgradeHeaders.entrySet().stream().filter(c -> c.getKey().startsWith(TraceConstants.TRACE_PREFIX)).forEach(e -> tracer.addProperty(e.getKey(), e.getValue()));
        Executor.submit(onUpgradeResource, new CallableUnitCallback() {

            @Override
            public void notifySuccess() {
                isResourceExeSuccessful.set(true);
                semaphore.release();
            }

            @Override
            public void notifyFailure(BStruct error) {
                ErrorHandlerUtils.printError("error: " + BLangVMErrors.getPrintableStackTrace(error));
                semaphore.release();
            }
        }, null, tracer, bValues);
        try {
            semaphore.acquire();
            if (isResourceExeSuccessful.get() && !webSocketInitMessage.isCancelled() && !webSocketInitMessage.isHandshakeStarted()) {
                WebSocketUtil.handleHandshake(wsService, null, serverConnector);
            }
        } catch (InterruptedException e) {
            throw new BallerinaConnectorException("Connection interrupted during handshake");
        }
    } else {
        WebSocketUtil.handleHandshake(wsService, null, serverConnector);
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) WebSocketTextMessage(org.wso2.transport.http.netty.contract.websocket.WebSocketTextMessage) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback) RequestCacheControlStruct(org.ballerinalang.net.http.caching.RequestCacheControlStruct) HashMap(java.util.HashMap) WebSocketControlMessage(org.wso2.transport.http.netty.contract.websocket.WebSocketControlMessage) WebSocketInitMessage(org.wso2.transport.http.netty.contract.websocket.WebSocketInitMessage) BLangConnectorSPIUtil(org.ballerinalang.connector.api.BLangConnectorSPIUtil) Executor(org.ballerinalang.connector.api.Executor) Resource(org.ballerinalang.connector.api.Resource) WebSocketBinaryMessage(org.wso2.transport.http.netty.contract.websocket.WebSocketBinaryMessage) WebSocketConnectorListener(org.wso2.transport.http.netty.contract.websocket.WebSocketConnectorListener) DefaultWebSocketInitMessage(org.wso2.transport.http.netty.contractimpl.websocket.message.DefaultWebSocketInitMessage) WEBSOCKET_ENDPOINT(org.ballerinalang.net.http.WebSocketConstants.WEBSOCKET_ENDPOINT) BString(org.ballerinalang.model.values.BString) WebSocketCloseMessage(org.wso2.transport.http.netty.contract.websocket.WebSocketCloseMessage) Map(java.util.Map) BStruct(org.ballerinalang.model.values.BStruct) BMap(org.ballerinalang.model.values.BMap) Logger(org.slf4j.Logger) Semaphore(java.util.concurrent.Semaphore) ErrorHandlerUtils(org.ballerinalang.services.ErrorHandlerUtils) BLangVMErrors(org.ballerinalang.bre.bvm.BLangVMErrors) Constants(org.ballerinalang.mime.util.Constants) REQUEST_CACHE_CONTROL(org.ballerinalang.net.http.HttpConstants.REQUEST_CACHE_CONTROL) SERVICE_ENDPOINT_CONNECTION_INDEX(org.ballerinalang.net.http.HttpConstants.SERVICE_ENDPOINT_CONNECTION_INDEX) TraceManagerWrapper(org.ballerinalang.util.tracer.TraceManagerWrapper) TraceConstants(org.ballerinalang.util.tracer.TraceConstants) PROTOCOL_PACKAGE_HTTP(org.ballerinalang.net.http.HttpConstants.PROTOCOL_PACKAGE_HTTP) List(java.util.List) Tracer(org.ballerinalang.util.tracer.Tracer) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BValue(org.ballerinalang.model.values.BValue) BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) HashMap(java.util.HashMap) BMap(org.ballerinalang.model.values.BMap) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) Tracer(org.ballerinalang.util.tracer.Tracer) Resource(org.ballerinalang.connector.api.Resource) BString(org.ballerinalang.model.values.BString) Semaphore(java.util.concurrent.Semaphore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParamDetail(org.ballerinalang.connector.api.ParamDetail) RequestCacheControlStruct(org.ballerinalang.net.http.caching.RequestCacheControlStruct) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 4 with ServerConnector

use of org.wso2.transport.http.netty.contract.ServerConnector in project ballerina by ballerina-lang.

the class HttpConnectionManager method createHttpServerConnector.

public ServerConnector createHttpServerConnector(ListenerConfiguration listenerConfig) {
    String listenerInterface = listenerConfig.getHost() + ":" + listenerConfig.getPort();
    HttpServerConnectorContext httpServerConnectorContext = serverConnectorPool.get(listenerInterface);
    if (httpServerConnectorContext != null) {
        if (checkForConflicts(listenerConfig, httpServerConnectorContext)) {
            throw new BallerinaConnectorException("Conflicting configuration detected for listener " + "configuration id " + listenerConfig.getId());
        }
        httpServerConnectorContext.incrementReferenceCount();
        return httpServerConnectorContext.getServerConnector();
    }
    if (isHTTPTraceLoggerEnabled()) {
        listenerConfig.setHttpTraceLogEnabled(true);
    }
    serverBootstrapConfiguration = HTTPConnectorUtil.getServerBootstrapConfiguration(trpConfig.getTransportProperties());
    ServerConnector serverConnector = httpConnectorFactory.createServerConnector(serverBootstrapConfiguration, listenerConfig);
    httpServerConnectorContext = new HttpServerConnectorContext(serverConnector, listenerConfig);
    serverConnectorPool.put(serverConnector.getConnectorID(), httpServerConnectorContext);
    httpServerConnectorContext.incrementReferenceCount();
    addStartupDelayedHTTPServerConnector(listenerInterface, serverConnector);
    return serverConnector;
}
Also used : ServerConnector(org.wso2.transport.http.netty.contract.ServerConnector) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException)

Example 5 with ServerConnector

use of org.wso2.transport.http.netty.contract.ServerConnector in project ballerina by ballerina-lang.

the class CancelUpgradeToWebSocket method execute.

@Override
public void execute(Context context) {
    BStruct serverConnector = (BStruct) context.getRefArgument(0);
    int statusCode = (int) context.getIntArgument(0);
    String reason = context.getStringArgument(0);
    WebSocketInitMessage initMessage = (WebSocketInitMessage) serverConnector.getNativeData(WebSocketConstants.WEBSOCKET_MESSAGE);
    initMessage.cancelHandShake(statusCode, reason);
    context.setReturnValues();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) WebSocketInitMessage(org.wso2.transport.http.netty.contract.websocket.WebSocketInitMessage)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)4 ServerConnector (org.wso2.transport.http.netty.contract.ServerConnector)4 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)3 Struct (org.ballerinalang.connector.api.Struct)2 HttpConnectorPortBindingListener (org.ballerinalang.net.http.HttpConnectorPortBindingListener)2 ServerConnectorFuture (org.wso2.transport.http.netty.contract.ServerConnectorFuture)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BLangVMErrors (org.ballerinalang.bre.bvm.BLangVMErrors)1 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)1 BLangConnectorSPIUtil (org.ballerinalang.connector.api.BLangConnectorSPIUtil)1 Executor (org.ballerinalang.connector.api.Executor)1 ParamDetail (org.ballerinalang.connector.api.ParamDetail)1 Resource (org.ballerinalang.connector.api.Resource)1 BLogManager (org.ballerinalang.logging.BLogManager)1 TraceLogConfigurationException (org.ballerinalang.logging.exceptions.TraceLogConfigurationException)1