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