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:
018: import java.util.EventObject;
019:
020: /***
021: * There exists a large class of IETF protocols that work by sending an
022: * ASCII text command and arguments to a server, and then receiving an
023: * ASCII text reply. For debugging and other purposes, it is extremely
024: * useful to log or keep track of the contents of the protocol messages.
025: * The ProtocolCommandEvent class coupled with the
026: * {@link org.apache.commons.net.ProtocolCommandListener}
027: * interface facilitate this process.
028: * <p>
029: * <p>
030: * @see ProtocolCommandListener
031: * @see ProtocolCommandSupport
032: * @author Daniel F. Savarese
033: ***/
034:
035: public class ProtocolCommandEvent extends EventObject {
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,
053: String message) {
054: super (source);
055: __replyCode = 0;
056: __message = message;
057: __isCommand = true;
058: __command = command;
059: }
060:
061: /***
062: * Creates a ProtocolCommandEvent signalling a reply to a command was
063: * received. ProtocolCommandEvents created with this constructor
064: * should only be sent after a complete command reply has been received
065: * fromt a server.
066: * <p>
067: * @param source The source of the event.
068: * @param replyCode The integer code indicating the natureof the reply.
069: * This will be the protocol integer value for protocols
070: * that use integer reply codes, or the reply class constant
071: * corresponding to the reply for protocols like POP3 that use
072: * strings like OK rather than integer codes (i.e., POP3Repy.OK).
073: * @param message The entire reply as received from the server.
074: ***/
075: public ProtocolCommandEvent(Object source, int replyCode,
076: String message) {
077: super (source);
078: __replyCode = replyCode;
079: __message = message;
080: __isCommand = false;
081: __command = null;
082: }
083:
084: /***
085: * Returns the string representation of the command type sent (e.g., "STAT"
086: * or "GET"). If the ProtocolCommandEvent is a reply event, then null
087: * is returned.
088: * <p>
089: * @return The string representation of the command type sent, or null
090: * if this is a reply event.
091: ***/
092: public String getCommand() {
093: return __command;
094: }
095:
096: /***
097: * Returns the reply code of the received server reply. Undefined if
098: * this is not a reply event.
099: * <p>
100: * @return The reply code of the received server reply. Undefined if
101: * not a reply event.
102: ***/
103: public int getReplyCode() {
104: return __replyCode;
105: }
106:
107: /***
108: * Returns true if the ProtocolCommandEvent was generated as a result
109: * of sending a command.
110: * <p>
111: * @return true If the ProtocolCommandEvent was generated as a result
112: * of sending a command. False otherwise.
113: ***/
114: public boolean isCommand() {
115: return __isCommand;
116: }
117:
118: /***
119: * Returns true if the ProtocolCommandEvent was generated as a result
120: * of receiving a reply.
121: * <p>
122: * @return true If the ProtocolCommandEvent was generated as a result
123: * of receiving a reply. False otherwise.
124: ***/
125: public boolean isReply() {
126: return !isCommand();
127: }
128:
129: /***
130: * Returns the entire message sent to or received from the server.
131: * <p>
132: * @return The entire message sent to or received from the server.
133: ***/
134: public String getMessage() {
135: return __message;
136: }
137: }
|