View Javadoc

1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.net;
17  import java.util.EventObject;
18  
19  /***
20   * There exists a large class of IETF protocols that work by sending an
21   * ASCII text command and arguments to a server, and then receiving an
22   * ASCII text reply.  For debugging and other purposes, it is extremely
23   * useful to log or keep track of the contents of the protocol messages.
24   * The ProtocolCommandEvent class coupled with the
25   * {@link org.apache.commons.net.ProtocolCommandListener}
26   *  interface facilitate this process.
27   * <p>
28   * <p>
29   * @see ProtocolCommandListener
30   * @see ProtocolCommandSupport
31   * @author Daniel F. Savarese
32   ***/
33  
34  public class ProtocolCommandEvent extends EventObject
35  {
36      private int __replyCode;
37      private boolean __isCommand;
38      private String __message, __command;
39  
40      /***
41       * Creates a ProtocolCommandEvent signalling a command was sent to
42       * the server.  ProtocolCommandEvents created with this constructor
43       * should only be sent after a command has been sent, but before the
44       * reply has been received.
45       * <p>
46       * @param source  The source of the event.
47       * @param command The string representation of the command type sent, not
48       *      including the arguments (e.g., "STAT" or "GET").
49       * @param message The entire command string verbatim as sent to the server,
50       *        including all arguments.
51       ***/
52      public ProtocolCommandEvent(Object source, String command, String message)
53      {
54          super(source);
55          __replyCode = 0;
56          __message = message;
57          __isCommand = true;
58          __command = command;
59      }
60  
61  
62      /***
63       * Creates a ProtocolCommandEvent signalling a reply to a command was
64       * received.  ProtocolCommandEvents created with this constructor
65       * should only be sent after a complete command reply has been received
66       * fromt a server.
67       * <p>
68       * @param source  The source of the event.
69       * @param replyCode The integer code indicating the natureof the reply.
70       *   This will be the protocol integer value for protocols
71       *   that use integer reply codes, or the reply class constant
72       *   corresponding to the reply for protocols like POP3 that use
73       *   strings like OK rather than integer codes (i.e., POP3Repy.OK).
74       * @param message The entire reply as received from the server.
75       ***/
76      public ProtocolCommandEvent(Object source, int replyCode, String message)
77      {
78          super(source);
79          __replyCode = replyCode;
80          __message = message;
81          __isCommand = false;
82          __command = null;
83      }
84  
85      /***
86       * Returns the string representation of the command type sent (e.g., "STAT"
87       * or "GET").  If the ProtocolCommandEvent is a reply event, then null
88       * is returned.
89       * <p>
90       * @return The string representation of the command type sent, or null
91       *         if this is a reply event.
92       ***/
93      public String getCommand()
94      {
95          return __command;
96      }
97  
98  
99      /***
100      * Returns the reply code of the received server reply.  Undefined if
101      * this is not a reply event.
102      * <p>
103      * @return The reply code of the received server reply.  Undefined if
104      *         not a reply event.
105      ***/
106     public int getReplyCode()
107     {
108         return __replyCode;
109     }
110 
111     /***
112      * Returns true if the ProtocolCommandEvent was generated as a result
113      * of sending a command.
114      * <p>
115      * @return true If the ProtocolCommandEvent was generated as a result
116      * of sending a command.  False otherwise.
117      ***/
118     public boolean isCommand()
119     {
120         return __isCommand;
121     }
122 
123     /***
124      * Returns true if the ProtocolCommandEvent was generated as a result
125      * of receiving a reply.
126      * <p>
127      * @return true If the ProtocolCommandEvent was generated as a result
128      * of receiving a reply.  False otherwise.
129      ***/
130     public boolean isReply()
131     {
132         return !isCommand();
133     }
134 
135     /***
136      * Returns the entire message sent to or received from the server.
137      * <p>
138      * @return The entire message sent to or received from the server.
139      ***/
140     public String getMessage()
141     {
142         return __message;
143     }
144 }