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