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 * https://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 20 import java.util.EventObject; 21 22 /** 23 * There exists a large class of IETF protocols that work by sending an ASCII text command and arguments to a server, and then receiving an ASCII text reply. 24 * For debugging and other purposes, it is extremely useful to log or keep track of the contents of the protocol messages. The ProtocolCommandEvent class 25 * coupled with the {@link org.apache.commons.net.ProtocolCommandListener} interface facilitate this process. 26 * 27 * 28 * @see ProtocolCommandListener 29 * @see ProtocolCommandSupport 30 */ 31 32 public class ProtocolCommandEvent extends EventObject { 33 private static final long serialVersionUID = 403743538418947240L; 34 35 /** 36 * The integer code indicating the natureof the reply. This will be the protocol integer value for protocols that use integer reply codes, or the reply 37 * class constant corresponding to the reply for protocols like POP3 that use strings like OK rather than integer codes (i.e., POP3Repy.OK). 38 */ 39 private final int replyCode; 40 41 /** 42 * Whether the ProtocolCommandEvent was generated as a result of sending a command. 43 */ 44 private final boolean isCommand; 45 46 /** 47 * The entire reply as received from the server. 48 */ 49 private final String message; 50 51 /** 52 * The string representation of the command type sent, not including the arguments (e.g., "STAT" or "GET"). 53 */ 54 private final String command; 55 56 /** 57 * Creates a ProtocolCommandEvent signaling a reply to a command was received. ProtocolCommandEvents created with this constructor should only be sent 58 * after a complete command reply has been received from a server. 59 * 60 * @param source The source of the event. 61 * @param replyCode The integer code indicating the natureof the reply. This will be the protocol integer value for protocols that use integer reply codes, 62 * or the reply class constant corresponding to the reply for protocols like POP3 that use strings like OK rather than integer codes (i.e., 63 * POP3Repy.OK). 64 * @param message The entire reply as received from the server. 65 */ 66 public ProtocolCommandEvent(final Object source, final int replyCode, final String message) { 67 super(source); 68 this.replyCode = replyCode; 69 this.message = message; 70 this.isCommand = false; 71 this.command = null; 72 } 73 74 /** 75 * Creates a ProtocolCommandEvent signaling a command was sent to the server. ProtocolCommandEvents created with this constructor should only be sent after 76 * a command has been sent, but before the reply has been received. 77 * 78 * @param source The source of the event. 79 * @param command The string representation of the command type sent, not including the arguments (e.g., "STAT" or "GET"). 80 * @param message The entire command string verbatim as sent to the server, including all arguments. 81 */ 82 public ProtocolCommandEvent(final Object source, final String command, final String message) { 83 super(source); 84 this.replyCode = 0; 85 this.message = message; 86 this.isCommand = true; 87 this.command = command; 88 } 89 90 /** 91 * Gets the string representation of the command type sent (e.g., "STAT" or "GET"). If the ProtocolCommandEvent is a reply event, then null is returned. 92 * 93 * @return The string representation of the command type sent, or null if this is a reply event. 94 */ 95 public String getCommand() { 96 return command; 97 } 98 99 /** 100 * Gets the entire message sent to or received from the server. Includes the line terminator. 101 * 102 * @return The entire message sent to or received from the server. 103 */ 104 public String getMessage() { 105 return message; 106 } 107 108 /** 109 * Gets the reply code of the received server reply. Undefined if this is not a reply event. 110 * 111 * @return The reply code of the received server reply. Undefined if not a reply event. 112 */ 113 public int getReplyCode() { 114 return replyCode; 115 } 116 117 /** 118 * Tests whether the ProtocolCommandEvent was generated as a result of sending a command. 119 * 120 * @return true If the ProtocolCommandEvent was generated as a result of sending a command. False otherwise. 121 */ 122 public boolean isCommand() { 123 return isCommand; 124 } 125 126 /** 127 * Tests whether the ProtocolCommandEvent was generated as a result of receiving a reply. 128 * 129 * @return true If the ProtocolCommandEvent was generated as a result of receiving a reply. False otherwise. 130 */ 131 public boolean isReply() { 132 return !isCommand(); 133 } 134 }