View Javadoc

    1   package org.codehaus.xfire.service.binding;
    2
    3   import java.util.ArrayList;
    4   import java.util.Iterator;
    5   import java.util.List;
    6
    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.exchange.InMessage;
    13  import org.codehaus.xfire.exchange.OutMessage;
    14  import org.codehaus.xfire.fault.XFireFault;
    15  import org.codehaus.xfire.service.MessagePartInfo;
    16  import org.codehaus.xfire.service.OperationInfo;
    17  import org.codehaus.xfire.service.Service;
    18  import org.codehaus.xfire.soap.SoapConstants;
    19  import org.codehaus.xfire.util.STAXUtils;
    20  import org.codehaus.xfire.util.stax.DepthXMLStreamReader;
    21
    22  /***
    23   * Handles messages.
    24   *
    25   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse </a>
    26   * @since Feb 18, 2004
    27   */
    28  public class MessageBinding
    29      extends AbstractBinding
    30  {
    31      private static final Log logger = LogFactory.getLog(MessageBinding.class.getName());
    32
    33      public MessageBinding()
    34      {
    35          setStyle(SoapConstants.STYLE_MESSAGE);
    36          setUse(SoapConstants.USE_LITERAL);
    37      }
    38
    39      public void readMessage(InMessage message, MessageContext context)
    40          throws XFireFault
    41      {
    42          final Service endpoint = context.getService();
    43
    44          OperationInfo operation = context.getExchange().getOperation();
    45
    46          if (context.getExchange().getOperation() == null)
    47          {
    48              operation = (OperationInfo) endpoint.getServiceInfo().getOperations().iterator().next();
    49
    50              setOperation(operation, context);
    51          }
    52
    53          DepthXMLStreamReader dr = new DepthXMLStreamReader(message.getXMLStreamReader());
    54
    55          STAXUtils.toNextElement(dr);
    56
    57          final List params = new ArrayList();
    58
    59          for (Iterator itr = operation.getInputMessage().getMessageParts().iterator(); itr.hasNext();)
    60          {
    61              MessagePartInfo p = (MessagePartInfo) itr.next();
    62              params.add( getBindingProvider().readParameter(p, message.getXMLStreamReader(), context) );
    63              nextEvent(message.getXMLStreamReader());
    64          }
    65
    66          message.setBody( params );
    67      }
    68
    69      public void writeMessage(OutMessage message, XMLStreamWriter writer, MessageContext context)
    70          throws XFireFault
    71      {
    72          Object[] values = (Object[]) message.getBody();
    73          final OperationInfo operation = context.getExchange().getOperation();
    74
    75          int i = 0;
    76          for (Iterator itr = operation.getOutputMessage().getMessageParts().iterator(); itr.hasNext();)
    77          {
    78              MessagePartInfo p = (MessagePartInfo) itr.next();
    79
    80              getBindingProvider().writeParameter(p, writer, context, values[i]);
    81              i++;
    82          }
    83      }
    84
    85      public Object clone()
    86      {
    87          MessageBinding binding = new MessageBinding();
    88          binding.setBindingProvider(getBindingProvider());
    89
    90          return binding;
    91      }
    92  }