use of org.ballerinalang.connector.api.Value in project ballerina by ballerina-lang.
the class StructImpl method getArrayField.
@Override
public Value[] getArrayField(String fieldName) {
final BNewArray refField = (BNewArray) value.getRefField(getFieldIndex(fieldName));
if (refField == null) {
return null;
}
List<Value> list = new ArrayList<>();
final BIterator bIterator = refField.newIterator();
while (bIterator.hasNext()) {
list.add(ValueImpl.createValue(bIterator.getNext(1)[0]));
}
return list.toArray(new Value[0]);
}
use of org.ballerinalang.connector.api.Value in project ballerina by ballerina-lang.
the class StructImpl method getMapField.
@Override
public Map<String, Value> getMapField(String fieldName) {
final BMap refField = (BMap) value.getRefField(getFieldIndex(fieldName));
if (refField == null) {
return null;
}
Map<String, Value> valueMap = new LinkedHashMap<>();
final BIterator bIterator = refField.newIterator();
while (bIterator.hasNext()) {
final BValue[] next = bIterator.getNext(2);
valueMap.put(next[0].stringValue(), ValueImpl.createValue(next[1]));
}
return valueMap;
}
use of org.ballerinalang.connector.api.Value in project ballerina by ballerina-lang.
the class CreateHttpClient method populateSenderConfigurationOptions.
private void populateSenderConfigurationOptions(SenderConfiguration senderConfiguration, Struct clientEndpointConfig) {
ProxyServerConfiguration proxyServerConfiguration = null;
boolean followRedirect = false;
int maxRedirectCount = DEFAULT_MAX_REDIRECT_COUNT;
Struct followRedirects = clientEndpointConfig.getStructField(HttpConstants.FOLLOW_REDIRECT_STRUCT_REFERENCE);
if (followRedirects != null) {
followRedirect = followRedirects.getBooleanField(HttpConstants.FOLLOW_REDIRECT_ENABLED);
maxRedirectCount = (int) followRedirects.getIntField(HttpConstants.FOLLOW_REDIRECT_MAXCOUNT);
}
Struct secureSocket = null;
Value[] targetServices = clientEndpointConfig.getArrayField(HttpConstants.TARGET_SERVICES);
for (Value targetService : targetServices) {
secureSocket = targetService.getStructValue().getStructField(HttpConstants.ENDPOINT_CONFIG_SECURE_SOCKET);
if (secureSocket != null) {
Struct trustStore = secureSocket.getStructField(HttpConstants.ENDPOINT_CONFIG_TRUST_STORE);
Struct keyStore = secureSocket.getStructField(HttpConstants.ENDPOINT_CONFIG_KEY_STORE);
Struct protocols = secureSocket.getStructField(HttpConstants.ENDPOINT_CONFIG_PROTOCOLS);
Struct validateCert = secureSocket.getStructField(HttpConstants.ENDPOINT_CONFIG_VALIDATE_CERT);
List<Parameter> clientParams = new ArrayList<>();
if (trustStore != null) {
String trustStoreFile = trustStore.getStringField(HttpConstants.FILE_PATH);
if (StringUtils.isNotBlank(trustStoreFile)) {
senderConfiguration.setTrustStoreFile(trustStoreFile);
}
String trustStorePassword = trustStore.getStringField(HttpConstants.PASSWORD);
if (StringUtils.isNotBlank(trustStorePassword)) {
senderConfiguration.setTrustStorePass(trustStorePassword);
}
}
if (keyStore != null) {
String keyStoreFile = keyStore.getStringField(HttpConstants.FILE_PATH);
if (StringUtils.isNotBlank(keyStoreFile)) {
senderConfiguration.setKeyStoreFile(keyStoreFile);
}
String keyStorePassword = keyStore.getStringField(HttpConstants.PASSWORD);
if (StringUtils.isNotBlank(keyStorePassword)) {
senderConfiguration.setKeyStorePassword(keyStorePassword);
}
}
if (protocols != null) {
String sslEnabledProtocols = protocols.getStringField(HttpConstants.ENABLED_PROTOCOLS);
if (StringUtils.isNotBlank(sslEnabledProtocols)) {
Parameter clientProtocols = new Parameter(HttpConstants.SSL_ENABLED_PROTOCOLS, sslEnabledProtocols);
clientParams.add(clientProtocols);
}
String sslProtocol = protocols.getStringField(HttpConstants.PROTOCOL_VERSION);
if (StringUtils.isNotBlank(sslProtocol)) {
senderConfiguration.setSSLProtocol(sslProtocol);
}
}
if (validateCert != null) {
boolean validateCertEnabled = validateCert.getBooleanField(HttpConstants.ENABLE);
int cacheSize = (int) validateCert.getIntField(HttpConstants.SSL_CONFIG_CACHE_SIZE);
int cacheValidityPeriod = (int) validateCert.getIntField(HttpConstants.SSL_CONFIG_CACHE_VALIDITY_PERIOD);
senderConfiguration.setValidateCertEnabled(validateCertEnabled);
if (cacheValidityPeriod != 0) {
senderConfiguration.setCacheValidityPeriod(cacheValidityPeriod);
}
if (cacheSize != 0) {
senderConfiguration.setCacheSize(cacheSize);
}
}
boolean hostNameVerificationEnabled = secureSocket.getBooleanField(HttpConstants.SSL_CONFIG_HOST_NAME_VERIFICATION_ENABLED);
senderConfiguration.setHostNameVerificationEnabled(hostNameVerificationEnabled);
String ciphers = secureSocket.getStringField(HttpConstants.SSL_CONFIG_CIPHERS);
if (StringUtils.isNotBlank(ciphers)) {
Parameter clientCiphers = new Parameter(HttpConstants.CIPHERS, ciphers);
clientParams.add(clientCiphers);
}
String enableSessionCreation = String.valueOf(secureSocket.getBooleanField(HttpConstants.SSL_CONFIG_ENABLE_SESSION_CREATION));
Parameter clientEnableSessionCreation = new Parameter(HttpConstants.SSL_CONFIG_ENABLE_SESSION_CREATION, enableSessionCreation);
clientParams.add(clientEnableSessionCreation);
if (!clientParams.isEmpty()) {
senderConfiguration.setParameters(clientParams);
}
}
}
Struct proxy = clientEndpointConfig.getStructField(HttpConstants.PROXY_STRUCT_REFERENCE);
if (proxy != null) {
String proxyHost = proxy.getStringField(HttpConstants.PROXY_HOST);
int proxyPort = (int) proxy.getIntField(HttpConstants.PROXY_PORT);
String proxyUserName = proxy.getStringField(HttpConstants.PROXY_USERNAME);
String proxyPassword = proxy.getStringField(HttpConstants.PROXY_PASSWORD);
try {
proxyServerConfiguration = new ProxyServerConfiguration(proxyHost, proxyPort);
} catch (UnknownHostException e) {
throw new BallerinaConnectorException("Failed to resolve host" + proxyHost, e);
}
if (!proxyUserName.isEmpty()) {
proxyServerConfiguration.setProxyUsername(proxyUserName);
}
if (!proxyPassword.isEmpty()) {
proxyServerConfiguration.setProxyPassword(proxyPassword);
}
senderConfiguration.setProxyServerConfiguration(proxyServerConfiguration);
}
senderConfiguration.setFollowRedirect(followRedirect);
senderConfiguration.setMaxRedirectCount(maxRedirectCount);
// For the moment we don't have to pass it down to transport as we only support
// chunking. Once we start supporting gzip, deflate, etc, we need to parse down the config.
String transferEncoding = clientEndpointConfig.getEnumField(HttpConstants.CLIENT_EP_TRNASFER_ENCODING);
if (transferEncoding != null && !HttpConstants.ANN_CONFIG_ATTR_CHUNKING.equalsIgnoreCase(transferEncoding)) {
throw new BallerinaConnectorException("Unsupported configuration found for Transfer-Encoding : " + transferEncoding);
}
String chunking = clientEndpointConfig.getEnumField(HttpConstants.CLIENT_EP_CHUNKING);
senderConfiguration.setChunkingConfig(HttpUtil.getChunkConfig(chunking));
long endpointTimeout = clientEndpointConfig.getIntField(HttpConstants.CLIENT_EP_ENDPOINT_TIMEOUT);
if (endpointTimeout < 0 || !isInteger(endpointTimeout)) {
throw new BallerinaConnectorException("invalid idle timeout: " + endpointTimeout);
}
senderConfiguration.setSocketIdleTimeout((int) endpointTimeout);
boolean isKeepAlive = clientEndpointConfig.getBooleanField(HttpConstants.CLIENT_EP_IS_KEEP_ALIVE);
senderConfiguration.setKeepAlive(isKeepAlive);
String httpVersion = clientEndpointConfig.getStringField(HttpConstants.CLIENT_EP_HTTP_VERSION);
if (httpVersion != null) {
senderConfiguration.setHttpVersion(httpVersion);
}
String forwardedExtension = clientEndpointConfig.getStringField(HttpConstants.CLIENT_EP_FORWARDED);
senderConfiguration.setForwardedExtensionConfig(HttpUtil.getForwardedExtensionConfig(forwardedExtension));
}
use of org.ballerinalang.connector.api.Value in project ballerina by ballerina-lang.
the class SQLDatasource method populatePropertiesMap.
private BMap<String, BRefType> populatePropertiesMap(Struct options) {
Map<String, Value> dataSourceConfigMap = options.getMapField(Constants.Options.DATASOURCE_PROPERTIES);
BMap<String, BRefType> mapProperties = null;
if (dataSourceConfigMap.size() > 0) {
mapProperties = new BMap<>();
for (Map.Entry<String, Value> entry : dataSourceConfigMap.entrySet()) {
Value propValue = entry.getValue();
BRefType dataValue = null;
switch(propValue.getType()) {
case INT:
dataValue = new BInteger(propValue.getIntValue());
break;
case FLOAT:
dataValue = new BFloat(propValue.getFloatValue());
break;
case BOOLEAN:
dataValue = new BBoolean(propValue.getBooleanValue());
break;
case NULL:
break;
default:
dataValue = new BString(propValue.getStringValue());
}
mapProperties.put(entry.getKey(), dataValue);
}
}
return mapProperties;
}
use of org.ballerinalang.connector.api.Value in project ballerina by ballerina-lang.
the class HTTPServicesRegistry method registerWebSocketUpgradePath.
private void registerWebSocketUpgradePath(ProgramFile programFile, Struct websocketConfig, String basePath) {
String upgradePath = sanitizeBasePath(websocketConfig.getStringField(HttpConstants.ANN_WEBSOCKET_ATTR_UPGRADE_PATH));
Value serviceType = websocketConfig.getTypeField(WebSocketConstants.WEBSOCKET_UPGRADE_SERVICE_CONFIG);
String uri = basePath.concat(upgradePath);
WebSocketService service = new WebSocketService(BLangConnectorSPIUtil.getServiceFromType(programFile, serviceType));
webSocketServicesRegistry.addUpgradableServiceByName(service, uri);
}
Aggregations