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; }
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 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; }
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); }; }
protected static Object lookupParameter(String nextParam, Exchange exchange, Object body) { Map<?, ?> bodyMap = safeMap(exchange.getContext().getTypeConverter().tryConvertTo(Map.class, body)); Map<?, ?> headersMap = safeMap(exchange.getIn().getHeaders()); Object answer = null; if ((nextParam.startsWith("$simple{") || nextParam.startsWith("${")) && nextParam.endsWith("}")) { answer = SimpleLanguage.expression(nextParam).evaluate(exchange, Object.class); } else if (bodyMap.containsKey(nextParam)) { answer = bodyMap.get(nextParam); } else if (headersMap.containsKey(nextParam)) { answer = headersMap.get(nextParam); } return answer; }
/** * 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; }
private Object readWithInputStream(JsonPath path, Exchange exchange) throws IOException { Object json = headerName != null ? exchange.getIn().getHeader(headerName) : exchange.getIn().getBody(); LOG.trace("JSonPath: {} is read as InputStream: {}", path, json); InputStream is = exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, exchange, json); if (is != null) { String jsonEncoding = exchange.getIn().getHeader(JsonPathConstants.HEADER_JSON_ENCODING, String.class); if (jsonEncoding != null) { // json encoding specified in header return path.read(is, jsonEncoding, configuration); } else { // No json encoding specified --> assume json encoding is unicode and determine the specific unicode encoding according to RFC-4627. // This is a temporary solution, it can be removed as soon as jsonpath offers the encoding detection JsonStream jsonStream = new JsonStream(is); return path.read(jsonStream, jsonStream.getEncoding().name(), configuration); } } return null; }
@FallbackConverter @SuppressWarnings("unchecked") public static <T extends Payload> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) throws IOException { Class<?> sourceType = value.getClass(); if (type == Payload.class && WrappedFile.class.isAssignableFrom(sourceType)) { // attempt to convert to JClouds Payload from a file WrappedFile wf = (WrappedFile) value; if (wf.getFile() != null) { TypeConverter converter = registry.lookup(Payload.class, wf.getFile().getClass()); if (converter != null) { return (T) converter.tryConvertTo(Payload.class, wf.getFile()); } } } return null; } }
protected String populateContentOnBodyPart(BodyPart part, MailConfiguration configuration, Exchange exchange) throws MessagingException, IOException { String contentType = determineContentType(configuration, exchange); if (contentType != null) { LOG.trace("Using Content-Type {} for BodyPart: {}", contentType, part); // always store content in a byte array data store to avoid various content type and charset issues String data = exchange.getContext().getTypeConverter().tryConvertTo(String.class, exchange.getIn().getBody()); // use empty data if the body was null for some reason (otherwise there is a NPE) data = data != null ? data : ""; DataSource ds = new ByteArrayDataSource(data, contentType); part.setDataHandler(new DataHandler(ds)); // set the content type header afterwards part.setHeader("Content-Type", contentType); } return contentType; }
insert = exchange.getContext().getTypeConverter().tryConvertTo(DBObject.class, exchange, exchange.getIn().getBody()); if (insert == null) {
private Function<Exchange, Object> createDoDistinct() { return exchange -> { Iterable<String> result = new ArrayList<>(); MongoCollection<BasicDBObject> dbCol = calculateCollection(exchange); // get the parameters out of the Exchange Header String distinctFieldName = exchange.getIn().getHeader(MongoDbConstants.DISTINCT_QUERY_FIELD, String.class); BasicDBObject query = exchange.getContext().getTypeConverter().tryConvertTo(BasicDBObject.class, exchange, exchange.getIn().getBody()); DistinctIterable<String> ret; if (query != null) { ret = dbCol.distinct(distinctFieldName, query, String.class); } else { ret = dbCol.distinct(distinctFieldName, String.class); } try { ret.iterator().forEachRemaining(((List<String>) result)::add); exchange.getOut().setHeader(MongoDbConstants.RESULT_PAGE_SIZE, ((List<String>) result).size()); } finally { ret.iterator().close(); } return result; }; }
@Override public List<ServiceDefinition> getServices(String name) { try { final CamelContext ctx = getCamelContext(); final String host = ctx.resolvePropertyPlaceholders("{{service.host:" + name + "}}"); final String num = ctx.resolvePropertyPlaceholders("{{service.port:" + name + "}}"); final int port = ctx.getTypeConverter().tryConvertTo(int.class, num); return Collections.singletonList(new DefaultServiceDefinition(name, host, port)); } catch (IllegalArgumentException e) { return Collections.emptyList(); } catch (Exception e) { throw new RuntimeCamelException(e); } }
public ElsqlSqlMapSource(Exchange exchange, Object body) { this.exchange = exchange; this.bodyMap = safeMap(exchange.getContext().getTypeConverter().tryConvertTo(Map.class, body)); this.headersMap = safeMap(exchange.getIn().getHeaders()); }
private Iterable<?> getRecordValues(Exchange exchange, Object data) throws NoTypeConversionAvailableException { // each row must be a map or list based Map<?, ?> map = exchange.getContext().getTypeConverter().tryConvertTo(Map.class, exchange, data); if (map != null) { return getMapRecordValues(map); } return ExchangeHelper.convertToMandatoryType(exchange, List.class, data); }
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { BonitaConfiguration configuration = new BonitaConfiguration(); setProperties(configuration, parameters); BonitaOperation op = getCamelContext().getTypeConverter().tryConvertTo(BonitaOperation.class, remaining); if (op == null) { throw new IllegalArgumentException("Invalid operation " + remaining); } configuration.setOperation(op); return new BonitaEndpoint(uri, this, configuration); } }
} else if (body instanceof Serializable) { type = Object; } else if (exchange.getContext().getTypeConverter().tryConvertTo(File.class, body) != null || exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, body) != null) { type = streamingEnabled ? Stream : Bytes;
query = exchange.getContext().getTypeConverter().tryConvertTo(BasicDBObject.class, exchange, exchange.getIn().getBody());
DOMSource document = exchange.getContext().getTypeConverter().tryConvertTo(DOMSource.class, exchange, source); if (document != null) { li.set(document);
final ByteBuffer bodyAsByte = tc.tryConvertTo(ByteBuffer.class, body);
boolean isFault = exchange.getContext().getTypeConverter().tryConvertTo(boolean.class, faultHeader); log.debug("Transfer fault on OUT message: {}", isFault); if (isFault) {