StreamSourceEntityProvider implements EntityProvider<StreamSource> { @Override public void writeTo(StreamSource streamSource, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { StreamResult streamResult = new StreamResult(entityStream); try { TransformerFactory factory = createFeaturedTransformerFactory(); factory.newTransformer().transform(streamSource, streamResult); } catch (TransformerException | TransformerFactoryConfigurationError e) { throw new IOException(String.format("Can't write to output stream, %s", e)); } }  @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override StreamSource readFrom(Class<StreamSource> type,
                                 Type genericType,
                                 Annotation[] annotations,
                                 MediaType mediaType,
                                 MultivaluedMap<String, String> httpHeaders,
                                 InputStream entityStream); @Override long getSize(StreamSource streamSource, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(StreamSource streamSource,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream);  }
@Test public void writesStreamSourceToOutputStream() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); streamSourceEntityProvider.writeTo(new StreamSource(new ByteArrayInputStream(TEST_XML_CONTENT_BYTES)), StreamSource.class, null, null, APPLICATION_XML_TYPE, null, out); Document serializedDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(out.toByteArray())); assertThatXmlDocumentContainsAllExpectedNodes(serializedDocument); }