Search in sources :

Example 1 with RetryConfig

use of org.ballerinalang.net.http.RetryConfig 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 RetryConfig

use of org.ballerinalang.net.http.RetryConfig in project ballerina by ballerina-lang.

the class AbstractHTTPAction method getRetryConfiguration.

private RetryConfig getRetryConfiguration(Struct httpClient) {
    Struct clientEndpointConfigs = httpClient.getStructField(CLIENT_ENDPOINT_CONFIG);
    if (clientEndpointConfigs == null) {
        return new RetryConfig();
    }
    Struct retryConfig = clientEndpointConfigs.getStructField(CLIENT_EP_RETRY);
    if (retryConfig == null) {
        return new RetryConfig();
    }
    long retryCount = retryConfig.getIntField(RETRY_COUNT);
    long interval = retryConfig.getIntField(RETRY_INTERVAL);
    return new RetryConfig(retryCount, interval);
}
Also used : RetryConfig(org.ballerinalang.net.http.RetryConfig) ResponseCacheControlStruct(org.ballerinalang.net.http.caching.ResponseCacheControlStruct) Struct(org.ballerinalang.connector.api.Struct) BStruct(org.ballerinalang.model.values.BStruct)

Aggregations

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