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 * https://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 * The TelnetOptionHandler class is the base class to be used for implementing handlers for Telnet options.
22 * <p>
23 * TelnetOptionHandler implements basic option handling functionality and defines abstract methods that must be implemented to define subnegotiation behavior.
24 * </p>
25 */
26 public abstract class TelnetOptionHandler {
27 /**
28 * Option code
29 */
30 private int optionCode = -1;
31
32 /**
33 * true if the option should be activated on the local side
34 */
35 private boolean initialLocal;
36
37 /**
38 * true if the option should be activated on the remote side
39 */
40 private boolean initialRemote;
41
42 /**
43 * true if the option should be accepted on the local side
44 */
45 private boolean acceptLocal;
46
47 /**
48 * true if the option should be accepted on the remote side
49 */
50 private boolean acceptRemote;
51
52 /**
53 * true if the option is active on the local side
54 */
55 private boolean doFlag;
56
57 /**
58 * true if the option is active on the remote side
59 */
60 private boolean willFlag;
61
62 /**
63 * Constructor for the TelnetOptionHandler. Allows defining desired initial setting for local/remote activation of this option and behavior in case a
64 * local/remote activation request for this option is received.
65 *
66 * @param optcode Option code.
67 * @param initlocal if set to true, a {@code WILL} is sent upon connection.
68 * @param initremote if set to true, a {@code DO} is sent upon connection.
69 * @param acceptlocal if set to true, any {@code DO} request is accepted.
70 * @param acceptremote if set to true, any {@code WILL} request is accepted.
71 */
72 public TelnetOptionHandler(final int optcode, final boolean initlocal, final boolean initremote, final boolean acceptlocal, final boolean acceptremote) {
73 optionCode = optcode;
74 initialLocal = initlocal;
75 initialRemote = initremote;
76 acceptLocal = acceptlocal;
77 acceptRemote = acceptremote;
78 }
79
80 /**
81 * Method called upon reception of a subnegotiation for this option coming from the other end.
82 * <p>
83 * This implementation returns null, and must be overridden by the actual TelnetOptionHandler to specify which response must be sent for the subnegotiation
84 * request.
85 * </p>
86 *
87 * @param suboptionData the sequence received, without IAC SB & IAC SE
88 * @param suboptionLength the length of data in suboption_data
89 * @return response to be sent to the subnegotiation sequence. TelnetClient will add IAC SB & IAC SE. null means no response
90 */
91 public int[] answerSubnegotiation(final int suboptionData[], final int suboptionLength) {
92 return null;
93 }
94
95 /**
96 * Gets a boolean indicating whether to accept a DO request coming from the other end.
97 *
98 * @return true if a {@code DO} request shall be accepted.
99 */
100 public boolean getAcceptLocal() {
101 return acceptLocal;
102 }
103
104 /**
105 * Gets a boolean indicating whether to accept a WILL request coming from the other end.
106 *
107 * @return true if a {@code WILL} request shall be accepted.
108 */
109 public boolean getAcceptRemote() {
110 return acceptRemote;
111 }
112
113 /**
114 * Gets a boolean indicating whether a {@code DO} request sent to the other side has been acknowledged.
115 *
116 * @return true if a {@code DO} sent to the other side has been acknowledged.
117 */
118 boolean getDo() {
119 return doFlag;
120 }
121
122 /**
123 * Gets a boolean indicating whether to send a WILL request to the other end upon connection.
124 *
125 * @return true if a {@code WILL} request shall be sent upon connection.
126 */
127 public boolean getInitLocal() {
128 return initialLocal;
129 }
130
131 /**
132 * Gets a boolean indicating whether to send a DO request to the other end upon connection.
133 *
134 * @return true if a {@code DO} request shall be sent upon connection.
135 */
136 public boolean getInitRemote() {
137 return initialRemote;
138 }
139
140 /**
141 * Gets the option code for this option.
142 *
143 * @return Option code.
144 */
145 public int getOptionCode() {
146 return optionCode;
147 }
148
149 /**
150 * Gets a boolean indicating whether a {@code WILL} request sent to the other side has been acknowledged.
151 *
152 * @return true if a {@code WILL} sent to the other side has been acknowledged.
153 */
154 boolean getWill() {
155 return willFlag;
156 }
157
158 /**
159 * Sets behavior of the option for DO requests coming from the other end.
160 *
161 * @param accept if true, subsequent DO requests will be accepted.
162 */
163 public void setAcceptLocal(final boolean accept) {
164 acceptLocal = accept;
165 }
166
167 /**
168 * Sets behavior of the option for {@code WILL} requests coming from the other end.
169 *
170 * @param accept if true, subsequent {@code WILL} requests will be accepted.
171 */
172 public void setAcceptRemote(final boolean accept) {
173 acceptRemote = accept;
174 }
175
176 /**
177 * Sets this option whether a {@code DO} request sent to the other side has been acknowledged (invoked by TelnetClient).
178 *
179 * @param state if true, a {@code DO} request has been acknowledged.
180 */
181 void setDo(final boolean state) {
182 doFlag = state;
183 }
184
185 /**
186 * Sets this option whether to send a {@code WILL} request upon connection.
187 *
188 * @param init if true, a {@code WILL} request will be sent upon subsequent connections.
189 */
190 public void setInitLocal(final boolean init) {
191 initialLocal = init;
192 }
193
194 /**
195 * Sets this option whether to send a {@code DO} request upon connection.
196 *
197 * @param init if true, a {@code DO} request will be sent upon subsequent connections.
198 */
199 public void setInitRemote(final boolean init) {
200 initialRemote = init;
201 }
202
203 /**
204 * Sets this option whether a {@code WILL} request sent to the other side has been acknowledged (invoked by TelnetClient).
205 *
206 * @param state if true, a {@code WILL} request has been acknowledged.
207 */
208 void setWill(final boolean state) {
209 willFlag = state;
210 }
211
212 /**
213 * 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
214 * to specify a subnegotiation sequence that will be sent by TelnetClient when the option is activated.
215 * <p>
216 * This implementation returns null, and must be overriden by the actual TelnetOptionHandler to specify which response must be sent for the subnegotiation
217 * request.
218 * </p>
219 *
220 * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient will add IAC SB & IAC SE. null means no subnegotiation.
221 */
222 public int[] startSubnegotiationLocal() {
223 return null;
224 }
225
226 /**
227 * 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
228 * used 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 overridden by the actual TelnetOptionHandler to specify which response must be sent for the subnegotiation
231 * request.
232 * </p>
233 *
234 * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient will add IAC SB & IAC SE. null means no subnegotiation.
235 */
236 public int[] startSubnegotiationRemote() {
237 return null;
238 }
239 }