View Javadoc

    1   package org.codehaus.xfire.transport;
    2
    3   import javax.xml.stream.XMLStreamException;
    4   import javax.xml.stream.XMLStreamReader;
    5
    6   import org.apache.commons.logging.Log;
    7   import org.apache.commons.logging.LogFactory;
    8   import org.codehaus.xfire.MessageContext;
    9   import org.codehaus.xfire.exchange.InExchange;
    10  import org.codehaus.xfire.exchange.InMessage;
    11  import org.codehaus.xfire.exchange.OutMessage;
    12  import org.codehaus.xfire.fault.XFireFault;
    13  import org.codehaus.xfire.handler.HandlerPipeline;
    14  import org.codehaus.xfire.service.Service;
    15
    16  /***
    17   * A <code>ChannelEndpoint</code> which executes the in pipeline
    18   * on the service and starts a <code>MessageExchange</code>.
    19   *
    20   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    21   */
    22  public class DefaultEndpoint
    23      implements ChannelEndpoint
    24  {
    25      private static final Log log = LogFactory.getLog(DefaultEndpoint.class);
    26
    27      public DefaultEndpoint()
    28      {
    29      }
    30
    31      public void onReceive(MessageContext context, InMessage msg)
    32      {
    33          if (log.isDebugEnabled()) log.debug("Received message to " + msg.getUri());
    34
    35          if (context.getExchange() == null)
    36          {
    37              InExchange exchange = new InExchange(context);
    38              exchange.setInMessage(msg);
    39          }
    40
    41          // Create the handlerpipeline and invoke it
    42          HandlerPipeline pipeline = new HandlerPipeline(context.getXFire().getInPhases());
    43          pipeline.addHandlers(context.getXFire().getInHandlers());
    44          pipeline.addHandlers(msg.getChannel().getTransport().getInHandlers());
    45
    46          context.setInPipeline(pipeline);
    47
    48          try
    49          {
    50              pipeline.invoke(context);
    51
    52              finishReadingMessage(msg, context);
    53          }
    54          catch (Exception e)
    55          {
    56              log.debug("Fault occurred!", e);
    57              XFireFault fault = XFireFault.createFault(e);
    58
    59              // Give the previously invoked pipeline a chance to clean up.
    60              pipeline.handleFault(fault, context);
    61
    62              Service service = context.getService();
    63              if (service == null || service.getFaultSerializer() == null)
    64              {
    65                  sendToDeadLetter(fault, context);
    66              }
    67              else
    68              {
    69                  sendFault(fault, context);
    70              }
    71          }
    72      }
    73
    74      protected void sendToDeadLetter(XFireFault fault, MessageContext context)
    75      {
    76          log.error("Could not find service.", fault);
    77      }
    78
    79      protected void sendFault(XFireFault fault, MessageContext context)
    80      {
    81          // Create the outgoing fault message
    82          OutMessage outMsg = (OutMessage) context.getExchange().getFaultMessage();
    83
    84          outMsg.setSerializer(context.getService().getFaultSerializer());
    85          outMsg.setBody(fault);
    86
    87          // Create a fault pipeline
    88          HandlerPipeline faultPipe = new HandlerPipeline(context.getXFire().getFaultPhases());
    89
    90          faultPipe.addHandlers(context.getXFire().getFaultHandlers());
    91
    92          Channel faultChannel = context.getExchange().getFaultMessage().getChannel();
    93          if (faultChannel != null)
    94          {
    95              faultPipe.addHandlers(faultChannel.getTransport().getFaultHandlers());
    96          }
    97
    98          if (context.getService() != null)
    99          {
    100             faultPipe.addHandlers(context.getService().getFaultHandlers());
    101         }
    102
    103         try
    104         {
    105             faultPipe.invoke(context);
    106         }
    107         catch (Exception e1)
    108         {
    109             // An exception occurred while sending the fault. Log and move on.
    110             XFireFault fault2 = XFireFault.createFault(e1);
    111             faultPipe.handleFault(fault2, context);
    112
    113             log.error("Could not send fault.", e1);
    114         }
    115     }
    116
    117     public void finishReadingMessage(InMessage message, MessageContext context)
    118         throws XFireFault
    119     {
    120         XMLStreamReader reader = message.getXMLStreamReader();
    121
    122         try
    123         {
    124             while (reader.hasNext()) reader.next();
    125         }
    126         catch (XMLStreamException e)
    127         {
    128             throw new XFireFault("Couldn't parse message.", e, XFireFault.SENDER);
    129         }
    130     }
    131 }