Search in sources :

Example 1 with HttpConnectionManager

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

Aggregations

BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 Struct (org.ballerinalang.connector.api.Struct)1 BStruct (org.ballerinalang.model.values.BStruct)1 HttpConnectionManager (org.ballerinalang.net.http.HttpConnectionManager)1 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)1 SenderConfiguration (org.wso2.transport.http.netty.config.SenderConfiguration)1 HttpClientConnector (org.wso2.transport.http.netty.contract.HttpClientConnector)1