TerminalTypeOptionHandler.java

  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. package org.apache.commons.net.telnet;

  18. /**
  19.  * Implements the telnet terminal type option RFC 1091.
  20.  */
  21. public class TerminalTypeOptionHandler extends TelnetOptionHandler {
  22.     /**
  23.      * Terminal type option
  24.      */
  25.     protected static final int TERMINAL_TYPE = 24;

  26.     /**
  27.      * Send (for subnegotiation)
  28.      */
  29.     protected static final int TERMINAL_TYPE_SEND = 1;

  30.     /**
  31.      * Is (for subnegotiation)
  32.      */
  33.     protected static final int TERMINAL_TYPE_IS = 0;

  34.     /**
  35.      * Terminal type
  36.      */
  37.     private final String termType;

  38.     /**
  39.      * Constructor for the TerminalTypeOptionHandler. Initial and accept behavior flags are set to false
  40.      *
  41.      * @param termtype - terminal type that will be negotiated.
  42.      */
  43.     public TerminalTypeOptionHandler(final String termtype) {
  44.         super(TelnetOption.TERMINAL_TYPE, false, false, false, false);
  45.         termType = termtype;
  46.     }

  47.     /**
  48.      * Constructor for the TerminalTypeOptionHandler. Allows defining desired initial setting for local/remote activation of this option and behavior in case a
  49.      * local/remote activation request for this option is received.
  50.      *
  51.      * @param termtype     - terminal type that will be negotiated.
  52.      * @param initlocal    - if set to true, a {@code WILL} is sent upon connection.
  53.      * @param initremote   - if set to true, a {@code DO} is sent upon connection.
  54.      * @param acceptlocal  - if set to true, any {@code DO} request is accepted.
  55.      * @param acceptremote - if set to true, any {@code WILL} request is accepted.
  56.      */
  57.     public TerminalTypeOptionHandler(final String termtype, final boolean initlocal, final boolean initremote, final boolean acceptlocal,
  58.             final boolean acceptremote) {
  59.         super(TelnetOption.TERMINAL_TYPE, initlocal, initremote, acceptlocal, acceptremote);
  60.         termType = termtype;
  61.     }

  62.     /**
  63.      * Implements the abstract method of TelnetOptionHandler.
  64.      *
  65.      * @param suboptionData   - the sequence received, without IAC SB & IAC SE
  66.      * @param suboptionLength - the length of data in suboption_data
  67.      * @return terminal type information
  68.      */
  69.     @Override
  70.     public int[] answerSubnegotiation(final int suboptionData[], final int suboptionLength) {
  71.         if (suboptionData != null && suboptionLength > 1 && termType != null) {
  72.             if (suboptionData[0] == TERMINAL_TYPE && suboptionData[1] == TERMINAL_TYPE_SEND) {
  73.                 final int[] response = new int[termType.length() + 2];

  74.                 response[0] = TERMINAL_TYPE;
  75.                 response[1] = TERMINAL_TYPE_IS;

  76.                 for (int ii = 0; ii < termType.length(); ii++) {
  77.                     response[ii + 2] = termType.charAt(ii);
  78.                 }

  79.                 return response;
  80.             }
  81.         }
  82.         return null;
  83.     }
  84. }