View Javadoc

1   /*
2    * $Id: StreamClosingInterceptor.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.cxf.support;
12  
13  import org.mule.module.xml.stax.DelegateXMLStreamReader;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  
18  import javax.xml.stream.XMLStreamException;
19  import javax.xml.stream.XMLStreamReader;
20  
21  import org.apache.cxf.interceptor.Fault;
22  import org.apache.cxf.interceptor.StaxInInterceptor;
23  import org.apache.cxf.message.Message;
24  import org.apache.cxf.phase.AbstractPhaseInterceptor;
25  import org.apache.cxf.phase.Phase;
26  
27  /**
28   * Replaces the original XMLStreamReader with another one which
29   * closes the underlying InputStream.
30   */
31  public class StreamClosingInterceptor extends AbstractPhaseInterceptor<Message>
32  {
33      public StreamClosingInterceptor()
34      {
35          super(Phase.POST_STREAM);
36          addAfter(StaxInInterceptor.class.getName());
37      }
38  
39      public void handleMessage(final Message message) throws Fault
40      {
41          XMLStreamReader xsr = message.getContent(XMLStreamReader.class);
42          final InputStream is = message.getContent(InputStream.class);
43          DelegateXMLStreamReader xsr2 = new DelegateXMLStreamReader(xsr) {
44  
45              @Override
46              public void close() throws XMLStreamException
47              {
48                  super.close();
49                  try
50                  {
51                      is.close();
52                  }
53                  catch (IOException e)
54                  {
55                      throw new XMLStreamException(e);
56                  }
57              }
58          };
59          message.setContent(XMLStreamReader.class, xsr2);
60      }
61  }
62