TelnetOptionHandler.java

  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. package org.apache.commons.net.telnet;

  18. /**
  19.  * The TelnetOptionHandler class is the base class to be used for implementing handlers for telnet options.
  20.  * <p>
  21.  * TelnetOptionHandler implements basic option handling functionality and defines abstract methods that must be implemented to define subnegotiation behavior.
  22.  * </p>
  23.  */
  24. public abstract class TelnetOptionHandler {
  25.     /**
  26.      * Option code
  27.      */
  28.     private int optionCode = -1;

  29.     /**
  30.      * true if the option should be activated on the local side
  31.      */
  32.     private boolean initialLocal;

  33.     /**
  34.      * true if the option should be activated on the remote side
  35.      */
  36.     private boolean initialRemote;

  37.     /**
  38.      * true if the option should be accepted on the local side
  39.      */
  40.     private boolean acceptLocal;

  41.     /**
  42.      * true if the option should be accepted on the remote side
  43.      */
  44.     private boolean acceptRemote;

  45.     /**
  46.      * true if the option is active on the local side
  47.      */
  48.     private boolean doFlag;

  49.     /**
  50.      * true if the option is active on the remote side
  51.      */
  52.     private boolean willFlag;

  53.     /**
  54.      * Constructor for the TelnetOptionHandler. Allows defining desired initial setting for local/remote activation of this option and behavior in case a
  55.      * local/remote activation request for this option is received.
  56.      *
  57.      * @param optcode      - Option code.
  58.      * @param initlocal    - if set to true, a {@code WILL} is sent upon connection.
  59.      * @param initremote   - if set to true, a {@code DO} is sent upon connection.
  60.      * @param acceptlocal  - if set to true, any {@code DO} request is accepted.
  61.      * @param acceptremote - if set to true, any {@code WILL} request is accepted.
  62.      */
  63.     public TelnetOptionHandler(final int optcode, final boolean initlocal, final boolean initremote, final boolean acceptlocal, final boolean acceptremote) {
  64.         optionCode = optcode;
  65.         initialLocal = initlocal;
  66.         initialRemote = initremote;
  67.         acceptLocal = acceptlocal;
  68.         acceptRemote = acceptremote;
  69.     }

  70.     /**
  71.      * Method called upon reception of a subnegotiation for this option coming from the other end.
  72.      * <p>
  73.      * This implementation returns null, and must be overridden by the actual TelnetOptionHandler to specify which response must be sent for the subnegotiation
  74.      * request.
  75.      * </p>
  76.      *
  77.      * @param suboptionData   - the sequence received, without IAC SB &amp; IAC SE
  78.      * @param suboptionLength - the length of data in suboption_data
  79.      * @return response to be sent to the subnegotiation sequence. TelnetClient will add IAC SB &amp; IAC SE. null means no response
  80.      */
  81.     public int[] answerSubnegotiation(final int suboptionData[], final int suboptionLength) {
  82.         return null;
  83.     }

  84.     /**
  85.      * Gets a boolean indicating whether to accept a DO request coming from the other end.
  86.      *
  87.      * @return true if a {@code DO} request shall be accepted.
  88.      */
  89.     public boolean getAcceptLocal() {
  90.         return acceptLocal;
  91.     }

  92.     /**
  93.      * Gets a boolean indicating whether to accept a WILL request coming from the other end.
  94.      *
  95.      * @return true if a {@code WILL} request shall be accepted.
  96.      */
  97.     public boolean getAcceptRemote() {
  98.         return acceptRemote;
  99.     }

  100.     /**
  101.      * Gets a boolean indicating whether a {@code DO} request sent to the other side has been acknowledged.
  102.      *
  103.      * @return true if a {@code DO} sent to the other side has been acknowledged.
  104.      */
  105.     boolean getDo() {
  106.         return doFlag;
  107.     }

  108.     /**
  109.      * Gets a boolean indicating whether to send a WILL request to the other end upon connection.
  110.      *
  111.      * @return true if a {@code WILL} request shall be sent upon connection.
  112.      */
  113.     public boolean getInitLocal() {
  114.         return initialLocal;
  115.     }

  116.     /**
  117.      * Gets a boolean indicating whether to send a DO request to the other end upon connection.
  118.      *
  119.      * @return true if a {@code DO} request shall be sent upon connection.
  120.      */
  121.     public boolean getInitRemote() {
  122.         return initialRemote;
  123.     }

  124.     /**
  125.      * Gets the option code for this option.
  126.      *
  127.      * @return Option code.
  128.      */
  129.     public int getOptionCode() {
  130.         return optionCode;
  131.     }

  132.     /**
  133.      * Gets a boolean indicating whether a {@code WILL} request sent to the other side has been acknowledged.
  134.      *
  135.      * @return true if a {@code WILL} sent to the other side has been acknowledged.
  136.      */
  137.     boolean getWill() {
  138.         return willFlag;
  139.     }

  140.     /**
  141.      * Sets behavior of the option for DO requests coming from the other end.
  142.      *
  143.      * @param accept - if true, subsequent DO requests will be accepted.
  144.      */
  145.     public void setAcceptLocal(final boolean accept) {
  146.         acceptLocal = accept;
  147.     }

  148.     /**
  149.      * Sets behavior of the option for {@code WILL} requests coming from the other end.
  150.      *
  151.      * @param accept - if true, subsequent {@code WILL} requests will be accepted.
  152.      */
  153.     public void setAcceptRemote(final boolean accept) {
  154.         acceptRemote = accept;
  155.     }

  156.     /**
  157.      * Sets this option whether a {@code DO} request sent to the other side has been acknowledged (invoked by TelnetClient).
  158.      *
  159.      * @param state - if true, a {@code DO} request has been acknowledged.
  160.      */
  161.     void setDo(final boolean state) {
  162.         doFlag = state;
  163.     }

  164.     /**
  165.      * Sets this option whether to send a {@code WILL} request upon connection.
  166.      *
  167.      * @param init - if true, a {@code WILL} request will be sent upon subsequent connections.
  168.      */
  169.     public void setInitLocal(final boolean init) {
  170.         initialLocal = init;
  171.     }

  172.     /**
  173.      * Sets this option whether to send a {@code DO} request upon connection.
  174.      *
  175.      * @param init - if true, a {@code DO} request will be sent upon subsequent connections.
  176.      */
  177.     public void setInitRemote(final boolean init) {
  178.         initialRemote = init;
  179.     }

  180.     /**
  181.      * Sets this option whether a {@code WILL} request sent to the other side has been acknowledged (invoked by TelnetClient).
  182.      *
  183.      * @param state - if true, a {@code WILL} request has been acknowledged.
  184.      */
  185.     void setWill(final boolean state) {
  186.         willFlag = state;
  187.     }

  188.     /**
  189.      * 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
  190.      * to specify a subnegotiation sequence that will be sent by TelnetClient when the option is activated.
  191.      * <p>
  192.      * This implementation returns null, and must be overriden by the actual TelnetOptionHandler to specify which response must be sent for the subnegotiation
  193.      * request.
  194.      * </p>
  195.      *
  196.      * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient will add IAC SB &amp; IAC SE. null means no subnegotiation.
  197.      */
  198.     public int[] startSubnegotiationLocal() {
  199.         return null;
  200.     }

  201.     /**
  202.      * 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
  203.      * used to specify a subnegotiation sequence that will be sent by TelnetClient when the option is activated.
  204.      * <p>
  205.      * This implementation returns null, and must be overridden by the actual TelnetOptionHandler to specify which response must be sent for the subnegotiation
  206.      * request.
  207.      * </p>
  208.      *
  209.      * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient will add IAC SB &amp; IAC SE. null means no subnegotiation.
  210.      */
  211.     public int[] startSubnegotiationRemote() {
  212.         return null;
  213.     }
  214. }