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
18 package org.apache.commons.net.telnet;
19
20 /***
21 * Implements the telnet terminal type option RFC 1091.
22 * <p>
23 * @author Bruno D'Avanzo
24 ***/
25 public class TerminalTypeOptionHandler extends TelnetOptionHandler
26 {
27 /***
28 * Terminal type
29 ***/
30 private String termType = null;
31
32 /***
33 * Terminal type option
34 ***/
35 protected static final int TERMINAL_TYPE = 24;
36
37 /***
38 * Send (for subnegotiation)
39 ***/
40 protected static final int TERMINAL_TYPE_SEND = 1;
41
42 /***
43 * Is (for subnegotiation)
44 ***/
45 protected static final int TERMINAL_TYPE_IS = 0;
46
47 /***
48 * Constructor for the TerminalTypeOptionHandler. Allows defining desired
49 * initial setting for local/remote activation of this option and
50 * behaviour in case a local/remote activation request for this
51 * option is received.
52 * <p>
53 * @param termtype - terminal type that will be negotiated.
54 * @param initlocal - if set to true, a WILL is sent upon connection.
55 * @param initremote - if set to true, a DO is sent upon connection.
56 * @param acceptlocal - if set to true, any DO request is accepted.
57 * @param acceptremote - if set to true, any WILL request is accepted.
58 ***/
59 public TerminalTypeOptionHandler(String termtype,
60 boolean initlocal,
61 boolean initremote,
62 boolean acceptlocal,
63 boolean acceptremote)
64 {
65 super(TelnetOption.TERMINAL_TYPE, initlocal, initremote,
66 acceptlocal, acceptremote);
67 termType = termtype;
68 }
69
70 /***
71 * Constructor for the TerminalTypeOptionHandler. Initial and accept
72 * behaviour flags are set to false
73 * <p>
74 * @param termtype - terminal type that will be negotiated.
75 ***/
76 public TerminalTypeOptionHandler(String termtype)
77 {
78 super(TelnetOption.TERMINAL_TYPE, false, false, false, false);
79 termType = termtype;
80 }
81
82 /***
83 * Implements the abstract method of TelnetOptionHandler.
84 * <p>
85 * @param suboptionData - the sequence received, whithout IAC SB & IAC SE
86 * @param suboptionLength - the length of data in suboption_data
87 * <p>
88 * @return terminal type information
89 ***/
90 @Override
91 public int[] answerSubnegotiation(int suboptionData[], int suboptionLength)
92 {
93 if ((suboptionData != null) && (suboptionLength > 1)
94 && (termType != null))
95 {
96 if ((suboptionData[0] == TERMINAL_TYPE)
97 && (suboptionData[1] == TERMINAL_TYPE_SEND))
98 {
99 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 }