View Javadoc
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  
18  package org.apache.commons.net.telnet;
19  
20  /***
21   * The TelnetOptionHandler class is the base class to be used
22   * for implementing handlers for telnet options.
23   * <p>
24   * TelnetOptionHandler implements basic option handling
25   * functionality and defines abstract methods that must be
26   * implemented to define subnegotiation behaviour.
27   ***/
28  public abstract class TelnetOptionHandler
29  {
30      /***
31       * Option code
32       ***/
33      private int optionCode = -1;
34  
35      /***
36       * true if the option should be activated on the local side
37       ***/
38      private boolean initialLocal = false;
39  
40      /***
41       * true if the option should be activated on the remote side
42       ***/
43      private boolean initialRemote = false;
44  
45      /***
46       * true if the option should be accepted on the local side
47       ***/
48      private boolean acceptLocal = false;
49  
50      /***
51       * true if the option should be accepted on the remote side
52       ***/
53      private boolean acceptRemote = false;
54  
55      /***
56       * true if the option is active on the local side
57       ***/
58      private boolean doFlag = false;
59  
60      /***
61       * true if the option is active on the remote side
62       ***/
63      private boolean willFlag = false;
64  
65      /***
66       * Constructor for the TelnetOptionHandler. Allows defining desired
67       * initial setting for local/remote activation of this option and
68       * behaviour in case a local/remote activation request for this
69       * option is received.
70       * <p>
71       * @param optcode - Option code.
72       * @param initlocal - if set to true, a WILL is sent upon connection.
73       * @param initremote - if set to true, a DO is sent upon connection.
74       * @param acceptlocal - if set to true, any DO request is accepted.
75       * @param acceptremote - if set to true, any WILL request is accepted.
76       ***/
77      public TelnetOptionHandler(int optcode,
78                                  boolean initlocal,
79                                  boolean initremote,
80                                  boolean acceptlocal,
81                                  boolean acceptremote)
82      {
83          optionCode = optcode;
84          initialLocal = initlocal;
85          initialRemote = initremote;
86          acceptLocal = acceptlocal;
87          acceptRemote = acceptremote;
88      }
89  
90  
91      /***
92       * Returns the option code for this option.
93       * <p>
94       * @return Option code.
95       ***/
96      public int getOptionCode()
97      {
98          return (optionCode);
99      }
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 behaviour 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(boolean accept)
130     {
131         acceptLocal = accept;
132     }
133 
134     /***
135      * Set behaviour 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(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(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(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 &amp; 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 &amp; IAC SE. null means no response
202      ***/
203     public int[] answerSubnegotiation(int suboptionData[], 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 &amp; 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 &amp; 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(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(boolean state)
280     {
281         doFlag = state;
282     }
283 }