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.DepthXMLStreamReader;
    20  import org.codehaus.xfire.util.STAXUtils;
    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 Invoker invoker = getInvoker();
    58
    59          final List params = new ArrayList();
    60
    61          for (Iterator itr = operation.getInputMessage().getMessageParts().iterator(); itr.hasNext();)
    62          {
    63              MessagePartInfo p = (MessagePartInfo) itr.next();
    64              params.add( getBindingProvider().readParameter(p, message.getXMLStreamReader(), context) );
    65              nextEvent(message.getXMLStreamReader());
    66          }
    67
    68          message.setBody( params );
    69      }
    70
    71      public void writeMessage(OutMessage message, XMLStreamWriter writer, MessageContext context)
    72          throws XFireFault
    73      {
    74          Object[] values = (Object[]) message.getBody();
    75          final OperationInfo operation = context.getExchange().getOperation();
    76
    77          int i = 0;
    78          for (Iterator itr = operation.getOutputMessage().getMessageParts().iterator(); itr.hasNext();)
    79          {
    80              MessagePartInfo p = (MessagePartInfo) itr.next();
    81
    82              getBindingProvider().writeParameter(p, writer, context, values[i]);
    83              i++;
    84          }
    85      }
    86
    87      public Object clone()
    88      {
    89          MessageBinding binding = new MessageBinding();
    90          binding.setBindingProvider(getBindingProvider());
    91
    92          return binding;
    93      }
    94  }