@Override public org.springframework.amqp.core.Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { throw new MessageConversionException("intended"); }
@Test public void shouldThrowAnExceptionWhenClassIdNotPresent() { try { classMapper.toClass(props); } catch (MessageConversionException e) { String classIdFieldName = classMapper.getClassIdFieldName(); assertThat(e.getMessage(), containsString("Could not resolve " + classIdFieldName + " in header")); } }
@Test public void shouldThrowAnExceptionWhenContentClassIdIsNotPresentWhenClassIdIsContainerType() { properties.getHeaders().put(javaTypeMapper.getClassIdFieldName(), ArrayList.class.getName()); try { javaTypeMapper.toJavaType(properties); } catch (MessageConversionException e) { String contentClassIdFieldName = javaTypeMapper.getContentClassIdFieldName(); assertThat(e.getMessage(), containsString("Could not resolve " + contentClassIdFieldName + " in header")); return; } fail(); }
@Override public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException { throw new MessageConversionException("fail"); }
@Test public void shouldThrowAnExceptionWhenKeyClassIdIsNotPresentWhenClassIdIsAMap() { properties.getHeaders().put(javaTypeMapper.getClassIdFieldName(), HashMap.class.getName()); properties.getHeaders().put(javaTypeMapper.getKeyClassIdFieldName(), String.class.getName()); try { javaTypeMapper.toJavaType(properties); } catch (MessageConversionException e) { String contentClassIdFieldName = javaTypeMapper.getContentClassIdFieldName(); assertThat(e.getMessage(), containsString("Could not resolve " + contentClassIdFieldName + " in header")); return; } 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 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; } }
protected void checkMessageBody(@NotNull final Message message) { if (isMessageBodyEmpty(message)) { throw new MessageConversionException("Message body cannot be null"); } }
/** * 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); } }
private Class<?> toClass(String classId) { if (this.idClassMapping.containsKey(classId)) { return this.idClassMapping.get(classId); } if (classId.equals(DEFAULT_HASHTABLE_TYPE_ID)) { return this.defaultMapClass; } try { if (!isTrustedPackage(classId)) { throw new IllegalArgumentException("The class '" + classId + "' is not in the trusted packages: " + this.trustedPackages + ". " + "If you believe this class is safe to deserialize, please provide its name. " + "If the serialization is only done by a trusted source, you can also enable trust all (*)."); } else { return ClassUtils.forName(classId, ClassUtils.getDefaultClassLoader()); } } catch (ClassNotFoundException | LinkageError e) { throw new MessageConversionException( "failed to resolve class name [" + classId + "]", 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 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); }
/** * Marshals the given object to a {@link Message}. */ @Override protected Message createMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { try { if (this.contentType != null) { messageProperties.setContentType(this.contentType); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(bos); this.marshaller.marshal(object, streamResult); return new Message(bos.toByteArray(), messageProperties); } catch (XmlMappingException ex) { throw new MessageConversionException("Could not marshal [" + object + "]", ex); } catch (IOException ex) { throw new MessageConversionException("Could not marshal [" + object + "]", ex); } }