001    /*
002     * Copyright 2003-2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.net.telnet;
017    
018    /***
019     * Implements the telnet terminal type option RFC 1091.
020     * <p>
021     * @author Bruno D'Avanzo
022     ***/
023    public class TerminalTypeOptionHandler extends TelnetOptionHandler
024    {
025        /***
026         * Terminal type
027         ***/
028        private String termType = null;
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         * behaviour 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         * behaviour 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, whithout IAC SB & IAC SE
084         * @param suboptionLength - the length of data in suboption_data
085         * <p>
086         * @return terminal type information
087         ***/
088        public int[] answerSubnegotiation(int suboptionData[], int suboptionLength)
089        {
090            if ((suboptionData != null) && (suboptionLength > 1)
091                && (termType != null))
092            {
093                if ((suboptionData[0] == TERMINAL_TYPE)
094                    && (suboptionData[1] == TERMINAL_TYPE_SEND))
095                {
096                    int response[] = new int[termType.length() + 2];
097    
098                    response[0] = TERMINAL_TYPE;
099                    response[1] = TERMINAL_TYPE_IS;
100    
101                    for (int ii = 0; ii < termType.length(); ii++)
102                    {
103                        response[ii + 2] = (int) termType.charAt(ii);
104                    }
105    
106                    return response;
107                }
108            }
109            return null;
110        }
111    
112        /***
113         * Implements the abstract method of TelnetOptionHandler.
114         * <p>
115         * @return always null (no response to subnegotiation)
116         ***/
117        public int[] startSubnegotiationLocal()
118        {
119            return null;
120        }
121    
122        /***
123         * Implements the abstract method of TelnetOptionHandler.
124         * <p>
125         * @return always null (no response to subnegotiation)
126         ***/
127        public int[] startSubnegotiationRemote()
128        {
129            return null;
130        }
131    }