001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.net.telnet; 019 020/** 021 * Implements the telnet terminal type option RFC 1091. 022 */ 023public class TerminalTypeOptionHandler extends TelnetOptionHandler { 024 /** 025 * Terminal type option 026 */ 027 protected static final int TERMINAL_TYPE = 24; 028 029 /** 030 * Send (for subnegotiation) 031 */ 032 protected static final int TERMINAL_TYPE_SEND = 1; 033 034 /** 035 * Is (for subnegotiation) 036 */ 037 protected static final int TERMINAL_TYPE_IS = 0; 038 039 /** 040 * Terminal type 041 */ 042 private final String termType; 043 044 /** 045 * Constructor for the TerminalTypeOptionHandler. Initial and accept behavior flags are set to false 046 * 047 * @param termtype - terminal type that will be negotiated. 048 */ 049 public TerminalTypeOptionHandler(final String termtype) { 050 super(TelnetOption.TERMINAL_TYPE, false, false, false, false); 051 termType = termtype; 052 } 053 054 /** 055 * Constructor for the TerminalTypeOptionHandler. Allows defining desired initial setting for local/remote activation of this option and behavior in case a 056 * local/remote activation request for this option is received. 057 * 058 * @param termtype - terminal type that will be negotiated. 059 * @param initlocal - if set to true, a {@code WILL} is sent upon connection. 060 * @param initremote - if set to true, a {@code DO} is sent upon connection. 061 * @param acceptlocal - if set to true, any {@code DO} request is accepted. 062 * @param acceptremote - if set to true, any {@code WILL} request is accepted. 063 */ 064 public TerminalTypeOptionHandler(final String termtype, final boolean initlocal, final boolean initremote, final boolean acceptlocal, 065 final boolean acceptremote) { 066 super(TelnetOption.TERMINAL_TYPE, initlocal, initremote, acceptlocal, acceptremote); 067 termType = termtype; 068 } 069 070 /** 071 * Implements the abstract method of TelnetOptionHandler. 072 * 073 * @param suboptionData - the sequence received, without IAC SB & IAC SE 074 * @param suboptionLength - the length of data in suboption_data 075 * @return terminal type information 076 */ 077 @Override 078 public int[] answerSubnegotiation(final int suboptionData[], final int suboptionLength) { 079 if (suboptionData != null && suboptionLength > 1 && termType != null) { 080 if (suboptionData[0] == TERMINAL_TYPE && suboptionData[1] == TERMINAL_TYPE_SEND) { 081 final int[] response = new int[termType.length() + 2]; 082 083 response[0] = TERMINAL_TYPE; 084 response[1] = TERMINAL_TYPE_IS; 085 086 for (int ii = 0; ii < termType.length(); ii++) { 087 response[ii + 2] = termType.charAt(ii); 088 } 089 090 return response; 091 } 092 } 093 return null; 094 } 095}