    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 behaviour.
    027 * <p>
    028 * @author Bruno D'Avanzo
    029 ***/
    030public abstract class TelnetOptionHandler
    031{
    032    /***
    033     * Option code
    034     ***/
    035    private int optionCode = -1;
    036
    037    /***
    038     * true if the option should be activated on the local side
    039     ***/
    040    private boolean initialLocal = false;
    041
    042    /***
    043     * true if the option should be activated on the remote side
    044     ***/
    045    private boolean initialRemote = false;
    046
    047    /***
    048     * true if the option should be accepted on the local side
    049     ***/
    050    private boolean acceptLocal = false;
    051
    052    /***
    053     * true if the option should be accepted on the remote side
    054     ***/
    055    private boolean acceptRemote = false;
    056
    057    /***
    058     * true if the option is active on the local side
    059     ***/
    060    private boolean doFlag = false;
    061
    062    /***
    063     * true if the option is active on the remote side
    064     ***/
    065    private boolean willFlag = false;
    066
    067    /***
    068     * Constructor for the TelnetOptionHandler. Allows defining desired
    069     * initial setting for local/remote activation of this option and
    070     * behaviour in case a local/remote activation request for this
    071     * option is received.
    072     * <p>
    073     * @param optcode - Option code.
    074     * @param initlocal - if set to true, a WILL is sent upon connection.
    075     * @param initremote - if set to true, a DO is sent upon connection.
    076     * @param acceptlocal - if set to true, any DO request is accepted.
    077     * @param acceptremote - if set to true, any WILL request is accepted.
    078     ***/
    079    public TelnetOptionHandler(int optcode,
    080                                boolean initlocal,
    081                                boolean initremote,
    082                                boolean acceptlocal,
    083                                boolean acceptremote)
    084    {
    085        optionCode = optcode;
    086        initialLocal = initlocal;
    087        initialRemote = initremote;
    088        acceptLocal = acceptlocal;
    089        acceptRemote = acceptremote;
    090    }
    091
    092
    093    /***
    094     * Returns the option code for this option.
    095     * <p>
    096     * @return Option code.
    097     ***/
    098    public int getOptionCode()
    099    {
    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}