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