View Javadoc

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