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 * The TelnetOptionHandler class is the base class to be used 022 * for implementing handlers for telnet options. 023 * <p> 024 * TelnetOptionHandler implements basic option handling 025 * functionality and defines abstract methods that must be 026 * implemented to define subnegotiation behavior. 027 */ 028public abstract class TelnetOptionHandler 029{ 030 /** 031 * Option code 032 */ 033 private int optionCode = -1; 034 035 /** 036 * true if the option should be activated on the local side 037 */ 038 private boolean initialLocal; 039 040 /** 041 * true if the option should be activated on the remote side 042 */ 043 private boolean initialRemote; 044 045 /** 046 * true if the option should be accepted on the local side 047 */ 048 private boolean acceptLocal; 049 050 /** 051 * true if the option should be accepted on the remote side 052 */ 053 private boolean acceptRemote; 054 055 /** 056 * true if the option is active on the local side 057 */ 058 private boolean doFlag; 059 060 /** 061 * true if the option is active on the remote side 062 */ 063 private boolean willFlag; 064 065 /** 066 * Constructor for the TelnetOptionHandler. Allows defining desired 067 * initial setting for local/remote activation of this option and 068 * behavior in case a local/remote activation request for this 069 * option is received. 070 * <p> 071 * @param optcode - Option code. 072 * @param initlocal - if set to true, a WILL is sent upon connection. 073 * @param initremote - if set to true, a DO is sent upon connection. 074 * @param acceptlocal - if set to true, any DO request is accepted. 075 * @param acceptremote - if set to true, any WILL request is accepted. 076 */ 077 public TelnetOptionHandler(final int optcode, 078 final boolean initlocal, 079 final boolean initremote, 080 final boolean acceptlocal, 081 final boolean acceptremote) 082 { 083 optionCode = optcode; 084 initialLocal = initlocal; 085 initialRemote = initremote; 086 acceptLocal = acceptlocal; 087 acceptRemote = acceptremote; 088 } 089 090 091 /** 092 * Returns the option code for this option. 093 * <p> 094 * @return Option code. 095 */ 096 public int getOptionCode() 097 { 098 return optionCode; 099 } 100 101 /** 102 * Returns a boolean indicating whether to accept a DO 103 * request coming from the other end. 104 * <p> 105 * @return true if a DO request shall be accepted. 106 */ 107 public boolean getAcceptLocal() 108 { 109 return acceptLocal; 110 } 111 112 /** 113 * Returns a boolean indicating whether to accept a WILL 114 * request coming from the other end. 115 * <p> 116 * @return true if a WILL request shall be accepted. 117 */ 118 public boolean getAcceptRemote() 119 { 120 return acceptRemote; 121 } 122 123 /** 124 * Set behavior of the option for DO requests coming from 125 * the other end. 126 * <p> 127 * @param accept - if true, subsequent DO requests will be accepted. 128 */ 129 public void setAcceptLocal(final boolean accept) 130 { 131 acceptLocal = accept; 132 } 133 134 /** 135 * Set behavior of the option for WILL requests coming from 136 * the other end. 137 * <p> 138 * @param accept - if true, subsequent WILL requests will be accepted. 139 */ 140 public void setAcceptRemote(final boolean accept) 141 { 142 acceptRemote = accept; 143 } 144 145 /** 146 * Returns a boolean indicating whether to send a WILL request 147 * to the other end upon connection. 148 * <p> 149 * @return true if a WILL request shall be sent upon connection. 150 */ 151 public boolean getInitLocal() 152 { 153 return initialLocal; 154 } 155 156 /** 157 * Returns a boolean indicating whether to send a DO request 158 * to the other end upon connection. 159 * <p> 160 * @return true if a DO request shall be sent upon connection. 161 */ 162 public boolean getInitRemote() 163 { 164 return initialRemote; 165 } 166 167 /** 168 * Tells this option whether to send a WILL request upon connection. 169 * <p> 170 * @param init - if true, a WILL request will be sent upon subsequent 171 * connections. 172 */ 173 public void setInitLocal(final boolean init) 174 { 175 initialLocal = init; 176 } 177 178 /** 179 * Tells this option whether to send a DO request upon connection. 180 * <p> 181 * @param init - if true, a DO request will be sent upon subsequent 182 * connections. 183 */ 184 public void setInitRemote(final boolean init) 185 { 186 initialRemote = init; 187 } 188 189 /** 190 * Method called upon reception of a subnegotiation for this option 191 * coming from the other end. 192 * <p> 193 * This implementation returns null, and 194 * must be overridden by the actual TelnetOptionHandler to specify 195 * which response must be sent for the subnegotiation request. 196 * <p> 197 * @param suboptionData - the sequence received, without IAC SB & IAC SE 198 * @param suboptionLength - the length of data in suboption_data 199 * <p> 200 * @return response to be sent to the subnegotiation sequence. TelnetClient 201 * will add IAC SB & IAC SE. null means no response 202 */ 203 public int[] answerSubnegotiation(final int suboptionData[], final int suboptionLength) { 204 return null; 205 } 206 207 /** 208 * This method is invoked whenever this option is acknowledged active on 209 * the local end (TelnetClient sent a WILL, remote side sent a DO). 210 * The method is used to specify a subnegotiation sequence that will be 211 * sent by TelnetClient when the option is activated. 212 * <p> 213 * This implementation returns null, and must be overriden by 214 * the actual TelnetOptionHandler to specify 215 * which response must be sent for the subnegotiation request. 216 * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient 217 * will add IAC SB & IAC SE. null means no subnegotiation. 218 */ 219 public int[] startSubnegotiationLocal() { 220 return null; 221 } 222 223 /** 224 * This method is invoked whenever this option is acknowledged active on 225 * the remote end (TelnetClient sent a DO, remote side sent a WILL). 226 * The method is used to specify a subnegotiation sequence that will be 227 * sent by TelnetClient when the option is activated. 228 * <p> 229 * This implementation returns null, and must be overriden by 230 * the actual TelnetOptionHandler to specify 231 * which response must be sent for the subnegotiation request. 232 * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient 233 * will add IAC SB & IAC SE. null means no subnegotiation. 234 */ 235 public int[] startSubnegotiationRemote() { 236 return null; 237 } 238 239 /** 240 * Returns a boolean indicating whether a WILL request sent to the other 241 * side has been acknowledged. 242 * <p> 243 * @return true if a WILL sent to the other side has been acknowledged. 244 */ 245 boolean getWill() 246 { 247 return willFlag; 248 } 249 250 /** 251 * Tells this option whether a WILL request sent to the other 252 * side has been acknowledged (invoked by TelnetClient). 253 * <p> 254 * @param state - if true, a WILL request has been acknowledged. 255 */ 256 void setWill(final boolean state) 257 { 258 willFlag = state; 259 } 260 261 /** 262 * Returns a boolean indicating whether a DO request sent to the other 263 * side has been acknowledged. 264 * <p> 265 * @return true if a DO sent to the other side has been acknowledged. 266 */ 267 boolean getDo() 268 { 269 return doFlag; 270 } 271 272 273 /** 274 * Tells this option whether a DO request sent to the other 275 * side has been acknowledged (invoked by TelnetClient). 276 * <p> 277 * @param state - if true, a DO request has been acknowledged. 278 */ 279 void setDo(final boolean state) 280 { 281 doFlag = state; 282 } 283}