@Override protected Object unmarshalDomNode(Node node) throws XmlMappingException { try { return transformAndUnmarshal(new DOMSource(node), null); } catch (IOException ex) { throw new UnmarshallingFailureException("JiBX unmarshalling exception", ex); } }
@Override protected Object unmarshalXmlEventReader(XMLEventReader eventReader) { try { XMLStreamReader streamReader = StaxUtils.createEventStreamReader(eventReader); return unmarshalXmlStreamReader(streamReader); } catch (XMLStreamException ex) { return new UnmarshallingFailureException("JiBX unmarshalling exception", ex); } }
/** * Build a new {@link Document} from this marshaller's {@link DocumentBuilderFactory}, * as a placeholder for a DOM node. * @see #createDocumentBuilderFactory() * @see #createDocumentBuilder(DocumentBuilderFactory) */ protected Document buildDocument() { try { DocumentBuilder documentBuilder; synchronized (this.documentBuilderFactoryMonitor) { if (this.documentBuilderFactory == null) { this.documentBuilderFactory = createDocumentBuilderFactory(); } documentBuilder = createDocumentBuilder(this.documentBuilderFactory); } return documentBuilder.newDocument(); } catch (ParserConfigurationException ex) { throw new UnmarshallingFailureException("Could not create document placeholder: " + ex.getMessage(), ex); } }
@Override public Object unmarshal(Source source) throws XmlMappingException, IOException { throw new UnmarshallingFailureException(MESSAGE); }
/** * Convert the given {@code JiBXException} to an appropriate exception from the * {@code org.springframework.oxm} hierarchy. * <p>A boolean flag is used to indicate whether this exception occurs during marshalling or * unmarshalling, since JiBX itself does not make this distinction in its exception hierarchy. * @param ex {@code JiBXException} that occurred * @param marshalling indicates whether the exception occurs during marshalling ({@code true}), * or unmarshalling ({@code false}) * @return the corresponding {@code XmlMappingException} */ public XmlMappingException convertJibxException(JiBXException ex, boolean marshalling) { if (ex instanceof ValidationException) { return new ValidationFailureException("JiBX validation exception", ex); } else { if (marshalling) { return new MarshallingFailureException("JiBX marshalling exception", ex); } else { return new UnmarshallingFailureException("JiBX unmarshalling exception", ex); } } }
@Override public byte[] getAttachmentAsByteArray(String cid) { try { DataHandler dataHandler = getAttachmentAsDataHandler(cid); return FileCopyUtils.copyToByteArray(dataHandler.getInputStream()); } catch (IOException ex) { throw new UnmarshallingFailureException("Couldn't read attachment", ex); } }
/** * Convert the given XStream exception to an appropriate exception from the * {@code org.springframework.oxm} hierarchy. * <p>A boolean flag is used to indicate whether this exception occurs during marshalling or * unmarshalling, since XStream itself does not make this distinction in its exception hierarchy. * @param ex the XStream exception that occurred * @param marshalling indicates whether the exception occurs during marshalling ({@code true}), * or unmarshalling ({@code false}) * @return the corresponding {@code XmlMappingException} */ protected XmlMappingException convertXStreamException(Exception ex, boolean marshalling) { if (ex instanceof StreamException || ex instanceof CannotResolveClassException || ex instanceof ConversionException) { if (marshalling) { return new MarshallingFailureException("XStream marshalling exception", ex); } else { return new UnmarshallingFailureException("XStream unmarshalling exception", ex); } } else { // fallback return new UncategorizedMappingException("Unknown XStream exception", ex); } }
/** * Template method for handling {@code DOMSource}s. * <p>This implementation delegates to {@code unmarshalDomNode}. * If the given source is empty, an empty source {@code Document} * will be created as a placeholder. * @param domSource the {@code DOMSource} * @return the object graph * @throws XmlMappingException if the given source cannot be mapped to an object * @throws IllegalArgumentException if the {@code domSource} is empty * @see #unmarshalDomNode(org.w3c.dom.Node) */ protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException { if (domSource.getNode() == null) { domSource.setNode(buildDocument()); } try { return unmarshalDomNode(domSource.getNode()); } catch (NullPointerException ex) { if (!isSupportDtd()) { throw new UnmarshallingFailureException("NPE while unmarshalling. " + "This can happen on JDK 1.6 due to the presence of DTD " + "declarations, which are disabled.", ex); } throw ex; } }
/** * Convert the given {@code JAXBException} to an appropriate exception from the * {@code org.springframework.oxm} hierarchy. * @param ex {@code JAXBException} that occurred * @return the corresponding {@code XmlMappingException} */ protected XmlMappingException convertJaxbException(JAXBException ex) { if (ex instanceof ValidationException) { return new ValidationFailureException("JAXB validation exception", ex); } else if (ex instanceof MarshalException) { return new MarshallingFailureException("JAXB marshalling exception", ex); } else if (ex instanceof UnmarshalException) { return new UnmarshallingFailureException("JAXB unmarshalling exception", ex); } else { // fallback return new UncategorizedMappingException("Unknown JAXB exception", ex); } }
@Override public Object unmarshal(Source source, @Nullable MimeContainer mimeContainer) throws XmlMappingException { source = processSource(source); try { Unmarshaller unmarshaller = createUnmarshaller(); if (this.mtomEnabled && mimeContainer != null) { unmarshaller.setAttachmentUnmarshaller(new Jaxb2AttachmentUnmarshaller(mimeContainer)); } if (StaxUtils.isStaxSource(source)) { return unmarshalStaxSource(unmarshaller, source); } else if (this.mappedClass != null) { return unmarshaller.unmarshal(source, this.mappedClass).getValue(); } else { return unmarshaller.unmarshal(source); } } catch (NullPointerException ex) { if (!isSupportDtd()) { throw new UnmarshallingFailureException("NPE while unmarshalling: " + "This can happen due to the presence of DTD declarations which are disabled.", ex); } throw ex; } catch (JAXBException ex) { throw convertJaxbException(ex); } }
throw new UnmarshallingFailureException("Could not create XMLReader for SAXSource", ex); throw new UnmarshallingFailureException("NPE while unmarshalling. " + "This can happen on JDK 1.6 due to the presence of DTD " + "declarations, which are disabled.");
@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); } }
/** * Build a new {@link Document} from this marshaller's {@link DocumentBuilderFactory}, * as a placeholder for a DOM node. * @see #createDocumentBuilderFactory() * @see #createDocumentBuilder(DocumentBuilderFactory) */ protected Document buildDocument() { try { DocumentBuilder documentBuilder; synchronized (this.documentBuilderFactoryMonitor) { if (this.documentBuilderFactory == null) { this.documentBuilderFactory = createDocumentBuilderFactory(); } documentBuilder = createDocumentBuilder(this.documentBuilderFactory); } return documentBuilder.newDocument(); } catch (ParserConfigurationException ex) { throw new UnmarshallingFailureException("Could not create document placeholder: " + ex.getMessage(), ex); } }
/** * Convert the given XStream exception to an appropriate exception from the * {@code org.springframework.oxm} hierarchy. * <p>A boolean flag is used to indicate whether this exception occurs during marshalling or * unmarshalling, since XStream itself does not make this distinction in its exception hierarchy. * @param ex XStream exception that occurred * @param marshalling indicates whether the exception occurs during marshalling ({@code true}), * or unmarshalling ({@code false}) * @return the corresponding {@code XmlMappingException} */ protected XmlMappingException convertXStreamException(Exception ex, boolean marshalling) { if (ex instanceof StreamException || ex instanceof CannotResolveClassException || ex instanceof ConversionException) { if (marshalling) { return new MarshallingFailureException("XStream marshalling exception", ex); } else { return new UnmarshallingFailureException("XStream unmarshalling exception", ex); } } else { // fallback return new UncategorizedMappingException("Unknown XStream exception", ex); } }
/** * Template method for handling <code>DOMSource</code>s. This implementation defers to * <code>unmarshalDomNode</code>. If the given source is empty, an empty source <code>Document</code> will be * created as a placeholder. * * @param domSource the <code>DOMSource</code> * @return the object graph * @throws IllegalArgumentException if the <code>domSource</code> is empty * @throws XmlMappingException if the given source cannot be mapped to an object * @see #unmarshalDomNode(org.w3c.dom.Node) */ protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException { if (domSource.getNode() == null) { try { if (documentBuilderFactory == null) { documentBuilderFactory = createDocumentBuilderFactory(); } DocumentBuilder documentBuilder = createDocumentBuilder(documentBuilderFactory); domSource.setNode(documentBuilder.newDocument()); } catch (ParserConfigurationException ex) { throw new UnmarshallingFailureException( "Could not create document placeholder for DOMSource: " + ex.getMessage(), ex); } } return unmarshalDomNode(domSource.getNode()); }
@Override public byte[] getAttachmentAsByteArray(String cid) { try { DataHandler dataHandler = getAttachmentAsDataHandler(cid); return FileCopyUtils.copyToByteArray(dataHandler.getInputStream()); } catch (IOException ex) { throw new UnmarshallingFailureException("Couldn't read attachment", ex); } }
/** * Template method for handling {@code DOMSource}s. * <p>This implementation delegates to {@code unmarshalDomNode}. * If the given source is empty, an empty source {@code Document} * will be created as a placeholder. * @param domSource the {@code DOMSource} * @return the object graph * @throws XmlMappingException if the given source cannot be mapped to an object * @throws IllegalArgumentException if the {@code domSource} is empty * @see #unmarshalDomNode(org.w3c.dom.Node) */ protected Object unmarshalDomSource(DOMSource domSource) throws XmlMappingException { if (domSource.getNode() == null) { domSource.setNode(buildDocument()); } try { return unmarshalDomNode(domSource.getNode()); } catch (NullPointerException ex) { if (!isSupportDtd()) { throw new UnmarshallingFailureException("NPE while unmarshalling. " + "This can happen on JDK 1.6 due to the presence of DTD " + "declarations, which are disabled.", ex); } throw ex; } }
/** * Convert the given XMLBeans exception to an appropriate exception from the * {@code org.springframework.oxm} hierarchy. * <p>A boolean flag is used to indicate whether this exception occurs during marshalling or * unmarshalling, since XMLBeans itself does not make this distinction in its exception hierarchy. * @param ex XMLBeans Exception that occured * @param marshalling indicates whether the exception occurs during marshalling ({@code true}), * or unmarshalling ({@code false}) * @return the corresponding {@code XmlMappingException} */ protected XmlMappingException convertXmlBeansException(Exception ex, boolean marshalling) { if (ex instanceof XMLStreamValidationException) { return new ValidationFailureException("XMLBeans validation exception", ex); } else if (ex instanceof XmlException || ex instanceof SAXException) { if (marshalling) { return new MarshallingFailureException("XMLBeans marshalling exception", ex); } else { return new UnmarshallingFailureException("XMLBeans unmarshalling exception", ex); } } else { // fallback return new UncategorizedMappingException("Unknown XMLBeans exception", ex); } }
/** * Template method for handling <code>SAXSource</code>s. This implementation defers to * <code>unmarshalSaxReader</code>. * * @param saxSource the <code>SAXSource</code> * @return the object graph * @throws XmlMappingException if the given source cannot be mapped to an object * @throws IOException if an I/O Exception occurs * @see #unmarshalSaxReader(org.xml.sax.XMLReader,org.xml.sax.InputSource) */ protected Object unmarshalSaxSource(SAXSource saxSource) throws XmlMappingException, IOException { if (saxSource.getXMLReader() == null) { try { saxSource.setXMLReader(createXmlReader()); } catch (SAXException ex) { throw new UnmarshallingFailureException("Could not create XMLReader for SAXSource: " + ex.getMessage(), ex); } } if (saxSource.getInputSource() == null) { saxSource.setInputSource(new InputSource()); } return unmarshalSaxReader(saxSource.getXMLReader(), saxSource.getInputSource()); }
throw new UnmarshallingFailureException("Could not create XMLReader for SAXSource", ex); throw new UnmarshallingFailureException("NPE while unmarshalling. " + "This can happen on JDK 1.6 due to the presence of DTD " + "declarations, which are disabled.");