Example usage for org.springframework.amqp.support.converter MessageConversionException MessageConversionException

List of usage examples for org.springframework.amqp.support.converter MessageConversionException MessageConversionException

Introduction

In this page you can find the example usage for org.springframework.amqp.support.converter MessageConversionException MessageConversionException.

Prototype

public MessageConversionException(String message, Throwable cause) 

Source Link

Usage

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

@Override
protected Message createMessage(Object object, MessageProperties messageProperties) {
    try {/*from  w  ww  .  ja  v  a  2s .  c  o m*/
        byte[] body = null;
        if (object != null) {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            StaxWriter writer = new StaxWriter(new QNameMap(),
                    this.outputFactory.createXMLStreamWriter(outStream));
            this.objectMapper.marshal(object, writer);
            body = outStream.toByteArray();
        }

        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
        messageProperties.setContentEncoding(this.encoding);
        messageProperties.setContentLength(body != null ? body.length : 0);
        classMapper.fromClass(object.getClass(), messageProperties);
        return new Message(body, messageProperties);
    } catch (XMLStreamException ex) {
        String typeId = (String) messageProperties.getHeaders()
                .get(DefaultClassMapper.DEFAULT_CLASSID_FIELD_NAME);
        LOG.error("XMLStreamException trying to marshal message of type {}", typeId, ex);
        throw new MessageConversionException("Could not marshal 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 marshal message of type {}", typeId, ex);
        throw new MessageConversionException("Could not marshal message of type " + typeId, ex);
    }
}

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 a va  2  s  .  c o  m
        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:org.springframework.amqp.support.converter.Jackson2JsonMessageConverter.java

@Override
public Object fromMessage(Message message) throws MessageConversionException {
    Object content = null;//from   w  ww  .  j a  v a 2  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.Jackson2JsonMessageConverter.java

@Override
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties)
        throws MessageConversionException {
    byte[] bytes;
    try {// ww  w  .  j  a v  a  2 s . com
        String jsonString = this.jsonObjectMapper.writeValueAsString(objectToConvert);
        bytes = jsonString.getBytes(getDefaultCharset());
    } catch (IOException e) {
        throw new MessageConversionException("Failed to convert Message content", e);
    }
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setContentEncoding(getDefaultCharset());
    messageProperties.setContentLength(bytes.length);

    if (getClassMapper() == null) {
        getJavaTypeMapper().fromJavaType(this.jsonObjectMapper.constructType(objectToConvert.getClass()),
                messageProperties);

    } else {
        getClassMapper().fromClass(objectToConvert.getClass(), messageProperties);

    }

    return new Message(bytes, messageProperties);
}

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

@Override
public Object fromMessage(Message message) throws MessageConversionException {
    Object content = null;//from ww  w  .j  a v  a2s .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;
}

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

@Override
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties)
        throws MessageConversionException {
    byte[] bytes = null;
    try {// w w w .j  a  va2 s . c o  m
        String jsonString = jsonObjectMapper.writeValueAsString(objectToConvert);
        bytes = jsonString.getBytes(this.defaultCharset);
    } catch (UnsupportedEncodingException e) {
        throw new MessageConversionException("Failed to convert Message content", e);
    } catch (JsonGenerationException 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);
    }
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    messageProperties.setContentEncoding(this.defaultCharset);
    if (bytes != null) {
        messageProperties.setContentLength(bytes.length);
    }

    if (getClassMapper() == null) {
        getJavaTypeMapper().fromJavaType(type(objectToConvert.getClass()), messageProperties);

    } else {
        getClassMapper().fromClass(objectToConvert.getClass(), messageProperties);

    }

    return new Message(bytes, messageProperties);

}