View Javadoc

1   /*
2    * $Id: InboundLoggingMessageProcessor.java 20221 2010-11-17 17:49:45Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.endpoint.inbound;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.api.processor.MessageProcessor;
18  import org.mule.util.ObjectUtils;
19  import org.mule.util.StringMessageUtils;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  public class InboundLoggingMessageProcessor implements MessageProcessor
25  {
26      protected final transient Log logger = LogFactory.getLog(getClass());
27      protected InboundEndpoint endpoint;
28  
29      public InboundLoggingMessageProcessor(InboundEndpoint endpoint)
30      {
31          this.endpoint = endpoint;
32      }
33  
34      public MuleEvent process(MuleEvent event) throws MuleException
35      {
36          MuleMessage message = event.getMessage();
37          if (logger.isDebugEnabled())
38          {
39              logger.debug("Message Received on: " + endpoint.getEndpointURI());
40          }
41          if (logger.isTraceEnabled())
42          {
43              try
44              {
45                  logger.trace("Message Payload: \n"
46                               + StringMessageUtils.truncate(StringMessageUtils.toString(message.getPayload()),
47                                   200, false));
48                  logger.trace("Message detail: \n" + StringMessageUtils.headersToString(message));
49              }
50              catch (Exception e)
51              {
52                  // ignore
53              }
54          }
55  
56          return event;
57      }
58  
59      @Override
60      public String toString()
61      {
62          return ObjectUtils.toString(this);
63      }
64  }