Example usage for org.springframework.amqp.core MessageProperties getContentType

List of usage examples for org.springframework.amqp.core MessageProperties getContentType

Introduction

In this page you can find the example usage for org.springframework.amqp.core MessageProperties getContentType.

Prototype

public String getContentType() 

Source Link

Usage

From source file:amqp.spring.converter.ContentTypeConverterFactory.java

@Override
protected Message createMessage(Object object, MessageProperties messageProperties) {
    String contentType = messageProperties.getContentType();
    MessageConverter converter = converters.get(contentType);
    if (converter == null) //Try to fall back
        converter = this.fallbackConverter;
    if (converter == null) //Can't even fall back, punt
        throw new MessageConversionException("Cannot find converter for content type of " + contentType);

    return converter.toMessage(object, messageProperties);
}

From source file:amqp.spring.converter.ContentTypeConverterFactory.java

@Override
public Object fromMessage(Message message) throws MessageConversionException {
    MessageProperties messageProperties = message.getMessageProperties();
    String contentType = messageProperties.getContentType();
    if (messageProperties == null)
        throw new MessageConversionException("Cannot decode a message with no properties!");

    MessageConverter converter = converters.get(contentType);
    if (converter == null) //Try to fall back
        converter = this.fallbackConverter;
    if (converter == null) //Can't even fall back, punt
        throw new MessageConversionException("Cannot find converter for content type of " + contentType);

    return converter.fromMessage(message);
}

From source file:com.jbrisbin.vpc.jobsched.batch.BatchMessageConverter.java

public Object fromMessage(Message message) throws MessageConversionException {
    MessageProperties props = message.getMessageProperties();
    if (isAuthorized(props) && props.getContentType().equals("application/zip")) {
        BatchMessage msg = new BatchMessage();
        msg.setId(new String(props.getCorrelationId()));
        String timeout = "2";
        if (props.getHeaders().containsKey("timeout")) {
            timeout = props.getHeaders().get("timeout").toString();
        }/* w  w w  .j ava 2s .com*/
        msg.setTimeout(Integer.parseInt(timeout));

        ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(message.getBody()));
        ZipEntry zentry;
        try {
            while (null != (zentry = zin.getNextEntry())) {
                byte[] buff = new byte[4096];
                StringWriter json = new StringWriter();
                for (int bytesRead = 0; bytesRead > -1; bytesRead = zin.read(buff)) {
                    json.write(new String(buff, 0, bytesRead));
                }
                msg.getMessages().put(zentry.getName(), json.toString());
            }
        } catch (IOException e) {
            throw new MessageConversionException(e.getMessage(), e);
        }

        return msg;
    } else {
        throw new MessageConversionException("Invalid security key.");
    }
}

From source file:amqp.spring.converter.StringConverter.java

@Override
public Object fromMessage(Message message) throws MessageConversionException {
    MessageProperties messageProperties = message.getMessageProperties();
    if (messageProperties == null)
        throw new MessageConversionException("Cannot decode a message with no properties!");

    byte[] body = message.getBody();
    if (body == null)
        return null;

    String messageEncoding = messageProperties.getContentEncoding();
    if (messageEncoding == null)
        messageEncoding = this.encoding;

    String messageContentType = messageProperties.getContentType();
    if (this.contentType != null && !this.contentType.equalsIgnoreCase(messageContentType))
        throw new MessageConversionException("Cannot understand a message of type " + messageContentType);

    try {// ww  w . j  a v  a 2s.  co m
        return new String(body, messageEncoding);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Cannot dencode strings as {}", this.encoding, ex);
        throw new MessageConversionException("Cannot dencode strings as " + this.encoding, ex);
    }
}

From source file:amqp.spring.converter.XStreamConverter.java

@Override
public Object fromMessage(Message message) throws MessageConversionException {
    MessageProperties messageProperties = message.getMessageProperties();
    if (messageProperties == null)
        throw new MessageConversionException("Cannot decode a message with no properties!");

    byte[] body = message.getBody();
    if (body == null)
        return null;

    String messageEncoding = messageProperties.getContentEncoding();
    if (messageEncoding == null)
        messageEncoding = getEncoding();

    String contentType = messageProperties.getContentType();
    if (!MessageProperties.CONTENT_TYPE_JSON.equalsIgnoreCase(contentType))
        throw new MessageConversionException("Cannot understand a message of type " + contentType);

    try {//from ww  w . ja  va 2s  . c  o  m
        ByteArrayInputStream inStream = new ByteArrayInputStream(body);
        StaxReader reader = new StaxReader(new QNameMap(),
                this.inputFactory.createXMLStreamReader(inStream, messageEncoding));
        return this.objectMapper.unmarshal(reader);
    } catch (XMLStreamException ex) {
        String typeId = (String) messageProperties.getHeaders()
                .get(DefaultClassMapper.DEFAULT_CLASSID_FIELD_NAME);
        LOG.error("XMLStreamException trying to unmarshal message of type {}", typeId, ex);
        throw new MessageConversionException("Could not unmarshal message of type " + typeId, ex);
    } catch (XStreamException ex) {
        //For some reason messages appear to be getting eaten at this stage... very nasty when you try to troubleshoot.
        String typeId = (String) messageProperties.getHeaders()
                .get(DefaultClassMapper.DEFAULT_CLASSID_FIELD_NAME);
        LOG.error("XStreamException trying to unmarshal message of type {}", typeId, ex);
        throw new MessageConversionException("Could not unmarshal message of type " + typeId, ex);
    }
}

From source file:org.springframework.amqp.rabbit.support.RabbitUtils.java

public static BasicProperties extractBasicProperties(Message message, String charset) {
    if (message == null || message.getMessageProperties() == null) {
        return null;
    }/*from w  ww .j a  v a 2  s.  c o m*/
    MessageProperties source = message.getMessageProperties();
    BasicProperties target = new BasicProperties();
    target.setHeaders(source.getHeaders());
    target.setTimestamp(source.getTimestamp());
    target.setMessageId(source.getMessageId());
    target.setUserId(source.getUserId());
    target.setAppId(source.getAppId());
    target.setClusterId(source.getClusterId());
    target.setType(source.getType());
    MessageDeliveryMode deliveryMode = source.getDeliveryMode();
    if (deliveryMode != null) {
        target.setDeliveryMode(MessageDeliveryMode.toInt(deliveryMode));
    }
    target.setExpiration(source.getExpiration());
    target.setPriority(source.getPriority());
    target.setContentType(source.getContentType());
    target.setContentEncoding(source.getContentEncoding());
    byte[] correlationId = source.getCorrelationId();
    if (correlationId != null && correlationId.length > 0) {
        try {
            target.setCorrelationId(new String(correlationId, charset));
        } catch (UnsupportedEncodingException ex) {
            throw new AmqpUnsupportedEncodingException(ex);
        }
    }
    Address replyTo = source.getReplyTo();
    if (replyTo != null) {
        target.setReplyTo(replyTo.toString());
    }
    return target;
}

From source file:org.springframework.amqp.support.converter.Jackson2JsonMessageConverter.java

@Override
public Object fromMessage(Message message) throws MessageConversionException {
    Object content = null;/*  w  w w  . j ava2 s . co  m*/
    MessageProperties properties = message.getMessageProperties();
    if (properties != null) {
        String contentType = properties.getContentType();
        if (contentType != null && contentType.contains("json")) {
            String encoding = properties.getContentEncoding();
            if (encoding == null) {
                encoding = getDefaultCharset();
            }
            try {

                if (getClassMapper() == null) {
                    JavaType targetJavaType = getJavaTypeMapper().toJavaType(message.getMessageProperties());
                    content = convertBytesToObject(message.getBody(), encoding, targetJavaType);
                } else {
                    Class<?> targetClass = getClassMapper().toClass(message.getMessageProperties());
                    content = convertBytesToObject(message.getBody(), encoding, targetClass);
                }
            } catch (IOException e) {
                throw new MessageConversionException("Failed to convert Message content", e);
            }
        } else {
            if (log.isWarnEnabled()) {
                log.warn("Could not convert incoming message with content-type [" + contentType + "]");
            }
        }
    }
    if (content == null) {
        content = message.getBody();
    }
    return content;
}

From source file:org.springframework.amqp.support.converter.JsonMessageConverter.java

@Override
public Object fromMessage(Message message) throws MessageConversionException {
    Object content = null;//from w w w .ja  v a2  s. co  m
    MessageProperties properties = message.getMessageProperties();
    if (properties != null) {
        String contentType = properties.getContentType();
        if (contentType != null && contentType.contains("json")) {
            String encoding = properties.getContentEncoding();
            if (encoding == null) {
                encoding = this.defaultCharset;
            }
            try {

                if (getClassMapper() == null) {
                    JavaType targetJavaType = getJavaTypeMapper().toJavaType(message.getMessageProperties());
                    content = convertBytesToObject(message.getBody(), encoding, targetJavaType);
                } else {
                    Class<?> targetClass = getClassMapper().toClass(message.getMessageProperties());
                    content = convertBytesToObject(message.getBody(), encoding, targetClass);
                }
            } catch (UnsupportedEncodingException e) {
                throw new MessageConversionException("Failed to convert json-based Message content", e);
            } catch (JsonParseException e) {
                throw new MessageConversionException("Failed to convert Message content", e);
            } catch (JsonMappingException e) {
                throw new MessageConversionException("Failed to convert Message content", e);
            } catch (IOException e) {
                throw new MessageConversionException("Failed to convert Message content", e);
            }
        } else {
            log.warn("Could not convert incoming message with content-type [" + contentType + "]");
        }
    }
    if (content == null) {
        content = message.getBody();
    }
    return content;
}