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   * @author Daniel F. Savarese
34   ***/
35  
36  public class ProtocolCommandEvent extends EventObject
37  {
38      private int __replyCode;
39      private boolean __isCommand;
40      private String __message, __command;
41  
42      /***
43       * Creates a ProtocolCommandEvent signalling a command was sent to
44       * the server.  ProtocolCommandEvents created with this constructor
45       * should only be sent after a command has been sent, but before the
46       * reply has been received.
47       * <p>
48       * @param source  The source of the event.
49       * @param command The string representation of the command type sent, not
50       *      including the arguments (e.g., "STAT" or "GET").
51       * @param message The entire command string verbatim as sent to the server,
52       *        including all arguments.
53       ***/
54      public ProtocolCommandEvent(Object source, String command, String message)
55      {
56          super(source);
57          __replyCode = 0;
58          __message = message;
59          __isCommand = true;
60          __command = command;
61      }
62  
63  
64      /***
65       * Creates a ProtocolCommandEvent signalling a reply to a command was
66       * received.  ProtocolCommandEvents created with this constructor
67       * should only be sent after a complete command reply has been received
68       * fromt a server.
69       * <p>
70       * @param source  The source of the event.
71       * @param replyCode The integer code indicating the natureof the reply.
72       *   This will be the protocol integer value for protocols
73       *   that use integer reply codes, or the reply class constant
74       *   corresponding to the reply for protocols like POP3 that use
75       *   strings like OK rather than integer codes (i.e., POP3Repy.OK).
76       * @param message The entire reply as received from the server.
77       ***/
78      public ProtocolCommandEvent(Object source, int replyCode, String message)
79      {
80          super(source);
81          __replyCode = replyCode;
82          __message = message;
83          __isCommand = false;
84          __command = null;
85      }
86  
87      /***
88       * Returns the string representation of the command type sent (e.g., "STAT"
89       * or "GET").  If the ProtocolCommandEvent is a reply event, then null
90       * is returned.
91       * <p>
92       * @return The string representation of the command type sent, or null
93       *         if this is a reply event.
94       ***/
95      public String getCommand()
96      {
97          return __command;
98      }
99  
100 
101     /***
102      * Returns the reply code of the received server reply.  Undefined if
103      * this is not a reply event.
104      * <p>
105      * @return The reply code of the received server reply.  Undefined if
106      *         not a reply event.
107      ***/
108     public int getReplyCode()
109     {
110         return __replyCode;
111     }
112 
113     /***
114      * Returns true if the ProtocolCommandEvent was generated as a result
115      * of sending a command.
116      * <p>
117      * @return true If the ProtocolCommandEvent was generated as a result
118      * of sending a command.  False otherwise.
119      ***/
120     public boolean isCommand()
121     {
122         return __isCommand;
123     }
124 
125     /***
126      * Returns true if the ProtocolCommandEvent was generated as a result
127      * of receiving a reply.
128      * <p>
129      * @return true If the ProtocolCommandEvent was generated as a result
130      * of receiving a reply.  False otherwise.
131      ***/
132     public boolean isReply()
133     {
134         return !isCommand();
135     }
136 
137     /***
138      * Returns the entire message sent to or received from the server.
139      * <p>
140      * @return The entire message sent to or received from the server.
141      ***/
142     public String getMessage()
143     {
144         return __message;
145     }
146 }