Source code: org/media/datastore/xmlrpcserver/handlers/UrlHandler.java
1 /*
2 * $COPYRIGHT$
3 * $Id: UrlHandler.java,v 1.6 2002/07/24 15:45:16 crow Exp $
4 *
5 * Date Author Changes
6 * May 28 2001 Antal Attila Created
7 */
8
9 package org.media.datastore.xmlrpcserver.handlers;
10
11 import org.media.datastore.sepengine.driver.*;
12 import org.media.datastore.sepengine.SepInterpreter;
13 import java.io.BufferedReader;
14 import java.io.InputStreamReader;
15 import java.io.InputStream;
16 import java.util.Vector;
17 import org.apache.xmlrpc.AuthenticatedXmlRpcHandler;
18
19 /**
20 * @author <a href="mailto:atech@nolimits.ro">Antal Attila</a>
21 * @version $Revision: 1.6 $ $Date: 2001/08/21 15:00:00
22 * @see java.lang.String
23 */
24
25 public class UrlHandler implements AuthenticatedXmlRpcHandler {
26
27 private SepStoreConnection con = null;
28
29
30 public Object execute(String methodName, Vector parameters,
31 String username, String password) throws Exception {
32 if (checkPassword(username, password)) {
33 return execute(methodName, parameters);
34 } else {
35 throw new Exception( "Unauthorized user for: " + methodName );
36 }
37 }
38
39
40 protected Object execute(String methodName, Vector params)
41 throws Exception {
42 if ( "executeSep".equals(methodName) ) {
43 String sep = (String) params.elementAt(0);
44 String db = (String) params.elementAt(1);
45 String user = (String) params.elementAt(2);
46 String hash = (String) params.elementAt(3);
47 return executeSep( sep, db, user, hash );
48 }
49 else if ( "echo".equals(methodName) ) return echo( (String) params.elementAt(0) );
50 else throw new Exception("No such method: " + methodName );
51 }
52
53
54 private String echo( String param ) {
55 return param;
56 }
57
58
59 private String executeSep( String sep, String db, String user, String hash ) {
60 String ret = "";
61 try {
62 hash = new SepInterpreter().getPasswdHash( new SepInterpreter().preDigest( hash ) );
63 } catch ( Exception e ){}
64 openConnection( db, user, hash);
65 try {
66 InputStream is = con.executeSEP( sep );
67 BufferedReader reader = new BufferedReader( new InputStreamReader ( is ) );
68 String line = null;
69 while ( ( line = reader.readLine() ) != null ) {
70 ret += line + "\n";
71 }
72 }
73 catch (Exception e){
74 System.out.println("Error occured while returning data:" + e.getMessage());
75 }
76 closeConnection();
77 return ret;
78 }
79
80
81 private void openConnection( String dbName, String user, String hash ) {
82 try {
83 System.setProperty("sep.drivers",
84 "org.media.datastore.sepengine.driver.localDriver.LocalDriver");
85 String url = "sep:local:" + dbName;
86 String userFile = dbName + "_" + user;
87 con = SepStoreDriverManager.getConnection(url, userFile, hash);
88 }
89 catch ( Exception e ) {
90 e.printStackTrace();
91 }
92 }
93
94
95 private void closeConnection() {
96 try {
97 con.close();
98 }
99 catch ( Exception e ) {
100 e.printStackTrace();
101 }
102 }
103
104
105 /*
106 * Of course this method is only an example for testing.
107 * You need to use a more sophisticated password checking
108 * method.
109 *
110 */
111 private boolean checkPassword( String user, String pwd ) {
112 if ( user != null && pwd != null ) {
113 if ( user.equals(pwd) ) return true;
114 return false;
115 }
116 if ( user == null && pwd == null ) return true;
117 return false;
118 }
119 }
120