KickJava   Java API By Example, From Geeks To Geeks.

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


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;
19 import org.quickserver.net.AppException;
20
21 /**
22  * This class is used to authenticate a client when
23  * it connects to QuickServer.
24  * <p>
25  * Every client connected will have separate instance of this class which
26  * will be used to handle the authentication. If single instance is needed
27  * use {@link QuickAuthenticator}. You should only use this class if your
28  * authentication need you to have dedicate an object.
29  * <br>
30  * Recommendations to be followed when extending ServerAuthenticator.
31  * <ul>
32  * <li>The inheriting class must be public and in its own file.
33  * <li>It should have a default constructor.
34  * </ul>
35  * </p>
36  * <p>
37  * Ex:
38  * <code><BLOCKQUOTE><pre>
39 package echoserver;
40
41 import org.quickserver.net.server.*;
42 import java.io.*;
43
44 public class MyServerAuthenticator extends ServerAuthenticator {
45
46     public boolean askAuthorisation() throws IOException {
47         pout.println("User Name :");
48         username=in.readLine();
49         pout.println("Password :");
50         password=in.readLine();
51
52         if(username==null || password ==null)
53             return false;
54         if(username.equals(password))
55             return true;
56         else {
57             pout.println("Auth Failed");
58             return false;
59         }
60     }
61 }
62  </pre></BLOCKQUOTE></code></p>
63  * @author Akshathkumar Shetty
64  */

65 public class ServerAuthenticator implements Authenticator {
66
67     protected String error;
68     /** Client input BufferedReader */
69     protected BufferedReader in;
70     /** Client output OutputStream */
71     protected OutputStream out;
72     /** Client output PrintWriter */
73     protected PrintWriter pout;
74     protected ClientData data;
75     protected ClientHandler handler;
76
77     /** Member to store username */
78     protected String username;
79     /** Member to store password */
80     protected String password;
81
82     //v1.3
83
private boolean freeOnUse = true;
84
85     /**
86      * Sets input and output streams associated with the client socket.
87      * Used by {@link ClientHandler} class.
88      */

89     private final void setInputOutput(BufferedReader in,
90             OutputStream out) {
91         this.in = in;
92         this.out = out;
93         pout = new PrintWriter(out,true);
94     }
95
96     /**
97      * This method is called by {@link QuickServer}
98      * if ServerAuthenticator was set, to authenticate any client
99      * connection.
100      * @return result of authentication.
101      * @exception org.quickserver.net.AppException
102      * if ServerAuthenticator wants QuickServer to close the
103      * client connection. <br>
104      * Can be used for exiting on Timeouts<br>
105      * Can be used when Quit commands is received when
106      * Authenticating.
107      * @exception java.io.IOException if there is socket error
108      */

109     public boolean askAuthorisation()
110         throws IOException, AppException {
111         return true;
112     }
113
114     public boolean askAuthorisation(ClientHandler clientHandler)
115         throws IOException, AppException {
116         if(getClientHandler()==null)
117             setClientHandler(clientHandler);
118         return askAuthorisation();
119     }
120
121     /**
122      * Gives description for last Authorisation failure if any.
123      *
124      * Ones getError() is called the error gets reset to null
125      * @return error description for last Authorisation failure.
126      */

127     public String getError() {
128         String temp = error;
129         error = null;
130         return temp;
131     }
132     /**
133      * Sets description for last Authorisation failure if any.
134      * @since 1.3
135      */

136     public void setError(String error) {
137         this.error = error;
138     }
139
140     /**
141      * Called by {@link ClientHandler} ones.
142      * Used to close any resources that was opened by the
143      * inheriting class. Do call the base class version
144      * eg: <code>super.cleanup()</code> if you override.
145      * This will set all io/output streams,
146      * {@link #data}, {@link #handler} to <code>null</code>.
147      */

148     public void cleanup() {
149         pout = null;
150         in = null;
151         out = null;
152         data = null;
153         handler = null;
154     }
155
156     /**
157      * Sets ClientData object associated with Client being
158      * authenticated.
159      * Need not be called externally it is set by
160      * {@link #askAuthorisation(ClientHandler)}
161      */

162     private final void setClientData(ClientData data) {
163         this.data = data;
164     }
165
166     /**
167      * Return ClientData object associated with Client being
168      * authenticated or null if no ClientData was set in
169      * {@link QuickServer} object.
170      */

171     public ClientData getClientData() {
172         return getClientHandler().getClientData();
173     }
174
175     /**
176      * Sets ClientHandler object associated with Client being
177      * authenticated.
178      * @see #getClientHandler
179      */

180     public final void setClientHandler(ClientHandler handler) {
181         this.handler = handler;
182         setClientData(handler.getClientData());
183         setInputOutput(handler.getBufferedReader(),
184             handler.getOutputStream());
185     }
186     
187     /**
188      * Returns ClientHandler object associated with Client being
189      * authenticated.
190      * Can be used to find out more information about the client, like
191      * ip eg:<br>
192      * getClientHandler().getSocket().getInetAddress()
193      */

194     public ClientHandler getClientHandler() {
195         return handler;
196     }
197
198     /**
199      * Sets if ServerAuthenticator should be freed after
200      * client is authenticated. If set <code>true</code>
201      * QuickServer will set ServerAuthenticator to null ones
202      * client is authenticated. Default is <code>true</code>
203      * @see #getFreeOnUse
204      * @since 1.3
205      */

206     public void setFreeOnUse(boolean flag) {
207         this.freeOnUse = flag;
208     }
209     /**
210      * Returns if ServerAuthenticator should be freed after
211      * client is authenticated. Default is <code>true</code>
212      * @see #setFreeOnUse
213      * @since 1.3
214      */

215     public boolean getFreeOnUse() {
216         return freeOnUse;
217     }
218 }
219
Popular Tags