View Javadoc

    1   package org.codehaus.xfire.soap;
    2
    3   import javax.xml.namespace.QName;
    4   import javax.xml.stream.XMLStreamException;
    5   import javax.xml.stream.XMLStreamWriter;
    6
    7   import org.codehaus.xfire.MessageContext;
    8   import org.codehaus.xfire.exchange.AbstractMessage;
    9   import org.codehaus.xfire.exchange.InMessage;
    10  import org.codehaus.xfire.exchange.MessageSerializer;
    11  import org.codehaus.xfire.exchange.OutMessage;
    12  import org.codehaus.xfire.fault.XFireFault;
    13  import org.codehaus.yom.Element;
    14  import org.codehaus.yom.Elements;
    15  import org.codehaus.yom.stax.StaxSerializer;
    16
    17  public class SoapSerializer
    18      implements MessageSerializer
    19  {
    20      public static final String SERIALIZE_PROLOG = "xfire.serializeProlog";
    21      private MessageSerializer serializer;
    22
    23      public SoapSerializer(MessageSerializer serializer)
    24      {
    25          this.serializer = serializer;
    26      }
    27
    28      public MessageSerializer getSerializer()
    29      {
    30          return serializer;
    31      }
    32
    33      /***
    34       * Sends a message wrapped in a SOAP Envelope and Body.
    35       *
    36       * @param message
    37       * @param writer
    38       * @param context
    39       * @throws XFireFault
    40       */
    41
    42      public void writeMessage(OutMessage message, XMLStreamWriter writer, MessageContext context)
    43          throws XFireFault
    44      {
    45          try
    46          {
    47              QName env = message.getSoapVersion().getEnvelope();
    48
    49              Boolean serializeObj = (Boolean) context.getProperty(SERIALIZE_PROLOG);
    50              boolean serializeProlog = (serializeObj != null) ? serializeObj.booleanValue() : true;
    51              if (serializeProlog)
    52                  writer.writeStartDocument(message.getEncoding(), "1.0");
    53
    54              writer.setPrefix(env.getPrefix(), env.getNamespaceURI());
    55              writer.writeStartElement(env.getPrefix(),
    56                                       env.getLocalPart(),
    57                                       env.getNamespaceURI());
    58              writer.writeNamespace(env.getPrefix(), env.getNamespaceURI());
    59
    60              if (message.getHeader() != null && message.getHeader().getChildCount() > 0)
    61              {
    62                  QName header = message.getSoapVersion().getHeader();
    63                  writer.writeStartElement(header.getPrefix(),
    64                                           header.getLocalPart(),
    65                                           header.getNamespaceURI());
    66
    67                  writeHeaders(message, writer);
    68
    69                  writer.writeEndElement();
    70              }
    71
    72              QName body = message.getSoapVersion().getBody();
    73              writer.writeStartElement(body.getPrefix(),
    74                                       body.getLocalPart(),
    75                                       body.getNamespaceURI());
    76
    77              serializer.writeMessage(message, writer, context);
    78
    79              writer.writeEndElement();
    80              writer.writeEndElement();
    81
    82              if (serializeProlog)
    83                  writer.writeEndDocument();
    84
    85              writer.close();
    86          }
    87          catch (XMLStreamException e)
    88          {
    89              throw new XFireFault("Couldn't write message.", e, XFireFault.RECEIVER);
    90          }
    91      }
    92
    93      protected void writeHeaders(AbstractMessage msg, XMLStreamWriter writer)
    94              throws XMLStreamException
    95      {
    96          StaxSerializer ser = new StaxSerializer();
    97
    98          Elements elements = msg.getHeader().getChildElements();
    99          for (int i = 0; i < elements.size(); i++)
    100         {
    101             Element e = elements.get(i);
    102
    103             ser.writeElement(e, writer);
    104         }
    105     }
    106
    107     public void readMessage(InMessage message, MessageContext context)
    108         throws XFireFault
    109     {
    110         throw new UnsupportedOperationException();
    111     }
    112 }