1. /*
  2. * Copyright 2003-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.telnet;
  17. /***
  18. * The TelnetOptionHandler class is the base class to be used
  19. * for implementing handlers for telnet options.
  20. * <p>
  21. * TelnetOptionHandler implements basic option handling
  22. * functionality and defines abstract methods that must be
  23. * implemented to define subnegotiation behaviour.
  24. * <p>
  25. * @author Bruno D'Avanzo
  26. ***/
  27. public abstract class TelnetOptionHandler
  28. {
  29. /***
  30. * Option code
  31. ***/
  32. private int optionCode = -1;
  33. /***
  34. * true if the option should be activated on the local side
  35. ***/
  36. private boolean initialLocal = false;
  37. /***
  38. * true if the option should be activated on the remote side
  39. ***/
  40. private boolean initialRemote = false;
  41. /***
  42. * true if the option should be accepted on the local side
  43. ***/
  44. private boolean acceptLocal = false;
  45. /***
  46. * true if the option should be accepted on the remote side
  47. ***/
  48. private boolean acceptRemote = false;
  49. /***
  50. * true if the option is active on the local side
  51. ***/
  52. private boolean doFlag = false;
  53. /***
  54. * true if the option is active on the remote side
  55. ***/
  56. private boolean willFlag = false;
  57. /***
  58. * Constructor for the TelnetOptionHandler. Allows defining desired
  59. * initial setting for local/remote activation of this option and
  60. * behaviour in case a local/remote activation request for this
  61. * option is received.
  62. * <p>
  63. * @param optcode - Option code.
  64. * @param initlocal - if set to true, a WILL is sent upon connection.
  65. * @param initremote - if set to true, a DO is sent upon connection.
  66. * @param acceptlocal - if set to true, any DO request is accepted.
  67. * @param acceptremote - if set to true, any WILL request is accepted.
  68. ***/
  69. public TelnetOptionHandler(int optcode,
  70. boolean initlocal,
  71. boolean initremote,
  72. boolean acceptlocal,
  73. boolean acceptremote)
  74. {
  75. optionCode = optcode;
  76. initialLocal = initlocal;
  77. initialRemote = initremote;
  78. acceptLocal = acceptlocal;
  79. acceptRemote = acceptremote;
  80. }
  81. /***
  82. * Returns the option code for this option.
  83. * <p>
  84. * @return Option code.
  85. ***/
  86. public int getOptionCode()
  87. {
  88. return (optionCode);
  89. }
  90. /***
  91. * Returns a boolean indicating whether to accept a DO
  92. * request coming from the other end.
  93. * <p>
  94. * @return true if a DO request shall be accepted.
  95. ***/
  96. public boolean getAcceptLocal()
  97. {
  98. return (acceptLocal);
  99. }
  100. /***
  101. * Returns a boolean indicating whether to accept a WILL
  102. * request coming from the other end.
  103. * <p>
  104. * @return true if a WILL request shall be accepted.
  105. ***/
  106. public boolean getAcceptRemote()
  107. {
  108. return (acceptRemote);
  109. }
  110. /***
  111. * Set behaviour of the option for DO requests coming from
  112. * the other end.
  113. * <p>
  114. * @param accept - if true, subsequent DO requests will be accepted.
  115. ***/
  116. public void setAcceptLocal(boolean accept)
  117. {
  118. acceptLocal = accept;
  119. }
  120. /***
  121. * Set behaviour of the option for WILL requests coming from
  122. * the other end.
  123. * <p>
  124. * @param accept - if true, subsequent WILL requests will be accepted.
  125. ***/
  126. public void setAcceptRemote(boolean accept)
  127. {
  128. acceptRemote = accept;
  129. }
  130. /***
  131. * Returns a boolean indicating whether to send a WILL request
  132. * to the other end upon connection.
  133. * <p>
  134. * @return true if a WILL request shall be sent upon connection.
  135. ***/
  136. public boolean getInitLocal()
  137. {
  138. return (initialLocal);
  139. }
  140. /***
  141. * Returns a boolean indicating whether to send a DO request
  142. * to the other end upon connection.
  143. * <p>
  144. * @return true if a DO request shall be sent upon connection.
  145. ***/
  146. public boolean getInitRemote()
  147. {
  148. return (initialRemote);
  149. }
  150. /***
  151. * Tells this option whether to send a WILL request upon connection.
  152. * <p>
  153. * @param init - if true, a WILL request will be sent upon subsequent
  154. * connections.
  155. ***/
  156. public void setInitLocal(boolean init)
  157. {
  158. initialLocal = init;
  159. }
  160. /***
  161. * Tells this option whether to send a DO request upon connection.
  162. * <p>
  163. * @param init - if true, a DO request will be sent upon subsequent
  164. * connections.
  165. ***/
  166. public void setInitRemote(boolean init)
  167. {
  168. initialRemote = init;
  169. }
  170. /***
  171. * Method called upon reception of a subnegotiation for this option
  172. * coming from the other end.
  173. * Must be implemented by the actual TelnetOptionHandler to specify
  174. * which response must be sent for the subnegotiation request.
  175. * <p>
  176. * @param suboptionData - the sequence received, whithout IAC SB & IAC SE
  177. * @param suboptionLength - the length of data in suboption_data
  178. * <p>
  179. * @return response to be sent to the subnegotiation sequence. TelnetClient
  180. * will add IAC SB & IAC SE. null means no response
  181. ***/
  182. public abstract int[] answerSubnegotiation(int suboptionData[],
  183. int suboptionLength);
  184. /***
  185. * This method is invoked whenever this option is acknowledged active on
  186. * the local end (TelnetClient sent a WILL, remote side sent a DO).
  187. * The method is used to specify a subnegotiation sequence that will be
  188. * sent by TelnetClient when the option is activated.
  189. * <p>
  190. * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient
  191. * will add IAC SB & IAC SE. null means no subnegotiation.
  192. ***/
  193. public abstract int[] startSubnegotiationLocal();
  194. /***
  195. * This method is invoked whenever this option is acknowledged active on
  196. * the remote end (TelnetClient sent a DO, remote side sent a WILL).
  197. * The method is used to specify a subnegotiation sequence that will be
  198. * sent by TelnetClient when the option is activated.
  199. * <p>
  200. * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient
  201. * will add IAC SB & IAC SE. null means no subnegotiation.
  202. ***/
  203. public abstract int[] startSubnegotiationRemote();
  204. /***
  205. * Returns a boolean indicating whether a WILL request sent to the other
  206. * side has been acknowledged.
  207. * <p>
  208. * @return true if a WILL sent to the other side has been acknowledged.
  209. ***/
  210. boolean getWill()
  211. {
  212. return willFlag;
  213. }
  214. /***
  215. * Tells this option whether a WILL request sent to the other
  216. * side has been acknowledged (invoked by TelnetClient).
  217. * <p>
  218. * @param state - if true, a WILL request has been acknowledged.
  219. ***/
  220. void setWill(boolean state)
  221. {
  222. willFlag = state;
  223. }
  224. /***
  225. * Returns a boolean indicating whether a DO request sent to the other
  226. * side has been acknowledged.
  227. * <p>
  228. * @return true if a DO sent to the other side has been acknowledged.
  229. ***/
  230. boolean getDo()
  231. {
  232. return doFlag;
  233. }
  234. /***
  235. * Tells this option whether a DO request sent to the other
  236. * side has been acknowledged (invoked by TelnetClient).
  237. * <p>
  238. * @param state - if true, a DO request has been acknowledged.
  239. ***/
  240. void setDo(boolean state)
  241. {
  242. doFlag = state;
  243. }
  244. }