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