/** * Unmarshal the given {@link TextMessage} into an object. * @param message the message * @param unmarshaller the unmarshaller to use * @return the unmarshalled object * @throws JMSException if thrown by JMS methods * @throws IOException in case of I/O errors * @throws XmlMappingException in case of OXM mapping errors * @see Unmarshaller#unmarshal(Source) */ protected Object unmarshalFromTextMessage(TextMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException { Source source = new StreamSource(new StringReader(message.getText())); return unmarshaller.unmarshal(source); }
@Override protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) { return (supportsMimeType(message.getHeaders()) && this.unmarshaller != null && this.unmarshaller.supports(targetClass)); }
@Override protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws Exception { Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required"); Object result = this.unmarshaller.unmarshal(source); if (!clazz.isInstance(result)) { throw new TypeMismatchException(result, clazz); } return result; }
@Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return (canRead(mediaType) && this.unmarshaller != null && this.unmarshaller.supports(clazz)); }
@Override @Nullable protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) { Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required"); try { Source source = getSource(message.getPayload()); Object result = this.unmarshaller.unmarshal(source); if (!targetClass.isInstance(result)) { throw new TypeMismatchException(result, targetClass); } return result; } catch (Exception ex) { throw new MessageConversionException(message, "Could not unmarshal XML: " + ex.getMessage(), ex); } }
@Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return (canRead(mediaType) && this.unmarshaller != null && this.unmarshaller.supports(clazz)); }
@Override protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws Exception { Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required"); Object result = this.unmarshaller.unmarshal(source); if (!clazz.isInstance(result)) { throw new TypeMismatchException(result, clazz); } return result; }
@Test public void canRead() { Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.supports(Integer.class)).willReturn(false); given(unmarshaller.supports(String.class)).willReturn(true); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); assertFalse(converter.canRead(Boolean.class, MediaType.TEXT_PLAIN)); assertFalse(converter.canRead(Integer.class, MediaType.TEXT_XML)); assertTrue(converter.canRead(String.class, MediaType.TEXT_XML)); }
/** * Unmarshal the given {@link BytesMessage} into an object. * @param message the message * @param unmarshaller the unmarshaller to use * @return the unmarshalled object * @throws JMSException if thrown by JMS methods * @throws IOException in case of I/O errors * @throws XmlMappingException in case of OXM mapping errors * @see Unmarshaller#unmarshal(Source) */ protected Object unmarshalFromBytesMessage(BytesMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException { byte[] bytes = new byte[(int) message.getBodyLength()]; message.readBytes(bytes); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); StreamSource source = new StreamSource(bis); return unmarshaller.unmarshal(source); }
@Override protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) { return (supportsMimeType(message.getHeaders()) && this.unmarshaller != null && this.unmarshaller.supports(targetClass)); }
@Override public Object deserialize(@Nullable byte[] bytes) throws SerializationException { if (SerializationUtils.isEmpty(bytes)) { return null; } try { return unmarshaller.unmarshal(new StreamSource(new ByteArrayInputStream(bytes))); } catch (Exception ex) { throw new SerializationException("Cannot deserialize bytes", ex); } }
private boolean supportsParameters(Method method) { if (method.getParameterTypes().length != 1) { return false; } else { return getUnmarshaller().supports(method.getParameterTypes()[0]); } } }
@Test @SuppressWarnings("deprecation") public void unmarshalSAXSource() throws Exception { XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING))); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Override protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) { return (supportsMimeType(message.getHeaders()) && this.unmarshaller != null && this.unmarshaller.supports(targetClass)); }
@Test public void unmarshalStreamSourceInputStream() throws Exception { StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8"))); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Override protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) { return (supportsMimeType(message.getHeaders()) && this.unmarshaller != null && this.unmarshaller.supports(targetClass)); }
@Test public void unmarshalDomSource() throws Exception { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.newDocument(); Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights"); document.appendChild(flightsElement); Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight"); flightsElement.appendChild(flightElement); Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number"); flightElement.appendChild(numberElement); Text text = document.createTextNode("42"); numberElement.appendChild(text); DOMSource source = new DOMSource(document); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
private boolean supportsParameters(Method method) { if (method.getParameterTypes().length != 1) { return false; } else { return getUnmarshaller().supports(method.getParameterTypes()[0]); } } }
@Test public void unmarshalStreamSourceReader() throws Exception { StreamSource source = new StreamSource(new StringReader(INPUT_STRING)); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
private boolean supportsParameters(Method method) { if (method.getParameterTypes().length != 1) { return false; } else { return getUnmarshaller().supports(method.getParameterTypes()[0]); } } }