001: package com.salmonllc.remote;
002:
003: import com.salmonllc.sql.Base64Encoder;
004:
005: import java.net.URLConnection;
006: import java.net.URL;
007: import java.io.ObjectOutputStream;
008: import java.io.Serializable;
009: import java.io.ObjectInputStream;
010: import java.io.EOFException;
011: import java.lang.reflect.InvocationTargetException;
012: import java.util.Properties;
013:
014: /**
015: * Created by IntelliJ IDEA.
016: * User: Fred Cahill
017: * Date: Sep 14, 2004
018: * Time: 10:05:19 AM
019: * To change this template use Options | File Templates.
020: */
021: /**
022: * This class provides a way to reference an object in the session on the server. Access is achieved via the RemoteReflector servlet.
023: * This class is intended to be use by applets served up by framework html pages that need to instantiate and invoke methods of objects on the server side. The following classes need to be in the applets jar file in order for the RemoteProxy to work correctly:RemoteProxy, RemoteReflection, RemoteReflectionException, Reflect
024: */
025: public class RemoteProxy implements Serializable {
026:
027: public static final int REMOTE_STATUS_OK = 0;
028: public static final int REMOTE_STATUS_BAD_REQUEST = 1;
029: public static final int REMOTE_STATUS_METHOD_NOT_FOUND = 2;
030: public static final int REMOTE_STATUS_OBJECT_NOT_FOUND = 3;
031: public static final int REMOTE_STATUS_ACCESS_DENIED = 4;
032: public static final int REMOTE_STATUS_EXCEPTION_OCCURED = 5;
033: public static final int REMOTE_STATUS_CONSTRUCTOR_NOT_FOUND = 6;
034: public static final int REMOTE_STATUS_CLASS_NOT_FOUND = 7;
035:
036: private String _url;
037: private RemoteReflection _rr;
038: private String _proxyUserID;
039: private String _proxyPw;
040: private ObjectOutputStream _out;
041: private String _sessionId;
042: private String _user;
043: private String _pass;
044:
045: private class SessionKey implements RemoteReflection {
046: private String _sessionkey;
047:
048: public SessionKey(String sSessionKey) {
049: _sessionkey = sSessionKey;
050: }
051:
052: public String getSessionKey() {
053: return null;
054: }
055: }
056:
057: /**
058: * Creates a remote proxy to an object held on the session. This class is intended to be use by applets served up by framework html pages that need to instantiate and invoke methods of objects on the server side. The following classes need to be in the applets jar file in order for the RemoteProxy to work correctly:RemoteProxy, RemoteReflection, RemoteReflectionException
059: * @param sUrl The url of the RemoteReflector Servlet. Usual URL is http://hostname/Reflect
060: * @param rr An instance of an object that implements RemoteReflection interface, needed for the session key associated with the object.
061: * @param sessionId The server side session id to use.
062: */
063: public RemoteProxy(String sUrl, RemoteReflection rr,
064: String sessionId) {
065: _sessionId = sessionId;
066: _url = sUrl;
067: _rr = rr;
068: }
069:
070: /**
071: * Creates a remote proxy to an object held on the session. This class is intended to be use by applets served up by framework html pages that need to instantiate and invoke methods of objects on the server side. The following classes need to be in the applets jar file in order for the RemoteProxy to work correctly:RemoteProxy, RemoteReflection, RemoteReflectionException
072: * @param sUrl The url of the RemoteReflector Servlet. Usual URL is http://hostname/Reflect
073: * @param rr An instance of an object that implements RemoteReflection interface, needed for the session key associated with the object.
074: * @param sSession The server side session id to use.
075: * @param userID The user id for authorization to the servlet.
076: * @param passWord The password for authorization to the servlet.
077: */
078: public RemoteProxy(String sUrl, RemoteReflection rr,
079: String sSession, String userID, String passWord) {
080: this (sUrl, rr, sSession);
081: _user = userID;
082: _pass = passWord;
083: }
084:
085: /**
086: * Creates a remote proxy to an object held on the session. This class is intended to be use by applets served up by framework html pages that need to instantiate and invoke methods of objects on the server side. The following classes need to be in the applets jar file in order for the RemoteProxy to work correctly:RemoteProxy, RemoteReflection, RemoteReflectionException
087: * @param sUrl The url of the RemoteReflector Servlet. Usual URL is http://hostname/Reflect
088: * @param rr An instance of an object that implements RemoteReflection interface, needed for the session key associated with the object.
089: * @param sSession The server side session id to use.
090: * @param userID The user id for authorization to the servlet.
091: * @param passWord The password for authorization to the servlet.
092: * @param proxyHost The host name of a proxy server to use
093: * @param proxyPort The port number of a proxy server to use
094: * @param proxyUser The user id to get through the proxy server
095: * @param proxyPassword The password to get through the proxy server
096: */
097: public RemoteProxy(String sUrl, RemoteReflection rr,
098: String sSession, String userID, String passWord,
099: String proxyHost, String proxyPort, String proxyUser,
100: String proxyPassword) {
101: this (sUrl, rr, sSession, userID, passWord);
102: if (proxyHost != null) {
103: Properties p = System.getProperties();
104: p.put("proxySet", "true");
105: p.put("proxyHost", proxyHost);
106: p.put("proxyPort", proxyPort);
107: }
108: _proxyUserID = proxyUser;
109: _proxyPw = proxyPassword;
110: }
111:
112: /**
113: * Creates a remote proxy to an object held on the session. This class is intended to be use by applets served up by framework html pages that need to instantiate and invoke methods of objects on the server side. The following classes need to be in the applets jar file in order for the RemoteProxy to work correctly:RemoteProxy, RemoteReflection, RemoteReflectionException
114: * @param sUrl The url of the RemoteReflector Servlet. Usual URL is http://hostname/Reflect
115: * @param sessionkey the session key associated with the object.
116: * @param sessionId The server side session id to use.
117: */
118: public RemoteProxy(String sUrl, String sessionkey, String sessionId) {
119: SessionKey sk = new SessionKey(sessionkey);
120: _sessionId = sessionId;
121: _url = sUrl;
122: _rr = sk;
123: }
124:
125: /**
126: * Creates a remote proxy to an object held on the session. This class is intended to be use by applets served up by framework html pages that need to instantiate and invoke methods of objects on the server side. The following classes need to be in the applets jar file in order for the RemoteProxy to work correctly:RemoteProxy, RemoteReflection, RemoteReflectionException
127: * @param sUrl The url of the RemoteReflector Servlet. Usual URL is http://hostname/Reflect
128: * @param sessionkey the session key associated with the object.
129: * @param sSession The server side session id to use.
130: * @param userID The user id for authorization to the servlet.
131: * @param passWord The password for authorization to the servlet.
132: */
133: public RemoteProxy(String sUrl, String sessionkey, String sSession,
134: String userID, String passWord) {
135: this (sUrl, sessionkey, sSession);
136: _user = userID;
137: _pass = passWord;
138: }
139:
140: /**
141: * Creates a remote proxy to an object held on the session. This class is intended to be use by applets served up by framework html pages that need to instantiate and invoke methods of objects on the server side. The following classes need to be in the applets jar file in order for the RemoteProxy to work correctly:RemoteProxy, RemoteReflection, RemoteReflectionException
142: * @param sUrl The url of the RemoteReflector Servlet. Usual URL is http://hostname/Reflect
143: * @param sessionkey the session key associated with the object.
144: * @param sSession The server side session id to use.
145: * @param userID The user id for authorization to the servlet.
146: * @param passWord The password for authorization to the servlet.
147: * @param proxyHost The host name of a proxy server to use
148: * @param proxyPort The port number of a proxy server to use
149: * @param proxyUser The user id to get through the proxy server
150: * @param proxyPassword The password to get through the proxy server
151: */
152: public RemoteProxy(String sUrl, String sessionkey, String sSession,
153: String userID, String passWord, String proxyHost,
154: String proxyPort, String proxyUser, String proxyPassword) {
155: this (sUrl, sessionkey, sSession, userID, passWord);
156: if (proxyHost != null) {
157: Properties p = System.getProperties();
158: p.put("proxySet", "true");
159: p.put("proxyHost", proxyHost);
160: p.put("proxyPort", proxyPort);
161: }
162: _proxyUserID = proxyUser;
163: _proxyPw = proxyPassword;
164: }
165:
166: private int getResponse(URLConnection conn) {
167: return conn.getHeaderFieldInt("RemoteReflectorResponse", 0);
168: }
169:
170: private void handleError(String sMethod, int ret, Throwable t)
171: throws NoSuchMethodException, RemoteReflectionException,
172: IllegalAccessException, InvocationTargetException {
173: if (ret == REMOTE_STATUS_CLASS_NOT_FOUND) {
174: throw new NoSuchMethodException("Remote Class not found: "
175: + (t != null ? t.getMessage() : sMethod));
176: } else if (ret == REMOTE_STATUS_CONSTRUCTOR_NOT_FOUND) {
177: throw new NoSuchMethodException(
178: "Remote Constructor not found: "
179: + (t != null ? t.getMessage() : sMethod));
180: } else if (ret == REMOTE_STATUS_METHOD_NOT_FOUND) {
181: throw new NoSuchMethodException("Remote Method not found: "
182: + (t != null ? t.getMessage() : sMethod));
183: } else if (ret == REMOTE_STATUS_BAD_REQUEST) {
184: throw new RemoteReflectionException(
185: "Bad request to Remote Reflector: "
186: + (t != null ? t.getMessage() : sMethod), t);
187: } else if (ret == REMOTE_STATUS_ACCESS_DENIED) {
188: if (t != null) {
189: if (t instanceof IllegalAccessException) {
190: throw new IllegalAccessException(
191: "Remote Illegal Access: "
192: + (t != null ? t.getMessage()
193: : sMethod));
194: } else {
195: throw new InvocationTargetException(
196: ((InvocationTargetException) t)
197: .getTargetException(),
198: "Remote Invocation Target: "
199: + (t != null ? t.getMessage()
200: : sMethod));
201: }
202: } else
203: throw new RemoteReflectionException(
204: "Remote Illegal Access.");
205: } else if (ret == REMOTE_STATUS_OBJECT_NOT_FOUND) {
206: throw new RemoteReflectionException(
207: "Remote Object not found: "
208: + (t != null ? t.getMessage() : sMethod), t);
209: }
210:
211: }
212:
213: /**
214: * Invokes the specified method on the object held on the session represented by the RemoteReflection Instance.
215: * @param sMethod The method to execute on the object on the server.
216: * @return Object The result of the method call if there is one.
217: */
218: public Object invokeMethod(String sMethod)
219: throws RemoteReflectionException {
220: return invokeMethod(sMethod, null, null);
221: }
222:
223: /**
224: * Invokes the specified method on the object held on the session represented by the RemoteReflection Instance.
225: * @param sMethod The method to execute on the object on the server.
226: * @param oaParms The parameters to pass to the method.
227: * @return Object The result of the method call if there is one.
228: */
229: public Object invokeMethod(String sMethod, Serializable[] oaParms)
230: throws RemoteReflectionException {
231: Class[] caParms = oaParms == null ? null
232: : new Class[oaParms.length];
233: if (caParms != null) {
234: for (int i = 0; i < caParms.length; i++) {
235: caParms[i] = oaParms[i].getClass();
236: }
237: }
238: return invokeMethod(sMethod, caParms, oaParms);
239: }
240:
241: /**
242: * Invokes the specified method on the object held on the session represented by the RemoteReflection Instance.
243: * @param sMethod The method to execute on the object on the server.
244: * @param caParms The class type of the parameters to pass to the method.
245: * @param oaParms The parameters to pass to the method.
246: * @return Object The result of the method call if there is one.
247: */
248: public Object invokeMethod(String sMethod, Class[] caParms,
249: Serializable[] oaParms) throws RemoteReflectionException {
250: Object obj = null;
251: if (sMethod == null) {
252: throw new RemoteReflectionException("Method Name Required.");
253: }
254: try {
255: URLConnection conn = openConnection(sMethod, caParms,
256: oaParms, false);
257: int result = getResponse(conn);
258: if (result == REMOTE_STATUS_OK) {
259:
260: try {
261: ObjectInputStream in = new ObjectInputStream(conn
262: .getInputStream());
263: obj = in.readObject();
264: in.close();
265: } catch (EOFException eofe) {
266: ;
267: }
268: _out.close();
269:
270: } else {
271: if (result == REMOTE_STATUS_BAD_REQUEST) {
272: try {
273: ObjectInputStream in = new ObjectInputStream(
274: conn.getInputStream());
275: obj = in.readObject();
276: in.close();
277: if (obj != null && obj instanceof Exception)
278: throw ((Exception) obj);
279: return obj;
280: } catch (EOFException eofe) {
281: ;
282: }
283: }
284: handleError(sMethod, result, null);
285: }
286: } catch (Exception ex) {
287: ex.printStackTrace();
288: throw new RemoteReflectionException(ex.toString(), ex);
289: }
290: return obj;
291: }
292:
293: /**
294: * Instantiates an instance of the specified class on the server and places it on the session such that it can be referred to by RemoteReflection instance.
295: * @param sClass The class to instantiate on the server.
296: * @return Object The instantiated object if serializable or RemoteReflection instance refering to it.
297: */
298: public Object instantiate(String sClass)
299: throws RemoteReflectionException {
300: return instantiate(sClass, null, null);
301: }
302:
303: /**
304: * Instantiates an instance of the specified class on the server and places it on the session such that it can be referred to by RemoteReflection instance.
305: * @param sClass The class to instantiate on the server.
306: * @return Object The instantiated object if serializable or RemoteReflection instance refering to it.
307: */
308: public Object instantiate(String sClass, Serializable[] oaParms)
309: throws RemoteReflectionException {
310: Class[] caParms = oaParms == null ? null
311: : new Class[oaParms.length];
312: if (caParms != null) {
313: for (int i = 0; i < caParms.length; i++) {
314: caParms[i] = oaParms[i].getClass();
315: }
316: }
317: return instantiate(sClass, caParms, oaParms);
318: }
319:
320: /**
321: * Instantiates an instance of the specified class on the server and places it on the session such that it can be referred to by RemoteReflection instance.
322: * @param sClass The class to instantiate on the server.
323: * @param caParms The class type of the parameters to pass to the method.
324: * @param oaParms The parameters to pass to the method.
325: * @return Object The instantiated object if serializable or RemoteReflection instance refering to it.
326: */
327: public Object instantiate(String sClass, Class[] caParms,
328: Serializable[] oaParms) throws RemoteReflectionException {
329: Object obj = null;
330: if (sClass == null) {
331: throw new RemoteReflectionException("Class Name Required.");
332: }
333: try {
334: URLConnection conn = openConnection(sClass, caParms,
335: oaParms, true);
336: int result = getResponse(conn);
337: if (result == REMOTE_STATUS_OK) {
338:
339: try {
340: ObjectInputStream in = new ObjectInputStream(conn
341: .getInputStream());
342: obj = in.readObject();
343: in.close();
344: if (obj.equals(_rr.getSessionKey())) {
345: obj = _rr;
346: }
347: } catch (EOFException eofe) {
348: ;
349: }
350: _out.close();
351:
352: } else {
353: if (result == REMOTE_STATUS_BAD_REQUEST) {
354: try {
355: ObjectInputStream in = new ObjectInputStream(
356: conn.getInputStream());
357: obj = in.readObject();
358: in.close();
359: if (obj != null && obj instanceof Exception)
360: throw ((Exception) obj);
361: return obj;
362: } catch (EOFException eofe) {
363: ;
364: }
365: }
366: handleError(sClass, result, null);
367: }
368: } catch (Exception ex) {
369: ex.printStackTrace();
370: throw new RemoteReflectionException(ex.toString(), ex);
371: }
372: return obj;
373: }
374:
375: private URLConnection openConnection(String method,
376: Class[] caParms, Object[] oaParms, boolean bInstantiate)
377: throws Exception {
378:
379: String session = _sessionId;
380: String sessionKey = _rr.getSessionKey();
381: if (session == null && sessionKey != null) {
382: int pos = sessionKey.lastIndexOf("-");
383: if (pos > -1)
384: session = sessionKey.substring(pos + 1);
385: }
386:
387: URL u = new URL(_url);
388: URLConnection conn = u.openConnection();
389: conn.setDoInput(true);
390: conn.setDoOutput(true);
391: conn.setUseCaches(false);
392:
393: conn.setRequestProperty("Cookie", "sesessionid=" + session
394: + ";session=" + session + ";sessionid=" + session
395: + ";JSESSIONID=" + session + ";jsessionid=" + session);
396: if (_proxyUserID != null) {
397: String authString = _proxyUserID + ":" + _proxyPw;
398: String auth = "Basic "
399: + new Base64Encoder().encode(authString);
400: conn.setRequestProperty("Proxy-Authorization", auth);
401: }
402:
403: if (_user != null) {
404: String sEncodedUserPassword = null;
405: sEncodedUserPassword = "Basic "
406: + new sun.misc.BASE64Encoder()
407: .encode((_user + ":" + _pass).getBytes());
408: conn.setRequestProperty("Authorization",
409: sEncodedUserPassword);
410: }
411:
412: _out = new ObjectOutputStream(conn.getOutputStream());
413: _out.writeObject(new Boolean(bInstantiate));
414: _out.writeObject(sessionKey);
415: _out.writeObject(method);
416: _out.writeObject(caParms);
417: _out.writeObject(oaParms);
418: return conn;
419: }
420:
421: /**
422: * Returns the RemoteReflection Instance.
423: * @return RemoteReflection The RemoteReflection instance passed in the constructor.
424: */
425: public RemoteReflection getRemoteReflection() {
426: return _rr;
427: }
428:
429: }
|