001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.net.server;
016:
017: import java.io.*;
018: import java.net.SocketTimeoutException;
019: import org.quickserver.net.AppException;
020: import org.quickserver.net.ConnectionLostException;
021: import org.quickserver.util.io.*;
022:
023: /**
024: * This class is used to authenticate a client when
025: * it connects to QuickServer. Only single instance of this class
026: * will be used per QuickServer to handle all authentication.
027: * Should have a default constructor.
028: * <p>
029: * Ex:
030: * <code><BLOCKQUOTE><pre>
031: package echoserver;
032:
033: import org.quickserver.net.server.*;
034: import java.io.*;
035:
036: public class EchoServerQuickAuthenticator extends QuickAuthenticator {
037:
038: public boolean askAuthorisation(ClientHandler clientHandler)
039: throws IOException {
040: String username = askStringInput(clientHandler, "User Name :");
041: String password = askStringInput(clientHandler, "Password :");
042:
043: if(username==null || password ==null)
044: return false;
045:
046: if(username.equals(password)) {
047: sendString(clientHandler, "Auth OK");
048: return true;
049: } else {
050: sendString(clientHandler, "Auth Failed");
051: return false;
052: }
053: }
054: }
055: </pre></BLOCKQUOTE></code></p>
056: * @author Akshathkumar Shetty
057: * @since 1.3
058: */
059: public abstract class QuickAuthenticator implements Authenticator {
060:
061: public abstract boolean askAuthorisation(ClientHandler clientHandler)
062: throws IOException, AppException;
063:
064: /**
065: * Prints the given message to the client.
066: * @param msg Message to send.
067: * If <code>null</code> is passed it will not send any thing.
068: */
069: public void sendString(ClientHandler clientHandler, String msg)
070: throws IOException {
071: if (msg != null) {
072: if (clientHandler.getDataMode(DataType.OUT) != DataMode.STRING)
073: clientHandler
074: .setDataMode(DataMode.STRING, DataType.OUT);
075: clientHandler.sendClientMsg(msg);
076: }
077: }
078:
079: /**
080: * Prints the given message to the client and reads a line of input.
081: * @return the line of input read from the client.
082: * @param msg Message to send before reading input. If received String is
083: * <code>null</code> it will throw {@link ConnectionLostException}.
084: * If <code>null</code> is passed it will not send any thing.
085: * @exception IOException if an I/O error occurs
086: */
087: public String askStringInput(ClientHandler clientHandler, String msg)
088: throws IOException {
089: if (msg != null) {
090: if (clientHandler.getDataMode(DataType.OUT) != DataMode.STRING)
091: clientHandler
092: .setDataMode(DataMode.STRING, DataType.OUT);
093: clientHandler.sendClientMsg(msg);
094: }
095: if (clientHandler.getDataMode(DataType.IN) != DataMode.STRING)
096: clientHandler.setDataMode(DataMode.STRING, DataType.IN);
097:
098: String data = null;
099: if (clientHandler.hasEvent(ClientEvent.RUN_BLOCKING)) {
100: data = clientHandler.getBufferedReader().readLine();
101: } else {
102: ByteBufferInputStream bbin = (ByteBufferInputStream) clientHandler
103: .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 msg)
119: throws IOException {
120: if (msg != null) {
121: if (clientHandler.getDataMode(DataType.OUT) != DataMode.OBJECT)
122: clientHandler
123: .setDataMode(DataMode.OBJECT, DataType.OUT);
124: clientHandler.sendClientObject(msg);
125: }
126: }
127:
128: /**
129: * Prints the given message to the client and reads a Object from input.
130: * @return the Object from input read from the client. If received Object is
131: * <code>null</code> it will throw {@link ConnectionLostException}.
132: * @param msg Message to send before reading input.
133: * If <code>null</code> is passed it will not send any thing.
134: * @exception IOException if an I/O error occurs
135: */
136: public Object askObjectInput(ClientHandler clientHandler, Object msg)
137: throws IOException, ClassNotFoundException {
138: if (msg != null) {
139: if (clientHandler.getDataMode(DataType.OUT) != DataMode.OBJECT)
140: clientHandler
141: .setDataMode(DataMode.OBJECT, DataType.OUT);
142: clientHandler.sendClientObject(msg);
143: }
144: if (clientHandler.getDataMode(DataType.IN) != DataMode.OBJECT)
145: clientHandler.setDataMode(DataMode.OBJECT, DataType.IN);
146: Object data = clientHandler.getObjectInputStream().readObject();
147: if (data != null)
148: return data;
149: else
150: throw new ConnectionLostException();
151: }
152:
153: /**
154: * Prints the given message to the client.
155: * @param msg Message to send.
156: * If <code>null</code> is passed it will not send any thing.
157: * @since 1.3.2
158: */
159: public void sendByte(ClientHandler clientHandler, String msg)
160: throws IOException {
161: if (msg != null) {
162: if (clientHandler.getDataMode(DataType.OUT) != DataMode.BYTE)
163: clientHandler.setDataMode(DataMode.BYTE, DataType.OUT);
164: clientHandler.sendClientBytes(msg);
165: }
166: }
167:
168: /**
169: * Prints the given message to the client and reads a line of input.
170: * @return the line of input read from the client. If received byte is
171: * <code>null</code> it will throw {@link ConnectionLostException}.
172: * @param msg Message to send before reading input.
173: * If <code>null</code> is passed it will not send any thing.
174: * @exception IOException if an I/O error occurs
175: * @since 1.3.2
176: */
177: public String askByteInput(ClientHandler clientHandler, String msg)
178: throws IOException {
179: if (msg != null) {
180: if (clientHandler.getDataMode(DataType.OUT) != DataMode.BYTE)
181: clientHandler.setDataMode(DataMode.BYTE, DataType.OUT);
182: clientHandler.sendClientBytes(msg);
183: }
184: if (clientHandler.getDataMode(DataType.IN) != DataMode.BYTE)
185: clientHandler.setDataMode(DataMode.BYTE, DataType.IN);
186: String data = clientHandler.readBytes();
187: if (data != null)
188: return data;
189: else
190: throw new ConnectionLostException();
191: }
192:
193: /**
194: * Sends the given binary data to the client.
195: * @param msg binary data to send.
196: * If <code>null</code> is passed it will not send any thing.
197: * @since 1.4
198: */
199: public void sendBinary(ClientHandler clientHandler, byte msg[])
200: throws IOException {
201: if (msg != null) {
202: if (clientHandler.getDataMode(DataType.OUT) != DataMode.BINARY)
203: clientHandler
204: .setDataMode(DataMode.BINARY, DataType.OUT);
205: clientHandler.sendClientBinary(msg);
206: }
207: }
208:
209: /**
210: * Sends the given binary data to the client and reads binary data input.
211: * @return the binary data input read from the client. If received byte is
212: * <code>null</code> it will throw {@link ConnectionLostException}.
213: * @param msg binary data to send before reading input.
214: * If <code>null</code> is passed it will not send any thing.
215: * @exception IOException if an I/O error occurs
216: * @since 1.4
217: */
218: public byte[] askBinaryInput(ClientHandler clientHandler,
219: byte msg[]) throws IOException {
220: if (msg != null) {
221: if (clientHandler.getDataMode(DataType.OUT) != DataMode.BINARY)
222: clientHandler
223: .setDataMode(DataMode.BINARY, DataType.OUT);
224: clientHandler.sendClientBinary(msg);
225: }
226: if (clientHandler.getDataMode(DataType.IN) != DataMode.BINARY)
227: clientHandler.setDataMode(DataMode.BINARY, DataType.IN);
228: byte[] data = clientHandler.readBinary();
229: if (data != null)
230: return data;
231: else
232: throw new ConnectionLostException();
233: }
234: }
|