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