View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.endpoint.inbound;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.processor.MessageProcessor;
13  import org.mule.api.transport.Connector;
14  import org.mule.config.ExceptionHelper;
15  import org.mule.util.ObjectUtils;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  
20  /**
21   * Sets error message properties as specified by the transport based on the exception
22   * type of the exception payload. This mechanism uses a transport properties file in
23   * the META-INF/services/org/mule/config directory called
24   * mule-exception-codes.properties. This property file maps the fully qualified class
25   * names of exceptions to the value of the property that should be set. The name of
26   * the property is defined by the error.code.property property in the same properties
27   * file.
28   */
29  public class InboundExceptionDetailsMessageProcessor implements MessageProcessor
30  {
31  
32      private static final Log logger = LogFactory.getLog(InboundExceptionDetailsMessageProcessor.class);
33  
34      protected Connector connector;
35  
36      public InboundExceptionDetailsMessageProcessor(Connector connector)
37      {
38          this.connector = connector;
39      }
40  
41      public MuleEvent process(MuleEvent event) throws MuleException
42      {
43          if (event != null)
44          {
45              MuleMessage resultMessage = event.getMessage();
46              if (resultMessage != null)
47              {
48                  if (resultMessage.getExceptionPayload() != null)
49                  {
50                      setExceptionDetails(resultMessage, connector, resultMessage.getExceptionPayload()
51                          .getException());
52                  }
53              }
54          }
55          return event;
56      }
57  
58      /**
59       * This method is used to set any additional and possibly transport specific
60       * information on the return message where it has an exception payload.
61       * 
62       * @param message
63       * @param exception
64       */
65      protected void setExceptionDetails(MuleMessage message, Connector connector, Throwable exception)
66      {
67          String propName = ExceptionHelper.getErrorCodePropertyName(connector.getProtocol());
68          // If we dont find a error code property we can assume there are not
69          // error code mappings for this connector
70          if (propName != null)
71          {
72              String code = ExceptionHelper.getErrorMapping(connector.getProtocol(), exception.getClass());
73              if (logger.isDebugEnabled())
74              {
75                  logger.debug("Setting error code for: " + connector.getProtocol() + ", " + propName + "="
76                               + code);
77              }
78              message.setOutboundProperty(propName, code);
79          }
80      }
81  
82      @Override
83      public String toString()
84      {
85          return ObjectUtils.toString(this);
86      }
87  }