    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 * Implements the telnet terminal type option RFC 1091.
    022 ***/
    023public class TerminalTypeOptionHandler extends TelnetOptionHandler
    024{
    025    /***
    026     * Terminal type
    027     ***/
    028    private final String termType;
    029
    030    /***
    031     * Terminal type option
    032     ***/
    033    protected static final int TERMINAL_TYPE = 24;
    034
    035    /***
    036     * Send (for subnegotiation)
    037     ***/
    038    protected static final int TERMINAL_TYPE_SEND =  1;
    039
    040    /***
    041     * Is (for subnegotiation)
    042     ***/
    043    protected static final int TERMINAL_TYPE_IS =  0;
    044
    045    /***
    046     * Constructor for the TerminalTypeOptionHandler. Allows defining desired
    047     * initial setting for local/remote activation of this option and
    048     * behavior in case a local/remote activation request for this
    049     * option is received.
    050     * <p>
    051     * @param termtype - terminal type that will be negotiated.
    052     * @param initlocal - if set to true, a WILL is sent upon connection.
    053     * @param initremote - if set to true, a DO is sent upon connection.
    054     * @param acceptlocal - if set to true, any DO request is accepted.
    055     * @param acceptremote - if set to true, any WILL request is accepted.
    056     ***/
    057    public TerminalTypeOptionHandler(String termtype,
    058                                boolean initlocal,
    059                                boolean initremote,
    060                                boolean acceptlocal,
    061                                boolean acceptremote)
    062    {
    063        super(TelnetOption.TERMINAL_TYPE, initlocal, initremote,
    064                                      acceptlocal, acceptremote);
    065        termType = termtype;
    066    }
    067
    068    /***
    069     * Constructor for the TerminalTypeOptionHandler. Initial and accept
    070     * behavior flags are set to false
    071     * <p>
    072     * @param termtype - terminal type that will be negotiated.
    073     ***/
    074    public TerminalTypeOptionHandler(String termtype)
    075    {
    076        super(TelnetOption.TERMINAL_TYPE, false, false, false, false);
    077        termType = termtype;
    078    }
    079
    080    /***
    081     * Implements the abstract method of TelnetOptionHandler.
    082     * <p>
    083     * @param suboptionData - the sequence received, without IAC SB &amp; IAC SE
    084     * @param suboptionLength - the length of data in suboption_data
    085     * <p>
    086     * @return terminal type information
    087     ***/
    088    @Override
    089    public int[] answerSubnegotiation(int suboptionData[], int suboptionLength)
    090    {
    091        if ((suboptionData != null) && (suboptionLength > 1)
    092            && (termType != null))
    093        {
    094            if ((suboptionData[0] == TERMINAL_TYPE)
    095                && (suboptionData[1] == TERMINAL_TYPE_SEND))
    096            {
    097                int response[] = new int[termType.length() + 2];
    098
    099                response[0] = TERMINAL_TYPE;
    100                response[1] = TERMINAL_TYPE_IS;
    101
    102                for (int ii = 0; ii < termType.length(); ii++)
    103                {
    104                    response[ii + 2] = termType.charAt(ii);
    105                }
    106
    107                return response;
    108            }
    109        }
    110        return null;
    111    }
    112}