/** * Indicates whether external XML entities are processed when converting to a Source. * <p>Default is {@code false}, meaning that external entities are not resolved. * <p><strong>Note:</strong> setting this option to {@code true} also * automatically sets {@link #setSupportDtd} to {@code true}. */ public void setProcessExternalEntities(boolean processExternalEntities) { this.processExternalEntities = processExternalEntities; if (processExternalEntities) { setSupportDtd(true); } }
/** * Indicates whether external XML entities are processed when converting to a Source. * <p>Default is {@code false}, meaning that external entities are not resolved. * <p><strong>Note:</strong> setting this option to {@code true} also * automatically sets {@link #setSupportDtd} to {@code true}. */ public void setProcessExternalEntities(boolean processExternalEntities) { this.processExternalEntities = processExternalEntities; if (processExternalEntities) { setSupportDtd(true); } }
@Override @Nullable protected Long getContentLength(T t, @Nullable MediaType contentType) { if (t instanceof DOMSource) { try { CountingOutputStream os = new CountingOutputStream(); transform(t, new StreamResult(os)); return os.count; } catch (TransformerException ex) { // ignore } } return null; }
@Before public void setup() { converter = new Jaxb2RootElementHttpMessageConverter(); rootElement = new RootElement(); DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory(); AdvisedSupport advisedSupport = new AdvisedSupport(); advisedSupport.setTarget(rootElement); advisedSupport.setProxyTargetClass(true); AopProxy proxy = proxyFactory.createAopProxy(advisedSupport); rootElementCglib = (RootElement) proxy.getProxy(); }
@Test public void customXmlMapper() { new MappingJackson2XmlHttpMessageConverter(new MyXmlMapper()); // Assert no exception is thrown }
@Before public void setup() { converter = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>(); rootElementListType = new ParameterizedTypeReference<List<RootElement>>() {}.getType(); rootElementSetType = new ParameterizedTypeReference<Set<RootElement>>() {}.getType(); typeListType = new ParameterizedTypeReference<List<TestType>>() {}.getType(); typeSetType = new ParameterizedTypeReference<Set<TestType>>() {}.getType(); }
@Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return (clazz.isAnnotationPresent(XmlRootElement.class) || clazz.isAnnotationPresent(XmlType.class)) && canRead(mediaType); }
@Test(expected = UnsupportedOperationException.class) public void supports() throws Exception { new MarshallingHttpMessageConverter().supports(Object.class); } }
@Test public void customizeUnmarshaller() throws Exception { byte[] body = "<myRootElement><element>a|||b</element></myRootElement>".getBytes("UTF-8"); MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter(); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); MyRootElement result = (MyRootElement) myConverter.read(MyRootElement.class, inputMessage); assertEquals("a", result.getElement().getField1()); assertEquals("b", result.getElement().getField2()); }
@Test public void customizeMarshaller() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter(); myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<myRootElement><element>a|||b</element></myRootElement>").withDifferenceEvaluator(ev)); }
@Test public void readXmlRootElementSubclass() throws Exception { byte[] body = "<rootElement><type s=\"Hello World\"/></rootElement>".getBytes("UTF-8"); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); RootElementSubclass result = (RootElementSubclass) converter.read(RootElementSubclass.class, inputMessage); assertEquals("Invalid result", "Hello World", result.getType().s); }
@Override protected void customizeUnmarshaller(Unmarshaller unmarshaller) { unmarshaller.setAdapter(new MyCustomElementAdapter()); } }
@Override public int hashCode() { return type.hashCode(); } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof RootElement) { RootElement other = (RootElement) o; return this.type.equals(other.type); } return false; }
public RootElement(String s) { this.type = new TestType(s); }
@Override public MyCustomElement unmarshal(String c) throws Exception { String[] t = c.split("\\|\\|\\|"); return new MyCustomElement(t[0], t[1]); }
@Override protected XMLInputFactory createXmlInputFactory() { XMLInputFactory inputFactory = super.createXmlInputFactory(); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, true); return inputFactory; } };
@Override protected void customizeMarshaller(Marshaller marshaller) { marshaller.setAdapter(new MyCustomElementAdapter()); }