@Override public org.springframework.amqp.core.Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { throw new MessageConversionException("intended"); }
@Override public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException { throw new MessageConversionException("fail"); }
@Override public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException { throw new MessageConversionException("intended"); }
@Override public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException { throw new MessageConversionException("intended"); }
@Test public void messageConversionTests() { RabbitTemplate amqpTemplate = new RabbitTemplate(this.connectionFactory); MessageConverter messageConverter = mock(MessageConverter.class); amqpTemplate.setMessageConverter(messageConverter); PointToPointSubscribableAmqpChannel channel = new PointToPointSubscribableAmqpChannel("testConvertFail", new SimpleMessageListenerContainer(this.connectionFactory), amqpTemplate); channel.afterPropertiesSet(); MessageListener listener = TestUtils.getPropertyValue(channel, "container.messageListener", MessageListener.class); willThrow(new MessageConversionException("foo", new IllegalStateException("bar"))) .given(messageConverter).fromMessage(any(org.springframework.amqp.core.Message.class)); this.exception.expect(MessageConversionException.class); this.exception.expectCause(instanceOf(IllegalStateException.class)); listener.onMessage(mock(org.springframework.amqp.core.Message.class)); }
protected String retrieveHeader(MessageProperties properties, String headerName) { String classId = retrieveHeaderAsString(properties, headerName); if (classId == null) { throw new MessageConversionException( "failed to convert Message content. Could not resolve " + headerName + " in header"); } return classId; }
@Override protected org.springframework.amqp.core.Message createMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { throw new MessageConversionException("Test exception"); } });
protected void checkMessageBody(@NotNull final Message message) { if (isMessageBodyEmpty(message)) { throw new MessageConversionException("Message body cannot be null"); } }
protected MessageConverter getConverterForContentType(String contentType) { MessageConverter delegate = getDelegates().get(contentType); if (delegate == null) { delegate = this.defaultConverter; } if (delegate == null) { throw new MessageConversionException("No delegate converter is specified for content type " + contentType); } else { return delegate; } }
/** * Unmarshals the given {@link Message} into an object. */ @Override public Object fromMessage(Message message) throws MessageConversionException { try { ByteArrayInputStream bis = new ByteArrayInputStream(message.getBody()); StreamSource source = new StreamSource(bis); return this.unmarshaller.unmarshal(source); } catch (IOException ex) { throw new MessageConversionException("Could not access message content: " + message, ex); } catch (XmlMappingException ex) { throw new MessageConversionException("Could not unmarshal message: " + message, ex); } }
private Object asString(Message message, MessageProperties properties) { String encoding = properties.getContentEncoding(); if (encoding == null) { encoding = this.defaultCharset; } try { return new String(message.getBody(), encoding); } catch (UnsupportedEncodingException e) { throw new MessageConversionException("failed to convert text-based Message content", e); } }
@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); } }
@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); } }
@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); }
@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); }
private Object deserialize(Message message) { try { ByteArrayInputStream inputStream = new ByteArrayInputStream(message.getBody()); if (this.usingDefaultDeserializer) { return deserialize(inputStream); } else { return this.deserializer.deserialize(inputStream); } } catch (IOException e) { throw new MessageConversionException("Could not convert message body", e); } }
@Override protected Message createMessage(Object object, MessageProperties messageProperties) { try { byte[] body = null; if(object != null) { body = object.toString().getBytes(this.encoding); } String msgContentType = this.contentType == null ? DEFAULT_CONTENT_TYPE : this.contentType; messageProperties.setContentType(msgContentType); messageProperties.setContentEncoding(this.encoding); messageProperties.setContentLength(body != null ? body.length : 0); return new Message(body, messageProperties); } catch (UnsupportedEncodingException ex) { LOG.error("Cannot encode strings as {}", this.encoding, ex); throw new MessageConversionException("Cannot encode strings as "+this.encoding, ex); } }
@RabbitListener(queues = "test.expiry.main") public void listen(Message in) { this.latch.countDown(); this.counter++; throw new MessageConversionException("test.expiry"); }
@Override public Object fromMessage(Message message) throws MessageConversionException { Object result = this.delegate.fromMessage(message); if (result instanceof RemoteInvocationResult) { try { result = ((RemoteInvocationResult) result).recreate(); if (result == null) { throw new MessageConversionException("RemoteInvocationResult returned null"); } } catch (Throwable e) { // NOSONAR throw new AmqpRemoteException(e); } } return result; }
@SuppressWarnings("unchecked") @Override public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException { if (message == null) { return null; } Map<String, Object> mappedHeaders = this.headerMapper.toHeaders(message.getMessageProperties()); Object convertedObject = extractPayload(message); if (convertedObject == null) { throw new MessageConversionException("Message converter returned null"); } MessageBuilder<Object> builder = (convertedObject instanceof org.springframework.messaging.Message) ? MessageBuilder.fromMessage((org.springframework.messaging.Message<Object>) convertedObject) : // NOSONAR MessageBuilder.withPayload(convertedObject); return builder.copyHeadersIfAbsent(mappedHeaders).build(); }