ProtocolCommandEvent.java

  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. package org.apache.commons.net;

  18. import java.util.EventObject;

  19. /**
  20.  * 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.
  21.  * For debugging and other purposes, it is extremely useful to log or keep track of the contents of the protocol messages. The ProtocolCommandEvent class
  22.  * coupled with the {@link org.apache.commons.net.ProtocolCommandListener} interface facilitate this process.
  23.  *
  24.  *
  25.  * @see ProtocolCommandListener
  26.  * @see ProtocolCommandSupport
  27.  */

  28. public class ProtocolCommandEvent extends EventObject {
  29.     private static final long serialVersionUID = 403743538418947240L;

  30.     private final int replyCode;
  31.     private final boolean isCommand;
  32.     private final String message, command;

  33.     /**
  34.      * Creates a ProtocolCommandEvent signalling a reply to a command was received. ProtocolCommandEvents created with this constructor should only be sent
  35.      * after a complete command reply has been received from a server.
  36.      *
  37.      * @param source    The source of the event.
  38.      * @param replyCode The integer code indicating the natureof the reply. This will be the protocol integer value for protocols that use integer reply codes,
  39.      *                  or the reply class constant corresponding to the reply for protocols like POP3 that use strings like OK rather than integer codes (i.e.,
  40.      *                  POP3Repy.OK).
  41.      * @param message   The entire reply as received from the server.
  42.      */
  43.     public ProtocolCommandEvent(final Object source, final int replyCode, final String message) {
  44.         super(source);
  45.         this.replyCode = replyCode;
  46.         this.message = message;
  47.         this.isCommand = false;
  48.         this.command = null;
  49.     }

  50.     /**
  51.      * Creates a ProtocolCommandEvent signalling a command was sent to the server. ProtocolCommandEvents created with this constructor should only be sent after
  52.      * a command has been sent, but before the reply has been received.
  53.      *
  54.      * @param source  The source of the event.
  55.      * @param command The string representation of the command type sent, not including the arguments (e.g., "STAT" or "GET").
  56.      * @param message The entire command string verbatim as sent to the server, including all arguments.
  57.      */
  58.     public ProtocolCommandEvent(final Object source, final String command, final String message) {
  59.         super(source);
  60.         this.replyCode = 0;
  61.         this.message = message;
  62.         this.isCommand = true;
  63.         this.command = command;
  64.     }

  65.     /**
  66.      * Returns the string representation of the command type sent (e.g., "STAT" or "GET"). If the ProtocolCommandEvent is a reply event, then null is returned.
  67.      *
  68.      * @return The string representation of the command type sent, or null if this is a reply event.
  69.      */
  70.     public String getCommand() {
  71.         return command;
  72.     }

  73.     /**
  74.      * Returns the entire message sent to or received from the server. Includes the line terminator.
  75.      *
  76.      * @return The entire message sent to or received from the server.
  77.      */
  78.     public String getMessage() {
  79.         return message;
  80.     }

  81.     /**
  82.      * Returns the reply code of the received server reply. Undefined if this is not a reply event.
  83.      *
  84.      * @return The reply code of the received server reply. Undefined if not a reply event.
  85.      */
  86.     public int getReplyCode() {
  87.         return replyCode;
  88.     }

  89.     /**
  90.      * Returns true if the ProtocolCommandEvent was generated as a result of sending a command.
  91.      *
  92.      * @return true If the ProtocolCommandEvent was generated as a result of sending a command. False otherwise.
  93.      */
  94.     public boolean isCommand() {
  95.         return isCommand;
  96.     }

  97.     /**
  98.      * Returns true if the ProtocolCommandEvent was generated as a result of receiving a reply.
  99.      *
  100.      * @return true If the ProtocolCommandEvent was generated as a result of receiving a reply. False otherwise.
  101.      */
  102.     public boolean isReply() {
  103.         return !isCommand();
  104.     }
  105. }