use of org.apache.camel.StreamCache in project camel by apache.
the class BulkApiProcessor method processGetRequest.
private void processGetRequest(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.getRequest(jobId, batchId, new BulkApiClient.StreamResponseCallback() {
@Override
public void onResponse(InputStream inputStream, SalesforceException ex) {
// read the request 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 request: " + 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 DefaultHttpBinding method readBody.
protected void readBody(HttpServletRequest request, HttpMessage message) {
LOG.trace("readBody {}", request);
// lets parse the body
Object body = message.getBody();
// reset the stream cache if the body is the instance of StreamCache
if (body instanceof StreamCache) {
((StreamCache) body).reset();
}
// if content type is serialized java object, then de-serialize it to a Java object
if (request.getContentType() != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(request.getContentType())) {
// only deserialize java if allowed
if (allowJavaSerializedObject || isTransferException()) {
try {
InputStream is = message.getExchange().getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body);
Object object = HttpHelper.deserializeJavaObjectFromStream(is, message.getExchange().getContext());
if (object != null) {
message.setBody(object);
}
} catch (Exception e) {
throw new RuntimeCamelException("Cannot deserialize body to Java object", e);
}
} else {
// set empty body
message.setBody(null);
}
}
populateAttachments(request, message);
}
use of org.apache.camel.StreamCache in project camel by apache.
the class JcloudsBlobStoreProducerTest method testBlobStorePutWithStreamAndGet.
@Test
public void testBlobStorePutWithStreamAndGet() throws InterruptedException, TransformerException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(MESSAGE.getBytes());
Exchange exchange = new DefaultExchange(context);
StreamCache streamCache = StreamCacheConverter.convertToStreamCache(new SAXSource(new InputSource(inputStream)), exchange);
template.sendBody("direct:put-and-get", streamCache);
Object result = template.requestBodyAndHeader("direct:put-and-get", null, JcloudsConstants.OPERATION, JcloudsConstants.GET, String.class);
assertEquals(MESSAGE, result);
}
use of org.apache.camel.StreamCache in project camel by apache.
the class MethodInfo method createMethodInvocation.
public MethodInvocation createMethodInvocation(final Object pojo, final Exchange exchange) {
final Object[] arguments = parametersExpression.evaluate(exchange, Object[].class);
return new MethodInvocation() {
public Method getMethod() {
return method;
}
public Object[] getArguments() {
return arguments;
}
public boolean proceed(AsyncCallback callback) {
Object body = exchange.getIn().getBody();
if (body != null && body instanceof StreamCache) {
// ensure the stream cache is reset before calling the method
((StreamCache) body).reset();
}
try {
return doProceed(callback);
} catch (InvocationTargetException e) {
exchange.setException(e.getTargetException());
callback.done(true);
return true;
} catch (Throwable e) {
exchange.setException(e);
callback.done(true);
return true;
}
}
private boolean doProceed(AsyncCallback callback) throws Exception {
// dynamic router should be invoked beforehand
if (dynamicRouter != null) {
if (!dynamicRouter.isStarted()) {
ServiceHelper.startService(dynamicRouter);
}
// use a expression which invokes the method to be used by dynamic router
Expression expression = new DynamicRouterExpression(pojo);
return dynamicRouter.doRoutingSlip(exchange, expression, callback);
}
// invoke pojo
if (LOG.isTraceEnabled()) {
LOG.trace(">>>> invoking: {} on bean: {} with arguments: {} for exchange: {}", new Object[] { method, pojo, asString(arguments), exchange });
}
Object result = invoke(method, pojo, arguments, exchange);
// the method may be a closure or chained method returning a callable which should be called
if (result instanceof Callable) {
LOG.trace("Method returned Callback which will be called: {}", result);
Object callableResult = ((Callable) result).call();
if (callableResult != null) {
result = callableResult;
} else {
// if callable returned null we should not change the body
result = Void.TYPE;
}
}
if (recipientList != null) {
// ensure its started
if (!recipientList.isStarted()) {
ServiceHelper.startService(recipientList);
}
return recipientList.sendToRecipientList(exchange, result, callback);
}
if (routingSlip != null) {
if (!routingSlip.isStarted()) {
ServiceHelper.startService(routingSlip);
}
return routingSlip.doRoutingSlip(exchange, result, callback);
}
//If it's Java 8 async result
if (CompletionStage.class.isAssignableFrom(getMethod().getReturnType())) {
CompletionStage<?> completionStage = (CompletionStage<?>) result;
completionStage.whenComplete((resultObject, e) -> {
if (e != null) {
exchange.setException(e);
} else if (resultObject != null) {
fillResult(exchange, resultObject);
}
callback.done(false);
});
return false;
}
// if the method returns something then set the value returned on the Exchange
if (!getMethod().getReturnType().equals(Void.TYPE) && result != Void.TYPE) {
fillResult(exchange, result);
}
// we did not use any of the eips, but just invoked the bean
// so notify the callback we are done synchronously
callback.done(true);
return true;
}
public Object getThis() {
return pojo;
}
public AccessibleObject getStaticPart() {
return method;
}
};
}
use of org.apache.camel.StreamCache in project camel by apache.
the class MessageHelper method extractValueForLogging.
/**
* Extracts the value for logging purpose.
* <p/>
* Will clip the value if its too big for logging.
*
* @see org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS
* @param obj the value
* @param message the message
* @param prepend a message to prepend
* @param allowStreams whether or not streams is allowed
* @param allowFiles whether or not files is allowed (currently not in use)
* @param maxChars limit to maximum number of chars. Use 0 for not limit, and -1 for turning logging message body off.
* @return the logging message
*/
public static String extractValueForLogging(Object obj, Message message, String prepend, boolean allowStreams, boolean allowFiles, int maxChars) {
if (maxChars < 0) {
return prepend + "[Body is not logged]";
}
if (obj == null) {
return prepend + "[Body is null]";
}
if (!allowStreams) {
if (obj instanceof Source && !(obj instanceof StringSource || obj instanceof BytesSource)) {
// all other kinds we should not touch the body
return prepend + "[Body is instance of java.xml.transform.Source]";
} else if (obj instanceof StreamCache) {
return prepend + "[Body is instance of org.apache.camel.StreamCache]";
} else if (obj instanceof InputStream) {
return prepend + "[Body is instance of java.io.InputStream]";
} else if (obj instanceof OutputStream) {
return prepend + "[Body is instance of java.io.OutputStream]";
} else if (obj instanceof Reader) {
return prepend + "[Body is instance of java.io.Reader]";
} else if (obj instanceof Writer) {
return prepend + "[Body is instance of java.io.Writer]";
} else if (obj instanceof WrappedFile || obj instanceof File) {
if (!allowFiles) {
return prepend + "[Body is file based: " + obj + "]";
}
}
}
if (!allowFiles) {
if (obj instanceof WrappedFile || obj instanceof File) {
return prepend + "[Body is file based: " + obj + "]";
}
}
// is the body a stream cache or input stream
StreamCache cache = null;
InputStream is = null;
if (obj instanceof StreamCache) {
cache = (StreamCache) obj;
is = null;
} else if (obj instanceof InputStream) {
cache = null;
is = (InputStream) obj;
}
// grab the message body as a string
String body = null;
if (message.getExchange() != null) {
try {
body = message.getExchange().getContext().getTypeConverter().tryConvertTo(String.class, message.getExchange(), obj);
} catch (Throwable e) {
// ignore as the body is for logging purpose
}
}
if (body == null) {
try {
body = obj.toString();
} catch (Throwable e) {
// ignore as the body is for logging purpose
}
}
// reset stream cache after use
if (cache != null) {
cache.reset();
} else if (is != null && is.markSupported()) {
try {
is.reset();
} catch (IOException e) {
// ignore
}
}
if (body == null) {
return prepend + "[Body is null]";
}
// clip body if length enabled and the body is too big
if (maxChars > 0 && body.length() > maxChars) {
body = body.substring(0, maxChars) + "... [Body clipped after " + maxChars + " chars, total length is " + body.length() + "]";
}
return prepend + body;
}
Aggregations