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);
}
}
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;
}
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);
}
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);
}
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;
}
Aggregations