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