View Javadoc

1   /*
2    * $Id: SftpTransportNotificationTestListener.java 21125 2011-01-26 21:21:10Z dzapata $
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.transport.sftp.notification;
12  
13  import java.util.Date;
14  
15  import org.mule.api.MuleMessage;
16  import org.mule.api.context.notification.ServerNotification;
17  import org.mule.transport.sftp.notification.SftpTransportNotification;
18  import org.mule.transport.sftp.notification.SftpTransportNotificationListener;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  import static org.mule.transport.sftp.notification.SftpTransportNotification.SFTP_GET_ACTION_MSG;
23  import static org.mule.transport.sftp.notification.SftpTransportNotification.SFTP_PUT_ACTION_MSG;
24  import static org.mule.transport.sftp.notification.SftpTransportNotification.SFTP_RENAME_ACTION_MSG;
25  import static org.mule.transport.sftp.notification.SftpTransportNotification.SFTP_DELETE_ACTION_MSG;
26  
27  public class SftpTransportNotificationTestListener implements SftpTransportNotificationListener
28  {
29  
30      private final Logger logger = LoggerFactory.getLogger(getClass());
31  
32      private static boolean gotSftpPutNotification = false;
33      private static boolean gotSftpRenameNotification = false;
34      private static boolean gotSftpGetNotification = false;
35      private static boolean gotSftpDeleteNotification = false;
36  
37      public void onNotification(ServerNotification notification)
38      {
39  
40          SftpTransportNotification sftpNotification;
41          if (notification instanceof SftpTransportNotification)
42          {
43              sftpNotification = (SftpTransportNotification) notification;
44          }
45          else
46          {
47              logger.debug("SftpTransportNotificationTestListener RECEIVED UNKNOWN NOTIFICATION OF TYPE {}",
48                  notification.getClass().getName());
49              return;
50          }
51  
52          String action = notification.getActionName();
53  
54          if (action.equals(SFTP_GET_ACTION_MSG))
55          {
56              gotSftpGetNotification = true;
57  
58          }
59          else if (action.equals(SFTP_PUT_ACTION_MSG))
60          {
61              gotSftpPutNotification = true;
62  
63          }
64          else if (action.equals(SFTP_RENAME_ACTION_MSG))
65          {
66              gotSftpRenameNotification = true;
67  
68          }
69          else if (action.equals(SFTP_DELETE_ACTION_MSG))
70          {
71              gotSftpDeleteNotification = true;
72          }
73  
74          String resourceId = notification.getResourceIdentifier();
75          String timestamp = new Date(notification.getTimestamp()).toString();
76  
77          String endpoint = sftpNotification.getEndpoint().getEndpointURI().toString();
78          String info = sftpNotification.getInfo();
79          long size = sftpNotification.getSize();
80  
81          String msgType = "???";
82          String correlationId = "???";
83          if (notification.getSource() instanceof MuleMessage)
84          {
85              MuleMessage message = (MuleMessage) notification.getSource();
86              msgType = message.getPayload().getClass().getName();
87              correlationId = (String) message.getProperty("MULE_CORRELATION_ID", "?");
88          }
89  
90          if (logger.isDebugEnabled())
91          {
92              logger.debug("OnNotification: " + notification.EVENT_NAME + "\nAction=" + action + " " + info
93                           + " " + size + "\nEndpoint=" + endpoint + "\nTimestamp=" + timestamp + "\nMsgType="
94                           + msgType + "\nResourceId=" + resourceId + "\nCorrelationId=" + correlationId + "");
95          }
96      }
97  
98      public static void reset()
99      {
100         gotSftpPutNotification = false;
101         gotSftpRenameNotification = false;
102         gotSftpGetNotification = false;
103         gotSftpDeleteNotification = false;
104     }
105 
106     public static boolean gotSftpPutNotification()
107     {
108         return gotSftpPutNotification;
109     }
110 
111     public static boolean gotSftpRenameNotification()
112     {
113         return gotSftpRenameNotification;
114     }
115 
116     public static boolean gotSftpGetNotification()
117     {
118         return gotSftpGetNotification;
119     }
120 
121     public static boolean gotSftpDeleteNotification()
122     {
123         return gotSftpDeleteNotification;
124     }
125 
126 }