use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class InitEndpoint method execute.
@Override
public void execute(Context context) {
Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
Struct clientEndpointConfig = clientEndpoint.getStructField(Constants.CLIENT_ENDPOINT_CONFIG);
// Extract parameters from the endpoint config
String database = clientEndpointConfig.getEnumField(Constants.EndpointConfig.DATABASE);
String host = clientEndpointConfig.getStringField(Constants.EndpointConfig.HOST);
int port = (int) clientEndpointConfig.getIntField(Constants.EndpointConfig.PORT);
String name = clientEndpointConfig.getStringField(Constants.EndpointConfig.NAME);
String username = clientEndpointConfig.getStringField(Constants.EndpointConfig.USERNAME);
String password = clientEndpointConfig.getStringField(Constants.EndpointConfig.PASSWORD);
Struct options = clientEndpointConfig.getStructField(Constants.EndpointConfig.OPTIONS);
SQLDatasource datasource = new SQLDatasource();
datasource.init(options, database, host, port, username, password, name);
BStruct ballerinaClientConnector;
if (clientEndpoint.getNativeData(Constants.B_CONNECTOR) != null) {
ballerinaClientConnector = (BStruct) clientEndpoint.getNativeData(Constants.B_CONNECTOR);
} else {
ballerinaClientConnector = BLangConnectorSPIUtil.createBStruct(context.getProgramFile(), Constants.SQL_PACKAGE_PATH, Constants.CLIENT_CONNECTOR, database, host, port, name, username, password, options, clientEndpointConfig);
clientEndpoint.addNativeData(Constants.B_CONNECTOR, ballerinaClientConnector);
}
ballerinaClientConnector.addNativeData(Constants.CLIENT_CONNECTOR, datasource);
context.setReturnValues();
}
use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class AbstractHTTPAction method getAcceptEncodingConfigFromEndpointConfig.
private String getAcceptEncodingConfigFromEndpointConfig(BStruct httpClientStruct) {
Struct clientEndpointConfig = BLangConnectorSPIUtil.toStruct(httpClientStruct);
Struct epConfig = (Struct) clientEndpointConfig.getNativeData(HttpConstants.CLIENT_ENDPOINT_CONFIG);
if (epConfig == null) {
return HttpConstants.AUTO;
}
return epConfig.getStringField(HttpConstants.CLIENT_EP_ACCEPT_ENCODING);
}
use of org.ballerinalang.connector.api.Struct in project ballerina by ballerina-lang.
the class AbstractHTTPAction method getRetryConfiguration.
private RetryConfig getRetryConfiguration(Struct httpClient) {
Struct clientEndpointConfigs = httpClient.getStructField(CLIENT_ENDPOINT_CONFIG);
if (clientEndpointConfigs == null) {
return new RetryConfig();
}
Struct retryConfig = clientEndpointConfigs.getStructField(CLIENT_EP_RETRY);
if (retryConfig == null) {
return new RetryConfig();
}
long retryCount = retryConfig.getIntField(RETRY_COUNT);
long interval = retryConfig.getIntField(RETRY_INTERVAL);
return new RetryConfig(retryCount, interval);
}
use of org.ballerinalang.connector.api.Struct 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.Struct in project ballerina by ballerina-lang.
the class EndpointUtils method getEndpointConfiguration.
/**
* Generate server endpoint object from service endpoint configuration struct.
*
* @param endpointConfig service endpoint configuration struct.
* @return server endpoint object
*/
public static EndpointConfiguration getEndpointConfiguration(Struct endpointConfig) {
String host = endpointConfig.getStringField(EndpointConstants.ENDPOINT_CONFIG_HOST);
long port = endpointConfig.getIntField(EndpointConstants.ENDPOINT_CONFIG_PORT);
Struct sslConfig = endpointConfig.getStructField(EndpointConstants.ENDPOINT_CONFIG_SSL);
EndpointConfiguration endpointConfiguration = new EndpointConfiguration();
if (host == null || host.isEmpty()) {
endpointConfiguration.setHost(EndpointConstants.HTTP_DEFAULT_HOST);
} else {
endpointConfiguration.setHost(host);
}
endpointConfiguration.setPort(Math.toIntExact(port));
if (sslConfig != null && (!EMPTY_STRING.equals(sslConfig.getStringField(TRUST_FILE)) || !EMPTY_STRING.equals(sslConfig.getStringField(KEY_FILE)))) {
endpointConfiguration.setScheme(EndpointConstants.PROTOCOL_HTTPS);
endpointConfiguration.setSslConfig(getSslConfig(sslConfig));
return endpointConfiguration;
}
return endpointConfiguration;
}
Aggregations