Refine search
public <T> T evaluate(Exchange exchange, Class<T> tClass) { try { Expression expression = parseExpression(); EvaluationContext evaluationContext = createEvaluationContext(exchange); Object value = expression.getValue(evaluationContext); // Let Camel handle the type conversion return exchange.getContext().getTypeConverter().convertTo(tClass, value); } catch (Exception e) { throw new ExpressionEvaluationException(this, exchange, e); } }
public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, graph); String body = reverseBytes(bytes); stream.write(body.getBytes()); }
@Converter public static InputStream toInputStream(Response response, Exchange exchange) { Object obj = response.getEntity(); if (obj == null) { return null; } if (obj instanceof InputStream) { // short circuit the lookup return (InputStream)obj; } TypeConverterRegistry registry = exchange.getContext().getTypeConverterRegistry(); TypeConverter tc = registry.lookup(InputStream.class, obj.getClass()); if (tc != null) { return tc.convertTo(InputStream.class, exchange, obj); } return null; }
private static String asString(Exchange exchange, Object value) { return exchange.getContext().getTypeConverter().convertTo(String.class, exchange, value); }
@SuppressWarnings("rawtypes") private List<DBObject> attemptConvertToList(List insertList, Exchange exchange) throws CamelMongoDbException { List<DBObject> dbObjectList = new ArrayList<>(insertList.size()); TypeConverter converter = exchange.getContext().getTypeConverter(); for (Object item : insertList) { try { DBObject dbObject = converter.mandatoryConvertTo(DBObject.class, item); dbObjectList.add(dbObject); } catch (Exception e) { throw new CamelMongoDbException("MongoDB operation = insert, Assuming List variant of MongoDB insert operation, but List contains non-DBObject items", e); } } return dbObjectList; }
private OutputStream resolveStreamFromHeader(Object o, Exchange exchange) throws CamelExchangeException { return exchange.getContext().getTypeConverter().convertTo(OutputStream.class, o); }
protected static boolean hasParameter(String nextParam, Exchange exchange, Object body) { Map<?, ?> bodyMap = safeMap(exchange.getContext().getTypeConverter().tryConvertTo(Map.class, body)); Map<?, ?> headersMap = safeMap(exchange.getIn().getHeaders()); if ((nextParam.startsWith("$simple{") || nextParam.startsWith("${")) && nextParam.endsWith("}")) { return true; } else if (bodyMap.containsKey(nextParam)) { return true; } else if (headersMap.containsKey(nextParam)) { return true; } return false; }
@Override public <T> T evaluate(Exchange exchange, Class<T> type) { Object result = evaluate(exchange); return exchange.getContext().getTypeConverter().convertTo(type, exchange, result); } }
/** * Attempts to convert the object to the same type as the serialized class specified */ protected Object tryConvertToSerializedType(Exchange exchange, Object object, String serializerClass) { Object answer = null; if (KafkaConstants.KAFKA_DEFAULT_SERIALIZER.equals(serializerClass)) { answer = exchange.getContext().getTypeConverter().tryConvertTo(String.class, exchange, object); } else if ("org.apache.kafka.common.serialization.ByteArraySerializer".equals(serializerClass)) { answer = exchange.getContext().getTypeConverter().tryConvertTo(byte[].class, exchange, object); } else if ("org.apache.kafka.common.serialization.ByteBufferSerializer".equals(serializerClass)) { answer = exchange.getContext().getTypeConverter().tryConvertTo(ByteBuffer.class, exchange, object); } else if ("org.apache.kafka.common.serialization.BytesSerializer".equals(serializerClass)) { // we need to convert to byte array first byte[] array = exchange.getContext().getTypeConverter().tryConvertTo(byte[].class, exchange, object); if (array != null) { answer = new Bytes(array); } } return answer != null ? answer : object; }
@Override public <T> T evaluate(Exchange exchange, Class<T> type) { Object result = evaluate(exchange); return exchange.getContext().getTypeConverter().convertTo(type, exchange, result); }
public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, stream); String body = reverseBytes(bytes); return body; }
@Override public <T> T evaluate(Exchange exchange, Class<T> type) { Object result = evaluate(exchange); return exchange.getContext().getTypeConverter().convertTo(type, exchange, result); } }
private Function<Exchange, Object> createDoCount() { return exchange -> { BasicDBObject query = exchange.getContext().getTypeConverter().tryConvertTo(BasicDBObject.class, exchange, exchange.getIn().getBody()); if (query == null) { query = new BasicDBObject(); } return (Long) calculateCollection(exchange).count(query); }; }
@Override public <T> T evaluate(Exchange exchange, Class<T> type) { Object result = ClosureSupport.call(closure, exchange); return exchange.getContext().getTypeConverter().convertTo(type, result); }
private void doRun(final Exchange exchange, final AsyncCallback callback, IgniteCompute compute) throws Exception { Object job = exchange.getIn().getBody(); if (Collection.class.isAssignableFrom(job.getClass())) { Collection<?> col = (Collection<?>) job; TypeConverter tc = exchange.getContext().getTypeConverter(); Collection<IgniteRunnable> runnables = new ArrayList<>(col.size()); for (Object o : col) { runnables.add(tc.mandatoryConvertTo(IgniteRunnable.class, o)); } compute.run(runnables); } else if (IgniteRunnable.class.isAssignableFrom(job.getClass())) { compute.run((IgniteRunnable) job); } else { throw new RuntimeCamelException(String.format( "Ignite Compute endpoint with RUN executionType is only " + "supported for IgniteRunnable payloads, or collections of them. The payload type was: %s.", job.getClass().getName())); } }
private static void getParamsFromHeaders( Exchange exchange, Collection<String> args) { Map<String, Object> headers = exchange.getIn().getHeaders(); for (Map.Entry<String, Object> header : headers.entrySet()) { Object headerValue = header.getValue(); if (headerValue != null) { String headerStringValue = exchange.getContext().getTypeConverter() .convertTo(String.class, exchange, headerValue); args.add("--context_param " + header.getKey() + '=' + headerStringValue); } } }
private Object getMultiInsertBody(Exchange exchange) { Object insert; // we try List first, because it should be the common case insert = exchange.getIn().getBody(List.class); if (insert != null) { // if the body of type List was obtained, ensure that all items are of type DBObject and cast the List to List<DBObject> insert = attemptConvertToList((List<?>) insert, exchange); } else { insert = exchange.getContext().getTypeConverter().tryConvertTo(BasicDBList.class, exchange, exchange.getIn().getBody()); } if (insert == null) { throw new CamelMongoDbException("MongoDB operation = insert, Body is not conversible to type DBObject nor List<DBObject>"); } return insert; }
private static void getParamsFromHeaders( Exchange exchange, Collection<String> args) { Map<String, Object> headers = exchange.getIn().getHeaders(); for (Map.Entry<String, Object> header : headers.entrySet()) { Object headerValue = header.getValue(); if (headerValue != null) { String headerStringValue = exchange.getContext().getTypeConverter() .convertTo(String.class, exchange, headerValue); args.add("--context_param " + header.getKey() + '=' + headerStringValue); } } }
private InputStream getInputStreamFromExchange(Exchange exchange) throws Exception { Object body = exchange.getIn().getBody(); if (body instanceof WrappedFile) { // unwrap file body = ((WrappedFile) body).getFile(); } InputStream is; if (body instanceof InputStream) { is = (InputStream) body; } else if (body instanceof File) { is = new FileInputStream((File)body); } else if (body instanceof byte[]) { is = new ByteArrayInputStream((byte[]) body); } else { // try as input stream is = exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, exchange, body); } if (is == null) { // fallback to string based throw new IllegalArgumentException("Unsupported blob type:" + body.getClass().getName()); } return is; }
public <T> T evaluate(Exchange exchange, Class<T> tClass) { // Create (if needed) the ExpressionFactory first from the CamelContext using FactoryFinder ExpressionFactory factory = getExpressionFactory(exchange.getContext()); ELContext context = populateContext(createContext(), exchange); ValueExpression valueExpression = factory.createValueExpression(context, expression, type); Object value = valueExpression.getValue(context); LOG.trace("Value returned {}", value); return exchange.getContext().getTypeConverter().convertTo(tClass, value); }