org.apache.camel.Message.getMandatoryBody()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(241)

本文整理了Java中org.apache.camel.Message.getMandatoryBody()方法的一些代码示例,展示了Message.getMandatoryBody()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.getMandatoryBody()方法的具体详情如下:
包路径:org.apache.camel.Message
类名称:Message
方法名:getMandatoryBody

Message.getMandatoryBody介绍

暂无

代码示例

代码示例来源:origin: org.apache.camel/camel-test

/**
 * 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());
}

代码示例来源:origin: org.apache.camel/camel-test

/**
 * 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());
}

代码示例来源:origin: org.apache.camel/camel-hipchat

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

代码示例来源:origin: org.apache.camel/camel-azure

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

代码示例来源:origin: org.apache.camel/camel-beanstalk

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

代码示例来源:origin: org.apache.camel/camel-couchdb

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

代码示例来源:origin: org.apache.camel/camel-mongodb

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

代码示例来源:origin: org.openehealth.ipf.platform-camel/ipf-platform-camel-ihe-fhir-pixpdq

/**
 * 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));
  };
}

代码示例来源:origin: org.openehealth.ipf.platform-camel/ipf-platform-camel-ihe-fhir-dstu2-pixpdq

/**
 * 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));
  };
}

代码示例来源:origin: org.openehealth.ipf.platform-camel/ipf-platform-camel-ihe-hl7v2

/**
 * 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);
  });
}

代码示例来源:origin: org.apache.camel/camel-consul

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

代码示例来源:origin: org.apache.camel/camel-mongodb

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

代码示例来源:origin: org.apache.camel/camel-lucene

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

代码示例来源:origin: org.apache.camel/camel-mongodb

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

代码示例来源:origin: org.apache.camel/camel-dropbox

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

代码示例来源:origin: org.openehealth.ipf.platform-camel/ipf-platform-camel-ihe-hl7v2

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

代码示例来源:origin: org.apache.camel/camel-consul

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

代码示例来源:origin: org.apache.camel/camel-consul

@InvokeOnHeader(ConsulCatalogActions.DEREGISTER)
protected void deregister(Message message) throws Exception {
  getClient().deregister(message.getMandatoryBody(CatalogDeregistration.class));
  setBodyAndResult(message, null);
}

代码示例来源:origin: org.apache.camel/camel-consul

@InvokeOnHeader(ConsulPreparedQueryActions.CREATE)
protected void create(Message message) throws Exception {
  setBodyAndResult(
    message,
    getClient().createPreparedQuery(
      message.getMandatoryBody(PreparedQuery.class)
    )
  );
}

代码示例来源:origin: org.apache.camel/camel-consul

@InvokeOnHeader(ConsulCatalogActions.REGISTER)
protected void register(Message message) throws Exception {
  getClient().register(message.getMandatoryBody(CatalogRegistration.class));
  setBodyAndResult(message, null);
}

相关文章