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