KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > telnet > TelnetInputStream


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: TelnetInputStream.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.server.telnet;
46
47 import java.io.FilterInputStream JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.io.OutputStream JavaDoc;
51
52 /**
53  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
54  */

55 public class TelnetInputStream extends FilterInputStream JavaDoc implements TelnetCodes{
56    
57     // state table for what options have been negotiated
58
private TelnetOption[] options = new TelnetOption[256];
59     
60     private OutputStream JavaDoc out = null;
61     
62     /**
63      * We haven yet implemented any Telnet options, so we just explicitly
64      * disable some common options for safety sake.
65      *
66      * Certain Telnet clients (MS Windows Telnet) are enabling options without
67      * asking first. Shame, shame, shame.
68      *
69      * @exception IOException
70      */

71     public TelnetInputStream(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc{
72         super(in);
73         this.out = out;
74         negotiateOption(DONT, 1);
75         negotiateOption(DONT, 6);
76         negotiateOption(DONT, 24);
77         negotiateOption(DONT, 33);
78         negotiateOption(DONT, 34);
79    }
80     
81     
82     public int read() throws IOException JavaDoc {
83         int b = super.read();
84         
85         if (b == IAC) {
86             // The cosole has a reference
87
// to this input stream
88
processCommand();
89             // Call read recursively as
90
// the next character could
91
// also be a command
92
b = this.read();
93         }
94
95         //System.out.println("B="+b);
96
return b;
97     }
98     
99     /**
100      * This is only called by TelnetInputStream
101      * it is assumed that the IAC byte has already been read from the stream.
102      *
103      * @param in
104      * @exception IOException
105      */

106     private void processCommand() throws IOException JavaDoc{
107         // Debug statement
108
print("C: IAC ");
109         
110         int command = super.read();
111         
112         switch ( command ) {
113             case WILL: senderWillEnableOption(super.read()); break;
114             case DO: pleaseDoEnableOption(super.read()); break;
115             case WONT: senderWontEnableOption(super.read()); break;
116             case DONT: pleaseDontEnableOption(super.read()); break;
117             default: unimplementedCommand(command); break;
118         }
119         
120     }
121     
122     private void unimplementedCommand(int command){
123         println(command+": command not found");
124     }
125     
126     /**
127      * Client says: I will enable OptionX
128      *
129      * If the sender initiated the negotiation of the
130      * option, we must send a reply. Replies can be DO or DON'T.
131      *
132      * @param optionID
133      * @exception IOException
134      */

135     private void senderWillEnableOption(int optionID) throws IOException JavaDoc {
136         // Debug statement
137
println("WILL "+optionID);
138         TelnetOption option = getOption(optionID);
139         
140         if ( option.hasBeenNegotiated() ) return;
141         
142         if ( option.isInNegotiation() ) {
143             option.enable();
144         } else if ( !option.isInNegotiation() && option.isSupported() ) {
145             negotiateOption(DO, optionID);
146             option.enable();
147         } else if ( !option.isInNegotiation() && !option.isSupported() ) {
148             negotiateOption(DONT, optionID);
149             option.disable();
150         }
151     }
152     
153     /**
154      * Client says: Please, do enable OptionX
155      *
156      * If the sender initiated the negotiation of the
157      * option, we must send a reply.
158      *
159      * Replies can be WILL or WON'T.
160      *
161      * @param option
162      * @exception IOException
163      */

164     private void pleaseDoEnableOption(int optionID) throws IOException JavaDoc {
165         // Debug statement
166
println("DO "+optionID);
167         TelnetOption option = getOption(optionID);
168         
169         if ( option.hasBeenNegotiated() ) return;
170         
171         if ( option.isInNegotiation() ) {
172             option.enable();
173         } else if ( !option.isInNegotiation() && option.isSupported() ) {
174             negotiateOption(WILL, optionID);
175             option.enable();
176         } else if ( !option.isInNegotiation() && !option.isSupported() ) {
177             negotiateOption(WONT, optionID);
178             option.disable();
179         }
180     }
181     
182     /**
183      * Client says: I won't enable OptionX
184      *
185      *
186      * If the sender initiated the negotiation of the
187      * option, we must send a reply.
188      *
189      * Replies can only be DON'T.
190      *
191      * @param optionID
192      * @exception IOException
193      */

194     private void senderWontEnableOption(int optionID) throws IOException JavaDoc {
195         println("WONT "+optionID);
196         TelnetOption option = getOption(optionID);
197         
198         if ( option.hasBeenNegotiated() ) return;
199         
200         if ( !option.isInNegotiation() ) {
201             negotiateOption(DONT, optionID);
202         }
203         option.disable();
204     }
205     
206     /**
207      * Client says: Please, don't enable OptionX
208      *
209      * If the sender initiated the negotiation of the
210      * option, we must send a reply.
211      *
212      * Replies can only be WON'T.
213      *
214      * @param optionID
215      * @exception IOException
216      */

217     private void pleaseDontEnableOption(int optionID) throws IOException JavaDoc {
218         // Debug statement
219
println("DONT "+optionID);
220         
221         TelnetOption option = getOption(optionID);
222         
223         if ( option.hasBeenNegotiated() ) return;
224         
225         if ( !option.isInNegotiation() ) {
226             negotiateOption(WONT, optionID);
227         }
228         option.disable();
229     }
230     
231     
232     
233     // TODO:0: Replace with actual logging
234
private void println(String JavaDoc s){
235         // System.out.println(s);
236
}
237     
238     // TODO:0: Replace with actual logging
239
private void print(String JavaDoc s){
240         // System.out.print(s);
241
}
242     
243     /**
244      * Send an option negitiation command to the client
245      *
246      * @param negotiate
247      * @param optionID
248      * @exception IOException
249      */

250     private void negotiateOption(int negotiate, int optionID)
251     throws IOException JavaDoc {
252         TelnetOption option = getOption(optionID);
253         option.inNegotiation = true;
254
255         String JavaDoc n = null;
256         switch (negotiate) {
257             case WILL :
258                 n = "WILL ";
259                 break;
260             case DO :
261                 n = "DO ";
262                 break;
263             case WONT :
264                 n = "WONT ";
265                 break;
266             case DONT :
267                 n = "DONT ";
268                 break;
269         }
270
271         // Debug statement
272
println("S: IAC " + n + optionID);
273
274         synchronized (out) {
275             out.write(IAC);
276             out.write(negotiate);
277             out.write(optionID);
278         }
279     }
280
281     private TelnetOption getOption(int optionID) {
282         TelnetOption opt = options[optionID];
283         if (opt == null) {
284             opt = new TelnetOption(optionID);
285             options[optionID] = opt;
286         }
287         return opt;
288     }
289     
290     
291 }
292
Popular Tags