View Javadoc

1   package org.codehaus.xfire.soap.handler;
2   
3   import javax.xml.namespace.QName;
4   
5   import org.codehaus.xfire.MessageContext;
6   import org.codehaus.xfire.fault.XFireFault;
7   import org.codehaus.xfire.handler.AbstractHandler;
8   import org.codehaus.xfire.handler.Phase;
9   import org.codehaus.xfire.soap.SoapVersion;
10  import org.codehaus.yom.Element;
11  import org.codehaus.yom.Elements;
12  
13  /***
14   * Validates that headers flagged as "mustUnderstand" are understood.
15   * 
16   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
17   */
18  public class ValidateHeadersHandler
19      extends AbstractHandler
20  {
21      public String getPhase()
22      {
23          return Phase.PRE_INVOKE;
24      }
25  
26      /***
27       * Validates that the mustUnderstand and role headers are processed correctly.
28       *
29       * @param context
30       * @throws XFireFault
31       */
32      public void invoke(MessageContext context)
33          throws Exception
34      {
35          if (context.getInMessage().getHeader() == null)
36              return;
37  
38          SoapVersion version = context.getInMessage().getSoapVersion();
39          Elements elements = context.getInMessage().getHeader().getChildElements();
40          for (int i = 0; i < elements.size(); i++)
41          {
42              Element e = elements.get(i);
43              String mustUnderstand = e.getAttributeValue("mustUnderstand",
44                      version.getNamespace());
45  
46              if (mustUnderstand != null && mustUnderstand.equals("1"))
47              {
48                  assertUnderstandsHeader(context, new QName(e.getNamespaceURI(), e.getLocalName()));
49              }
50          }
51      }
52  
53      /***
54       * Assert that a service understands a particular header.  If not, a fault is thrown.
55       *
56       * @param context
57       * @param name
58       * @throws XFireFault
59       */
60      protected void assertUnderstandsHeader(MessageContext context, QName name)
61              throws XFireFault
62      {
63          if (context.getInPipeline().understands(name))
64              return;
65  
66          if (context.getOutPipeline().understands(name))
67              return;
68  
69          // TODO: Check Out pipeline for understanding
70          
71          throw new XFireFault("Header {" + name.getLocalPart() + "}" + name.getNamespaceURI()
72                  + " was not undertsood by the service.", XFireFault.MUST_UNDERSTAND);
73      }
74  
75  }