@Override protected Message createMessage(Object object, MessageProperties messageProperties) { try { 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); } }
Class<?> targetClass = getClassMapper().toClass(// NOSONAR never null message.getMessageProperties()); content = convertBytesToObject(message.getBody(),
@Override protected Message createMessage(Object object, MessageProperties messageProperties) { try { 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); } }
@Override protected Message createMessage(Object objectToConvert, MessageProperties messageProperties, @Nullable Type genericType) throws MessageConversionException { byte[] bytes; try { String jsonString = this.objectMapper .writeValueAsString(objectToConvert); bytes = jsonString.getBytes(getDefaultCharset()); } catch (IOException e) { throw new MessageConversionException("Failed to convert Message content", e); } messageProperties.setContentType(this.supportedContentType.toString()); messageProperties.setContentEncoding(getDefaultCharset()); messageProperties.setContentLength(bytes.length); if (getClassMapper() == null) { getJavaTypeMapper().fromJavaType(this.objectMapper.constructType( genericType == null ? objectToConvert.getClass() : genericType), messageProperties); } else { getClassMapper().fromClass(objectToConvert.getClass(), messageProperties); // NOSONAR never null } return new Message(bytes, messageProperties); }