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