View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net;
18  
19  import java.io.ObjectInputStream;
20  import java.io.ObjectOutputStream;
21  import java.io.Serializable;
22  
23  import org.apache.commons.net.util.ListenerList;
24  
25  /**
26   * ProtocolCommandSupport is a convenience class for managing a list of ProtocolCommandListeners and firing ProtocolCommandEvents. You can simply delegate
27   * ProtocolCommandEvent firing and listener registering/unregistering tasks to this class.
28   *
29   * @see ProtocolCommandEvent
30   * @see ProtocolCommandListener
31   */
32  public class ProtocolCommandSupport implements Serializable {
33  
34      /**
35       * Serialization is unnecessary for this class. Reject attempts to do so until such time as the Serializable attribute can be dropped.
36       */
37      private static final long serialVersionUID = -8017692739988399978L;
38  
39      /**
40       * The source to use for all generated ProtocolCommandEvents.
41       */
42      private final Object source;
43  
44      /**
45       * The ProtocolCommandListener.
46       */
47      private final ListenerList<ProtocolCommandListener> listeners;
48  
49      /**
50       * Creates a ProtocolCommandSupport instance using the indicated source as the source of ProtocolCommandEvents.
51       *
52       * @param source The source to use for all generated ProtocolCommandEvents.
53       */
54      public ProtocolCommandSupport(final Object source) {
55          this.listeners = new ListenerList<>();
56          this.source = source;
57      }
58  
59      /**
60       * Adds a ProtocolCommandListener.
61       *
62       * @param listener The ProtocolCommandListener to add.
63       */
64      public void addProtocolCommandListener(final ProtocolCommandListener listener) {
65          listeners.addListener(listener);
66      }
67  
68      /**
69       * Fires a ProtocolCommandEvent signaling the sending of a command to all registered listeners, invoking their
70       * {@link org.apache.commons.net.ProtocolCommandListener#protocolCommandSent protocolCommandSent()} methods.
71       *
72       * @param command The string representation of the command type sent, not including the arguments (e.g., "STAT" or "GET").
73       * @param message The entire command string verbatim as sent to the server, including all arguments.
74       */
75      public void fireCommandSent(final String command, final String message) {
76          if (!listeners.isEmpty()) {
77              final ProtocolCommandEvent event = new ProtocolCommandEvent(source, command, message);
78              listeners.forEach(listener -> listener.protocolCommandSent(event));
79          }
80      }
81  
82      /**
83       * Fires a ProtocolCommandEvent signaling the reception of a command reply to all registered listeners, invoking their
84       * {@link org.apache.commons.net.ProtocolCommandListener#protocolReplyReceived protocolReplyReceived()} methods.
85       *
86       * @param replyCode The integer code indicating the nature of the reply. This will be the protocol integer value for protocols that use integer reply codes,
87       *                  or the reply class constant corresponding to the reply for protocols like POP3 that use strings like OK rather than integer codes (i.e.,
88       *                  POP3Repy.OK).
89       * @param message   The entire reply as received from the server.
90       */
91      public void fireReplyReceived(final int replyCode, final String message) {
92          if (!listeners.isEmpty()) {
93              final ProtocolCommandEvent event = new ProtocolCommandEvent(source, replyCode, message);
94              listeners.forEach(listener -> listener.protocolReplyReceived(event));
95          }
96      }
97  
98      /**
99       * Gets the number of ProtocolCommandListeners currently registered.
100      *
101      * @return The number of ProtocolCommandListeners currently registered.
102      */
103     public int getListenerCount() {
104         return listeners.getListenerCount();
105     }
106 
107     /**
108      * Throws UnsupportedOperationException.
109      *
110      * @param ignored Ignored.
111      */
112     private void readObject(final ObjectInputStream ignored) {
113         throw new UnsupportedOperationException("Serialization is not supported");
114     }
115 
116     /**
117      * Removes a ProtocolCommandListener.
118      *
119      * @param listener The ProtocolCommandListener to remove.
120      */
121     public void removeProtocolCommandListener(final ProtocolCommandListener listener) {
122         listeners.removeListener(listener);
123     }
124 
125     /**
126      * Always throws {@link UnsupportedOperationException}.
127      *
128      * @param ignored ignored.
129      * @throws UnsupportedOperationException Always thrown.
130      */
131     private void writeObject(final ObjectOutputStream ignored) {
132         throw new UnsupportedOperationException("Serialization is not supported");
133     }
134 }