MessageOutputStream implements AutoCloseable { public Writer asWriter() throws StreamingException { if (requestStream instanceof Writer) { if (log.isDebugEnabled()) log.debug(getLogPrefix()+"returning Writer as Writer"); return (Writer) requestStream; } if (requestStream instanceof OutputStream) { try { if (log.isDebugEnabled()) log.debug(getLogPrefix()+"returning OutputStream as Writer"); return new OutputStreamWriter((OutputStream) requestStream, StreamUtil.DEFAULT_INPUT_STREAM_ENCODING); } catch (UnsupportedEncodingException e) { throw new StreamingException(e); } } if (requestStream instanceof ContentHandler) { try { if (log.isDebugEnabled()) log.debug(getLogPrefix()+"returning ContentHandler as Writer"); return new OutputStreamWriter(new ContentHandlerOutputStream((ContentHandler) requestStream, threadConnector), StreamUtil.DEFAULT_INPUT_STREAM_ENCODING); } catch (UnsupportedEncodingException e) { throw new StreamingException(e); } } return null; } protected  MessageOutputStream(INamedObject owner, IForwardTarget next); protected  MessageOutputStream(INamedObject owner, MessageOutputStream nextStream);  MessageOutputStream(INamedObject owner, OutputStream stream, IForwardTarget next);  MessageOutputStream(INamedObject owner, OutputStream stream, MessageOutputStream nextStream);  MessageOutputStream(INamedObject owner, Writer writer, IForwardTarget next);  MessageOutputStream(INamedObject owner, Writer writer, MessageOutputStream nextStream);  MessageOutputStream(INamedObject owner, ContentHandler handler, IForwardTarget next, ThreadLifeCycleEventListener<Object> threadLifeCycleEventListener, IPipeLineSession session);  MessageOutputStream(INamedObject owner, ContentHandler handler, MessageOutputStream nextStream, ThreadLifeCycleEventListener<Object> threadLifeCycleEventListener, IPipeLineSession session); void closeRequestStream(); void afterClose(); @Override final void close(); Object asNative(); OutputStream asStream(); Writer asWriter(); ContentHandler asContentHandler(); void setResponse(Object response); Object getResponse(); void setForward(PipeForward forward); PipeRunResult getPipeRunResult(); PipeForward getForward(); static MessageOutputStream getTargetStream(INamedObject owner, IPipeLineSession session, IForwardTarget next);  }
@Test public void test12StreamAsWriter() throws Exception { CloseObservableOutputStream target = new CloseObservableOutputStream(); try (MessageOutputStream stream = new MessageOutputStream(null, target, (IForwardTarget)null)) { try (Writer writer = stream.asWriter()) { writer.write(testString); } } String actual = new String (target.toByteArray()); assertEquals(testString, actual); assertTrue(target.isCloseCalled()); }
@Test public void test22WriterAsWriter() throws Exception { CloseObservableWriter target = new CloseObservableWriter(); try (MessageOutputStream stream = new MessageOutputStream(null, target, (IForwardTarget)null)) { try (Writer writer = stream.asWriter()) { writer.write(testString); } } String actual = new String (target.toString()); assertEquals(testString, actual); assertTrue(target.isCloseCalled()); }
@Test public void test32ContentHandlerAsWriter() throws Exception { CloseObservableXmlWriter target = new CloseObservableXmlWriter(); try (MessageOutputStream stream = new MessageOutputStream(null, target, (IForwardTarget)null, null, null)) { try (Writer writer = stream.asWriter()) { writer.write(testString); } } String actual = new String (target.toString()); assertEquals(testString, actual); assertTrue(target.isCloseCalled()); }
@Test public void testX12StreamAsWriterError() throws Exception { CloseObservableOutputStream target = new CloseObservableOutputStream() { @Override public synchronized void write(byte[] arg0, int arg1, int arg2) { throw new RuntimeException("fakeFailure"); } }; try (MessageOutputStream stream = new MessageOutputStream(null, target, (IForwardTarget)null)) { try { try (Writer writer = stream.asWriter()) { writer.write(testString); } fail("exception should be thrown"); } catch (Exception e) { assertThat(e.getMessage(),StringContains.containsString("fakeFailure")); } } assertTrue(target.isCloseCalled()); }
@Test @Ignore("No contract to call endDocument() in case of an Exception") public void testX32ContentHandlerAsWriterError() throws Exception { CloseObservableWriter cow = new CloseObservableWriter() { @Override public void write(char[] arg0, int arg1, int arg2) { throw new RuntimeException("fakeFailure"); } }; Result result = new StreamResult(cow); SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); TransformerHandler transformerHandler = tf.newTransformerHandler(); transformerHandler.setResult(result); try (MessageOutputStream stream = new MessageOutputStream(null, transformerHandler, (IForwardTarget)null, null, null)) { try { try (Writer writer = stream.asWriter()) { writer.write(testString); } fail("exception should be thrown"); } catch (Exception e) { assertThat(e.getMessage(),StringContains.containsString("fakeFailure")); } } assertTrue(cow.isCloseCalled()); }
@Test @Ignore("No contract to call endDocument() in case of an Exception") public void testX31ContentHandlerAsStreamError() throws Exception { CloseObservableOutputStream cos = new CloseObservableOutputStream() { @Override public void write(byte[] arg0, int arg1, int arg2) { throw new RuntimeException("fakeFailure"); } }; Result result = new StreamResult(cos); SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); TransformerHandler transformerHandler = tf.newTransformerHandler(); transformerHandler.setResult(result); try (MessageOutputStream stream = new MessageOutputStream(null, transformerHandler, (IForwardTarget)null, null, null)) { try { try (Writer writer = stream.asWriter()) { writer.write(testString); } fail("exception should be thrown"); } catch (Exception e) { assertThat(e.getMessage(),StringContains.containsString("fakeFailure")); } } assertTrue(cos.isCloseCalled()); }