View Javadoc

    1   package org.codehaus.xfire.soap.handler;
    2
    3   import javax.xml.stream.XMLStreamException;
    4   import javax.xml.stream.XMLStreamReader;
    5
    6   import org.codehaus.xfire.MessageContext;
    7   import org.codehaus.xfire.exchange.InMessage;
    8   import org.codehaus.xfire.exchange.MessageSerializer;
    9   import org.codehaus.xfire.fault.XFireFault;
    10  import org.codehaus.xfire.handler.AbstractHandler;
    11  import org.codehaus.xfire.handler.Phase;
    12  import org.codehaus.yom.Element;
    13  import org.codehaus.yom.stax.StaxBuilder;
    14
    15  public class ReadHeadersHandler
    16      extends AbstractHandler
    17  {
    18      public String getPhase()
    19      {
    20          return Phase.PARSE;
    21      }
    22
    23      public void invoke(MessageContext context)
    24          throws Exception
    25      {
    26          InMessage message = context.getInMessage();
    27          if (message.hasHeader()) return;
    28
    29          XMLStreamReader reader = message.getXMLStreamReader();
    30
    31          boolean end = !reader.hasNext();
    32          while (!end && reader.hasNext())
    33          {
    34              int event = reader.next();
    35              switch (event)
    36              {
    37                  case XMLStreamReader.START_DOCUMENT:
    38                      String encoding = reader.getCharacterEncodingScheme();
    39                      message.setEncoding(encoding);
    40                      break;
    41                  case XMLStreamReader.END_DOCUMENT:
    42                      end = true;
    43                      return;
    44                  case XMLStreamReader.END_ELEMENT:
    45                      break;
    46                  case XMLStreamReader.START_ELEMENT:
    47                      if (reader.getLocalName().equals("Header"))
    48                      {
    49                          readHeaders(context);
    50                      }
    51                      else if (reader.getLocalName().equals("Body"))
    52                      {
    53                          seekToWhitespaceEnd(reader);
    54
    55                          checkForFault(context, message, reader);
    56
    57                          return;
    58                      }
    59                      else if (reader.getLocalName().equals("Envelope"))
    60                      {
    61                          message.setSoapVersion(reader.getNamespaceURI());
    62                      }
    63                      break;
    64                  default:
    65                      break;
    66              }
    67          }
    68      }
    69
    70      protected void checkForFault(MessageContext context, InMessage msg, XMLStreamReader reader)
    71          throws XFireFault
    72      {
    73          if (reader.getEventType() == XMLStreamReader.START_ELEMENT)
    74          {
    75              if (reader.getName().equals(msg.getSoapVersion().getFault()))
    76              {
    77                  MessageSerializer serializer = context.getService().getFaultSerializer();
    78
    79                  serializer.readMessage(msg, context);
    80
    81                  throw (XFireFault) msg.getBody();
    82              }
    83          }
    84      }
    85
    86      private void seekToWhitespaceEnd(XMLStreamReader reader)
    87          throws XMLStreamException
    88      {
    89          int event = reader.next();
    90          if (event != XMLStreamReader.SPACE) return;
    91
    92          do
    93          {
    94              event = reader.next();
    95          }
    96          while (event == XMLStreamReader.SPACE);
    97
    98          return;
    99      }
    100
    101     /***
    102      * Read in the headers as a YOM Element and create a response Header.
    103      *
    104      * @param context
    105      * @throws XMLStreamException
    106      */
    107     protected void readHeaders(MessageContext context)
    108             throws XMLStreamException
    109     {
    110         StaxBuilder builder = new StaxBuilder();
    111
    112         Element header = builder.buildElement(null, context.getInMessage().getXMLStreamReader());
    113
    114         context.getInMessage().setHeader(header);
    115     }
    116 }