Search in sources :

Example 11 with BallerinaConnectorException

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

the class HttpUtil method extractBasicConfig.

@Deprecated
private static void extractBasicConfig(Annotation configInfo, Set<ListenerConfiguration> listenerConfSet) {
    AnnAttrValue hostAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_HOST);
    AnnAttrValue portAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_PORT);
    AnnAttrValue keepAliveAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_KEEP_ALIVE);
    AnnAttrValue transferEncoding = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_TRANSFER_ENCODING);
    AnnAttrValue chunking = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_CHUNKING);
    AnnAttrValue maxUriLength = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_MAXIMUM_URL_LENGTH);
    AnnAttrValue maxHeaderSize = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_MAXIMUM_HEADER_SIZE);
    AnnAttrValue maxEntityBodySize = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_MAXIMUM_ENTITY_BODY_SIZE);
    AnnAttrValue versionAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_HTTP_VERSION);
    ListenerConfiguration listenerConfiguration = new ListenerConfiguration();
    if (portAttrVal != null && portAttrVal.getIntValue() > 0) {
        listenerConfiguration.setPort(Math.toIntExact(portAttrVal.getIntValue()));
        listenerConfiguration.setScheme(HttpConstants.PROTOCOL_HTTP);
        if (hostAttrVal != null && hostAttrVal.getStringValue() != null) {
            listenerConfiguration.setHost(hostAttrVal.getStringValue());
        } else {
            listenerConfiguration.setHost(HttpConstants.HTTP_DEFAULT_HOST);
        }
        // chunking. Once we start supporting gzip, deflate, etc, we need to parse down the config.
        if (transferEncoding != null && !HttpConstants.ANN_CONFIG_ATTR_CHUNKING.equalsIgnoreCase(transferEncoding.getStringValue())) {
            throw new BallerinaConnectorException("Unsupported configuration found for Transfer-Encoding : " + transferEncoding.getStringValue());
        }
        if (chunking != null) {
            ChunkConfig chunkConfig = getChunkConfig(chunking.getStringValue());
            listenerConfiguration.setChunkConfig(chunkConfig);
        } else {
            listenerConfiguration.setChunkConfig(ChunkConfig.AUTO);
        }
        if (keepAliveAttrVal != null) {
            KeepAliveConfig keepAliveConfig = getKeepAliveConfig(keepAliveAttrVal.getStringValue());
            listenerConfiguration.setKeepAliveConfig(keepAliveConfig);
        } else {
            listenerConfiguration.setKeepAliveConfig(KeepAliveConfig.AUTO);
        }
        RequestSizeValidationConfig requestSizeValidationConfig = listenerConfiguration.getRequestSizeValidationConfig();
        if (maxUriLength != null) {
            if (maxUriLength.getIntValue() > 0) {
                requestSizeValidationConfig.setMaxUriLength(Math.toIntExact(maxUriLength.getIntValue()));
            } else {
                throw new BallerinaConnectorException("Invalid configuration found for maxUriLength : " + maxUriLength.getIntValue());
            }
        }
        if (maxHeaderSize != null) {
            if (maxHeaderSize.getIntValue() > 0) {
                requestSizeValidationConfig.setMaxHeaderSize(Math.toIntExact(maxHeaderSize.getIntValue()));
            } else {
                throw new BallerinaConnectorException("Invalid configuration found for maxHeaderSize : " + maxHeaderSize.getIntValue());
            }
        }
        if (maxEntityBodySize != null) {
            if (maxEntityBodySize.getIntValue() > 0) {
                requestSizeValidationConfig.setMaxEntityBodySize(Math.toIntExact(maxEntityBodySize.getIntValue()));
            } else {
                throw new BallerinaConnectorException("Invalid configuration found for maxEntityBodySize : " + maxEntityBodySize.getIntValue());
            }
        }
        if (versionAttrVal != null) {
            listenerConfiguration.setVersion(versionAttrVal.getStringValue());
        }
        listenerConfiguration.setId(getListenerInterface(listenerConfiguration.getHost(), listenerConfiguration.getPort()));
        listenerConfSet.add(listenerConfiguration);
    }
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) AnnAttrValue(org.ballerinalang.connector.api.AnnAttrValue) ListenerConfiguration(org.wso2.transport.http.netty.config.ListenerConfiguration) ChunkConfig(org.wso2.transport.http.netty.config.ChunkConfig) RequestSizeValidationConfig(org.wso2.transport.http.netty.config.RequestSizeValidationConfig) KeepAliveConfig(org.wso2.transport.http.netty.config.KeepAliveConfig)

Example 12 with BallerinaConnectorException

use of org.ballerinalang.connector.api.BallerinaConnectorException 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 BallerinaConnectorException

use of org.ballerinalang.connector.api.BallerinaConnectorException 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 14 with BallerinaConnectorException

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

the class WebSubDispatcher method findResource.

/**
 * This method finds the matching resource for the incoming request.
 *
 * @param servicesRegistry  the WebSubServicesRegistry including registered WebSub Services
 * @param httpCarbonMessage incoming message.
 * @return matching resource.
 */
static HttpResource findResource(WebSubServicesRegistry servicesRegistry, HTTPCarbonMessage httpCarbonMessage) {
    HttpResource resource = null;
    String protocol = (String) httpCarbonMessage.getProperty(HttpConstants.PROTOCOL);
    if (protocol == null) {
        throw new BallerinaConnectorException("protocol not defined in the incoming request");
    }
    try {
        HttpService service = HttpDispatcher.findService(servicesRegistry, httpCarbonMessage);
        if (service == null) {
            throw new BallerinaConnectorException("no service found to handle the service request");
        // Finer details of the errors are thrown from the dispatcher itself, ideally we shouldn't get here.
        }
        resource = WebSubResourceDispatcher.findResource(service, httpCarbonMessage);
    } catch (Throwable throwable) {
        handleError(httpCarbonMessage, throwable);
    }
    return resource;
}
Also used : HttpResource(org.ballerinalang.net.http.HttpResource) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) HttpService(org.ballerinalang.net.http.HttpService)

Example 15 with BallerinaConnectorException

use of org.ballerinalang.connector.api.BallerinaConnectorException 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

BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)32 Struct (org.ballerinalang.connector.api.Struct)9 BString (org.ballerinalang.model.values.BString)9 BStruct (org.ballerinalang.model.values.BStruct)9 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)7 HashMap (java.util.HashMap)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 URITemplateException (org.ballerinalang.net.uri.URITemplateException)4 BValue (org.ballerinalang.model.values.BValue)3 HttpResource (org.ballerinalang.net.http.HttpResource)3 URI (java.net.URI)2 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)2 AnnAttrValue (org.ballerinalang.connector.api.AnnAttrValue)2 BLangConnectorSPIUtil (org.ballerinalang.connector.api.BLangConnectorSPIUtil)2 Executor (org.ballerinalang.connector.api.Executor)2 ParamDetail (org.ballerinalang.connector.api.ParamDetail)2 Resource (org.ballerinalang.connector.api.Resource)2