Search in sources :

Example 1 with HttpResponseFuture

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

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

the class ConnectionAction method sendOutboundResponseRobust.

private BValue[] sendOutboundResponseRobust(Context context, HTTPCarbonMessage requestMessage, BStruct outboundResponseStruct, HTTPCarbonMessage responseMessage) {
    String contentType = HttpUtil.getContentTypeFromTransportMessage(responseMessage);
    String boundaryString = null;
    if (HeaderUtil.isMultipart(contentType)) {
        boundaryString = HttpUtil.addBoundaryIfNotExist(responseMessage, contentType);
    }
    HttpResponseFuture outboundRespStatusFuture = HttpUtil.sendOutboundResponse(requestMessage, responseMessage);
    BStruct entityStruct = MimeUtil.extractEntity(outboundResponseStruct);
    if (entityStruct != null) {
        if (boundaryString != null) {
            serializeMultiparts(responseMessage, boundaryString, outboundRespStatusFuture, entityStruct);
        } else {
            MessageDataSource outboundMessageSource = EntityBodyHandler.getMessageDataSource(entityStruct);
            serializeMsgDataSource(responseMessage, outboundMessageSource, outboundRespStatusFuture, entityStruct);
        }
    }
    return handleResponseStatus(context, outboundRespStatusFuture);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) MessageDataSource(org.ballerinalang.runtime.message.MessageDataSource) HttpResponseFuture(org.wso2.transport.http.netty.contract.HttpResponseFuture)

Example 3 with HttpResponseFuture

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

the class Promise method execute.

@Override
public void execute(Context context) {
    BStruct connectionStruct = (BStruct) context.getRefArgument(0);
    HTTPCarbonMessage inboundRequestMsg = HttpUtil.getCarbonMsg(connectionStruct, null);
    HttpUtil.serverConnectionStructCheck(inboundRequestMsg);
    BStruct pushPromiseStruct = (BStruct) context.getRefArgument(1);
    Http2PushPromise http2PushPromise = HttpUtil.getPushPromise(pushPromiseStruct, HttpUtil.createHttpPushPromise(pushPromiseStruct));
    HttpResponseFuture outboundRespStatusFuture = HttpUtil.pushPromise(inboundRequestMsg, http2PushPromise);
    BValue[] outboundResponseStatus = handleResponseStatus(context, outboundRespStatusFuture);
    context.setReturnValues(outboundResponseStatus);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) Http2PushPromise(org.wso2.transport.http.netty.message.Http2PushPromise) BValue(org.ballerinalang.model.values.BValue) HttpResponseFuture(org.wso2.transport.http.netty.contract.HttpResponseFuture)

Example 4 with HttpResponseFuture

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

the class PushPromisedResponse method pushResponseRobust.

private BValue[] pushResponseRobust(Context context, HTTPCarbonMessage requestMessage, BStruct outboundResponseStruct, HTTPCarbonMessage responseMessage, Http2PushPromise http2PushPromise) {
    BStruct entityStruct = MimeUtil.extractEntity(outboundResponseStruct);
    HttpResponseFuture outboundRespStatusFuture = HttpUtil.pushResponse(requestMessage, responseMessage, http2PushPromise);
    if (entityStruct != null) {
        MessageDataSource outboundMessageSource = EntityBodyHandler.getMessageDataSource(entityStruct);
        serializeMsgDataSource(responseMessage, outboundMessageSource, outboundRespStatusFuture, entityStruct);
    }
    return handleResponseStatus(context, outboundRespStatusFuture);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) MessageDataSource(org.ballerinalang.runtime.message.MessageDataSource) HttpResponseFuture(org.wso2.transport.http.netty.contract.HttpResponseFuture)

Example 5 with HttpResponseFuture

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

the class ConnectionAction method getOutputStream.

private OutputStream getOutputStream(HTTPCarbonMessage responseMessage, HttpResponseFuture outboundResponseStatusFuture) {
    HttpMessageDataStreamer outboundMsgDataStreamer = new HttpMessageDataStreamer(responseMessage);
    HttpConnectorListener outboundResStatusConnectorListener = new HttpResponseConnectorListener(outboundMsgDataStreamer);
    outboundResponseStatusFuture.setHttpConnectorListener(outboundResStatusConnectorListener);
    return outboundMsgDataStreamer.getOutputStream();
}
Also used : HttpConnectorListener(org.wso2.transport.http.netty.contract.HttpConnectorListener) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)4 HttpResponseFuture (org.wso2.transport.http.netty.contract.HttpResponseFuture)4 MessageDataSource (org.ballerinalang.runtime.message.MessageDataSource)2 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)2 EncoderException (io.netty.handler.codec.EncoderException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Struct (org.ballerinalang.connector.api.Struct)1 BValue (org.ballerinalang.model.values.BValue)1 RetryConfig (org.ballerinalang.net.http.RetryConfig)1 ResponseCacheControlStruct (org.ballerinalang.net.http.caching.ResponseCacheControlStruct)1 HttpClientConnector (org.wso2.transport.http.netty.contract.HttpClientConnector)1 HttpConnectorListener (org.wso2.transport.http.netty.contract.HttpConnectorListener)1 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)1 Http2PushPromise (org.wso2.transport.http.netty.message.Http2PushPromise)1