Search in sources :

Example 1 with OverflowBlob

use of org.apache.axiom.util.blob.OverflowBlob in project wso2-synapse by wso2.

the class PassThroughHttpSender method sendRequestContent.

private void sendRequestContent(final MessageContext msgContext) throws AxisFault {
    if (Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
        synchronized (msgContext) {
            while (!Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.WAIT_BUILDER_IN_STREAM_COMPLETE)) && !Boolean.TRUE.equals(msgContext.getProperty("PASSTHRU_CONNECT_ERROR"))) {
                try {
                    msgContext.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        if (Boolean.TRUE.equals(msgContext.getProperty("PASSTHRU_CONNECT_ERROR"))) {
            return;
        }
        OutputStream out = (OutputStream) msgContext.getProperty(PassThroughConstants.BUILDER_OUTPUT_STREAM);
        if (out != null) {
            String disableChunking = (String) msgContext.getProperty(PassThroughConstants.DISABLE_CHUNKING);
            String forceHttp10 = (String) msgContext.getProperty(PassThroughConstants.FORCE_HTTP_1_0);
            Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
            if ("true".equals(disableChunking) || "true".equals(forceHttp10)) {
                MessageFormatter formatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
                OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgContext);
                long messageSize = 0;
                try {
                    OverflowBlob overflowBlob = setStreamAsTempData(formatter, msgContext, format);
                    messageSize = overflowBlob.getLength();
                    msgContext.setProperty(PassThroughConstants.PASSTROUGH_MESSAGE_LENGTH, messageSize);
                    overflowBlob.writeTo(out);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    handleException("IO while building message", e);
                }
                // to ignore any entity enclosed methods available.
                if (HTTPConstants.HTTP_METHOD_GET.equals(msgContext.getProperty(Constants.Configuration.HTTP_METHOD)) || RelayUtils.isDeleteRequestWithoutPayload(msgContext)) {
                    pipe.setSerializationCompleteWithoutData(true);
                } else if (messageSize == 0 && (msgContext.getProperty(PassThroughConstants.FORCE_POST_PUT_NOBODY) != null && (Boolean) msgContext.getProperty(PassThroughConstants.FORCE_POST_PUT_NOBODY))) {
                    pipe.setSerializationCompleteWithoutData(true);
                } else {
                    pipe.setSerializationComplete(true);
                }
            } else {
                // to ignore any entity enclosed methods available.
                if (HTTPConstants.HTTP_METHOD_GET.equals(msgContext.getProperty(Constants.Configuration.HTTP_METHOD)) || RelayUtils.isDeleteRequestWithoutPayload(msgContext)) {
                    pipe.setSerializationCompleteWithoutData(true);
                    return;
                }
                if ((msgContext.getProperty(PassThroughConstants.FORCE_POST_PUT_NOBODY) != null && (Boolean) msgContext.getProperty(PassThroughConstants.FORCE_POST_PUT_NOBODY))) {
                    pipe.setSerializationCompleteWithoutData(true);
                    return;
                }
                if ((disableChunking == null || !"true".equals(disableChunking)) || (forceHttp10 == null || !"true".equals(forceHttp10))) {
                    MessageFormatter formatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
                    OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgContext);
                    formatter.writeTo(msgContext, format, out, false);
                }
                if ((msgContext.getProperty(PassThroughConstants.REST_GET_DELETE_INVOKE) != null && (Boolean) msgContext.getProperty(PassThroughConstants.REST_GET_DELETE_INVOKE))) {
                    pipe.setSerializationCompleteWithoutData(true);
                } else {
                    pipe.setSerializationComplete(true);
                }
            }
        }
    }
}
Also used : OverflowBlob(org.apache.axiom.util.blob.OverflowBlob) OutputStream(java.io.OutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 2 with OverflowBlob

use of org.apache.axiom.util.blob.OverflowBlob in project wso2-synapse by wso2.

the class PassThroughHttpSender method setStreamAsTempData.

/**
 * Write the stream to a temporary storage and calculate the content length
 *
 * @throws IOException if an exception occurred while writing data
 */
private OverflowBlob setStreamAsTempData(MessageFormatter messageFormatter, MessageContext msgContext, OMOutputFormat format) throws IOException {
    OverflowBlob serialized = new OverflowBlob(256, 4096, "http-nio_", ".dat");
    OutputStream out = serialized.getOutputStream();
    try {
        messageFormatter.writeTo(msgContext, format, out, false);
    } finally {
        out.close();
    }
    return serialized;
}
Also used : OverflowBlob(org.apache.axiom.util.blob.OverflowBlob) OutputStream(java.io.OutputStream)

Example 3 with OverflowBlob

use of org.apache.axiom.util.blob.OverflowBlob in project wso2-synapse by wso2.

the class PassThroughHttpSender method sendRequestContent.

private void sendRequestContent(final MessageContext msgContext, final EndpointReference epr) throws AxisFault {
    // consume the buffer completely before sending a GET request or DELETE request without a payload
    if (HTTPConstants.HTTP_METHOD_GET.equals(msgContext.getProperty(Constants.Configuration.HTTP_METHOD)) || RelayUtils.isDeleteRequestWithoutPayload(msgContext)) {
        RelayUtils.discardMessage(msgContext);
    }
    if (Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
        String disableChunking = (String) msgContext.getProperty(PassThroughConstants.DISABLE_CHUNKING);
        String forceHttp10 = (String) msgContext.getProperty(PassThroughConstants.FORCE_HTTP_1_0);
        Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
        OutputStream out = null;
        MessageFormatter formatter = MessageFormatterDecoratorFactory.createMessageFormatterDecorator(msgContext);
        OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgContext);
        if ("true".equals(disableChunking) || "true".equals(forceHttp10)) {
            try {
                OverflowBlob overflowBlob = setStreamAsTempData(formatter, msgContext, format);
                long messageSize = overflowBlob.getLength();
                msgContext.setProperty(PassThroughConstants.PASSTROUGH_MESSAGE_LENGTH, messageSize);
                if (!deliveryAgent.submit(msgContext, epr)) {
                    return;
                }
                if (!waitForReady(msgContext)) {
                    return;
                }
                out = (OutputStream) msgContext.getProperty(PassThroughConstants.BUILDER_OUTPUT_STREAM);
                if (out != null) {
                    // Check HTTP method is GET or DELETE with no body
                    if (ignoreMessageBody(msgContext, pipe)) {
                        return;
                    }
                    overflowBlob.writeTo(out);
                    if (pipe.isStale) {
                        throw new IOException("Target Connection is stale..");
                    }
                    pipe.setSerializationComplete(true);
                }
            } catch (IOException e) {
                handleException("IO while building message", e);
            }
        } else {
            if (!deliveryAgent.submit(msgContext, epr)) {
                return;
            }
            if (!waitForReady(msgContext)) {
                return;
            }
            out = (OutputStream) msgContext.getProperty(PassThroughConstants.BUILDER_OUTPUT_STREAM);
            if (out != null) {
                // Check HTTP method is GET or DELETE with no body
                if (ignoreMessageBody(msgContext, pipe)) {
                    return;
                }
                formatter.writeTo(msgContext, format, out, false);
                if (pipe.isStale) {
                    handleException("IO while building message", new IOException("Target Connection is stale.."));
                }
                pipe.setSerializationComplete(true);
            }
        }
    } else {
        if (!deliveryAgent.submit(msgContext, epr)) {
            return;
        }
    }
}
Also used : OverflowBlob(org.apache.axiom.util.blob.OverflowBlob) OutputStream(java.io.OutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 4 with OverflowBlob

use of org.apache.axiom.util.blob.OverflowBlob in project wso2-synapse by wso2.

the class Axis2HttpRequest method setStreamAsTempData.

/**
 * Write the stream to a temporary storage and calculate the content length
 *
 * @param entity HTTPEntity
 * @throws IOException if an exception occurred while writing data
 */
private void setStreamAsTempData(BasicHttpEntity entity) throws IOException {
    OverflowBlob serialized = new OverflowBlob(256, 4096, "http-nio_", ".dat");
    OutputStream out = serialized.getOutputStream();
    try {
        messageFormatter.writeTo(msgContext, format, out, true);
    } finally {
        out.close();
    }
    msgContext.setProperty(NhttpConstants.SERIALIZED_BYTES, serialized);
    entity.setContentLength(serialized.getLength());
}
Also used : OverflowBlob(org.apache.axiom.util.blob.OverflowBlob) OutputStream(java.io.OutputStream) ContentOutputStream(org.apache.http.nio.entity.ContentOutputStream)

Example 5 with OverflowBlob

use of org.apache.axiom.util.blob.OverflowBlob in project wso2-synapse by wso2.

the class Axis2SynapseEnvironment method createOverflowBlob.

/**
 * Factory method to create the TemporaryData object as per on the parameters specified in the
 * synapse.properties file, so that the TemporaryData parameters like threshold chunk size
 * can be customized by using the properties file. This can be extended to enforce further
 * policies if required in the future.
 *
 * @return created TemporaryData object as per in the synapse.properties file
 */
public OverflowBlob createOverflowBlob() {
    String chkSize = synapseConfig.getProperty(SynapseConstants.CHUNK_SIZE);
    String chunkNumber = synapseConfig.getProperty(SynapseConstants.THRESHOLD_CHUNKS);
    int numberOfChunks = SynapseConstants.DEFAULT_THRESHOLD_CHUNKS;
    int chunkSize = SynapseConstants.DEFAULT_CHUNK_SIZE;
    if (chkSize != null) {
        chunkSize = Integer.parseInt(chkSize);
    }
    if (chunkNumber != null) {
        numberOfChunks = Integer.parseInt(chunkNumber);
    }
    String tempPrefix = synapseConfig.getProperty(SynapseConstants.TEMP_FILE_PREFIX, SynapseConstants.DEFAULT_TEMPFILE_PREFIX);
    String tempSuffix = synapseConfig.getProperty(SynapseConstants.TEMP_FILE_SUFIX, SynapseConstants.DEFAULT_TEMPFILE_SUFIX);
    return new OverflowBlob(numberOfChunks, chunkSize, tempPrefix, tempSuffix);
}
Also used : OverflowBlob(org.apache.axiom.util.blob.OverflowBlob) InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint)

Aggregations

OverflowBlob (org.apache.axiom.util.blob.OverflowBlob)6 OutputStream (java.io.OutputStream)5 IOException (java.io.IOException)2 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)2 MessageFormatter (org.apache.axis2.transport.MessageFormatter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ContentOutputStream (org.apache.http.nio.entity.ContentOutputStream)1 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)1