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