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    *      http://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  
18  package org.apache.commons.net;
19  
20  import java.util.EventObject;
21  
22  /**
23   * There exists a large class of IETF protocols that work by sending an ASCII text command and arguments to a server, and then receiving an ASCII text reply.
24   * For debugging and other purposes, it is extremely useful to log or keep track of the contents of the protocol messages. The ProtocolCommandEvent class
25   * coupled with the {@link org.apache.commons.net.ProtocolCommandListener} interface facilitate this process.
26   *
27   *
28   * @see ProtocolCommandListener
29   * @see ProtocolCommandSupport
30   */
31  
32  public class ProtocolCommandEvent extends EventObject {
33      private static final long serialVersionUID = 403743538418947240L;
34  
35      private final int replyCode;
36      private final boolean isCommand;
37      private final String message, command;
38  
39      /**
40       * Creates a ProtocolCommandEvent signalling a reply to a command was received. ProtocolCommandEvents created with this constructor should only be sent
41       * after a complete command reply has been received from a server.
42       *
43       * @param source    The source of the event.
44       * @param replyCode The integer code indicating the natureof the reply. This will be the protocol integer value for protocols that use integer reply codes,
45       *                  or the reply class constant corresponding to the reply for protocols like POP3 that use strings like OK rather than integer codes (i.e.,
46       *                  POP3Repy.OK).
47       * @param message   The entire reply as received from the server.
48       */
49      public ProtocolCommandEvent(final Object source, final int replyCode, final String message) {
50          super(source);
51          this.replyCode = replyCode;
52          this.message = message;
53          this.isCommand = false;
54          this.command = null;
55      }
56  
57      /**
58       * Creates a ProtocolCommandEvent signalling a command was sent to the server. ProtocolCommandEvents created with this constructor should only be sent after
59       * a command has been sent, but before the reply has been received.
60       *
61       * @param source  The source of the event.
62       * @param command The string representation of the command type sent, not including the arguments (e.g., "STAT" or "GET").
63       * @param message The entire command string verbatim as sent to the server, including all arguments.
64       */
65      public ProtocolCommandEvent(final Object source, final String command, final String message) {
66          super(source);
67          this.replyCode = 0;
68          this.message = message;
69          this.isCommand = true;
70          this.command = command;
71      }
72  
73      /**
74       * Returns the string representation of the command type sent (e.g., "STAT" or "GET"). If the ProtocolCommandEvent is a reply event, then null is returned.
75       *
76       * @return The string representation of the command type sent, or null if this is a reply event.
77       */
78      public String getCommand() {
79          return command;
80      }
81  
82      /**
83       * Returns the entire message sent to or received from the server. Includes the line terminator.
84       *
85       * @return The entire message sent to or received from the server.
86       */
87      public String getMessage() {
88          return message;
89      }
90  
91      /**
92       * Returns the reply code of the received server reply. Undefined if this is not a reply event.
93       *
94       * @return The reply code of the received server reply. Undefined if not a reply event.
95       */
96      public int getReplyCode() {
97          return replyCode;
98      }
99  
100     /**
101      * Returns true if the ProtocolCommandEvent was generated as a result of sending a command.
102      *
103      * @return true If the ProtocolCommandEvent was generated as a result of sending a command. False otherwise.
104      */
105     public boolean isCommand() {
106         return isCommand;
107     }
108 
109     /**
110      * Returns true if the ProtocolCommandEvent was generated as a result of receiving a reply.
111      *
112      * @return true If the ProtocolCommandEvent was generated as a result of receiving a reply. False otherwise.
113      */
114     public boolean isReply() {
115         return !isCommand();
116     }
117 }