MessageOutputStream implements AutoCloseable { public ContentHandler asContentHandler() throws StreamingException { if (requestStream instanceof ContentHandler) { if (log.isDebugEnabled()) log.debug(getLogPrefix()+"returning ContentHandler as ContentHandler"); return (ContentHandler) requestStream; } if (requestStream instanceof OutputStream) { if (log.isDebugEnabled()) log.debug(getLogPrefix()+"returning OutputStream as ContentHandler"); XmlWriter xmlWriter = new XmlWriter((OutputStream) requestStream); xmlWriter.setIncludeXmlDeclaration(true); return xmlWriter; } if (requestStream instanceof Writer) { if (log.isDebugEnabled()) log.debug(getLogPrefix()+"returning Writer as ContentHandler"); return new XmlWriter((Writer) requestStream); } 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 test13StreamAsContentHandler() throws Exception { CloseObservableOutputStream target = new CloseObservableOutputStream(); try (MessageOutputStream stream = new MessageOutputStream(null, target, (IForwardTarget)null)) { ContentHandler handler = stream.asContentHandler(); InputSource inputSource = new InputSource(new StringReader(testString)); XmlUtils.parseXml(inputSource, handler); } String actual = new String (target.toString()); assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+testString, actual); assertTrue(target.isCloseCalled()); }
@Test public void test23WriterAsContentHandler() throws Exception { CloseObservableWriter target = new CloseObservableWriter(); try (MessageOutputStream stream = new MessageOutputStream(null, target, (IForwardTarget)null)) { ContentHandler handler = stream.asContentHandler(); InputSource inputSource = new InputSource(new StringReader(testString)); XmlUtils.parseXml(inputSource, handler); } String actual = new String (target.toString()); assertEquals(testString, actual); assertTrue(target.isCloseCalled()); }
@Test public void testX13StreamAsContentHandlerError() throws Exception { CloseObservableOutputStream target = new CloseObservableOutputStream() { @Override public void write(byte[] arg0, int arg1, int arg2) { throw new RuntimeException("fakeFailure 1"); } @Override public void write(byte[] b) throws IOException { throw new RuntimeException("fakeFailure 2"); } }; try (MessageOutputStream stream = new MessageOutputStream(null, target, (IForwardTarget)null)) { ContentHandler handler = stream.asContentHandler(); try { InputSource inputSource = new InputSource(new StringReader(testString)); XmlUtils.parseXml(inputSource, handler); fail("exception should be thrown"); } catch (Exception e) { assertThat(e.getMessage(),StringContains.containsString("fakeFailure")); } } assertTrue(target.isCloseCalled()); }
@Test public void testX23WriterAsContentHandlerError() throws Exception { CloseObservableWriter target = new CloseObservableWriter() { @Override public void write(char[] arg0, int arg1, int arg2) { throw new RuntimeException("fakeFailure 1"); } @Override public StringWriter append(char arg0) { throw new RuntimeException("fakeFailure 2"); } @Override public StringWriter append(CharSequence arg0, int arg1, int arg2) { throw new RuntimeException("fakeFailure 3"); } @Override public StringWriter append(CharSequence arg0) { throw new RuntimeException("fakeFailure 4"); } @Override public void write(String arg0, int arg1, int arg2) { throw new RuntimeException("fakeFailure 5"); } @Override public void write(String arg0) { throw new RuntimeException("fakeFailure 6"); } }; try (MessageOutputStream stream = new MessageOutputStream(null, target, (IForwardTarget)null)) { ContentHandler handler = stream.asContentHandler(); try { InputSource inputSource = new InputSource(new StringReader(testString)); XmlUtils.parseXml(inputSource, handler); fail("exception should be thrown"); } catch (Exception e) { assertThat(e.getMessage(),StringContains.containsString("fakeFailure")); } } assertTrue(target.isCloseCalled()); }