View Javadoc

    1   package org.codehaus.xfire.service.binding;
    2
    3   import javax.xml.parsers.DocumentBuilder;
    4   import javax.xml.parsers.DocumentBuilderFactory;
    5   import javax.xml.stream.XMLStreamException;
    6   import javax.xml.stream.XMLStreamReader;
    7   import javax.xml.stream.XMLStreamWriter;
    8
    9   import org.apache.commons.logging.Log;
    10  import org.apache.commons.logging.LogFactory;
    11  import org.codehaus.xfire.MessageContext;
    12  import org.codehaus.xfire.XFireRuntimeException;
    13  import org.codehaus.xfire.fault.XFireFault;
    14  import org.codehaus.xfire.service.MessageHeaderInfo;
    15  import org.codehaus.xfire.service.MessagePartInfo;
    16  import org.codehaus.xfire.service.Service;
    17  import org.codehaus.xfire.util.STAXUtils;
    18  import org.codehaus.xfire.wsdl.SchemaType;
    19  import org.codehaus.yom.Element;
    20  import org.codehaus.yom.Node;
    21  import org.codehaus.yom.stax.StaxBuilder;
    22  import org.codehaus.yom.stax.StaxSerializer;
    23  import org.w3c.dom.Document;
    24
    25  public class MessageBindingProvider
    26      implements BindingProvider
    27  {
    28      private static final Log logger = LogFactory.getLog(MessageBindingProvider.class);
    29
    30      public void initialize(Service newParam)
    31      {
    32      }
    33
    34      public Object readParameter(MessagePartInfo p, XMLStreamReader reader, MessageContext context)
    35          throws XFireFault
    36      {
    37          if (p.getTypeClass().isAssignableFrom(XMLStreamReader.class))
    38          {
    39              return context.getInMessage().getXMLStreamReader();
    40          }
    41          else if (p.getTypeClass().isAssignableFrom(Element.class))
    42          {
    43              StaxBuilder builder = new StaxBuilder();
    44              try
    45              {
    46                  return builder.buildElement(null, context.getInMessage().getXMLStreamReader());
    47              }
    48              catch (XMLStreamException e)
    49              {
    50                  throw new XFireFault("Couldn't parse stream.", e, XFireFault.SENDER);
    51              }
    52          }
    53          else if (Document.class.isAssignableFrom(p.getTypeClass()))
    54          {
    55              try
    56              {
    57                  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    58                  DocumentBuilder builder = factory.newDocumentBuilder();
    59
    60                  return STAXUtils.read(builder, reader);
    61              }
    62              catch(Exception e)
    63              {
    64                  throw new XFireFault("Couldn't read message.", e, XFireFault.SENDER);
    65              }
    66          }
    67          else if (p.getTypeClass().isAssignableFrom(MessageContext.class))
    68          {
    69              return context;
    70          }
    71          else
    72          {
    73              logger.warn("Unknown type for serialization: " + p.getTypeClass());
    74              return null;
    75          }
    76      }
    77
    78      public void writeParameter(MessagePartInfo p,
    79                                 XMLStreamWriter writer,
    80                                 MessageContext context,
    81                                 Object value)
    82          throws XFireFault
    83      {
    84          if (value instanceof Element)
    85          {
    86              StaxSerializer serializer = new StaxSerializer();
    87              try
    88              {
    89                  serializer.writeElement((Element) value, writer);
    90              }
    91              catch (XMLStreamException e)
    92              {
    93                  throw new XFireRuntimeException("Couldn't write to stream.", e);
    94              }
    95          }
    96          else
    97          {
    98              logger.warn("Unknown type for serialization: " + p.getTypeClass());
    99          }
    100     }
    101
    102     public SchemaType getSchemaType(Service service, MessagePartInfo param)
    103     {
    104         return null;
    105     }
    106
    107     public Object readHeader(MessageHeaderInfo p, MessageContext context)
    108         throws XFireFault
    109     {
    110         return context.getInMessage().getHeader().getFirstChildElement(p.getName().getLocalPart(),
    111                                                                        p.getName().getNamespaceURI());
    112     }
    113
    114     public void writeHeader(MessagePartInfo p, MessageContext context, Object value)
    115         throws XFireFault
    116     {
    117         context.getOutMessage().getHeader().appendChild((Node) value);
    118     }
    119 }