/** * 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 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 @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 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; }
/** * 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 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); } }
@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); }
@Test public void unmarshalStreamSourceInputStream() throws Exception { StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8"))); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@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); }
@Test public void unmarshalStreamSourceReader() throws Exception { StreamSource source = new StreamSource(new StringReader(INPUT_STRING)); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void unmarshalJaxp14StaxSourceXmlEventReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING)); StAXSource source = new StAXSource(eventReader); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void unmarshalStaxSourceXmlEventReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING)); Source source = StaxUtils.createStaxSource(eventReader); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void unmarshalStaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); Source source = StaxUtils.createStaxSource(streamReader); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void unmarshalJaxp14StaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); StAXSource source = new StAXSource(streamReader); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); streamReader.nextTag(); // skip to flights assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"), streamReader.getName()); streamReader.nextTag(); // skip to flight assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"), streamReader.getName()); Source source = StaxUtils.createStaxSource(streamReader); Object flight = unmarshaller.unmarshal(source); testFlight(flight); }
@Test public void fromTextMessage() throws Exception { TextMessage textMessageMock = mock(TextMessage.class); Object unmarshalled = new Object(); String text = "foo"; given(textMessageMock.getText()).willReturn(text); given(unmarshallerMock.unmarshal(isA(Source.class))).willReturn(unmarshalled); Object result = converter.fromMessage(textMessageMock); assertEquals("Invalid result", result, unmarshalled); }
@Test public void read() throws Exception { String body = "<root>Hello World</root>"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); String result = (String) converter.read(Object.class, inputMessage); assertEquals("Invalid result", body, result); }
@Test public void readWithTypeMismatchException() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]); Marshaller marshaller = mock(Marshaller.class); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3)); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller); try { converter.read(String.class, inputMessage); fail("Should have thrown HttpMessageNotReadableException"); } catch (HttpMessageNotReadableException ex) { assertTrue(ex.getCause() instanceof TypeMismatchException); } }
@Test public void fromBytesMessage() throws Exception { BytesMessage bytesMessageMock = mock(BytesMessage.class); Object unmarshalled = new Object(); given(bytesMessageMock.getBodyLength()).willReturn(10L); given(bytesMessageMock.readBytes(isA(byte[].class))).willReturn(0); given(unmarshallerMock.unmarshal(isA(Source.class))).willReturn(unmarshalled); Object result = converter.fromMessage(bytesMessageMock); assertEquals("Invalid result", result, unmarshalled); }
@Test public void readWithMarshallingFailureException() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]); UnmarshallingFailureException ex = new UnmarshallingFailureException("forced"); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(ex); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); try { converter.read(Object.class, inputMessage); fail("HttpMessageNotReadableException should be thrown"); } catch (HttpMessageNotReadableException e) { assertTrue("Invalid exception hierarchy", e.getCause() == ex); } }