KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > server > QuickAuthenticator


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.net.server;
16
17 import java.io.*;
18 import java.net.SocketTimeoutException JavaDoc;
19 import org.quickserver.net.AppException;
20 import org.quickserver.net.ConnectionLostException;
21 import org.quickserver.util.io.*;
22
23 /**
24  * This class is used to authenticate a client when
25  * it connects to QuickServer. Only single instance of this class
26  * will be used per QuickServer to handle all authentication.
27  * Should have a default constructor.
28  * <p>
29  * Ex:
30  * <code><BLOCKQUOTE><pre>
31  package echoserver;
32
33 import org.quickserver.net.server.*;
34 import java.io.*;
35
36 public class EchoServerQuickAuthenticator extends QuickAuthenticator {
37
38     public boolean askAuthorisation(ClientHandler clientHandler)
39             throws IOException {
40         String username = askStringInput(clientHandler, "User Name :");
41         String password = askStringInput(clientHandler, "Password :");
42
43         if(username==null || password ==null)
44             return false;
45         
46         if(username.equals(password)) {
47             sendString(clientHandler, "Auth OK");
48             return true;
49         } else {
50             sendString(clientHandler, "Auth Failed");
51             return false;
52         }
53     }
54 }
55  </pre></BLOCKQUOTE></code></p>
56  * @author Akshathkumar Shetty
57  * @since 1.3
58  */

59 public abstract class QuickAuthenticator
60         implements Authenticator {
61
62     public abstract boolean askAuthorisation(ClientHandler clientHandler)
63         throws IOException, AppException;
64
65
66     /**
67      * Prints the given message to the client.
68      * @param msg Message to send.
69      * If <code>null</code> is passed it will not send any thing.
70      */

71     public void sendString(ClientHandler clientHandler, String JavaDoc msg)
72             throws IOException {
73         if(msg!=null) {
74             if(clientHandler.getDataMode(DataType.OUT)!=DataMode.STRING)
75                 clientHandler.setDataMode(DataMode.STRING, DataType.OUT);
76             clientHandler.sendClientMsg(msg);
77         }
78     }
79
80     /**
81      * Prints the given message to the client and reads a line of input.
82      * @return the line of input read from the client.
83      * @param msg Message to send before reading input. If received String is
84      * <code>null</code> it will throw {@link ConnectionLostException}.
85      * If <code>null</code> is passed it will not send any thing.
86      * @exception IOException if an I/O error occurs
87      */

88     public String JavaDoc askStringInput(ClientHandler clientHandler, String JavaDoc msg)
89             throws IOException {
90         if(msg!=null) {
91             if(clientHandler.getDataMode(DataType.OUT)!=DataMode.STRING)
92                 clientHandler.setDataMode(DataMode.STRING, DataType.OUT);
93             clientHandler.sendClientMsg(msg);
94         }
95         if(clientHandler.getDataMode(DataType.IN)!=DataMode.STRING)
96             clientHandler.setDataMode(DataMode.STRING, DataType.IN);
97
98         String JavaDoc data = null;
99         if(clientHandler.hasEvent(ClientEvent.RUN_BLOCKING)) {
100             data = clientHandler.getBufferedReader().readLine();
101         } else {
102             ByteBufferInputStream bbin = (ByteBufferInputStream)
103                 clientHandler.getInputStream();
104             data = bbin.readLine();
105         }
106
107         if(data!=null)
108             return data;
109         else
110             throw new ConnectionLostException();
111     }
112
113     /**
114      * Sends the given object to the client.
115      * @param msg Message to send.
116      * If <code>null</code> is passed it will not send any thing.
117      */

118     public void sendObject(ClientHandler clientHandler, Object JavaDoc msg)
119             throws IOException {
120         if(msg!=null) {
121             if(clientHandler.getDataMode(DataType.OUT)!=DataMode.OBJECT)
122                 clientHandler.setDataMode(DataMode.OBJECT, DataType.OUT);
123             clientHandler.sendClientObject(msg);
124         }
125     }
126
127     /**
128      * Prints the given message to the client and reads a Object from input.
129      * @return the Object from input read from the client. If received Object is
130      * <code>null</code> it will throw {@link ConnectionLostException}.
131      * @param msg Message to send before reading input.
132      * If <code>null</code> is passed it will not send any thing.
133      * @exception IOException if an I/O error occurs
134      */

135     public Object JavaDoc askObjectInput(ClientHandler clientHandler, Object JavaDoc msg)
136             throws IOException, ClassNotFoundException JavaDoc {
137         if(msg!=null) {
138             if(clientHandler.getDataMode(DataType.OUT)!=DataMode.OBJECT)
139                 clientHandler.setDataMode(DataMode.OBJECT, DataType.OUT);
140             clientHandler.sendClientObject(msg);
141         }
142         if(clientHandler.getDataMode(DataType.IN)!=DataMode.OBJECT)
143             clientHandler.setDataMode(DataMode.OBJECT, DataType.IN);
144         Object JavaDoc data = clientHandler.getObjectInputStream().readObject();
145         if(data!=null)
146             return data;
147         else
148             throw new ConnectionLostException();
149     }
150
151     /**
152      * Prints the given message to the client.
153      * @param msg Message to send.
154      * If <code>null</code> is passed it will not send any thing.
155      * @since 1.3.2
156      */

157     public void sendByte(ClientHandler clientHandler, String JavaDoc msg)
158             throws IOException {
159         if(msg!=null) {
160             if(clientHandler.getDataMode(DataType.OUT)!=DataMode.BYTE)
161                 clientHandler.setDataMode(DataMode.BYTE, DataType.OUT);
162             clientHandler.sendClientBytes(msg);
163         }
164     }
165
166     /**
167      * Prints the given message to the client and reads a line of input.
168      * @return the line of input read from the client. If received byte is
169      * <code>null</code> it will throw {@link ConnectionLostException}.
170      * @param msg Message to send before reading input.
171      * If <code>null</code> is passed it will not send any thing.
172      * @exception IOException if an I/O error occurs
173      * @since 1.3.2
174      */

175     public String JavaDoc askByteInput(ClientHandler clientHandler, String JavaDoc msg)
176             throws IOException {
177         if(msg!=null) {
178             if(clientHandler.getDataMode(DataType.OUT)!=DataMode.BYTE)
179                 clientHandler.setDataMode(DataMode.BYTE, DataType.OUT);
180             clientHandler.sendClientBytes(msg);
181         }
182         if(clientHandler.getDataMode(DataType.IN)!=DataMode.BYTE)
183             clientHandler.setDataMode(DataMode.BYTE, DataType.IN);
184         String JavaDoc data = clientHandler.readBytes();
185         if(data!=null)
186             return data;
187         else
188             throw new ConnectionLostException();
189     }
190
191     /**
192      * Sends the given binary data to the client.
193      * @param msg binary data to send.
194      * If <code>null</code> is passed it will not send any thing.
195      * @since 1.4
196      */

197     public void sendBinary(ClientHandler clientHandler, byte msg[])
198             throws IOException {
199         if(msg!=null) {
200             if(clientHandler.getDataMode(DataType.OUT)!=DataMode.BINARY)
201                 clientHandler.setDataMode(DataMode.BINARY, DataType.OUT);
202             clientHandler.sendClientBinary(msg);
203         }
204     }
205
206     /**
207      * Sends the given binary data to the client and reads binary data input.
208      * @return the binary data input read from the client. If received byte is
209      * <code>null</code> it will throw {@link ConnectionLostException}.
210      * @param msg binary data to send before reading input.
211      * If <code>null</code> is passed it will not send any thing.
212      * @exception IOException if an I/O error occurs
213      * @since 1.4
214      */

215     public byte[] askBinaryInput(ClientHandler clientHandler, byte msg[])
216             throws IOException {
217         if(msg!=null) {
218             if(clientHandler.getDataMode(DataType.OUT)!=DataMode.BINARY)
219                 clientHandler.setDataMode(DataMode.BINARY, DataType.OUT);
220             clientHandler.sendClientBinary(msg);
221         }
222         if(clientHandler.getDataMode(DataType.IN)!=DataMode.BINARY)
223             clientHandler.setDataMode(DataMode.BINARY, DataType.IN);
224         byte[] data = clientHandler.readBinary();
225         if(data!=null)
226             return data;
227         else
228             throw new ConnectionLostException();
229     }
230 }
231
Popular Tags