Search in sources :

Example 1 with HttpClientConnector

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

the class AbstractHTTPAction method send.

/**
 * Send outbound request through the client connector. If the Content-Type is multipart, check whether the boundary
 * exist. If not get a new boundary string and add it as a parameter to Content-Type, just before sending header
 * info through wire. If a boundary string exist at this point, serialize multipart entity body, else serialize
 * entity body which can either be a message data source or a byte channel.
 *
 * @param dataContext               holds the ballerina context and callback
 * @param outboundRequestMsg        Outbound request that needs to be sent across the wire
 * @param async                     whether a handle should be return
 * @return connector future for this particular request
 * @throws Exception When an error occurs while sending the outbound request via client connector
 */
private void send(DataContext dataContext, HTTPCarbonMessage outboundRequestMsg, boolean async) throws Exception {
    BStruct bConnector = (BStruct) dataContext.context.getRefArgument(0);
    Struct httpClient = BLangConnectorSPIUtil.toStruct(bConnector);
    HttpClientConnector clientConnector = (HttpClientConnector) httpClient.getNativeData(HttpConstants.HTTP_CLIENT);
    String contentType = HttpUtil.getContentTypeFromTransportMessage(outboundRequestMsg);
    String boundaryString = null;
    if (HeaderUtil.isMultipart(contentType)) {
        boundaryString = HttpUtil.addBoundaryIfNotExist(outboundRequestMsg, contentType);
    }
    HttpMessageDataStreamer outboundMsgDataStreamer = new HttpMessageDataStreamer(outboundRequestMsg);
    OutputStream messageOutputStream = outboundMsgDataStreamer.getOutputStream();
    RetryConfig retryConfig = getRetryConfiguration(httpClient);
    HTTPClientConnectorListener httpClientConnectorLister = new HTTPClientConnectorListener(dataContext, retryConfig, outboundRequestMsg, outboundMsgDataStreamer);
    HttpResponseFuture future = clientConnector.send(outboundRequestMsg);
    if (async) {
        future.setResponseHandleListener(httpClientConnectorLister);
    } else {
        future.setHttpConnectorListener(httpClientConnectorLister);
    }
    try {
        if (boundaryString != null) {
            serializeMultiparts(dataContext.context, messageOutputStream, boundaryString);
        } else {
            serializeDataSource(dataContext.context, messageOutputStream);
        }
    } catch (IOException | EncoderException serializerException) {
        // We don't have to do anything here as the client connector will notify
        // the error though the listener
        logger.warn("couldn't serialize the message", serializerException);
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) BStruct(org.ballerinalang.model.values.BStruct) HttpClientConnector(org.wso2.transport.http.netty.contract.HttpClientConnector) RetryConfig(org.ballerinalang.net.http.RetryConfig) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) OutputStream(java.io.OutputStream) HttpResponseFuture(org.wso2.transport.http.netty.contract.HttpResponseFuture) IOException(java.io.IOException) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct) Struct(org.ballerinalang.connector.api.Struct) BStruct(org.ballerinalang.model.values.BStruct)

Example 2 with HttpClientConnector

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

the class GetPromisedResponse method execute.

@Override
public void execute(Context context, CallableUnitCallback callback) {
    DataContext dataContext = new DataContext(context, 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.getPushResponse(http2PushPromise).setPushResponseListener(new PushResponseListener(dataContext), http2PushPromise.getPromisedStreamId());
}
Also used : DataContext(org.ballerinalang.net.http.DataContext) BStruct(org.ballerinalang.model.values.BStruct) HttpClientConnector(org.wso2.transport.http.netty.contract.HttpClientConnector) Http2PushPromise(org.wso2.transport.http.netty.message.Http2PushPromise) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 3 with HttpClientConnector

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

the class GetResponse method execute.

@Override
public void execute(Context context, CallableUnitCallback callback) {
    DataContext dataContext = new DataContext(context, callback);
    BStruct handleStruct = ((BStruct) context.getRefArgument(1));
    ResponseHandle responseHandle = (ResponseHandle) handleStruct.getNativeData(HttpConstants.TRANSPORT_HANDLE);
    if (responseHandle == null) {
        throw new BallerinaException("invalid http handle");
    }
    BStruct bConnector = (BStruct) context.getRefArgument(0);
    HttpClientConnector clientConnector = (HttpClientConnector) bConnector.getNativeData(HttpConstants.HTTP_CLIENT);
    clientConnector.getResponse(responseHandle).setHttpConnectorListener(new ResponseListener(dataContext));
}
Also used : DataContext(org.ballerinalang.net.http.DataContext) BStruct(org.ballerinalang.model.values.BStruct) ResponseHandle(org.wso2.transport.http.netty.message.ResponseHandle) HttpClientConnector(org.wso2.transport.http.netty.contract.HttpClientConnector) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 4 with HttpClientConnector

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

the class HasPromise method execute.

@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct handleStruct = ((BStruct) context.getRefArgument(1));
    ResponseHandle responseHandle = (ResponseHandle) handleStruct.getNativeData(HttpConstants.TRANSPORT_HANDLE);
    if (responseHandle == null) {
        throw new BallerinaException("invalid http handle");
    }
    BStruct bConnector = (BStruct) context.getRefArgument(0);
    HttpClientConnector clientConnector = (HttpClientConnector) bConnector.getNativeData(HttpConstants.HTTP_CLIENT);
    clientConnector.hasPushPromise(responseHandle).setPromiseAvailabilityListener(new PromiseAvailabilityCheckListener(context, callback));
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) ResponseHandle(org.wso2.transport.http.netty.message.ResponseHandle) HttpClientConnector(org.wso2.transport.http.netty.contract.HttpClientConnector) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 5 with HttpClientConnector

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

the class GetNextPromise method execute.

@Override
public void execute(Context context, CallableUnitCallback callback) {
    DataContext dataContext = new DataContext(context, callback);
    BStruct handleStruct = ((BStruct) context.getRefArgument(1));
    ResponseHandle responseHandle = (ResponseHandle) handleStruct.getNativeData(HttpConstants.TRANSPORT_HANDLE);
    if (responseHandle == null) {
        throw new BallerinaException("invalid http handle");
    }
    BStruct bConnector = (BStruct) context.getRefArgument(0);
    HttpClientConnector clientConnector = (HttpClientConnector) bConnector.getNativeData(HttpConstants.HTTP_CLIENT);
    clientConnector.getNextPushPromise(responseHandle).setPushPromiseListener(new PromiseListener(dataContext));
}
Also used : DataContext(org.ballerinalang.net.http.DataContext) BStruct(org.ballerinalang.model.values.BStruct) ResponseHandle(org.wso2.transport.http.netty.message.ResponseHandle) HttpClientConnector(org.wso2.transport.http.netty.contract.HttpClientConnector) 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