Search in sources :

Example 6 with ListenerConfiguration

use of org.wso2.transport.http.netty.config.ListenerConfiguration in project carbon-apimgt by wso2.

the class RestApiUtil method getHost.

public static String getHost(String protocol) {
    TransportsConfiguration transportsConfiguration = YAMLTransportConfigurationBuilder.build();
    Set<ListenerConfiguration> listenerConfigurationSet = transportsConfiguration.getListenerConfigurations();
    String host = apimConfigurations.getHostname();
    if (!apimConfigurations.isReverseProxyEnabled()) {
        if (HTTP.equals(protocol)) {
            for (ListenerConfiguration listenerConfiguration : listenerConfigurationSet) {
                if (HTTP.equals(listenerConfiguration.getScheme())) {
                    host = host.concat(":").concat(String.valueOf(listenerConfiguration.getPort()));
                    break;
                }
            }
        } else {
            for (ListenerConfiguration listenerConfiguration : listenerConfigurationSet) {
                if (HTTPS.equals(listenerConfiguration.getScheme())) {
                    host = host.concat(":").concat(String.valueOf(listenerConfiguration.getPort()));
                }
            }
        }
    }
    return host;
}
Also used : ListenerConfiguration(org.wso2.transport.http.netty.config.ListenerConfiguration) TransportsConfiguration(org.wso2.transport.http.netty.config.TransportsConfiguration)

Example 7 with ListenerConfiguration

use of org.wso2.transport.http.netty.config.ListenerConfiguration in project ballerina by ballerina-lang.

the class HttpUtil method extractHttpsConfig.

private static void extractHttpsConfig(Annotation configInfo, Set<ListenerConfiguration> listenerConfSet) {
    // Retrieve secure port from either http of ws configuration annotation.
    AnnAttrValue httpsPortAttrVal;
    if (configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_HTTPS_PORT) == null) {
        httpsPortAttrVal = configInfo.getAnnAttrValue(WebSocketConstants.ANN_CONFIG_ATTR_WSS_PORT);
    } else {
        httpsPortAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_HTTPS_PORT);
    }
    AnnAttrValue keyStoreFileAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_KEY_STORE_FILE);
    AnnAttrValue keyStorePasswordAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_KEY_STORE_PASS);
    AnnAttrValue certPasswordAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_CERT_PASS);
    AnnAttrValue trustStoreFileAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_TRUST_STORE_FILE);
    AnnAttrValue trustStorePasswordAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_TRUST_STORE_PASS);
    AnnAttrValue sslVerifyClientAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_SSL_VERIFY_CLIENT);
    AnnAttrValue sslEnabledProtocolsAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_SSL_ENABLED_PROTOCOLS);
    AnnAttrValue ciphersAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_CIPHERS);
    AnnAttrValue sslProtocolAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_SSL_PROTOCOL);
    AnnAttrValue hostAttrVal = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_HOST);
    AnnAttrValue certificateValidationEnabledAttrValue = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_VALIDATE_CERT_ENABLED);
    AnnAttrValue cacheSizeAttrValue = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_CACHE_SIZE);
    AnnAttrValue cacheValidityPeriodAttrValue = configInfo.getAnnAttrValue(HttpConstants.ANN_CONFIG_ATTR_CACHE_VALIDITY_PERIOD);
    ListenerConfiguration listenerConfiguration = new ListenerConfiguration();
    if (httpsPortAttrVal != null && httpsPortAttrVal.getIntValue() > 0) {
        listenerConfiguration.setPort(Math.toIntExact(httpsPortAttrVal.getIntValue()));
        listenerConfiguration.setScheme(HttpConstants.PROTOCOL_HTTPS);
        if (hostAttrVal != null && hostAttrVal.getStringValue() != null) {
            listenerConfiguration.setHost(hostAttrVal.getStringValue());
        } else {
            listenerConfiguration.setHost(HttpConstants.HTTP_DEFAULT_HOST);
        }
        if (keyStoreFileAttrVal == null || keyStoreFileAttrVal.getStringValue() == null) {
            // TODO get from language pack, and add location
            throw new BallerinaConnectorException("Keystore location must be provided for secure connection");
        }
        if (keyStorePasswordAttrVal == null || keyStorePasswordAttrVal.getStringValue() == null) {
            // TODO get from language pack, and add location
            throw new BallerinaConnectorException("Keystore password value must be provided for secure connection");
        }
        if (certPasswordAttrVal == null || certPasswordAttrVal.getStringValue() == null) {
            // TODO get from language pack, and add location
            throw new BallerinaConnectorException("Certificate password value must be provided for secure connection");
        }
        if ((trustStoreFileAttrVal == null || trustStoreFileAttrVal.getStringValue() == null) && sslVerifyClientAttrVal != null) {
            // TODO get from language pack, and add location
            throw new BallerinaException("Truststore location must be provided to enable Mutual SSL");
        }
        if ((trustStorePasswordAttrVal == null || trustStorePasswordAttrVal.getStringValue() == null) && sslVerifyClientAttrVal != null) {
            // TODO get from language pack, and add location
            throw new BallerinaException("Truststore password value must be provided to enable Mutual SSL");
        }
        listenerConfiguration.setTLSStoreType(HttpConstants.PKCS_STORE_TYPE);
        listenerConfiguration.setKeyStoreFile(keyStoreFileAttrVal.getStringValue());
        listenerConfiguration.setKeyStorePass(keyStorePasswordAttrVal.getStringValue());
        listenerConfiguration.setCertPass(certPasswordAttrVal.getStringValue());
        if (sslVerifyClientAttrVal != null) {
            listenerConfiguration.setVerifyClient(sslVerifyClientAttrVal.getStringValue());
        }
        if (trustStoreFileAttrVal != null) {
            listenerConfiguration.setTrustStoreFile(trustStoreFileAttrVal.getStringValue());
        }
        if (trustStorePasswordAttrVal != null) {
            listenerConfiguration.setTrustStorePass(trustStorePasswordAttrVal.getStringValue());
        }
        if (certificateValidationEnabledAttrValue != null && certificateValidationEnabledAttrValue.getBooleanValue()) {
            listenerConfiguration.setValidateCertEnabled(certificateValidationEnabledAttrValue.getBooleanValue());
            if (cacheSizeAttrValue != null) {
                listenerConfiguration.setCacheSize((int) cacheSizeAttrValue.getIntValue());
            }
            if (cacheValidityPeriodAttrValue != null) {
                listenerConfiguration.setCacheValidityPeriod((int) cacheValidityPeriodAttrValue.getIntValue());
            }
        }
        List<Parameter> serverParams = new ArrayList<>();
        Parameter serverCiphers;
        if (sslEnabledProtocolsAttrVal != null && sslEnabledProtocolsAttrVal.getStringValue() != null) {
            serverCiphers = new Parameter(HttpConstants.ANN_CONFIG_ATTR_SSL_ENABLED_PROTOCOLS, sslEnabledProtocolsAttrVal.getStringValue());
            serverParams.add(serverCiphers);
        }
        if (ciphersAttrVal != null && ciphersAttrVal.getStringValue() != null) {
            serverCiphers = new Parameter(HttpConstants.ANN_CONFIG_ATTR_CIPHERS, ciphersAttrVal.getStringValue());
            serverParams.add(serverCiphers);
        }
        if (!serverParams.isEmpty()) {
            listenerConfiguration.setParameters(serverParams);
        }
        if (sslProtocolAttrVal != null) {
            listenerConfiguration.setSSLProtocol(sslProtocolAttrVal.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) ArrayList(java.util.ArrayList) Parameter(org.wso2.transport.http.netty.config.Parameter) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 8 with ListenerConfiguration

use of org.wso2.transport.http.netty.config.ListenerConfiguration in project ballerina by ballerina-lang.

the class HttpConnectionManager method getDefaultListenerConfiugrationSet.

public Set<ListenerConfiguration> getDefaultListenerConfiugrationSet() {
    Set<ListenerConfiguration> listenerConfigurationSet = new HashSet<>();
    for (ListenerConfiguration listenerConfiguration : trpConfig.getListenerConfigurations()) {
        listenerConfiguration.setId(listenerConfiguration.getHost() == null ? "0.0.0.0" : listenerConfiguration.getHost() + ":" + listenerConfiguration.getPort());
        listenerConfigurationSet.add(listenerConfiguration);
    }
    return listenerConfigurationSet;
}
Also used : ListenerConfiguration(org.wso2.transport.http.netty.config.ListenerConfiguration) HashSet(java.util.HashSet)

Example 9 with ListenerConfiguration

use of org.wso2.transport.http.netty.config.ListenerConfiguration in project ballerina by ballerina-lang.

the class InitEndpoint method getListerConfig.

private ListenerConfiguration getListerConfig(Struct endpointConfig) {
    String host = endpointConfig.getStringField(HttpConstants.ENDPOINT_CONFIG_HOST);
    long port = endpointConfig.getIntField(HttpConstants.ENDPOINT_CONFIG_PORT);
    String keepAlive = endpointConfig.getEnumField(HttpConstants.ENDPOINT_CONFIG_KEEP_ALIVE);
    String transferEncoding = endpointConfig.getEnumField(HttpConstants.ENDPOINT_CONFIG_TRANSFER_ENCODING);
    String chunking = endpointConfig.getEnumField(HttpConstants.ENDPOINT_CONFIG_CHUNKING);
    Struct sslConfig = endpointConfig.getStructField(HttpConstants.ENDPOINT_CONFIG_SECURE_SOCKET);
    String httpVersion = endpointConfig.getStringField(HttpConstants.ENDPOINT_CONFIG_VERSION);
    Struct requestLimits = endpointConfig.getStructField(HttpConstants.ENDPOINT_REQUEST_LIMITS);
    ListenerConfiguration listenerConfiguration = new ListenerConfiguration();
    if (host == null || host.isEmpty()) {
        listenerConfiguration.setHost(HttpConstants.HTTP_DEFAULT_HOST);
    } else {
        listenerConfiguration.setHost(host);
    }
    listenerConfiguration.setPort(Math.toIntExact(port));
    listenerConfiguration.setKeepAliveConfig(HttpUtil.getKeepAliveConfig(keepAlive));
    // chunking. Once we start supporting gzip, deflate, etc, we need to parse down the config.
    if ((!transferEncoding.isEmpty()) && !HttpConstants.ANN_CONFIG_ATTR_CHUNKING.equalsIgnoreCase(transferEncoding)) {
        throw new BallerinaConnectorException("Unsupported configuration found for Transfer-Encoding : " + transferEncoding);
    }
    listenerConfiguration.setChunkConfig(HttpUtil.getChunkConfig(chunking));
    // Set Request validation limits.
    if (requestLimits != null) {
        setRequestSizeValidationConfig(requestLimits, listenerConfiguration);
    }
    // Set HTTP version
    if (httpVersion != null) {
        listenerConfiguration.setVersion(httpVersion);
    }
    if (sslConfig != null) {
        return setSslConfig(sslConfig, listenerConfiguration);
    }
    listenerConfiguration.setServerHeader(getServerName());
    return listenerConfiguration;
}
Also used : BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) ListenerConfiguration(org.wso2.transport.http.netty.config.ListenerConfiguration) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Aggregations

BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)6 ListenerConfiguration (org.wso2.transport.http.netty.config.ListenerConfiguration)6 Struct (org.ballerinalang.connector.api.Struct)3 BStruct (org.ballerinalang.model.values.BStruct)3 ArrayList (java.util.ArrayList)2 AnnAttrValue (org.ballerinalang.connector.api.AnnAttrValue)2 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)2 Parameter (org.wso2.transport.http.netty.config.Parameter)2 RequestSizeValidationConfig (org.wso2.transport.http.netty.config.RequestSizeValidationConfig)2 ServerConnector (org.wso2.transport.http.netty.contract.ServerConnector)2 HashSet (java.util.HashSet)1 HTTPServicesRegistry (org.ballerinalang.net.http.HTTPServicesRegistry)1 WebSocketServicesRegistry (org.ballerinalang.net.http.WebSocketServicesRegistry)1 ChunkConfig (org.wso2.transport.http.netty.config.ChunkConfig)1 KeepAliveConfig (org.wso2.transport.http.netty.config.KeepAliveConfig)1 TransportsConfiguration (org.wso2.transport.http.netty.config.TransportsConfiguration)1