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

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

Introduction

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

Prototype

public String getContentEncoding() 

Source Link

Usage

From source file:io.meles.amqp.spring.SnappyMessageConverter.java

@Override
public Object fromMessage(final Message message) throws MessageConversionException {
    final MessageProperties compressedProperties = message.getMessageProperties();
    if (!"snappy".equals(compressedProperties.getContentEncoding())) {
        return underlyingConverter.fromMessage(message);
    }//  w  ww  .  ja v  a  2s.  c o m

    final byte[] compressedBody = message.getBody();
    final byte[] decompressedBody = compressor.decompress(compressedBody);

    // TODO should we copy the properties
    compressedProperties.setContentEncoding(null);
    return underlyingConverter.fromMessage(new Message(decompressedBody, compressedProperties));
}

From source file:org.resthub.rpc.AMQPProxy.java

/**
 * Handles the object invocation.//from  w ww.j  a v a2  s.co m
 *
 * @param proxy  the proxy object to invoke
 * @param method the method to call
 * @param args   the arguments to the proxy object
 */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    String methodName = method.getName();
    Class<?>[] params = method.getParameterTypes();

    // equals and hashCode are special cased
    if (methodName.equals("equals") && params.length == 1 && params[0].equals(Object.class)) {
        Object value = args[0];
        if (value == null || !Proxy.isProxyClass(value.getClass())) {
            return Boolean.FALSE;
        }

        AMQPProxy handler = (AMQPProxy) Proxy.getInvocationHandler(value);

        return _factory.equals(handler._factory);
    } else if (methodName.equals("hashCode") && params.length == 0) {
        return _factory.hashCode();
    } else if (methodName.equals("toString") && params.length == 0) {
        return "[HessianProxy " + proxy.getClass() + "]";
    }

    ConnectionFactory connectionFactory = _factory.getConnectionFactory();

    Message response = sendRequest(connectionFactory, method, args);

    if (response == null) {
        throw new TimeoutException();
    }

    MessageProperties props = response.getMessageProperties();
    boolean compressed = "deflate".equals(props.getContentEncoding());

    InputStream is = new ByteArrayInputStream(response.getBody());
    if (compressed) {
        is = new InflaterInputStream(is, new Inflater(true));
    }

    return _factory.getSerializationHandler().readObject(method.getReturnType(), is);
}

From source file:org.resthub.rpc.AMQPHessianProxy.java

/**
 * Handles the object invocation./*from  w  w  w .  j a v  a2  s  . com*/
 *
 * @param proxy  the proxy object to invoke
 * @param method the method to call
 * @param args   the arguments to the proxy object
 */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    String methodName = method.getName();
    Class<?>[] params = method.getParameterTypes();

    // equals and hashCode are special cased
    if (methodName.equals("equals") && params.length == 1 && params[0].equals(Object.class)) {
        Object value = args[0];
        if (value == null || !Proxy.isProxyClass(value.getClass())) {
            return Boolean.FALSE;
        }

        AMQPHessianProxy handler = (AMQPHessianProxy) Proxy.getInvocationHandler(value);

        return _factory.equals(handler._factory);
    } else if (methodName.equals("hashCode") && params.length == 0) {
        return _factory.hashCode();
    } else if (methodName.equals("toString") && params.length == 0) {
        return "[HessianProxy " + proxy.getClass() + "]";
    }

    ConnectionFactory connectionFactory = _factory.getConnectionFactory();

    try {
        Message response = sendRequest(connectionFactory, method, args);

        if (response == null) {
            throw new TimeoutException();
        }

        MessageProperties props = response.getMessageProperties();
        boolean compressed = "deflate".equals(props.getContentEncoding());

        AbstractHessianInput in;

        InputStream is = new ByteArrayInputStream(response.getBody());
        if (compressed) {
            is = new InflaterInputStream(is, new Inflater(true));
        }

        int code = is.read();

        if (code == 'H') {
            int major = is.read();
            int minor = is.read();

            in = _factory.getHessian2Input(is);

            return in.readReply(method.getReturnType());
        } else if (code == 'r') {
            int major = is.read();
            int minor = is.read();

            in = _factory.getHessianInput(is);

            in.startReplyBody();

            Object value = in.readObject(method.getReturnType());

            in.completeReply();

            return value;
        } else {
            throw new HessianProtocolException("'" + (char) code + "' is an unknown code");
        }
    } catch (HessianProtocolException e) {
        throw new HessianRuntimeException(e);
    }
}

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 {//from w ww.ja  va 2 s.c  o 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   www  .  ja  v  a 2 s. 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. ja va2  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;/*from  w w  w .  ja va 2 s.c  o  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 ww.  j  a v  a  2s.  c  o  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.postprocessor.AbstractCompressingPostProcessor.java

@Override
public Message postProcessMessage(Message message) throws AmqpException {
    ByteArrayOutputStream zipped = new ByteArrayOutputStream();
    try {//  w w  w  .  j  a  v a2  s.c o  m
        OutputStream zipper = getCompressorStream(zipped);
        FileCopyUtils.copy(new ByteArrayInputStream(message.getBody()), zipper);
        MessageProperties messageProperties = message.getMessageProperties();
        String currentEncoding = messageProperties.getContentEncoding();
        messageProperties
                .setContentEncoding(getEncoding() + (currentEncoding == null ? "" : ":" + currentEncoding));
        if (this.autoDecompress) {
            messageProperties.setHeader(MessageProperties.SPRING_AUTO_DECOMPRESS, true);
        }
        byte[] compressed = zipped.toByteArray();
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("Compressed " + message.getBody().length + " to " + compressed.length);
        }
        return new Message(compressed, messageProperties);
    } catch (IOException e) {
        throw new AmqpIOException(e);
    }
}