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);
}
});
}
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;
}
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;
}
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;
}
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);
}
Aggregations