use of org.ballerinalang.net.http.HttpConnectionManager 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);
}
Aggregations