KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > auth > XmlRpcAuthenticator


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package freecs.auth;
19
20 import java.net.MalformedURLException JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.xmlrpc.XmlRpcClient;
30 import org.apache.xmlrpc.XmlRpcException;
31
32 import freecs.Server;
33 import freecs.core.User;
34 import freecs.interfaces.IRequest;
35
36 /**
37  * @author Stefan Pollach
38  */

39 public class XmlRpcAuthenticator extends AbstractAuthenticator {
40
41     XmlRpcClient xmlrpc = null;
42     
43     public XmlRpcAuthenticator() {
44     }
45
46     
47     public void init(Properties JavaDoc allProps, String JavaDoc additionalPrefix) {
48         super.init(allProps, additionalPrefix);
49         Server.log (this, "parsing config", Server.MSG_STATE, Server.LVL_MINOR);
50         synchronized (this) {
51             try {
52                 xmlrpc = new XmlRpcClient(props.getProperty("server"));
53             } catch (MalformedURLException JavaDoc wrongurl) {
54                 Server.log(this, "can't construct xmlrpc-client because of wrong url: " + wrongurl.toString(), Server.MSG_CONFIG, Server.LVL_MAJOR);
55             }
56         }
57     }
58
59     /*
60      * @see freecs.auth.IAuthenticator#shutdown()
61      */

62     public void shutdown() throws Exception JavaDoc {
63         xmlrpc = null;
64     }
65
66     /*
67      * @see freecs.auth.IAuthenticator#loginUser(java.lang.String,
68      * java.lang.String, java.lang.String)
69      */

70     public User loginUser(String JavaDoc usr, String JavaDoc pwd, String JavaDoc userCookie, IRequest request)
71             throws Exception JavaDoc {
72
73         // change cookie value to check if configured
74
String JavaDoc cookie = props.getProperty("cookie");
75         String JavaDoc checkCookie = (cookie != null && !cookie.trim().equals("")) ? request.getProperty("c_" + cookie) : userCookie;
76
77         // xmlrpc doesn't know null values, so make sure
78
// transmitted values are empty strings
79
pwd = (pwd == null) ? "" : pwd;
80         checkCookie = (checkCookie == null) ? "" : checkCookie;
81
82         Vector JavaDoc params = new Vector JavaDoc();
83         if ("session".equalsIgnoreCase(props.getProperty("check"))) {
84             params.add(checkCookie);
85         } else {
86             params.add(usr);
87             params.add(pwd);
88         }
89         Object JavaDoc result = null;
90         try {
91             result = xmlrpc.execute(props.getProperty("loginMethod"), params);
92         } catch (XmlRpcException xmlrpcEx) {
93             // errors thrown at the remote server (handler or function not available etc)
94
Server.debug (this, props.getProperty("server") + "/" + props.getProperty("loginMethod") + " reports an xmlrpc-error: ", xmlrpcEx, Server.MSG_AUTH, Server.LVL_MAJOR);
95             return null;
96         } catch (Exception JavaDoc ex) {
97             // lower level errors, network etc
98
Server.debug(this, "xmlrpc-request to " + props.getProperty("server") + "/" + props.getProperty("loginMethod") + " failed: ", ex, Server.MSG_AUTH, Server.LVL_MAJOR);
99             return null;
100         }
101
102         if (result == null || result == Boolean.FALSE) {
103             // invalid return data
104
return null;
105         }
106
107         if (!(result instanceof Hashtable JavaDoc)) {
108             if (!"1".equals(result)
109                     && !"true".equals(result)
110                     && !Boolean.TRUE.equals(result))
111                 return null;
112             return new User (usr, userCookie);
113         }
114         Hashtable JavaDoc userdata = (Hashtable JavaDoc) result;
115         
116         if (userdata.containsKey("errorcode")) {
117             // check error code returned by remote function
118
int errorCode = ((Integer JavaDoc)userdata.get("errorcode")).intValue();
119             if (errorCode>0) {
120                 Server.log(this, "login failed as " + props.getProperty("server") + " returned code " + errorCode, Server.MSG_AUTH, Server.LVL_VERBOSE);
121                 return null;
122             }
123         }
124         String JavaDoc remoteUsername = (String JavaDoc) userdata.get("username");
125         if (!usr.equalsIgnoreCase(remoteUsername)) {
126             // username given as URL-Parameter doesn't match the username retrieved via XML-RPC
127
return null;
128         }
129         
130         User u = new User (usr, userCookie);
131         u.isUnregistered = false;
132
133         if (userdata.containsKey("color")) {
134             u.setColCode((String JavaDoc)userdata.get("color"));
135         }
136         if (userdata.containsKey("chattime")) {
137             u.setProperty("chattime", Long.valueOf((String JavaDoc)
138             userdata.get("chattime")));
139         }
140         if (userdata.containsKey("id")) {
141             Object JavaDoc obj = userdata.get("id");
142             if (obj instanceof Number JavaDoc) {
143                 u.setID(((Number JavaDoc)obj).toString());
144             } else if (obj instanceof String JavaDoc) {
145                 u.setID((String JavaDoc)obj);
146             }
147         }
148         if (userdata.containsKey("lastlogin")) {
149             Object JavaDoc obj = userdata.get("lastlogin");
150             if (obj instanceof Number JavaDoc) {
151                 u.setProperty("lastlogin", new Timestamp JavaDoc(((Number JavaDoc)obj).longValue()));
152             } else if (obj instanceof Date JavaDoc) {
153                 u.setProperty("lastlogin", new Timestamp JavaDoc(((Date JavaDoc)obj).getTime()));
154             }
155         }
156         if (userdata.containsKey("friendslist")) {
157             List JavaDoc users = parseUserList((String JavaDoc)userdata.get("friendslist"));
158             for (Iterator JavaDoc i = users.iterator(); i.hasNext(); ) {
159                 u.addFriend((String JavaDoc) i.next());
160             }
161         }
162         if (parseBoolean(userdata.get("blocked"))==true) {
163             u.blocked = true;
164         }
165         if (parseBoolean(userdata.get("activated"))==true){
166             u.activated = true;
167         }
168         return (u);
169     
170     }
171
172     public User loginUser(User u, String JavaDoc username, String JavaDoc password, IRequest request) throws Exception JavaDoc {
173         return u;
174     }
175
176     
177     /*
178      * @see freecs.auth.IAuthenticator#logoutUser(freecs.core.User)
179      */

180     public void logoutUser(User u) throws Exception JavaDoc {
181         String JavaDoc logoutMethod = props.getProperty("logoutMethod");
182         if (logoutMethod == null
183                 || logoutMethod.length() <1)
184             return;
185         Hashtable JavaDoc userdata = new Hashtable JavaDoc();
186         userdata.put("username", u.getName());
187         userdata.put("chattime", new Integer JavaDoc((new Long JavaDoc(u.getChattime()).intValue())) );
188         userdata.put("color", u.getColCode());
189         userdata.put("cookie", u.getCookie());
190         if (props.containsKey("cookie")) {
191             String JavaDoc cookieName = props.getProperty("cookie");
192             userdata.put("c_" + cookieName, u.getProperty("c_" + cookieName));
193         }
194
195         Vector JavaDoc params = new Vector JavaDoc(1);
196         params.add(userdata);
197         try {
198             Object JavaDoc result = xmlrpc.execute(logoutMethod, params);
199         } catch (XmlRpcException xmlrpcEx) {
200             // errors thrown at the remote server (handler or function not available etc)
201
Server.debug(this, props.getProperty("server") + "/" + props.getProperty("logoutMethod") + " reports an xmlrpc-error: ", xmlrpcEx, Server.MSG_AUTH, Server.LVL_MAJOR);
202         } catch (Exception JavaDoc ex) {
203             // lower level errors, network etc
204
Server.debug(this, "xmlrpc-request to " + props.getProperty("server") + "/" + props.getProperty("logoutMethod") + " failed: ", ex, Server.MSG_AUTH, Server.LVL_MAJOR);
205         }
206     }
207
208
209 }
Popular Tags