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. * Implements the telnet terminal type option RFC 1091.
  19. * <p>
  20. * @author Bruno D'Avanzo
  21. ***/
  22. public class TerminalTypeOptionHandler extends TelnetOptionHandler
  23. {
  24. /***
  25. * Terminal type
  26. ***/
  27. private String termType = null;
  28. /***
  29. * Terminal type option
  30. ***/
  31. protected static final int TERMINAL_TYPE = 24;
  32. /***
  33. * Send (for subnegotiation)
  34. ***/
  35. protected static final int TERMINAL_TYPE_SEND = 1;
  36. /***
  37. * Is (for subnegotiation)
  38. ***/
  39. protected static final int TERMINAL_TYPE_IS = 0;
  40. /***
  41. * Constructor for the TerminalTypeOptionHandler. Allows defining desired
  42. * initial setting for local/remote activation of this option and
  43. * behaviour in case a local/remote activation request for this
  44. * option is received.
  45. * <p>
  46. * @param termtype - terminal type that will be negotiated.
  47. * @param initlocal - if set to true, a WILL is sent upon connection.
  48. * @param initremote - if set to true, a DO is sent upon connection.
  49. * @param acceptlocal - if set to true, any DO request is accepted.
  50. * @param acceptremote - if set to true, any WILL request is accepted.
  51. ***/
  52. public TerminalTypeOptionHandler(String termtype,
  53. boolean initlocal,
  54. boolean initremote,
  55. boolean acceptlocal,
  56. boolean acceptremote)
  57. {
  58. super(TelnetOption.TERMINAL_TYPE, initlocal, initremote,
  59. acceptlocal, acceptremote);
  60. termType = termtype;
  61. }
  62. /***
  63. * Constructor for the TerminalTypeOptionHandler. Initial and accept
  64. * behaviour flags are set to false
  65. * <p>
  66. * @param termtype - terminal type that will be negotiated.
  67. ***/
  68. public TerminalTypeOptionHandler(String termtype)
  69. {
  70. super(TelnetOption.TERMINAL_TYPE, false, false, false, false);
  71. termType = termtype;
  72. }
  73. /***
  74. * Implements the abstract method of TelnetOptionHandler.
  75. * <p>
  76. * @param suboptionData - the sequence received, whithout IAC SB & IAC SE
  77. * @param suboptionLength - the length of data in suboption_data
  78. * <p>
  79. * @return terminal type information
  80. ***/
  81. public int[] answerSubnegotiation(int suboptionData[], int suboptionLength)
  82. {
  83. if ((suboptionData != null) && (suboptionLength > 1)
  84. && (termType != null))
  85. {
  86. if ((suboptionData[0] == TERMINAL_TYPE)
  87. && (suboptionData[1] == TERMINAL_TYPE_SEND))
  88. {
  89. int response[] = new int[termType.length() + 2];
  90. response[0] = TERMINAL_TYPE;
  91. response[1] = TERMINAL_TYPE_IS;
  92. for (int ii = 0; ii < termType.length(); ii++)
  93. {
  94. response[ii + 2] = (int) termType.charAt(ii);
  95. }
  96. return response;
  97. }
  98. }
  99. return null;
  100. }
  101. /***
  102. * Implements the abstract method of TelnetOptionHandler.
  103. * <p>
  104. * @return always null (no response to subnegotiation)
  105. ***/
  106. public int[] startSubnegotiationLocal()
  107. {
  108. return null;
  109. }
  110. /***
  111. * Implements the abstract method of TelnetOptionHandler.
  112. * <p>
  113. * @return always null (no response to subnegotiation)
  114. ***/
  115. public int[] startSubnegotiationRemote()
  116. {
  117. return null;
  118. }
  119. }