@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; }
@Override @SuppressWarnings("unchecked") protected T readFromSource(Class<? extends T> clazz, HttpHeaders headers, Source source) throws IOException { try { if (DOMSource.class.equals(clazz)) { DOMResult domResult = new DOMResult(); transform(source, domResult); return (T) new DOMSource(domResult.getNode()); } else if (SAXSource.class.equals(clazz)) { ByteArrayInputStream bis = transformToByteArrayInputStream(source); return (T) new SAXSource(new InputSource(bis)); } else if (StreamSource.class.equals(clazz) || Source.class.equals(clazz)) { ByteArrayInputStream bis = transformToByteArrayInputStream(source); return (T) new StreamSource(bis); } else { throw new HttpMessageConversionException("Could not read class [" + clazz + "]. Only DOMSource, SAXSource, and StreamSource are supported."); } } catch (TransformerException ex) { throw new HttpMessageNotReadableException("Could not transform from [" + source + "] to [" + clazz + "]", ex); } }
@Override protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { try { Result result = new StreamResult(outputMessage.getBody()); transform(t, result); } catch (TransformerException ex) { throw new HttpMessageNotWritableException("Could not transform [" + t + "] to output message", ex); } }
@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; }
@Override protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { try { Result result = new StreamResult(outputMessage.getBody()); transform(t, result); } catch (TransformerException ex) { throw new HttpMessageNotWritableException("Could not transform [" + t + "] to output message", ex); } }
private void transform(Source source, Result result) throws TransformerException { this.transformerFactory.newTransformer().transform(source, result); }
private ByteArrayInputStream transformToByteArrayInputStream(Source source) throws TransformerException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); transform(source, new StreamResult(bos)); return new ByteArrayInputStream(bos.toByteArray()); }
@Override protected void writeToResult(T t, HttpHeaders headers, Result result) throws IOException { try { transform(t, result); } catch (TransformerException ex) { throw new HttpMessageNotWritableException("Could not transform [" + t + "] to [" + result + "]", ex); } }
@Override protected Long getContentLength(T t, 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; }
@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; }
@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; }
@Override protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { try { Result result = new StreamResult(outputMessage.getBody()); transform(t, result); } catch (TransformerException ex) { throw new HttpMessageNotWritableException("Could not transform [" + t + "] to output message", ex); } }
@Override protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { try { Result result = new StreamResult(outputMessage.getBody()); transform(t, result); } catch (TransformerException ex) { throw new HttpMessageNotWritableException("Could not transform [" + t + "] to output message", ex); } }