/** * Asserts that the given exchange has an OUT message of the given body value * * @param exchange the exchange which should have an OUT message * @param expected the expected value of the OUT message * @throws InvalidPayloadException is thrown if the payload is not the expected class type */ public static void assertInMessageBodyEquals(Exchange exchange, Object expected) throws InvalidPayloadException { assertNotNull("Should have a response exchange!", exchange); Object actual; if (expected == null) { actual = exchange.getIn().getMandatoryBody(); assertEquals("in body of: " + exchange, expected, actual); } else { actual = exchange.getIn().getMandatoryBody(expected.getClass()); } assertEquals("in body of: " + exchange, expected, actual); LOG.debug("Received response: " + exchange + " with in: " + exchange.getIn()); }
/** * Asserts that the given exchange has an OUT message of the given body value * * @param exchange the exchange which should have an OUT message * @param expected the expected value of the OUT message * @throws InvalidPayloadException is thrown if the payload is not the expected class type */ public static void assertOutMessageBodyEquals(Exchange exchange, Object expected) throws InvalidPayloadException { assertNotNull("Should have a response exchange!", exchange); Object actual; if (expected == null) { actual = exchange.getOut().getMandatoryBody(); assertEquals("output body of: " + exchange, expected, actual); } else { actual = exchange.getOut().getMandatoryBody(expected.getClass()); } assertEquals("output body of: " + exchange, expected, actual); LOG.debug("Received response: " + exchange + " with out: " + exchange.getOut()); }
private Map<String, String> getCommonHttpPostParam(Exchange exchange) throws InvalidPayloadException { String format = exchange.getIn().getHeader(HipchatConstants.MESSAGE_FORMAT, "text", String.class); String notify = exchange.getIn().getHeader(HipchatConstants.TRIGGER_NOTIFY, String.class); Map<String, String> jsonMap = new HashMap<>(4); jsonMap.put(HipchatApiConstants.API_MESSAGE, exchange.getIn().getMandatoryBody(String.class)); if (notify != null) { jsonMap.put(HipchatApiConstants.API_MESSAGE_NOTIFY, notify); } jsonMap.put(HipchatApiConstants.API_MESSAGE_FORMAT, format); return jsonMap; }
private CloudQueueMessage getCloudQueueMessage(Exchange exchange) throws Exception { Object body = exchange.getIn().getMandatoryBody(); CloudQueueMessage message = null; if (body instanceof CloudQueueMessage) { message = (CloudQueueMessage)body; } else if (body instanceof String) { message = new CloudQueueMessage((String)body); } if (message == null) { throw new IllegalArgumentException("Unsupported queue message type:" + body.getClass().getName()); } return message; }
@Override public void act(final Client client, final Exchange exchange) throws NoSuchHeaderException, InvalidPayloadException { final Integer jobs = exchange.getIn().getMandatoryBody(Integer.class); final int result = client.kick(jobs); if (LOG.isDebugEnabled()) { LOG.debug(String.format("Kick %d jobs. Kicked %d actually.", jobs, result)); } final Message answer = getAnswerMessage(exchange); answer.setBody(result, Integer.class); } }
JsonElement getBodyAsJsonElement(Exchange exchange) throws InvalidPayloadException { Object body = exchange.getIn().getMandatoryBody(); if (body instanceof String) { try { return new JsonParser().parse((String) body); } catch (JsonSyntaxException jse) { throw new InvalidPayloadException(exchange, body.getClass()); } } else if (body instanceof JsonElement) { return (JsonElement) body; } else { throw new InvalidPayloadException(exchange, body != null ? body.getClass() : null); } }
private Function<Exchange, Object> createDoCommand() { return exchange -> { try { MongoDatabase db = calculateDb(exchange); BasicDBObject cmdObj = exchange.getIn().getMandatoryBody(BasicDBObject.class); return db.runCommand(cmdObj); } catch (InvalidPayloadException e) { throw new CamelMongoDbException("Invalid payload for command", e); } }; }
/** * Returns a processor for translating HL7v2 messages to FHIR * using the given translator instance. */ public static Processor translatorHL7v2ToFhir(final TranslatorHL7v2ToFhir translator) { return exchange -> { // String initial = exchange.getProperty(HL7V3_ORIGINAL_REQUEST_PROPERTY, String.class); ca.uhn.hl7v2.model.Message msg = exchange.getIn().getMandatoryBody(ca.uhn.hl7v2.model.Message.class); Map<String, Object> parameters = exchange.getIn().getHeaders(); // exchange.setProperty(HL7V3_ORIGINAL_REQUEST_PROPERTY, msg); org.apache.camel.Message resultMessage = Exchanges.resultMessage(exchange); resultMessage.getHeaders().putAll(exchange.getIn().getHeaders()); resultMessage.setBody(translator.translateHL7v2ToFhir(msg, parameters)); }; }
/** * Returns a processor for translating HL7v2 messages to FHIR * using the given translator instance. */ public static Processor translatorHL7v2ToFhir(final ToFhirTranslator<Message> translator) { return exchange -> { // String initial = exchange.getProperty(HL7V3_ORIGINAL_REQUEST_PROPERTY, String.class); ca.uhn.hl7v2.model.Message msg = exchange.getIn().getMandatoryBody(ca.uhn.hl7v2.model.Message.class); Map<String, Object> parameters = exchange.getIn().getHeaders(); // exchange.setProperty(HL7V3_ORIGINAL_REQUEST_PROPERTY, msg); org.apache.camel.Message resultMessage = Exchanges.resultMessage(exchange); resultMessage.getHeaders().putAll(exchange.getIn().getHeaders()); resultMessage.setBody(translator.translateToFhir(msg, parameters)); }; }
/** * Converts outgoing request to a {@link Message} * and performs some exchange configuration. */ @Override public void process(Exchange exchange) throws Exception { Message msg = exchange.getIn().getMandatoryBody(Message.class); messageTracer.sendMessage(msg, getEndpoint().getEndpointUri(), (message, span) -> { exchange.getIn().setBody(message, Message.class); getWrappedProcessor().process(exchange); }); }
@InvokeOnHeader(ConsulSessionActions.CREATE) protected void create(Message message) throws Exception { setBodyAndResult( message, getClient().createSession( message.getMandatoryBody(Session.class), message.getHeader(ConsulConstants.CONSUL_DATACENTER, String.class) ) ); }
private Function<Exchange, Object> createDoBulkWrite() { return exchange -> { try { MongoCollection<BasicDBObject> dbCol = calculateCollection(exchange); Boolean ordered = exchange.getIn().getHeader(MongoDbConstants.BULK_ORDERED, Boolean.TRUE, Boolean.class); BulkWriteOptions options = new BulkWriteOptions().ordered(ordered); @SuppressWarnings("unchecked") List<WriteModel<BasicDBObject>> requests = exchange.getIn().getMandatoryBody((Class<List<WriteModel<BasicDBObject>>>)(Class<?>)List.class); BulkWriteResult result = dbCol.bulkWrite(requests, options); return result; } catch (InvalidPayloadException e) { throw new CamelMongoDbException("Invalid payload for bulk write", e); } }; }
public void index(Exchange exchange) throws Exception { LOG.debug("Indexing {}", exchange); openIndexWriter(); Map<String, Object> headers = exchange.getIn().getHeaders(); add("exchangeId", exchange.getExchangeId(), true); for (Entry<String, Object> entry : headers.entrySet()) { String field = entry.getKey(); String value = exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, entry.getValue()); add(field, value, true); } add("contents", exchange.getIn().getMandatoryBody(String.class), true); closeIndexWriter(); }
private Function<Exchange, Object> createDoSave() { return exchange -> { try { MongoCollection<BasicDBObject> dbCol = calculateCollection(exchange); BasicDBObject saveObj = exchange.getIn().getMandatoryBody(BasicDBObject.class); UpdateOptions options = new UpdateOptions().upsert(true); BasicDBObject queryObject = new BasicDBObject("_id", saveObj.get("_id")); UpdateResult result = dbCol.replaceOne(queryObject, saveObj, options); exchange.getIn().setHeader(MongoDbConstants.OID, saveObj.get("_id")); return result; } catch (InvalidPayloadException e) { throw new CamelMongoDbException("Body incorrect type for save", e); } }; }
private FileMetadata putSingleBody(Exchange exchange, String dropboxPath, DropboxUploadMode mode) throws Exception { byte[] data = exchange.getIn().getMandatoryBody(byte[].class); InputStream is = new ByteArrayInputStream(data); try { FileMetadata uploadedFile; WriteMode uploadMode; if (mode == DropboxUploadMode.force) { uploadMode = WriteMode.OVERWRITE; } else { uploadMode = WriteMode.ADD; } uploadedFile = client.files().uploadBuilder(dropboxPath).withMode(uploadMode).uploadAndFinish(is, data.length); return uploadedFile; } finally { IOHelper.close(is); } }
@Override public void process(Exchange exchange) throws Exception { Message msg = exchange.getIn().getMandatoryBody(Message.class); messageTracer.receiveMessage(msg, getEndpoint().getEndpointUri(), (message, span) -> getWrappedProcessor().process(exchange)); }
@InvokeOnHeader(ConsulSessionActions.DESTROY) protected void destroy(Message message) throws Exception { String sessionId = message.getHeader(ConsulConstants.CONSUL_SESSION, String.class); if (ObjectHelper.isEmpty(sessionId)) { getClient().destroySession( message.getMandatoryBody(String.class), message.getHeader(ConsulConstants.CONSUL_DATACENTER, String.class) ); } else { getClient().destroySession( sessionId, message.getHeader(ConsulConstants.CONSUL_DATACENTER, String.class) ); } setBodyAndResult(message, null, true); }
@InvokeOnHeader(ConsulCatalogActions.DEREGISTER) protected void deregister(Message message) throws Exception { getClient().deregister(message.getMandatoryBody(CatalogDeregistration.class)); setBodyAndResult(message, null); }
@InvokeOnHeader(ConsulPreparedQueryActions.CREATE) protected void create(Message message) throws Exception { setBodyAndResult( message, getClient().createPreparedQuery( message.getMandatoryBody(PreparedQuery.class) ) ); }
@InvokeOnHeader(ConsulCatalogActions.REGISTER) protected void register(Message message) throws Exception { getClient().register(message.getMandatoryBody(CatalogRegistration.class)); setBodyAndResult(message, null); }