1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net;
  17. import java.util.EventObject;
  18. /***
  19. * There exists a large class of IETF protocols that work by sending an
  20. * ASCII text command and arguments to a server, and then receiving an
  21. * ASCII text reply. For debugging and other purposes, it is extremely
  22. * useful to log or keep track of the contents of the protocol messages.
  23. * The ProtocolCommandEvent class coupled with the
  24. * <a href="org.apache.commons.net.ProtocolCommandListener.html">
  25. * ProtocolCommandListener </a> interface facilitate this process.
  26. * <p>
  27. * <p>
  28. * @see ProtocolCommandListener
  29. * @see ProtocolCommandSupport
  30. * @author Daniel F. Savarese
  31. ***/
  32. public class ProtocolCommandEvent extends EventObject
  33. {
  34. private int __replyCode;
  35. private boolean __isCommand;
  36. private String __message, __command;
  37. /***
  38. * Creates a ProtocolCommandEvent signalling a command was sent to
  39. * the server. ProtocolCommandEvents created with this constructor
  40. * should only be sent after a command has been sent, but before the
  41. * reply has been received.
  42. * <p>
  43. * @param source The source of the event.
  44. * @param command The string representation of the command type sent, not
  45. * including the arguments (e.g., "STAT" or "GET").
  46. * @param message The entire command string verbatim as sent to the server,
  47. * including all arguments.
  48. ***/
  49. public ProtocolCommandEvent(Object source, String command, String message)
  50. {
  51. super(source);
  52. __replyCode = 0;
  53. __message = message;
  54. __isCommand = true;
  55. __command = command;
  56. }
  57. /***
  58. * Creates a ProtocolCommandEvent signalling a reply to a command was
  59. * received. ProtocolCommandEvents created with this constructor
  60. * should only be sent after a complete command reply has been received
  61. * fromt a server.
  62. * <p>
  63. * @param source The source of the event.
  64. * @param replyCode The integer code indicating the natureof the reply.
  65. * This will be the protocol integer value for protocols
  66. * that use integer reply codes, or the reply class constant
  67. * corresponding to the reply for protocols like POP3 that use
  68. * strings like OK rather than integer codes (i.e., POP3Repy.OK).
  69. * @param message The entire reply as received from the server.
  70. ***/
  71. public ProtocolCommandEvent(Object source, int replyCode, String message)
  72. {
  73. super(source);
  74. __replyCode = replyCode;
  75. __message = message;
  76. __isCommand = false;
  77. __command = null;
  78. }
  79. /***
  80. * Returns the string representation of the command type sent (e.g., "STAT"
  81. * or "GET"). If the ProtocolCommandEvent is a reply event, then null
  82. * is returned.
  83. * <p>
  84. * @return The string representation of the command type sent, or null
  85. * if this is a reply event.
  86. ***/
  87. public String getCommand()
  88. {
  89. return __command;
  90. }
  91. /***
  92. * Returns the reply code of the received server reply. Undefined if
  93. * this is not a reply event.
  94. * <p>
  95. * @return The reply code of the received server reply. Undefined if
  96. * not a reply event.
  97. ***/
  98. public int getReplyCode()
  99. {
  100. return __replyCode;
  101. }
  102. /***
  103. * Returns true if the ProtocolCommandEvent was generated as a result
  104. * of sending a command.
  105. * <p>
  106. * @return true If the ProtocolCommandEvent was generated as a result
  107. * of sending a command. False otherwise.
  108. ***/
  109. public boolean isCommand()
  110. {
  111. return __isCommand;
  112. }
  113. /***
  114. * Returns true if the ProtocolCommandEvent was generated as a result
  115. * of receiving a reply.
  116. * <p>
  117. * @return true If the ProtocolCommandEvent was generated as a result
  118. * of receiving a reply. False otherwise.
  119. ***/
  120. public boolean isReply()
  121. {
  122. return !isCommand();
  123. }
  124. /***
  125. * Returns the entire message sent to or received from the server.
  126. * <p>
  127. * @return The entire message sent to or received from the server.
  128. ***/
  129. public String getMessage()
  130. {
  131. return __message;
  132. }
  133. }