View Javadoc

1   package org.codehaus.xfire.handler;
2   
3   import javax.xml.namespace.QName;
4   import javax.xml.stream.XMLOutputFactory;
5   import javax.xml.stream.XMLStreamException;
6   import javax.xml.stream.XMLStreamWriter;
7   import org.codehaus.xfire.AbstractXFireComponent;
8   import org.codehaus.xfire.MessageContext;
9   import org.codehaus.xfire.XFireRuntimeException;
10  import org.codehaus.xfire.service.Service;
11  
12  /***
13   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
14   * @since Feb 18, 2004
15   */
16  public abstract class AbstractHandler
17      extends AbstractXFireComponent
18      implements Handler
19  {
20      private static final Object STAX_WRITER_KEY = "stax-writer";
21  
22      /***
23       * Returns null by default, indicating that no headers
24       * were understood.
25       * 
26       * @see org.codehaus.xfire.handler.Handler#getUnderstoodHeaders()
27       */
28      public QName[] getUnderstoodHeaders()
29      {
30          return null;
31      }
32  
33      /***
34       * Locates the fault handler on the service to write
35       * the fault to the response message.
36       * 
37       * @see org.codehaus.xfire.handler.Handler#handleFault(org.codehaus.xfire.fault.XFireFault, org.codehaus.xfire.MessageContext)
38       */
39      public void handleFault( Exception e, MessageContext context )
40      {
41          Service service = context.getService();
42          
43          service.getFaultHandler().handleFault( e, context );
44      }
45      
46      public XMLStreamWriter getXMLStreamWriter( MessageContext context )
47      {
48          XMLStreamWriter writer = (XMLStreamWriter) context.getProperty(STAX_WRITER_KEY);
49          
50          if ( writer == null )
51          {
52              XMLOutputFactory factory = XMLOutputFactory.newInstance();
53              try
54              {
55                  writer = factory.createXMLStreamWriter(context.getResponseStream());
56              }
57              catch (XMLStreamException e)
58              {
59                  throw new XFireRuntimeException("Couldn't create STAX writer.", e);
60              }
61              
62              context.setProperty(STAX_WRITER_KEY, writer);
63          }
64          
65          return writer;
66      }
67  }