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      public static final String SERVICE_HANDLERS_REGISTERED = "service.handlers.registered";
27  
28      public DefaultEndpoint()
29      {
30      }
31      
32      public void onReceive(MessageContext context, InMessage msg)
33      {
34          if (log.isDebugEnabled()) log.debug("Received message to " + msg.getUri());
35          
36          if (context.getExchange() == null)
37          {
38              InExchange exchange = new InExchange(context);
39              exchange.setInMessage(msg);
40          }
41          
42          // Create the handlerpipeline and invoke it
43          HandlerPipeline pipeline = new HandlerPipeline(context.getXFire().getInPhases());
44          pipeline.addHandlers(context.getXFire().getInHandlers());
45          pipeline.addHandlers(msg.getChannel().getTransport().getInHandlers());
46  
47          if (context.getService() != null)
48          {
49              pipeline.addHandlers(context.getService().getInHandlers());
50              context.setProperty(SERVICE_HANDLERS_REGISTERED, Boolean.TRUE);
51          }
52          
53          context.setInPipeline(pipeline);
54          
55          try
56          {
57              pipeline.invoke(context);
58              
59              finishReadingMessage(msg, context);
60          }
61          catch (Exception e)
62          {
63              log.debug("Fault occurred!", e);
64              XFireFault fault = XFireFault.createFault(e);
65  
66              // Give the previously invoked pipeline a chance to clean up.
67              pipeline.handleFault(fault, context);
68              
69              Service service = context.getService();
70              if (service == null || service.getFaultSerializer() == null)
71              {
72                  sendToDeadLetter(fault, context);
73              }
74              else
75              {
76                  sendFault(fault, context);
77              }
78          }
79      }
80  
81      protected void sendToDeadLetter(XFireFault fault, MessageContext context)
82      {
83          log.error("Could not find service.", fault);
84      }
85  
86      protected void sendFault(XFireFault fault, MessageContext context)
87      {
88          // Create the outgoing fault message
89          OutMessage outMsg = (OutMessage) context.getExchange().getFaultMessage();
90          
91          outMsg.setSerializer(context.getService().getFaultSerializer());
92          outMsg.setBody(fault);
93          
94          // Create a fault pipeline
95          HandlerPipeline faultPipe = new HandlerPipeline(context.getXFire().getFaultPhases());
96          
97          faultPipe.addHandlers(context.getXFire().getFaultHandlers());
98          
99          Channel faultChannel = context.getExchange().getFaultMessage().getChannel();
100         if (faultChannel != null)
101         {
102             faultPipe.addHandlers(faultChannel.getTransport().getFaultHandlers());
103         }
104 
105         if (context.getService() != null)
106         {
107             faultPipe.addHandlers(context.getService().getFaultHandlers());
108         }
109         
110         try
111         {
112             faultPipe.invoke(context);
113         }
114         catch (Exception e1)
115         {
116             // An exception occurred while sending the fault. Log and move on.
117             XFireFault fault2 = XFireFault.createFault(e1);
118             faultPipe.handleFault(fault2, context);
119             
120             log.error("Could not send fault.", e1);
121         }
122     }
123 
124     public void finishReadingMessage(InMessage message, MessageContext context)
125         throws XFireFault
126     {
127         XMLStreamReader reader = message.getXMLStreamReader();
128 
129         try
130         {
131             while (reader.hasNext()) reader.next();
132         }
133         catch (XMLStreamException e)
134         {
135             throw new XFireFault("Couldn't parse message.", e, XFireFault.SENDER);
136         }
137     }
138 }