View Javadoc

    1   package org.codehaus.xfire.fault;
    2
    3   import java.util.Iterator;
    4   import java.util.Map;
    5
    6   import javax.xml.stream.XMLStreamException;
    7   import javax.xml.stream.XMLStreamReader;
    8   import javax.xml.stream.XMLStreamWriter;
    9
    10  import org.codehaus.xfire.MessageContext;
    11  import org.codehaus.xfire.XFireRuntimeException;
    12  import org.codehaus.xfire.exchange.InMessage;
    13  import org.codehaus.xfire.exchange.MessageSerializer;
    14  import org.codehaus.xfire.exchange.OutMessage;
    15  import org.codehaus.yom.Element;
    16  import org.codehaus.yom.Elements;
    17  import org.codehaus.yom.stax.StaxBuilder;
    18  import org.codehaus.yom.stax.StaxSerializer;
    19
    20  public class Soap11FaultSerializer
    21      implements MessageSerializer
    22  {
    23      private StaxBuilder builder = new StaxBuilder();
    24
    25      public void readMessage(InMessage message, MessageContext context)
    26          throws XFireFault
    27      {
    28          XFireFault fault = new XFireFault();
    29
    30          XMLStreamReader reader = message.getXMLStreamReader();
    31
    32          try
    33          {
    34              boolean end = false;
    35              while (!end && reader.hasNext())
    36              {
    37                  int event = reader.next();
    38                  switch (event)
    39                  {
    40                      case XMLStreamReader.START_DOCUMENT:
    41                          String encoding = reader.getCharacterEncodingScheme();
    42                          message.setEncoding(encoding);
    43                          break;
    44                      case XMLStreamReader.END_DOCUMENT:
    45                          end = true;
    46                          break;
    47                      case XMLStreamReader.END_ELEMENT:
    48                          break;
    49                      case XMLStreamReader.START_ELEMENT:
    50                          if (reader.getLocalName().equals("faultcode"))
    51                          {
    52                              fault.setFaultCode(reader.getElementText());
    53                          }
    54                          else if (reader.getLocalName().equals("faultstring"))
    55                          {
    56                              fault.setMessage(reader.getElementText());
    57                          }
    58                          else if (reader.getLocalName().equals("faultactor"))
    59                          {
    60                              fault.setRole(reader.getElementText());
    61                          }
    62                          else if (reader.getLocalName().equals("detail"))
    63                          {
    64                              fault.setDetail(builder.buildElement(null, reader));
    65                          }
    66                          break;
    67                      default:
    68                          break;
    69                  }
    70              }
    71          }
    72          catch (XMLStreamException e)
    73          {
    74              throw new XFireFault("Could not parse message.", e, XFireFault.SENDER);
    75          }
    76          message.setBody(fault);
    77      }
    78
    79      public void writeMessage(OutMessage message, XMLStreamWriter writer, MessageContext context)
    80          throws XFireFault
    81      {
    82          XFireFault fault = (XFireFault) message.getBody();
    83
    84          try
    85          {
    86              Map namespaces = fault.getNamespaces();
    87              for (Iterator itr = namespaces.keySet().iterator(); itr.hasNext();)
    88              {
    89                  String prefix = (String) itr.next();
    90                  writer.writeAttribute("xmlns:" + prefix, (String) namespaces.get(prefix));
    91              }
    92
    93              writer.writeStartElement("soap:Fault");
    94
    95              writer.writeStartElement("faultcode");
    96
    97              String codeString = fault.getFaultCode();
    98              if (codeString.equals(XFireFault.RECEIVER))
    99              {
    100                 codeString = "Server";
    101             }
    102             if (codeString.equals(XFireFault.SENDER))
    103             {
    104                 codeString = "Server";
    105             }
    106             else if (codeString.equals(XFireFault.DATA_ENCODING_UNKNOWN))
    107             {
    108                 codeString = "Client";
    109             }
    110
    111             writer.writeCharacters(codeString);
    112             writer.writeEndElement();
    113
    114             writer.writeStartElement("faultstring");
    115             writer.writeCharacters(fault.getMessage());
    116             writer.writeEndElement();
    117
    118             if (fault.hasDetails())
    119             {
    120                 Element detail = fault.getDetail();
    121
    122                 writer.writeStartElement("detail");
    123
    124                 StaxSerializer serializer = new StaxSerializer();
    125                 Elements details = detail.getChildElements();
    126                 for (int i = 0; i < details.size(); i++)
    127                 {
    128                     serializer.writeElement(details.get(i), writer);
    129                 }
    130
    131                 writer.writeEndElement(); // Details
    132             }
    133
    134             if (fault.getRole() != null)
    135             {
    136                 writer.writeStartElement("faultactor");
    137                 writer.writeCharacters(fault.getRole());
    138                 writer.writeEndElement();
    139             }
    140
    141             writer.writeEndElement(); // Fault
    142         }
    143         catch (XMLStreamException xe)
    144         {
    145             throw new XFireRuntimeException("Couldn't create fault.", xe);
    146         }
    147     }
    148 }