src/java/isac/util/InformationProcessor.java
author wneuper
Sat, 12 Mar 2005 06:14:55 +0100
changeset 2162 161819a0cbbc
parent 1773 8de024816985
child 2244 f505961e74de
permissions -rw-r--r--
all-050311a-beforeStart: of impl. userSettings
     1 /*
     2  * Created on Aug 16, 2003
     3  */
     4 package isac.util;
     5 
     6 import isac.browserdialog.BDialog;
     7 import isac.session.OManager;
     8 import isac.session.SDialog;
     9 
    10 import java.io.IOException;
    11 import java.net.MalformedURLException;
    12 import java.rmi.Naming;
    13 import java.rmi.NotBoundException;
    14 import java.rmi.RemoteException;
    15 import java.util.Vector;
    16 
    17 import org.apache.xmlrpc.XmlRpcClient;
    18 import org.apache.xmlrpc.XmlRpcException;
    19 
    20 /**
    21  * @author Andreas Griesmayer
    22  * 
    23  * The InformationProcessor builds the bridge between BrowserFrontend and
    24  * BrowserDialog. It encapsulates all tasks regarding communication and connects
    25  * to the proper dialog-modules.
    26  * 
    27  * Because the final communication-protocol is not fixed yet, it provides
    28  * methods to communicate via RMI as well as XML-RPC
    29  */
    30 public class InformationProcessor {
    31 
    32 	/**
    33 	 * Performes an login on the session dialog using the XML-RPC protocol. It
    34 	 * assumes that the SessionDialog is running its XML-RPC-handler on port
    35 	 * 1050 of the local machine.
    36 	 * 
    37 	 * @param username
    38 	 *            username of the user who wants to login.
    39 	 * @param password
    40 	 *            corresponding password for the user.
    41 	 * @return the id of the current sesson the user logged in or null if the
    42 	 *         login did not succeed
    43 	 * @throws NullPointerException
    44 	 *             carrying the error-message if an communication error occured.
    45 	 */
    46 	static protected String xmlLogin(String username, String password) {
    47 		try {
    48 			XmlRpcClient sdialog = new XmlRpcClient("http://localhost:1050");//WN fixed port
    49 			Vector params = new Vector();
    50 			params.add(username);
    51 			params.add(password);
    52 			Vector response = (Vector) sdialog.execute("SDialog.login", params);
    53 			if (response.size() > 0)
    54 				return (String) response.elementAt(0);
    55 		} catch (MalformedURLException e) {
    56 			throw new NullPointerException(
    57 					getErrorXML("Sorry, MalformedURLException while accessing the SDialog to login - is the SDialog runnig ?"
    58 							+ e.getMessage()));
    59 		} catch (XmlRpcException e) {
    60 			throw new NullPointerException(
    61 					getErrorXML("Sorry, XmlRpcException while accessing the SDialog to login - is the SDialog runnig ?"
    62 							+ e.getMessage()));
    63 		} catch (IOException e) {
    64 			throw new NullPointerException(
    65 					getErrorXML("Sorry, IOException while accessing the SDialog to login - is the SDialog runnig ?"
    66 							+ e.getMessage()));
    67 		}
    68 		return null;
    69 	}
    70 
    71 	/**
    72 	 * Performes a login on the Session dialog using RMI. It assumes the
    73 	 * SessionDialog to be found at //localhost/isac-ObjectManager
    74 	 * 
    75 	 * @param username
    76 	 *            username of the user who wants to login.
    77 	 * @param password
    78 	 *            corresponding password for the user.
    79 	 * @return the id of the current sesson the user logged in or null if the
    80 	 *         login did not succeed
    81 	 * @throws NullPointerException
    82 	 *             carrying the error-message if an communication error occured.
    83 	 */
    84 	static public String login(String username, String password) {
    85 		SDialog sd = getSDialog();
    86 		try {
    87 			/* LK 04.08.27 */
    88 			//if username != pwd -> empty Vector
    89 			Vector v = sd.login(username, password);
    90 			if (v.size() == 0) {
    91 				return null;
    92 			} else {
    93 				return (String) v.elementAt(0);
    94 			}
    95 		} catch (RemoteException e) {
    96 			throw new NullPointerException(
    97 					getErrorXML("Sorry, RemoteEcxeption while accessing the SDialog to login - is the SDialog runnig ?"
    98 							+ e.getMessage()));
    99 		}
   100 	}
   101 
   102 	public static BDialog getBDialog(String session) {
   103 		String name = "//localhost/isac-ObjectManager";
   104 		try {
   105 			System.out.println("connect to ObjectManager" + name);
   106 			Object lookup = Naming.lookup(name);
   107 			return ((OManager) lookup).getBrowserDialog(session);
   108 		} catch (RemoteException exc) {
   109 			System.err.println("no rmiregistry runing on server " + name
   110 					+ " no connection possible --- try later");
   111 			System.err.println("serverCom exception: " + exc.getMessage());
   112 		} catch (NotBoundException exc) {
   113 			System.err.println("servercom: notboundexc: " + exc.getMessage());
   114 			System.err.println("server not found - try to connect to Proxy");
   115 		} catch (java.net.MalformedURLException exc) {
   116 			System.err.println("servercom: MalformedURLException: "
   117 					+ exc.getMessage());
   118 			exc.printStackTrace();
   119 		}
   120 		return null;
   121 	}
   122 
   123 	public static SDialog getSDialog() {
   124 		String name = "//localhost/isac-ObjectManager";
   125 		try {
   126 			System.out.println("connect to ObjectManager: " + name);
   127 			Object lookup = Naming.lookup(name);
   128 			return ((OManager) lookup).getSessionDialog();
   129 		} catch (RemoteException exc) {
   130 			System.err.println("no rmiregistry runing on server " + name
   131 					+ " no connection possible --- try later");
   132 			System.err.println("serverCom exception: " + exc.getMessage());
   133 		} catch (NotBoundException exc) {
   134 			System.err.println("servercom: notboundexc: " + exc.getMessage());
   135 			System.err.println("server not found - try to connect to Proxy");
   136 		} catch (java.net.MalformedURLException exc) {
   137 			System.err.println("servercom: MalformedURLException: "
   138 					+ exc.getMessage());
   139 			exc.printStackTrace();
   140 		}
   141 		return null;
   142 	}
   143 
   144 	/*
   145 	 * static protected String oldloadString(String function, Vector parameter){
   146 	 * XmlRpcClient bdialog; String xmlsource=""; try { bdialog = new
   147 	 * XmlRpcClient("http://localhost:1040"); Vector result = (Vector)
   148 	 * bdialog.execute(function, parameter); xmlsource =
   149 	 * (String)result.firstElement(); } catch (MalformedURLException e) {
   150 	 * e.printStackTrace(); } catch (XmlRpcException e) { e.printStackTrace(); }
   151 	 * catch (IOException e) { e.printStackTrace(); } return xmlsource; }
   152 	 */
   153 	/*
   154 	 * static protected Vector oldloadVector(String function, Vector parameter){
   155 	 * Vector return_vect=new Vector(); XmlRpcClient bdialog; String
   156 	 * xmlsource=""; try { bdialog = new XmlRpcClient("http://localhost:1040");
   157 	 * return_vect = (Vector) bdialog.execute(function, parameter); } catch
   158 	 * (MalformedURLException e) { e.printStackTrace(); } catch (XmlRpcException
   159 	 * e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
   160 	 * return return_vect; }
   161 	 */
   162 
   163 	/**
   164 	 * Loads the hierarchy for the information-type <code>type</type>
   165 	 * @param session session to identify on the SessionDialog
   166 	 * @param type type of the hierarchy to load
   167 	 * @return  the requested hierarchy in its XML-representation, in case of an RemoteException, a XML-code 
   168 	 * carrying the error-message is returned
   169 	 */
   170 	static public String loadHierarchy(String session, String type) {
   171 		BDialog bd = getBDialog(session);
   172 		try {
   173 			return bd.loadHierarchy(type);
   174 		} catch (RemoteException e) {
   175 			return getErrorXML("Sorry, RemoteEcxeption while accessing the BDialog to login - is the BDialog runnig ?"
   176 					+ e.getMessage());
   177 		}
   178 	}
   179 
   180 	/**
   181 	 * Loads binary files from the KE-Base
   182 	 * 
   183 	 * @param session
   184 	 *            session to identify on the SessionDialog
   185 	 * @param type
   186 	 *            to identify the location of the binary file. An id has only to
   187 	 *            be unique within a type, although a globally unique id should
   188 	 *            be used.
   189 	 * @param content_id
   190 	 *            Id of the binary-file to load
   191 	 * @return the requested binary-file as byte-array, in case of an
   192 	 *         RemoteException, an empty array is returned
   193 	 */
   194 	static public byte[] loadBinary(String session, String type,
   195 			String content_id) {
   196 		BDialog bd = getBDialog(session);
   197 		try {
   198 			return bd.loadBinary(type, content_id);
   199 		} catch (RemoteException e) {
   200 			return new byte[0];
   201 		}
   202 	}
   203 
   204 	static public String loadXML(String session, String type, Vector idvect) {
   205 		BDialog bd = getBDialog(session);
   206 		try {
   207 			return bd.loadContent(type, idvect);
   208 		} catch (RemoteException e) {
   209 			return getErrorXML("Sorry, RemoteException while accessing the BDialog to fetch your content");
   210 		}
   211 	}
   212 
   213 	/**
   214 	 * Loads content-files from the KE-Base
   215 	 * 
   216 	 * @param session
   217 	 *            session to identify on the SessionDialog
   218 	 * @param type
   219 	 *            to identify the location of the KE-Object. An id has only to
   220 	 *            be unique within a type, although a globally unique id should
   221 	 *            be used.
   222 	 * @param content_id
   223 	 *            Id of the KE-Object to load
   224 	 * @return the requested KE-Object in its XML-representation, in case of an
   225 	 *         RemoteException, a XML-code carrying the error-message is
   226 	 *         returned
   227 	 */
   228 	static public String loadXML(String session, String type, String content_id) {
   229 		BDialog bd = getBDialog(session);
   230 		try {
   231 			return bd.loadContent(type, content_id);
   232 		} catch (RemoteException e) {
   233 			return getErrorXML("Sorry, RemoteException while accessing the BDialog to fetch your file \""
   234 					+ content_id + "\" is the BDialog running?");
   235 		}
   236 	}
   237 
   238 	/**
   239 	 * Evaluates the requested content in the given worksheet using the
   240 	 * BrowserDialog
   241 	 * 
   242 	 * @param session
   243 	 *            session to identify on the SessionDialog
   244 	 * @param type
   245 	 *            to identify the location of the KE-Object. An id has only to
   246 	 *            be unique within a type, although a globally unique id should
   247 	 *            be used.
   248 	 * @param content_id
   249 	 *            Id of the KE-Object to load
   250 	 * @param wd_id
   251 	 *            id of the worksheet the contanct for evaluating
   252 	 * @return the requested KE-Object in its XML-representation, in case of an
   253 	 *         RemoteException, a XML-code carrying the error-message is
   254 	 *         returned
   255 	 * @see BDialog#evaluateContent
   256 	 */
   257 	static public String evaluateXML(String session, String type,
   258 			String content_id, String wd_id) {
   259 		BDialog bd = getBDialog(session);
   260 		try {
   261 			return bd.evaluateContent(type, content_id, wd_id);
   262 		} catch (RemoteException e) {
   263 			return getErrorXML("Sorry, RemoteException while accessing the BDialog to evaluate \""
   264 					+ content_id + "\" is the BDialog running?");
   265 		}
   266 	}
   267 
   268 	/*
   269 	 * static protected Reader loadsKEObject(String function, Vector parameter){
   270 	 * XmlRpcClient bdialog; String xmlsource=""; try { bdialog = new
   271 	 * XmlRpcClient("http://localhost:1040"); Vector result = (Vector)
   272 	 * bdialog.execute(function, parameter); if(result.size()>0) xmlsource =
   273 	 * (String)result.firstElement(); else xmlsource = ""; } catch
   274 	 * (MalformedURLException e) { return new StringReader(getErrorXML("Sorry,
   275 	 * MalformedURLException while accessing the KEStore")); } catch
   276 	 * (XmlRpcException e) { return new StringReader(getErrorXML("Sorry,
   277 	 * XmlRpcException while accessing the KEStore")); } catch (IOException e) {
   278 	 * return new StringReader(getErrorXML("Sorry, IOException while accessing
   279 	 * the KEStore")); } return new StringReader(xmlsource); }
   280 	 */
   281 
   282 	/**
   283 	 * Get the corresponding user for a session.
   284 	 * 
   285 	 * @param session_id
   286 	 *            id of the session to identify the user
   287 	 * @return username or null if an invalid sessionid was given
   288 	 * @throws NullPointerException
   289 	 *             carrying the error-message if an RemoteException occures.
   290 	 */
   291 	public static String getUsername(String session_id) {
   292 		SDialog sd = getSDialog();
   293 		try {
   294 			String username;
   295 			if ((username = sd.getUsername(session_id)).equals(""))
   296 				return null;
   297 			return username;
   298 		} catch (RemoteException e) {
   299 			throw new NullPointerException(
   300 					getErrorXML("Sorry, RemoteException while accesing the SDialog - is it running ?"));
   301 		}
   302 	}
   303 
   304 	/**
   305 	 * @param session_id
   306 	 * @return
   307 	 */
   308 	private static String xmlGetUsername(String session_id) {
   309 		XmlRpcClient bdialog;
   310 		String xmlsource = "";
   311 		Vector parameter = new Vector();
   312 		parameter.add(session_id);
   313 		try {
   314 			bdialog = new XmlRpcClient("http://localhost:1050");
   315 			String result = (String) bdialog.execute("SDialog.getUsername",
   316 					parameter);
   317 			if (result.equals(""))
   318 				return null;
   319 			return result;
   320 		} catch (MalformedURLException e) {
   321 			return getErrorXML("Sorry, MalformedURLException while accesing the SDialog - is it running ?");
   322 		} catch (XmlRpcException e) {
   323 			return getErrorXML("Sorry, XmlRpcException while accesing the SDialog - is it running ?");
   324 		} catch (IOException e) {
   325 			return getErrorXML("Sorry, IOException while accesing the SDialog - is it running ?");
   326 		}
   327 	}
   328 
   329 	private static String getErrorXML(String string) {
   330 		return "<ERROR><DESCRIPTION> <h1> Error in file InformationPrecessor.java </h1>"
   331 				+ string + "</DESCRIPTION></ERROR>";
   332 	}
   333 }