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