@Override public void processPacket(final Stanza packet) { if (packet instanceof org.jivesoftware.smack.packet.Message) { org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) packet; Map<String, ?> mappedHeaders = headerMapper.toHeadersFromRequest(xmppMessage); String messageBody = xmppMessage.getBody(); /* * Since there are several types of chat messages with different ChatState (e.g., composing, paused etc) * we need to perform further validation since for now we only support messages that have * content (e.g., Use A says 'Hello' to User B). We don't yet support messages with no * content (e.g., User A is typing a message for User B etc.). * See https://jira.springsource.org/browse/INT-1728 * Also see: packet.getExtensions() */ if (StringUtils.hasText(messageBody)) { Object payload = (extractPayload ? messageBody : xmppMessage); AbstractIntegrationMessageBuilder<?> messageBuilder = ChatMessageListeningEndpoint.this .getMessageBuilderFactory() .withPayload(payload) .copyHeaders(mappedHeaders); sendMessage(messageBuilder.build()); } } }
private Message<?> sendAndReceive( String exchangeName, String routingKey, Message<?> requestMessage, CorrelationData correlationData) { Assert.isInstanceOf( RabbitTemplate.class, this.amqpTemplate, "RabbitTemplate implementation is required for publisher confirms"); MessageConverter converter = ((RabbitTemplate) this.amqpTemplate).getMessageConverter(); MessageProperties amqpMessageProperties = new MessageProperties(); org.springframework.amqp.core.Message amqpMessage = converter.toMessage(requestMessage.getPayload(), amqpMessageProperties); this.headerMapper.fromHeadersToRequest(requestMessage.getHeaders(), amqpMessageProperties); checkDeliveryMode(requestMessage, amqpMessageProperties); org.springframework.amqp.core.Message amqpReplyMessage = ((RabbitTemplate) this.amqpTemplate) .sendAndReceive(exchangeName, routingKey, amqpMessage, correlationData); if (amqpReplyMessage == null) { return null; } Object replyObject = converter.fromMessage(amqpReplyMessage); AbstractIntegrationMessageBuilder<?> builder = (replyObject instanceof Message) ? this.getMessageBuilderFactory().fromMessage((Message<?>) replyObject) : this.getMessageBuilderFactory().withPayload(replyObject); Map<String, ?> headers = this.headerMapper.toHeadersFromReply(amqpReplyMessage.getMessageProperties()); builder.copyHeadersIfAbsent(headers); return builder.build(); }
protected final void addStandardHeaders( TcpConnection connection, AbstractIntegrationMessageBuilder<?> messageBuilder) { String connectionId = connection.getConnectionId(); messageBuilder .setHeader(IpHeaders.HOSTNAME, connection.getHostName()) .setHeader(IpHeaders.IP_ADDRESS, connection.getHostAddress()) .setHeader(IpHeaders.REMOTE_PORT, connection.getPort()) .setHeader(IpHeaders.CONNECTION_ID, connectionId); if (this.applySequence) { messageBuilder .setCorrelationId(connectionId) .setSequenceNumber((int) connection.incrementAndGetConnectionSequence()); } }
protected final void addCustomHeaders( TcpConnection connection, AbstractIntegrationMessageBuilder<?> messageBuilder) { Map<String, ?> customHeaders = this.supplyCustomHeaders(connection); if (customHeaders != null) { messageBuilder.copyHeadersIfAbsent(customHeaders); } }
@Override public Message<?> toMessage(TcpConnection connection) throws Exception { Message<Object> message = null; Object payload = connection.getPayload(); if (payload != null) { AbstractIntegrationMessageBuilder<Object> messageBuilder = this.messageBuilderFactory.withPayload(payload); this.addStandardHeaders(connection, messageBuilder); this.addCustomHeaders(connection, messageBuilder); message = messageBuilder.build(); } else { if (logger.isWarnEnabled()) { logger.warn("Null payload from connection " + connection.getConnectionId()); } } return message; }
@Override public Message<?> toMessage(String topic, MqttMessage mqttMessage) { try { AbstractIntegrationMessageBuilder<Object> messageBuilder = getMessageBuilderFactory() .withPayload(mqttBytesToPayload(mqttMessage)) .setHeader(MqttHeaders.QOS, mqttMessage.getQos()) .setHeader(MqttHeaders.DUPLICATE, mqttMessage.isDuplicate()) .setHeader(MqttHeaders.RETAINED, mqttMessage.isRetained()); if (topic != null) { messageBuilder.setHeader(MqttHeaders.TOPIC, topic); } return messageBuilder.build(); } catch (Exception e) { throw new MessageConversionException("failed to convert object to Message", e); } }
@Override public Message<?> toMessage(Object object, 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.build(); }
@Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { Object userCorrelationData = correlationData; if (correlationData == null) { if (logger.isDebugEnabled()) { logger.debug("No correlation data provided for ack: " + ack + " cause:" + cause); } return; } if (correlationData instanceof CorrelationDataWrapper) { userCorrelationData = ((CorrelationDataWrapper) correlationData).getUserData(); } Map<String, Object> headers = new HashMap<String, Object>(); headers.put(AmqpHeaders.PUBLISH_CONFIRM, ack); if (!ack && StringUtils.hasText(cause)) { headers.put(AmqpHeaders.PUBLISH_CONFIRM_NACK_CAUSE, cause); } AbstractIntegrationMessageBuilder<?> builder = userCorrelationData instanceof Message ? this.getMessageBuilderFactory().fromMessage((Message<?>) userCorrelationData) : this.getMessageBuilderFactory().withPayload(userCorrelationData); Message<?> confirmMessage = builder.copyHeaders(headers).build(); if (ack && this.confirmAckChannel != null) { this.confirmAckChannel.send(confirmMessage); } else if (!ack && this.confirmNackChannel != null) { this.confirmNackChannel.send(confirmMessage); } else { if (logger.isInfoEnabled()) { logger.info( "Nowhere to send publisher confirm " + (ack ? "ack" : "nack") + " for " + userCorrelationData); } } }
@Override public void returnedMessage( org.springframework.amqp.core.Message message, int replyCode, String replyText, String exchange, String routingKey) { // safe to cast; we asserted we have a RabbitTemplate in doInit() MessageConverter converter = ((RabbitTemplate) this.amqpTemplate).getMessageConverter(); Object returnedObject = converter.fromMessage(message); AbstractIntegrationMessageBuilder<?> builder = (returnedObject instanceof Message) ? this.getMessageBuilderFactory().fromMessage((Message<?>) returnedObject) : this.getMessageBuilderFactory().withPayload(returnedObject); Map<String, ?> headers = this.headerMapper.toHeadersFromReply(message.getMessageProperties()); builder .copyHeadersIfAbsent(headers) .setHeader(AmqpHeaders.RETURN_REPLY_CODE, replyCode) .setHeader(AmqpHeaders.RETURN_REPLY_TEXT, replyText) .setHeader(AmqpHeaders.RETURN_EXCHANGE, exchange) .setHeader(AmqpHeaders.RETURN_ROUTING_KEY, routingKey); this.returnChannel.send(builder.build()); }