Java Code Examples for org.springframework.amqp.core.MessageProperties#setContentType()

The following examples show how to use org.springframework.amqp.core.MessageProperties#setContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DmfReceiverService.java    From hawkbit-examples with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Method to validate if content type is set in the message properties.
 *
 * @param message
 *            the message to get validated
 */
private void checkContentTypeJson(final Message message) {
    if (message.getBody().length == 0) {
        return;
    }
    final MessageProperties messageProperties = message.getMessageProperties();
    final String headerContentType = (String) messageProperties.getHeaders().get("content-type");
    if (null != headerContentType) {
        messageProperties.setContentType(headerContentType);
    }
    final String contentType = messageProperties.getContentType();
    if (contentType != null && contentType.contains("json")) {
        return;
    }
    throw new AmqpRejectAndDontRequeueException("Content-Type is not JSON compatible");
}
 
Example 2
Source File: CustomMessageConverter.java    From easy-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Override
protected Message createMessage(Object object, MessageProperties props) {
  byte[] bytes = null;
  if (object instanceof byte[]) {
    bytes = (byte[]) object;
    props.setContentType(MessageProperties.CONTENT_TYPE_BYTES);
  } else if (object instanceof Serializable) {
    bytes = JSON.toJSONBytes(object);
    props.setContentType(MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT);
  }
  if (bytes != null) {
    props.setContentLength(bytes.length);
  }
  return new Message(bytes, props);
}
 
Example 3
Source File: AmqpReporter.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
private void processMessageProperties(MessageProperties messageProperties) {
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setContentEncoding("UTF-8");
    messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
    messageProperties.setHeader(MessageHeaders.HOST, OSUtils.getHostName());
    messageProperties.setAppId(AppInfo.getApplicationName());
    if(this.messagePropertiesCallback != null) {
        this.messagePropertiesCallback.call(messageProperties);
    }
}
 
Example 4
Source File: DmfSenderService.java    From hawkbit-examples with Eclipse Public License 1.0 5 votes vote down vote up
public void ping(final String tenant, final String correlationId) {
    final MessageProperties messageProperties = new MessageProperties();
    messageProperties.getHeaders().put(MessageHeaderKey.TENANT, tenant);
    messageProperties.getHeaders().put(MessageHeaderKey.TYPE, MessageType.PING.toString());
    messageProperties.setCorrelationId(correlationId);
    messageProperties.setReplyTo(amqpProperties.getSenderForSpExchange());
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);

    sendMessage(spExchange, new Message(null, messageProperties));
}
 
Example 5
Source File: DmfSenderService.java    From hawkbit-examples with Eclipse Public License 1.0 5 votes vote down vote up
private Message thingCreatedMessage(final String tenant, final String targetId) {
    final MessageProperties messagePropertiesForSP = new MessageProperties();
    messagePropertiesForSP.setHeader(MessageHeaderKey.TYPE, MessageType.THING_CREATED.name());
    messagePropertiesForSP.setHeader(MessageHeaderKey.TENANT, tenant);
    messagePropertiesForSP.setHeader(MessageHeaderKey.THING_ID, targetId);
    messagePropertiesForSP.setHeader(MessageHeaderKey.SENDER, "simulator");
    messagePropertiesForSP.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messagePropertiesForSP.setReplyTo(amqpProperties.getSenderForSpExchange());
    return new Message(null, messagePropertiesForSP);
}
 
Example 6
Source File: DmfSenderService.java    From hawkbit-examples with Eclipse Public License 1.0 5 votes vote down vote up
private MessageProperties createAttributeUpdateMessage(final String tenant, final String targetId) {
    final MessageProperties messagePropertiesForSP = new MessageProperties();
    messagePropertiesForSP.setHeader(MessageHeaderKey.TYPE, MessageType.EVENT.name());
    messagePropertiesForSP.setHeader(MessageHeaderKey.TOPIC, EventTopic.UPDATE_ATTRIBUTES);
    messagePropertiesForSP.setHeader(MessageHeaderKey.TENANT, tenant);
    messagePropertiesForSP.setHeader(MessageHeaderKey.THING_ID, targetId);
    messagePropertiesForSP.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messagePropertiesForSP.setReplyTo(amqpProperties.getSenderForSpExchange());
    return messagePropertiesForSP;
}
 
Example 7
Source File: AmqpMessageDispatcherService.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private static MessageProperties createConnectorMessageProperties(final String tenant, final String controllerId) {
    final MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setHeader(MessageHeaderKey.CONTENT_TYPE, MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setHeader(MessageHeaderKey.THING_ID, controllerId);
    messageProperties.setHeader(MessageHeaderKey.TENANT, tenant);
    return messageProperties;
}
 
Example 8
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@Description("Tests not allowed content-type in message")
public void wrongContentType() {
    final MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("xml");
    final Message message = new Message(new byte[0], messageProperties);
    assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
            .as(FAIL_MESSAGE_AMQP_REJECT_REASON + "due to wrong content type")
            .isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.THING_CREATED.name(), TENANT,
                    VIRTUAL_HOST));
}
 
Example 9
Source File: AmqpMessageHandlerServiceTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private MessageProperties createMessageProperties(final MessageType type, final String replyTo) {
    final MessageProperties messageProperties = new MessageProperties();
    if (type != null) {
        messageProperties.setHeader(MessageHeaderKey.TYPE, type.name());
    }
    messageProperties.setHeader(MessageHeaderKey.TENANT, TENANT);
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setReplyTo(replyTo);
    return messageProperties;
}
 
Example 10
Source File: AmqpControllerAuthenticationTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private MessageProperties createMessageProperties(final MessageType type, final String replyTo) {
    final MessageProperties messageProperties = new MessageProperties();
    if (type != null) {
        messageProperties.setHeader(MessageHeaderKey.TYPE, type.name());
    }
    messageProperties.setHeader(MessageHeaderKey.TENANT, TENANT);
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setReplyTo(replyTo);
    return messageProperties;
}
 
Example 11
Source File: AbstractAmqpIntegrationTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
protected Message createMessage(final Object payload, final MessageProperties messageProperties) {
    if (payload == null) {
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
        return new Message(null, messageProperties);
    }
    return getDmfClient().getMessageConverter().toMessage(payload, messageProperties);
}
 
Example 12
Source File: BaseAmqpServiceTest.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
private MessageProperties createJsonProperties() {
    final MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    return messageProperties;
}