Search in sources :

Example 6 with HttpClientConnector

use of org.wso2.transport.http.netty.contract.HttpClientConnector 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)

Example 7 with HttpClientConnector

use of org.wso2.transport.http.netty.contract.HttpClientConnector in project ballerina by ballerina-lang.

the class RejectPromise method execute.

@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct pushPromiseStruct = (BStruct) context.getRefArgument(1);
    Http2PushPromise http2PushPromise = HttpUtil.getPushPromise(pushPromiseStruct, null);
    if (http2PushPromise == null) {
        throw new BallerinaException("invalid push promise");
    }
    BStruct bConnector = (BStruct) context.getRefArgument(0);
    HttpClientConnector clientConnector = (HttpClientConnector) bConnector.getNativeData(HttpConstants.HTTP_CLIENT);
    clientConnector.rejectPushResponse(http2PushPromise);
    // TODO: Implement a listener to see the progress
    context.setReturnValues(new BBoolean(true));
    callback.notifySuccess();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HttpClientConnector(org.wso2.transport.http.netty.contract.HttpClientConnector) Http2PushPromise(org.wso2.transport.http.netty.message.Http2PushPromise) BBoolean(org.ballerinalang.model.values.BBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)7 HttpClientConnector (org.wso2.transport.http.netty.contract.HttpClientConnector)7 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)6 DataContext (org.ballerinalang.net.http.DataContext)3 ResponseHandle (org.wso2.transport.http.netty.message.ResponseHandle)3 Struct (org.ballerinalang.connector.api.Struct)2 Http2PushPromise (org.wso2.transport.http.netty.message.Http2PushPromise)2 EncoderException (io.netty.handler.codec.EncoderException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 BBoolean (org.ballerinalang.model.values.BBoolean)1 HttpConnectionManager (org.ballerinalang.net.http.HttpConnectionManager)1 RetryConfig (org.ballerinalang.net.http.RetryConfig)1 ResponseCacheControlStruct (org.ballerinalang.net.http.caching.ResponseCacheControlStruct)1 SenderConfiguration (org.wso2.transport.http.netty.config.SenderConfiguration)1 HttpResponseFuture (org.wso2.transport.http.netty.contract.HttpResponseFuture)1 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)1