Search in sources :

Example 21 with BallerinaConnectorException

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

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

the class SignatureParams method validatePathParam.

private void validatePathParam(List<ParamDetail> paramDetails) {
    for (ParamDetail param : paramDetails) {
        int varTag = param.getVarType().getTag();
        if (varTag != TypeTags.STRING_TAG && varTag != TypeTags.INT_TAG && varTag != TypeTags.BOOLEAN_TAG && varTag != TypeTags.FLOAT_TAG) {
            throw new BallerinaConnectorException("incompatible resource signature parameter type");
        }
        paramCount++;
    }
    this.pathParams = paramDetails;
}
Also used : ParamDetail(org.ballerinalang.connector.api.ParamDetail) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException)

Example 23 with BallerinaConnectorException

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

the class WebSocketServicesRegistry method registerService.

public void registerService(WebSocketService service) {
    String basePath = findFullWebSocketUpgradePath(service);
    if (basePath == null) {
        basePath = "/";
    }
    basePath = urlDecode(basePath);
    service.setBasePath(basePath);
    // TODO: Add websocket services to the service registry when service creation get available.
    try {
        getUriTemplate().parse(basePath, service, new WebSocketDataElementFactory());
    } catch (URITemplateException | UnsupportedEncodingException e) {
        throw new BallerinaConnectorException(e.getMessage());
    }
    logger.info("Service deployed : " + service.getName() + " with context " + basePath);
}
Also used : URITemplateException(org.ballerinalang.net.uri.URITemplateException) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 24 with BallerinaConnectorException

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

the class CreateHttpClient method execute.

@Override
public void execute(Context context) {
    BStruct configBStruct = (BStruct) context.getRefArgument(0);
    Struct clientEndpointConfig = BLangConnectorSPIUtil.toStruct(configBStruct);
    String url = context.getStringArgument(0);
    HttpConnectionManager connectionManager = HttpConnectionManager.getInstance();
    String scheme;
    if (url.startsWith("http://")) {
        scheme = HttpConstants.PROTOCOL_HTTP;
    } else if (url.startsWith("https://")) {
        scheme = HttpConstants.PROTOCOL_HTTPS;
    } else {
        throw new BallerinaException("malformed URL: " + url);
    }
    Map<String, Object> properties = HTTPConnectorUtil.getTransportProperties(connectionManager.getTransportConfig());
    SenderConfiguration senderConfiguration = HTTPConnectorUtil.getSenderConfiguration(connectionManager.getTransportConfig(), scheme);
    if (connectionManager.isHTTPTraceLoggerEnabled()) {
        senderConfiguration.setHttpTraceLogEnabled(true);
    }
    senderConfiguration.setTLSStoreType(HttpConstants.PKCS_STORE_TYPE);
    populateSenderConfigurationOptions(senderConfiguration, clientEndpointConfig);
    Struct connectionThrottling = clientEndpointConfig.getStructField(HttpConstants.CONNECTION_THROTTLING_STRUCT_REFERENCE);
    if (connectionThrottling != null) {
        long maxActiveConnections = connectionThrottling.getIntField(HttpConstants.CONNECTION_THROTTLING_MAX_ACTIVE_CONNECTIONS);
        if (!isInteger(maxActiveConnections)) {
            throw new BallerinaConnectorException("invalid maxActiveConnections value: " + maxActiveConnections);
        }
        senderConfiguration.getPoolConfiguration().setMaxActivePerPool((int) maxActiveConnections);
        long waitTime = connectionThrottling.getIntField(HttpConstants.CONNECTION_THROTTLING_WAIT_TIME);
        senderConfiguration.getPoolConfiguration().setMaxWaitTime(waitTime);
    }
    HttpClientConnector httpClientConnector = httpConnectorFactory.createHttpClientConnector(properties, senderConfiguration);
    BStruct httpClient = BLangConnectorSPIUtil.createBStruct(context.getProgramFile(), HTTP_PACKAGE_PATH, HTTP_CLIENT, url, clientEndpointConfig);
    httpClient.addNativeData(HttpConstants.HTTP_CLIENT, httpClientConnector);
    httpClient.addNativeData(HttpConstants.CLIENT_ENDPOINT_CONFIG, clientEndpointConfig);
    context.setReturnValues(httpClient);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) HttpClientConnector(org.wso2.transport.http.netty.contract.HttpClientConnector) HttpConnectionManager(org.ballerinalang.net.http.HttpConnectionManager) SenderConfiguration(org.wso2.transport.http.netty.config.SenderConfiguration) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct)

Example 25 with BallerinaConnectorException

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

the class EndpointUtils method getSslConfig.

/**
 * Generate SSL configs.
 *
 * @param sslConfig service endpoint configuration struct.
 * @return SSL beans.
 */
private static SSLConfig getSslConfig(Struct sslConfig) {
    String keyStoreFile = sslConfig.getStringField(EndpointConstants.SSL_CONFIG_KEY_STORE_FILE);
    String keyStorePassword = sslConfig.getStringField(EndpointConstants.SSL_CONFIG_KEY_STORE_PASSWORD);
    String trustStoreFile = sslConfig.getStringField(EndpointConstants.SSL_CONFIG_STRUST_STORE_FILE);
    String trustStorePassword = sslConfig.getStringField(EndpointConstants.SSL_CONFIG_STRUST_STORE_PASSWORD);
    String sslVerifyClient = sslConfig.getStringField(EndpointConstants.SSL_CONFIG_SSL_VERIFY_CLIENT);
    String certPassword = sslConfig.getStringField(EndpointConstants.SSL_CONFIG_CERT_PASSWORD);
    String sslProtocol = sslConfig.getStringField(EndpointConstants.SSL_CONFIG_SSL_PROTOCOL);
    String tlsStoreType = sslConfig.getStringField(EndpointConstants.SSL_TLS_STORE_TYPE);
    sslProtocol = (sslProtocol != null && !EMPTY_STRING.equals(sslProtocol)) ? sslProtocol : "TLS";
    tlsStoreType = (tlsStoreType != null && !EMPTY_STRING.equals(tlsStoreType)) ? tlsStoreType : "PKCS12";
    boolean validateCertificateEnabled = sslConfig.getBooleanField(EndpointConstants.SSL_CONFIG_VALIDATE_CERT_ENABLED);
    long cacheSize = sslConfig.getIntField(EndpointConstants.SSL_CONFIG_CACHE_SIZE);
    long cacheValidationPeriod = sslConfig.getIntField(EndpointConstants.SSL_CONFIG_CACHE_VALIDITY_PERIOD);
    if (keyStoreFile == null) {
        // TODO get from language pack, and add location
        throw new BallerinaConnectorException("Keystore location must be provided for secure connection");
    }
    if (keyStorePassword == null) {
        // TODO get from language pack, and add location
        throw new BallerinaConnectorException("Keystore password value must be provided for secure connection");
    }
    if (certPassword == null) {
        // TODO get from language pack, and add location
        throw new BallerinaConnectorException("Certificate password value must be provided for secure connection");
    }
    if ((trustStoreFile == null) && sslVerifyClient != null) {
        // TODO get from language pack, and add location
        throw new BallerinaException("Truststore location must be provided to enable Mutual SSL");
    }
    if ((trustStorePassword == null) && sslVerifyClient != null) {
        // TODO get from language pack, and add location
        throw new BallerinaException("Truststore password value must be provided to enable Mutual SSL");
    }
    SSLConfig config = new SSLConfig();
    config.setTLSStoreType(EndpointConstants.PKCS_STORE_TYPE);
    config.setKeyStore(new File(substituteVariables(keyStoreFile)));
    config.setKeyStorePass(keyStorePassword);
    config.setCertPass(certPassword);
    config.setTLSStoreType(tlsStoreType);
    config.setSslVerifyClient(sslVerifyClient);
    if (trustStoreFile != null) {
        config.setTrustStore(new File(substituteVariables(trustStoreFile)));
        config.setTrustStorePass(trustStorePassword);
    }
    config.setValidateCertificateEnabled(validateCertificateEnabled);
    if (validateCertificateEnabled) {
        config.setCacheSize(Math.toIntExact(cacheSize));
        config.setCacheValidityPeriod(Math.toIntExact(cacheValidationPeriod));
    }
    config.setSslProtocol(sslProtocol);
    return config;
}
Also used : SSLConfig(org.ballerinalang.net.grpc.ssl.SSLConfig) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) File(java.io.File)

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