src/java/isac/util/InformationProcessor.java
changeset 1378 6844d06813f3
child 1404 3ad8e4df9c1a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/java/isac/util/InformationProcessor.java	Sun Feb 29 18:57:47 2004 +0100
     1.3 @@ -0,0 +1,302 @@
     1.4 +/*
     1.5 + * Created on Aug 16, 2003
     1.6 + *
     1.7 + * To change the template for this generated file go to
     1.8 + * Window>Preferences>Java>Code Generation>Code and Comments
     1.9 + */
    1.10 +package isac.util;
    1.11 +
    1.12 +import isac.browserdialog.BDialog;
    1.13 +import isac.session.OManager;
    1.14 +import isac.session.SDialog;
    1.15 +
    1.16 +import java.io.IOException;
    1.17 +import java.net.MalformedURLException;
    1.18 +import java.rmi.Naming;
    1.19 +import java.rmi.NotBoundException;
    1.20 +import java.rmi.RemoteException;
    1.21 +import java.util.Vector;
    1.22 +
    1.23 +import org.apache.xmlrpc.XmlRpcClient;
    1.24 +import org.apache.xmlrpc.XmlRpcException;
    1.25 +/**
    1.26 + * @author gismo
    1.27 + *
    1.28 + * The InformationProcessor builds the bridge between BrowserFrontend
    1.29 + * and BrowserDialog. It encapsulates all tasks regarding communication and
    1.30 + * connects to the proper dialog-modules.
    1.31 + * 
    1.32 + * Because the final communication-protocol is not fixed yet, it provides methods 
    1.33 + * to communicate via RMI as well as XML-RPC
    1.34 + */
    1.35 +public class InformationProcessor {
    1.36 +
    1.37 +/**
    1.38 + * Performes an login on the session dialog using the XML-RPC protocol. It assumes that the 
    1.39 + * SessionDialog is running its XML-RPC-handler on port 1050 of the local machine.
    1.40 + * 
    1.41 + * @param username username of the user who wants to login.
    1.42 + * @param password corresponding password for the user.
    1.43 + * @return the id of the current sesson the user logged in or null if the login did not succeed
    1.44 + * @throws NullPointerException carrying the error-message if an communication error occured.
    1.45 + */
    1.46 +    static protected String xmlLogin(String username, String password){
    1.47 +	try{
    1.48 +	    XmlRpcClient sdialog = new XmlRpcClient("http://localhost:1050");
    1.49 +	    Vector params = new Vector();
    1.50 +	    params.add(username);
    1.51 +	    params.add(password);
    1.52 +	    Vector response = (Vector) sdialog.execute("SDialog.login", params);
    1.53 +	    if(response.size()>0)
    1.54 +		return (String)response.elementAt(0);
    1.55 +	}catch(MalformedURLException e) {
    1.56 +		throw new NullPointerException(getErrorXML("Sorry, MalformedURLException while accessing the SDialog to login - is the SDialog runnig ?"+e.getMessage()));
    1.57 +	} catch (XmlRpcException e) {
    1.58 +		throw new NullPointerException(getErrorXML("Sorry, XmlRpcException while accessing the SDialog to login - is the SDialog runnig ?"+e.getMessage()));
    1.59 +	} catch (IOException e) {
    1.60 +		throw new NullPointerException(getErrorXML("Sorry, IOException while accessing the SDialog to login - is the SDialog runnig ?"+e.getMessage()));
    1.61 +	}
    1.62 +	return null;
    1.63 +    }
    1.64 +
    1.65 +/**
    1.66 + *  Performes a login on the Session dialog using RMI. It assumes the SessionDialog to be found 
    1.67 + * at //localhost/isac-ObjectManager
    1.68 + * 
    1.69 + * @param username username of the user who wants to login.
    1.70 + * @param password corresponding password for the user.
    1.71 + * @return the id of the current sesson the user logged in or null if the login did not succeed
    1.72 + * @throws NullPointerException carrying the error-message if an communication error occured.
    1.73 + */
    1.74 +	static public String login(String username, String password){
    1.75 +		SDialog sd = getSDialog();
    1.76 +		try {
    1.77 +			return (String)(sd.login(username, password).elementAt(0));
    1.78 +		} catch (RemoteException e) {
    1.79 +			throw new NullPointerException(getErrorXML("Sorry, RemoteEcxeption while accessing the SDialog to login - is the SDialog runnig ?"+e.getMessage()));
    1.80 +		}
    1.81 +	}
    1.82 +	
    1.83 +	public static BDialog getBDialog(String session){
    1.84 +		String name = "//localhost/isac-ObjectManager";
    1.85 +		try{
    1.86 +			System.out.println("connect to ObjectManager"+name);
    1.87 +			Object lookup = Naming.lookup(name);
    1.88 +			return ((OManager)lookup).getBrowserDialog(session);
    1.89 +		}
    1.90 +		catch(RemoteException exc){
    1.91 +			System.err.println("no rmiregistry runing on server "+name+
    1.92 +			" no connection possible --- try later");
    1.93 +			System.err.println("serverCom exception: " + exc.getMessage());
    1.94 +		}
    1.95 +		catch(NotBoundException exc){
    1.96 +			System.err.println("servercom: notboundexc: "+ exc.getMessage());
    1.97 +			System.err.println("server not found - try to connect to Proxy");
    1.98 +		}
    1.99 +		catch(java.net.MalformedURLException exc){
   1.100 +			System.err.println("servercom: MalformedURLException: "+ exc.getMessage());
   1.101 +			exc.printStackTrace();
   1.102 +		}
   1.103 +		return null;
   1.104 +	}
   1.105 +
   1.106 +  public static SDialog getSDialog(){
   1.107 +	String name= "//localhost/isac-ObjectManager";
   1.108 +	try {
   1.109 +		System.out.println("connect to ObjectManager: "+name);
   1.110 +		Object lookup =  Naming.lookup(name);
   1.111 +		return ((OManager) lookup).getSessionDialog();
   1.112 +	}
   1.113 +	catch(RemoteException exc){
   1.114 +		System.err.println("no rmiregistry runing on server "+name+
   1.115 +		" no connection possible --- try later");
   1.116 +		System.err.println("serverCom exception: " + exc.getMessage());
   1.117 +	}
   1.118 +	catch(NotBoundException exc){
   1.119 +		System.err.println("servercom: notboundexc: "+ exc.getMessage());
   1.120 +		System.err.println("server not found - try to connect to Proxy");
   1.121 +	}
   1.122 +	catch(java.net.MalformedURLException exc){
   1.123 +		System.err.println("servercom: MalformedURLException: "+ exc.getMessage());
   1.124 +		exc.printStackTrace();
   1.125 +	}
   1.126 +	return null;
   1.127 +}
   1.128 +
   1.129 +/*
   1.130 +	static protected String oldloadString(String function, Vector parameter){
   1.131 +	XmlRpcClient bdialog;
   1.132 +	String xmlsource="";
   1.133 +	try {
   1.134 +		bdialog = new XmlRpcClient("http://localhost:1040");
   1.135 +		Vector result = (Vector) bdialog.execute(function, parameter);
   1.136 +		xmlsource = (String)result.firstElement();
   1.137 +	} catch (MalformedURLException e) {
   1.138 +		e.printStackTrace();
   1.139 +	} catch (XmlRpcException e) {
   1.140 +		e.printStackTrace();
   1.141 +	} catch (IOException e) {
   1.142 +		e.printStackTrace();
   1.143 +	}
   1.144 +	return xmlsource;
   1.145 +	}
   1.146 +*/
   1.147 +/*
   1.148 +	static protected Vector oldloadVector(String function, Vector parameter){
   1.149 +	Vector return_vect=new Vector();
   1.150 +	XmlRpcClient bdialog;
   1.151 +	String xmlsource="";
   1.152 +	try {
   1.153 +		bdialog = new XmlRpcClient("http://localhost:1040");
   1.154 +		return_vect = (Vector) bdialog.execute(function, parameter);
   1.155 +	} catch (MalformedURLException e) {
   1.156 +		e.printStackTrace();
   1.157 +	} catch (XmlRpcException e) {
   1.158 +		e.printStackTrace();
   1.159 +	} catch (IOException e) {
   1.160 +		e.printStackTrace();
   1.161 +	}
   1.162 +	return return_vect;
   1.163 +	}
   1.164 +*/
   1.165 +
   1.166 +/**
   1.167 + * Loads the hierarchy for the information-type <code>type</type>
   1.168 + * @param session session to identify on the SessionDialog
   1.169 + * @param type type of the hierarchy to load
   1.170 + * @return  the requested hierarchy in its XML-representation, in case of an RemoteException, a XML-code 
   1.171 + * carrying the error-message is returned
   1.172 + */
   1.173 +	static public String loadHierarchy(String session, String type){
   1.174 +		BDialog bd = getBDialog(session);
   1.175 +		try {
   1.176 +			return bd.loadHierarchy(type);
   1.177 +		} catch (RemoteException e) {
   1.178 +			return getErrorXML("Sorry, RemoteEcxeption while accessing the BDialog to login - is the BDialog runnig ?"+e.getMessage());
   1.179 +		}
   1.180 +	}
   1.181 +
   1.182 +	/**
   1.183 +	 * Loads binary files from the KE-Base
   1.184 +	 * @param session session to identify on the SessionDialog
   1.185 +	 * @param type to identify the location of the binary file. An id has only to be unique within a type, although 
   1.186 +	 * a globally unique id should be used.
   1.187 +	 * @param content_id Id of the binary-file to load
   1.188 +	 * @return  the requested binary-file as byte-array, in case of an RemoteException, an empty array is returned
   1.189 +	 */
   1.190 +	static public byte[] loadBinary(String session, String type, String content_id){
   1.191 +		BDialog bd = getBDialog(session);
   1.192 +		try {
   1.193 +			return bd.loadBinary(type, content_id);
   1.194 +		} catch (RemoteException e) {
   1.195 +			return new byte[0];
   1.196 +		}
   1.197 +	}
   1.198 +
   1.199 +	/**
   1.200 +	 * Loads content-files from the KE-Base
   1.201 +	 * @param session session to identify on the SessionDialog
   1.202 +	 * @param type to identify the location of the KE-Object. An id has only to be unique within a type, although 
   1.203 +	 * a globally unique id should be used.
   1.204 +	 * @param content_id Id of the KE-Object to load
   1.205 +	 * @return  the requested KE-Object in its XML-representation, in case of an RemoteException, a XML-code 
   1.206 +	 * carrying the error-message is returned
   1.207 +	 */
   1.208 +	static public String loadXML(String session, String type, String content_id){
   1.209 +		BDialog bd = getBDialog(session);
   1.210 +		try {
   1.211 +			return bd.loadContent(type, content_id);
   1.212 +		} catch (RemoteException e) {
   1.213 +			return getErrorXML("Sorry, RemoteException while accessing the BDialog to fetch your file \""+content_id+"\" is the BDialog running?");
   1.214 +		}
   1.215 +	}
   1.216 +
   1.217 +	/**
   1.218 +	 * Evaluates the requested content in the given worksheet using the BrowserDialog
   1.219 +	 * @param session session to identify on the SessionDialog
   1.220 +	 * @param type to identify the location of the KE-Object. An id has only to be unique within a type, although 
   1.221 +	 * a globally unique id should be used.
   1.222 +	 * @param content_id Id of the KE-Object to load
   1.223 +	 * @param wd_id id of the worksheet the contanct for evaluating
   1.224 +	 * @return  the requested KE-Object in its XML-representation, in case of an RemoteException, a XML-code 
   1.225 +	 * carrying the error-message is returned
   1.226 +	 * @see BDialog#evaluateContent
   1.227 +	 */
   1.228 +	static public String evaluateXML(String session, String type, String content_id, String wd_id){
   1.229 +		BDialog bd = getBDialog(session);
   1.230 +		try {
   1.231 +			return bd.evaluateContent(type, content_id, wd_id);
   1.232 +		} catch (RemoteException e) {
   1.233 + 			return getErrorXML("Sorry, RemoteException while accessing the BDialog to evaluate \""+content_id+"\" is the BDialog running?");
   1.234 + 		}	
   1.235 +	}
   1.236 +	
   1.237 +	/*
   1.238 +    static protected Reader loadsKEObject(String function, Vector parameter){
   1.239 +	XmlRpcClient bdialog;
   1.240 +	String xmlsource="";
   1.241 +	try {
   1.242 +	    bdialog = new XmlRpcClient("http://localhost:1040");
   1.243 +	    Vector result = (Vector) bdialog.execute(function, parameter);
   1.244 +	    if(result.size()>0)
   1.245 +	    	xmlsource = (String)result.firstElement();
   1.246 +	    else
   1.247 +	    	xmlsource = "";
   1.248 +	} catch (MalformedURLException e) {
   1.249 +		return new StringReader(getErrorXML("Sorry, MalformedURLException while accessing the KEStore"));
   1.250 +	} catch (XmlRpcException e) {
   1.251 +		return new StringReader(getErrorXML("Sorry, XmlRpcException while accessing the KEStore"));
   1.252 +	} catch (IOException e) {
   1.253 +		return new StringReader(getErrorXML("Sorry, IOException while accessing the KEStore"));
   1.254 +	}
   1.255 +	return new StringReader(xmlsource);
   1.256 +    }
   1.257 +*/
   1.258 +
   1.259 +/**
   1.260 + * Get the corresponding user for a session.
   1.261 + * @param session_id id of the session to identify the user
   1.262 + * @return username or null if an invalid sessionid was given
   1.263 + * @throws NullPointerException carrying the error-message if an RemoteException occures.
   1.264 + */
   1.265 +	public static String getUsername(String session_id){
   1.266 +		SDialog sd = getSDialog();
   1.267 +		try {
   1.268 +			String username;
   1.269 +			if((username=sd.getUsername(session_id)).equals(""))
   1.270 +				return null;
   1.271 +			return username;
   1.272 +		} catch (RemoteException e) {
   1.273 +			throw new NullPointerException(getErrorXML("Sorry, RemoteException while accesing the SDialog - is it running ?"));
   1.274 +		}
   1.275 +	}
   1.276 +
   1.277 +
   1.278 +	/**
   1.279 +	 * @param session_id
   1.280 +	 * @return
   1.281 +	 */
   1.282 +	private static String xmlGetUsername(String session_id) {
   1.283 +		XmlRpcClient bdialog;
   1.284 +		String xmlsource="";
   1.285 +		Vector parameter=new Vector();
   1.286 +		parameter.add(session_id);
   1.287 +		try {
   1.288 +			bdialog = new XmlRpcClient("http://localhost:1050");
   1.289 +			String result = (String) bdialog.execute("SDialog.getUsername", parameter);
   1.290 +			if(result.equals(""))
   1.291 +				return null;
   1.292 +			return result;
   1.293 +		} catch (MalformedURLException e) {
   1.294 +			return getErrorXML("Sorry, MalformedURLException while accesing the SDialog - is it running ?");
   1.295 +		} catch (XmlRpcException e) {
   1.296 +			return getErrorXML("Sorry, XmlRpcException while accesing the SDialog - is it running ?");
   1.297 +		} catch (IOException e) {
   1.298 +			return getErrorXML("Sorry, IOException while accesing the SDialog - is it running ?");
   1.299 +		}		
   1.300 +	}
   1.301 +
   1.302 +	private static String getErrorXML(String string) {
   1.303 +		return "<ERROR><DESCRIPTION> <h1> Error in file InformationPrecessor.java </h1>"+string+"</DESCRIPTION></ERROR>";
   1.304 +	}
   1.305 +}