Search in sources :

Example 1 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class BulkApiProcessor method processGetResults.

private void processGetResults(final Exchange exchange, final AsyncCallback callback) throws SalesforceException {
    String jobId;
    BatchInfo batchBody;
    String batchId;
    batchBody = exchange.getIn().getBody(BatchInfo.class);
    if (batchBody != null) {
        jobId = batchBody.getJobId();
        batchId = batchBody.getId();
    } else {
        jobId = getParameter(JOB_ID, exchange, IGNORE_BODY, NOT_OPTIONAL);
        batchId = getParameter(BATCH_ID, exchange, USE_BODY, NOT_OPTIONAL);
    }
    bulkClient.getResults(jobId, batchId, new BulkApiClient.StreamResponseCallback() {

        @Override
        public void onResponse(InputStream inputStream, SalesforceException ex) {
            // read the result stream into a StreamCache temp file
            // ensures the connection is read
            StreamCache body = null;
            if (inputStream != null) {
                try {
                    body = StreamCacheConverter.convertToStreamCache(inputStream, exchange);
                } catch (IOException e) {
                    String msg = "Error retrieving batch results: " + e.getMessage();
                    ex = new SalesforceException(msg, e);
                } finally {
                    // close the input stream to release the Http connection
                    try {
                        inputStream.close();
                    } catch (IOException ignore) {
                    }
                }
            }
            processResponse(exchange, body, ex, callback);
        }
    });
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) StreamCache(org.apache.camel.StreamCache) InputStream(java.io.InputStream) IOException(java.io.IOException) DefaultBulkApiClient(org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient) BulkApiClient(org.apache.camel.component.salesforce.internal.client.BulkApiClient) BatchInfo(org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)

Example 2 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class ZipkinClientResponseAdaptor method responseAnnotations.

@Override
public Collection<KeyValueAnnotation> responseAnnotations() {
    KeyValueAnnotation key1 = KeyValueAnnotation.create("camel.client.endpoint.url", url);
    KeyValueAnnotation key2 = KeyValueAnnotation.create("camel.client.exchange.id", exchange.getExchangeId());
    KeyValueAnnotation key3 = KeyValueAnnotation.create("camel.client.exchange.pattern", exchange.getPattern().name());
    KeyValueAnnotation key4 = null;
    if (eventNotifier.isIncludeMessageBody() || eventNotifier.isIncludeMessageBodyStreams()) {
        boolean streams = eventNotifier.isIncludeMessageBodyStreams();
        StreamCache cache = prepareBodyForLogging(exchange, streams);
        String body = MessageHelper.extractBodyForLogging(exchange.hasOut() ? exchange.getOut() : exchange.getIn(), "", streams, streams);
        key4 = KeyValueAnnotation.create("camel.client.exchange.message.response.body", body);
        if (cache != null) {
            cache.reset();
        }
    }
    KeyValueAnnotation key5 = null;
    // lets capture http response code for http based components
    String responseCode = exchange.hasOut() ? exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE, String.class) : exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, String.class);
    if (responseCode != null) {
        key5 = KeyValueAnnotation.create("camel.client.exchange.message.response.code", responseCode);
    }
    List<KeyValueAnnotation> list = new ArrayList<>();
    list.add(key1);
    list.add(key2);
    list.add(key3);
    if (key4 != null) {
        list.add(key4);
    }
    if (key5 != null) {
        list.add(key5);
    }
    return list;
}
Also used : StreamCache(org.apache.camel.StreamCache) KeyValueAnnotation(com.github.kristofa.brave.KeyValueAnnotation) ArrayList(java.util.ArrayList)

Example 3 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class ZipkinHelper method prepareBodyForLogging.

public static StreamCache prepareBodyForLogging(Exchange exchange, boolean streams) {
    if (!streams) {
        // no need to prepare if streams is not enabled
        return null;
    }
    Message message = exchange.hasOut() ? exchange.getOut() : exchange.getIn();
    // check if body is already cached
    Object body = message.getBody();
    if (body == null) {
        return null;
    } else if (body instanceof StreamCache) {
        StreamCache sc = (StreamCache) body;
        // reset so the cache is ready to be used before processing
        sc.reset();
        return sc;
    }
    // cache the body and if we could do that replace it as the new body
    StreamCache sc = exchange.getContext().getStreamCachingStrategy().cache(exchange);
    if (sc != null) {
        message.setBody(sc);
    }
    return sc;
}
Also used : Message(org.apache.camel.Message) StreamCache(org.apache.camel.StreamCache)

Example 4 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class ZipkinServerResponseAdapter method responseAnnotations.

@Override
public Collection<KeyValueAnnotation> responseAnnotations() {
    String id = exchange.getExchangeId();
    String mep = exchange.getPattern().name();
    KeyValueAnnotation key1 = KeyValueAnnotation.create("camel.server.endpoint.url", url);
    KeyValueAnnotation key2 = KeyValueAnnotation.create("camel.server.exchange.id", id);
    KeyValueAnnotation key3 = KeyValueAnnotation.create("camel.server.exchange.pattern", mep);
    KeyValueAnnotation key4 = null;
    if (exchange.getException() != null) {
        String message = exchange.getException().getMessage();
        key4 = KeyValueAnnotation.create("camel.server.exchange.failure", message);
    } else if (eventNotifier.isIncludeMessageBody() || eventNotifier.isIncludeMessageBodyStreams()) {
        boolean streams = eventNotifier.isIncludeMessageBodyStreams();
        StreamCache cache = prepareBodyForLogging(exchange, streams);
        String body = MessageHelper.extractBodyForLogging(exchange.hasOut() ? exchange.getOut() : exchange.getIn(), "", streams, streams);
        key4 = KeyValueAnnotation.create("camel.server.exchange.message.response.body", body);
        if (cache != null) {
            cache.reset();
        }
    }
    KeyValueAnnotation key5 = null;
    // lets capture http response code for http based components
    String responseCode = exchange.hasOut() ? exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE, String.class) : exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, String.class);
    if (responseCode != null) {
        key5 = KeyValueAnnotation.create("camel.server.exchange.message.response.code", responseCode);
    }
    List<KeyValueAnnotation> list = new ArrayList<>();
    list.add(key1);
    list.add(key2);
    list.add(key3);
    if (key4 != null) {
        list.add(key4);
    }
    if (key5 != null) {
        list.add(key5);
    }
    return list;
}
Also used : StreamCache(org.apache.camel.StreamCache) KeyValueAnnotation(com.github.kristofa.brave.KeyValueAnnotation) ArrayList(java.util.ArrayList)

Example 5 with StreamCache

use of org.apache.camel.StreamCache in project camel by apache.

the class JettyContentExchange9 method send.

public void send(HttpClient client) throws IOException {
    org.eclipse.jetty.client.api.Request.Listener listener = new Request.Listener.Adapter() {

        @Override
        public void onSuccess(Request request) {
            onRequestComplete();
        }

        @Override
        public void onFailure(Request request, Throwable failure) {
            onConnectionFailed(failure);
        }
    };
    InputStreamResponseListener responseListener = new InputStreamResponseListener() {

        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);

        @Override
        public void onContent(Response response, ByteBuffer content, Callback callback) {
            byte[] buffer = new byte[content.limit()];
            content.get(buffer);
            try {
                osb.write(buffer);
                callback.succeeded();
            } catch (IOException e) {
                callback.failed(e);
            }
        }

        @Override
        public void onComplete(Result result) {
            if (result.isFailed()) {
                doTaskCompleted(result.getFailure());
            } else {
                try {
                    Object content = osb.build();
                    if (content instanceof byte[]) {
                        onResponseComplete(result, (byte[]) content);
                    } else {
                        StreamCache cos = (StreamCache) content;
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        cos.writeTo(baos);
                        onResponseComplete(result, baos.toByteArray());
                    }
                } catch (IOException e) {
                    doTaskCompleted(e);
                }
            }
        }
    };
    request.followRedirects(supportRedirect).listener(listener).send(responseListener);
}
Also used : InputStreamResponseListener(org.eclipse.jetty.client.util.InputStreamResponseListener) Request(org.eclipse.jetty.client.api.Request) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) Response(org.eclipse.jetty.client.api.Response) Callback(org.eclipse.jetty.util.Callback) AsyncCallback(org.apache.camel.AsyncCallback) StreamCache(org.apache.camel.StreamCache) OutputStreamBuilder(org.apache.camel.converter.stream.OutputStreamBuilder)

Aggregations

StreamCache (org.apache.camel.StreamCache)38 InputStream (java.io.InputStream)14 IOException (java.io.IOException)10 File (java.io.File)7 Exchange (org.apache.camel.Exchange)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ArrayList (java.util.ArrayList)5 StreamSource (javax.xml.transform.stream.StreamSource)5 KeyValueAnnotation (com.github.kristofa.brave.KeyValueAnnotation)4 StringReader (java.io.StringReader)4 OutputStream (java.io.OutputStream)3 SAXSource (javax.xml.transform.sax.SAXSource)3 Message (org.apache.camel.Message)3 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)3 BatchInfo (org.apache.camel.component.salesforce.api.dto.bulk.BatchInfo)3 BulkApiClient (org.apache.camel.component.salesforce.internal.client.BulkApiClient)3 DefaultBulkApiClient (org.apache.camel.component.salesforce.internal.client.DefaultBulkApiClient)3 DefaultExchange (org.apache.camel.impl.DefaultExchange)3 Writer (java.io.Writer)2 Source (javax.xml.transform.Source)2