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