org.springframework.integration.support.AbstractIntegrationMessageBuilder.copyHeaders()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(13.8k)|赞(0)|评价(0)|浏览(154)

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

AbstractIntegrationMessageBuilder.copyHeaders介绍

[英]Copy the name-value pairs from the provided Map. This operation will overwrite any existing values. Use { #copyHeadersIfAbsent(Map) to avoid overwriting values. Note that the 'id' and 'timestamp' header values will never be overwritten.
[中]从提供的映射复制名称-值对。此操作将覆盖任何现有值。使用{#CopyHeadersFabSent(Map)避免覆盖值。请注意,“id”和“timestamp”头值永远不会被覆盖。

代码示例

代码示例来源:origin: spring-projects/spring-integration

private void processMapArgument(Object messageOrPayload, boolean foundPayloadAnnotation,
    Map<String, Object> headersToPopulate, Map<?, ?> argumentValue) {
  if (messageOrPayload instanceof Map && !foundPayloadAnnotation) {
    if (GatewayMethodInboundMessageMapper.this.payloadExpression == null) {
      throw new MessagingException("Ambiguous method parameters; found more than one " +
          "Map-typed parameter and neither one contains a @Payload annotation");
    }
  }
  copyHeaders(argumentValue, headersToPopulate);
}

代码示例来源:origin: spring-projects/spring-integration

@Nullable
@Override
public Message<?> toMessage(Object object, @Nullable MessageHeaders messageHeaders) {
  Assert.isInstanceOf(Map.class, object, "This converter expects a Map");
  @SuppressWarnings("unchecked")
  Map<String, ?> map = (Map<String, ?>) object;
  Object payload = map.get("payload");
  Assert.notNull(payload, "'payload' entry cannot be null");
  AbstractIntegrationMessageBuilder<?> messageBuilder = getMessageBuilderFactory().withPayload(payload);
  @SuppressWarnings("unchecked")
  Map<String, ?> headers = (Map<String, ?>) map.get("headers");
  if (headers != null) {
    if (this.filterHeadersInToMessage) {
      headers.keySet().retainAll(Arrays.asList(this.headerNames));
    }
    messageBuilder.copyHeaders(headers);
  }
  return messageBuilder
      .copyHeadersIfAbsent(messageHeaders)
      .build();
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public Message<?> fromSyslog(Message<?> message) throws Exception {
  Map<String, ?> map = this.transformer.doTransform(message);
  Map<String, Object> out = new HashMap<String, Object>();
  for (Entry<String, ?> entry : map.entrySet()) {
    String key = entry.getKey();
    if (!SYSLOG_PAYLOAD_ENTRIES.contains(key)) {
      out.put(SyslogHeaders.PREFIX + entry.getKey(), entry.getValue());
    }
  }
  return getMessageBuilderFactory().withPayload(this.asMap ? map : message.getPayload())
      .copyHeaders(out)
      .build();
}

代码示例来源:origin: spring-projects/spring-integration

@Override
protected Object doTransform(Message<?> message) throws Exception {
  Assert.notNull(message, "message must not be null");
  UUID id = message.getHeaders().getId();
  Assert.notNull(id, "ID header must not be null");
  this.messageStore.addMessage(message);
  AbstractIntegrationMessageBuilder<?> responseBuilder = getMessageBuilderFactory().withPayload(id);
  // headers on the 'current' message take precedence
  responseBuilder.copyHeaders(message.getHeaders());
  return responseBuilder.build();
}

代码示例来源:origin: spring-projects/spring-integration

@Override
protected Object doTransform(Message<?> message) throws Exception {
  if (this.targetClass != null) {
    return this.jsonObjectMapper.fromJson(message.getPayload(), this.targetClass);
  }
  else {
    Object result = this.jsonObjectMapper.fromJson(message.getPayload(), message.getHeaders());
    AbstractIntegrationMessageBuilder<Object> messageBuilder = this.getMessageBuilderFactory().withPayload(result)
        .copyHeaders(message.getHeaders())
        .removeHeaders(JsonHeaders.HEADERS.toArray(new String[3]));
    return messageBuilder.build();
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Override
protected Object doTransform(Message<?> message) throws Exception {
  Assert.notNull(message, "message must not be null");
  Assert.isTrue(message.getPayload() instanceof UUID, "payload must be a UUID");
  UUID id = (UUID) message.getPayload();
  Message<?> retrievedMessage;
  if (this.removeMessage) {
    retrievedMessage = this.messageStore.removeMessage(id);
    if (logger.isDebugEnabled()) {
      logger.debug("Removed Message with claim-check '" + id + "' from the MessageStore.");
    }
  }
  else {
    retrievedMessage = this.messageStore.getMessage(id);
  }
  Assert.notNull(retrievedMessage, "unable to locate Message for ID: " + id
      + " within MessageStore [" + this.messageStore + "]");
  AbstractIntegrationMessageBuilder<?> responseBuilder = this.getMessageBuilderFactory().fromMessage(retrievedMessage);
  // headers on the 'current' message take precedence
  responseBuilder.copyHeaders(message.getHeaders());
  return responseBuilder.build();
}

代码示例来源:origin: spring-projects/spring-integration

@Override
protected Object doTransform(Message<?> message) throws Exception {
  Object payload = buildJsonPayload(message.getPayload());
  Map<String, Object> headers = new LinkedCaseInsensitiveMap<>();
  headers.putAll(message.getHeaders());
  if (headers.containsKey(MessageHeaders.CONTENT_TYPE)) {
    if (this.contentTypeExplicitlySet) {
      // override, unless empty
      if (StringUtils.hasLength(this.contentType)) {
        headers.put(MessageHeaders.CONTENT_TYPE, this.contentType);
      }
    }
  }
  else if (StringUtils.hasLength(this.contentType)) {
    headers.put(MessageHeaders.CONTENT_TYPE, this.contentType);
  }
  this.jsonObjectMapper.populateJavaTypes(headers, message.getPayload());
  return getMessageBuilderFactory()
      .withPayload(payload)
      .copyHeaders(headers)
      .build();
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public void handleFrame(StompHeaders headers, @Nullable Object body) {
  Message<?> message;
  if (body == null) {
    logger.info("No body in STOMP frame: nothing to produce.");
    return;
  }
  else if (body instanceof Message) {
    message = (Message<?>) body;
  }
  else {
    Map<String, Object> headersToCopy =
        StompInboundChannelAdapter.this.headerMapper.toHeaders(headers);
    message =
        getMessageBuilderFactory()
            .withPayload(body)
            .copyHeaders(headersToCopy)
            .build();
  }
  sendMessage(message);
}

代码示例来源:origin: spring-projects/spring-integration

private AbstractIntegrationMessageBuilder<?> buildReply(javax.jms.Message jmsReply) throws JMSException {
  Object result = jmsReply;
  if (this.extractReplyPayload) {
    result = this.messageConverter.fromMessage(jmsReply);
    if (logger.isDebugEnabled()) {
      logger.debug("converted JMS Message [" + jmsReply + "] to integration Message payload [" + result + "]");
    }
  }
  Map<String, Object> jmsReplyHeaders = this.headerMapper.toHeaders(jmsReply);
  if (this.replyContainer != null && this.correlationKey != null) {
    // do not propagate back the gateway's internal correlation id
    jmsReplyHeaders.remove(this.correlationKey);
  }
  if (result instanceof Message) {
    return getMessageBuilderFactory().fromMessage((Message<?>) result).copyHeaders(jmsReplyHeaders);
  }
  else {
    return getMessageBuilderFactory().withPayload(result).copyHeaders(jmsReplyHeaders);
  }
}

代码示例来源:origin: spring-projects/spring-integration

private void produceMessage(Message<?> message, SimpMessageHeaderAccessor headerAccessor) {
  headerAccessor.removeHeader(SimpMessageHeaderAccessor.NATIVE_HEADERS);
  Object payload = this.messageConverter.fromMessage(message, this.payloadType.get());
  Assert.state(payload != null,
      () -> "The message converter '" + this.messageConverter +
          "' produced no payload for message '" + message +
          "' and expected payload type: " + this.payloadType.get());
  Message<Object> messageToSend =
      getMessageBuilderFactory()
          .withPayload(payload)
          .copyHeaders(headerAccessor.toMap())
          .build();
  sendMessage(messageToSend);
}

代码示例来源:origin: spring-projects/spring-integration

private org.springframework.messaging.Message<Object> createMessage(Message message, Channel channel) {
  Object payload = AmqpInboundChannelAdapter.this.messageConverter.fromMessage(message);
  Map<String, Object> headers = AmqpInboundChannelAdapter.this.headerMapper
      .toHeadersFromRequest(message.getMessageProperties());
  if (AmqpInboundChannelAdapter.this.messageListenerContainer.getAcknowledgeMode()
      == AcknowledgeMode.MANUAL) {
    headers.put(AmqpHeaders.DELIVERY_TAG, message.getMessageProperties().getDeliveryTag());
    headers.put(AmqpHeaders.CHANNEL, channel);
  }
  if (AmqpInboundChannelAdapter.this.retryTemplate != null) {
    headers.put(IntegrationMessageHeaderAccessor.DELIVERY_ATTEMPT, new AtomicInteger());
  }
  final org.springframework.messaging.Message<Object> messagingMessage = getMessageBuilderFactory()
      .withPayload(payload)
      .copyHeaders(headers)
      .build();
  return messagingMessage;
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public Message<?> transform(Message<?> message) {
  Object payload = message.getPayload();
  if (!(payload instanceof javax.mail.Message)) {
    throw new MessageTransformationException(message, getClass().getSimpleName()
        + " requires a javax.mail.Message payload");
  }
  javax.mail.Message mailMessage = (javax.mail.Message) payload;
  AbstractIntegrationMessageBuilder<T> builder;
  try {
    builder = this.doTransform(mailMessage);
  }
  catch (Exception e) {
    throw new MessageTransformationException(message, "failed to transform mail message", e);
  }
  if (builder == null) {
    throw new MessageTransformationException(message, "failed to transform mail message");
  }
  builder.copyHeaders(extractHeaderMapFromMailMessage(mailMessage));
  return builder.build();
}

代码示例来源:origin: spring-projects/spring-integration

protected void fromSoapHeaders(MessageContext messageContext, AbstractIntegrationMessageBuilder<?> builder) {
  WebServiceMessage request = messageContext.getRequest();
  String[] propertyNames = messageContext.getPropertyNames();
  if (propertyNames != null) {
    for (String propertyName : propertyNames) {
      builder.setHeader(propertyName, messageContext.getProperty(propertyName));
    }
  }
  if (request instanceof SoapMessage) {
    SoapMessage soapMessage = (SoapMessage) request;
    Map<String, ?> headers = this.headerMapper.toHeadersFromRequest(soapMessage);
    if (!CollectionUtils.isEmpty(headers)) {
      builder.copyHeaders(headers);
    }
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public final Message<?> transform(Message<?> message) {
  try {
    Object result = this.doTransform(message);
    if (result == null) {
      return null;
    }
    return (result instanceof Message) ? (Message<?>) result
        : this.getMessageBuilderFactory().withPayload(result).copyHeaders(message.getHeaders()).build();
  }
  catch (MessageTransformationException e) {
    throw e;
  }
  catch (Exception e) {
    throw new MessageTransformationException(message, "failed to transform message", e);
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public Message<?> toMessage(Object payload, MessageHeaders headers) {
  Assert.isInstanceOf(byte[].class, payload);
  try {
    Message<?> decoded = (Message<?>) this.codec.decode((byte[]) payload, this.messageClass);
    if (headers != null) {
      AbstractIntegrationMessageBuilder<?> builder = getMessageBuilderFactory().fromMessage(decoded);
      builder.copyHeaders(headers);
      return builder.build();
    }
    else {
      return decoded;
    }
  }
  catch (IOException e) {
    throw new MessagingException("Failed to decode", e);
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public Object extractData(WebServiceMessage message)
    throws IOException, TransformerException {
  Object resultObject = this.doExtractData(message);
  if (resultObject != null && message instanceof SoapMessage) {
    Map<String, Object> mappedMessageHeaders =
        AbstractWebServiceOutboundGateway.this.headerMapper.toHeadersFromReply((SoapMessage) message);
    return AbstractWebServiceOutboundGateway.this.getMessageBuilderFactory()
        .withPayload(resultObject)
        .copyHeaders(mappedMessageHeaders)
        .build();
  }
  else {
    return resultObject;
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public void handleFrame(StompHeaders headers, Object payload) {
  Object thePayload = payload;
  if (thePayload == null) {
    thePayload = headers.getFirst(StompHeaderAccessor.STOMP_MESSAGE_HEADER);
  }
  if (thePayload != null) {
    Message<?> failedMessage = getMessageBuilderFactory().withPayload(thePayload)
        .copyHeaders(StompMessageHandler.this.headerMapper.toHeaders(headers))
        .build();
    MessagingException exception =
        new MessageDeliveryException(failedMessage, "STOMP frame handling error.");
    if (StompMessageHandler.this.applicationEventPublisher != null) {
      StompMessageHandler.this.applicationEventPublisher.publishEvent(
          new StompExceptionEvent(StompMessageHandler.this, exception));
    }
    else {
      logger.error(exception);
    }
  }
}

代码示例来源:origin: spring-projects/spring-integration

protected Message<Object> buildMessage(org.springframework.amqp.core.Message message, Object converted) {
  AbstractIntegrationMessageBuilder<Object> messageBuilder =
      this.messageBuilderFactory.withPayload(converted);
  if (this.channel.isExtractPayload()) {
    Map<String, Object> headers =
        this.inboundHeaderMapper.toHeadersFromRequest(message.getMessageProperties());
    messageBuilder.copyHeaders(headers);
  }
  return messageBuilder.build();
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public Message<?> doInParser(JsonInboundMessageMapper messageMapper, String jsonMessage,
    @Nullable Map<String, Object> headers) throws Exception {
  if (this.messageMapper == null) {
    this.messageMapper = messageMapper;
  }
  P parser = this.createJsonParser(jsonMessage);
  if (messageMapper.isMapToPayload()) {
    Object payload = readPayload(parser, jsonMessage);
    return getMessageBuilderFactory()
        .withPayload(payload)
        .copyHeaders(headers)
        .build();
  }
  else {
    return parseWithHeaders(parser, jsonMessage, headers);
  }
}

代码示例来源:origin: spring-projects/spring-integration

private Object[] convertMessagesIfNecessary(MimeMessage[] filteredMessages) {
  if (this.headerMapper != null) {
    org.springframework.messaging.Message<?>[] converted =
        new org.springframework.messaging.Message<?>[filteredMessages.length];
    int n = 0;
    for (MimeMessage message : filteredMessages) {
      Map<String, Object> headers = this.headerMapper.toHeaders(message);
      converted[n++] =
          getMessageBuilderFactory()
              .withPayload(extractContent(message, headers))
              .copyHeaders(headers)
              .build();
    }
    return converted;
  }
  else {
    return filteredMessages;
  }
}

相关文章