001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.net;
017    import java.util.EventObject;
018    
019    /***
020     * There exists a large class of IETF protocols that work by sending an
021     * ASCII text command and arguments to a server, and then receiving an
022     * ASCII text reply.  For debugging and other purposes, it is extremely
023     * useful to log or keep track of the contents of the protocol messages.
024     * The ProtocolCommandEvent class coupled with the
025     * {@link org.apache.commons.net.ProtocolCommandListener}
026     *  interface facilitate this process.
027     * <p>
028     * <p>
029     * @see ProtocolCommandListener
030     * @see ProtocolCommandSupport
031     * @author Daniel F. Savarese
032     ***/
033    
034    public class ProtocolCommandEvent extends EventObject
035    {
036        private int __replyCode;
037        private boolean __isCommand;
038        private String __message, __command;
039    
040        /***
041         * Creates a ProtocolCommandEvent signalling a command was sent to
042         * the server.  ProtocolCommandEvents created with this constructor
043         * should only be sent after a command has been sent, but before the
044         * reply has been received.
045         * <p>
046         * @param source  The source of the event.
047         * @param command The string representation of the command type sent, not
048         *      including the arguments (e.g., "STAT" or "GET").
049         * @param message The entire command string verbatim as sent to the server,
050         *        including all arguments.
051         ***/
052        public ProtocolCommandEvent(Object source, String command, String message)
053        {
054            super(source);
055            __replyCode = 0;
056            __message = message;
057            __isCommand = true;
058            __command = command;
059        }
060    
061    
062        /***
063         * Creates a ProtocolCommandEvent signalling a reply to a command was
064         * received.  ProtocolCommandEvents created with this constructor
065         * should only be sent after a complete command reply has been received
066         * fromt a server.
067         * <p>
068         * @param source  The source of the event.
069         * @param replyCode The integer code indicating the natureof the reply.
070         *   This will be the protocol integer value for protocols
071         *   that use integer reply codes, or the reply class constant
072         *   corresponding to the reply for protocols like POP3 that use
073         *   strings like OK rather than integer codes (i.e., POP3Repy.OK).
074         * @param message The entire reply as received from the server.
075         ***/
076        public ProtocolCommandEvent(Object source, int replyCode, String message)
077        {
078            super(source);
079            __replyCode = replyCode;
080            __message = message;
081            __isCommand = false;
082            __command = null;
083        }
084    
085        /***
086         * Returns the string representation of the command type sent (e.g., "STAT"
087         * or "GET").  If the ProtocolCommandEvent is a reply event, then null
088         * is returned.
089         * <p>
090         * @return The string representation of the command type sent, or null
091         *         if this is a reply event.
092         ***/
093        public String getCommand()
094        {
095            return __command;
096        }
097    
098    
099        /***
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    }