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