KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > xmlrpc > AuthXmlRpcHandler


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.xmlrpc;
26
27 import org.apache.xmlrpc.AuthenticatedXmlRpcHandler;
28 import org.apache.xmlrpc.XmlRpcException;
29
30 import java.lang.reflect.Method JavaDoc;
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 /**
35  * Authenticated XML-RPC Handler that overcomes the limits of the original
36  * implementation by handling authentication and calling methods in a selected
37  * target or a sub class.
38  *
39  * @author Matthias L. Jugel
40  * @version $Id: AuthXmlRpcHandler.java 1655 2004-06-13 09:34:04Z leo $
41  */

42 public abstract class AuthXmlRpcHandler implements AuthenticatedXmlRpcHandler {
43
44   protected Object JavaDoc target = null;
45
46   public AuthXmlRpcHandler() {
47     target = this;
48   }
49
50   public AuthXmlRpcHandler(Object JavaDoc target) {
51     this.target = target;
52   }
53
54   protected abstract boolean authenticate(String JavaDoc user, String JavaDoc password);
55
56   public Object JavaDoc execute(String JavaDoc method, Vector JavaDoc vector, String JavaDoc user, String JavaDoc password) throws Exception JavaDoc {
57     //System.out.println("execute("+method+","+vector+")");
58
if (authenticate(user, password)) {
59       return execute(method, vector);
60     } else {
61       throw new XmlRpcException(0, "Username or password does not match");
62     }
63   }
64
65   public Object JavaDoc execute(String JavaDoc methodName, Vector JavaDoc args) throws Exception JavaDoc {
66     Class JavaDoc argClasses[] = null;
67     if(args.size() > 0) {
68       argClasses = new Class JavaDoc[args.size()];
69       for (int argNum = 0; argNum < args.size(); argNum++) {
70         argClasses[argNum] = args.get(argNum).getClass();
71       }
72     }
73     if(methodName.indexOf('.') != -1) {
74       methodName = methodName.substring(methodName.indexOf(".")+1);
75     }
76
77     Method JavaDoc method = target.getClass().getMethod(methodName, argClasses);
78     try {
79       return method.invoke(target, (args.size() > 0 ? args.toArray() : null));
80     } catch (InvocationTargetException JavaDoc e) {
81       throw (Exception JavaDoc)e.getTargetException();
82     }
83   }
84 }
85
Popular Tags