001: package org.objectweb.salome_tmf.api;
002:
003: import java.applet.Applet;
004: import java.lang.reflect.Method;
005:
006: import javax.swing.JApplet;
007:
008: public class JavaScriptUtils {
009:
010: final String SALOME_TMF = "salome_tmf";
011: final String SALOME_TMF_MD5 = "salome_tmf_md5";
012:
013: String strProject = null;
014: String strLogin = null;
015: String strMD5Password = null;
016: JApplet parentApplet;
017: boolean cookiesLoaded = false;
018: boolean MD5CookiesLoaded = false;
019:
020: public JavaScriptUtils(JApplet parentApplet) {
021: this .parentApplet = parentApplet;
022:
023: Api.setStrUsername(getLoginCookies());
024: Api.setStrMD5Password(getMD5PasswordCookies());
025: }
026:
027: private void loadingCookies() {
028: try {
029: String cookiesValue = getCookie(SALOME_TMF);
030: Util
031: .log("[SalomeTMF_AppJWS->loadingFromCookies] Cookie salome_tmf value is "
032: + cookiesValue);
033:
034: if (cookiesValue != null && !cookiesValue.equals("")) {
035: int indexDebut = cookiesValue.indexOf("%2C");
036: if (indexDebut > 0) {
037: strProject = cookiesValue.substring(0, indexDebut);
038: strLogin = cookiesValue.substring(indexDebut + 3,
039: cookiesValue.length());
040: }
041: if (strProject == null) {
042: indexDebut = cookiesValue.indexOf("=");
043: if (indexDebut > 0) {
044: strProject = cookiesValue.substring(0,
045: indexDebut);
046: strLogin = cookiesValue.substring(
047: indexDebut + 1, cookiesValue.length());
048: }
049: }
050: }
051: System.out.println("Cookies read is strProject = "
052: + strProject + ", strLogin = " + strLogin);
053: } catch (Exception e) {
054: strProject = null;
055: strLogin = null;
056: e.printStackTrace();
057: }
058: cookiesLoaded = true;
059: }
060:
061: private String getCookie(String name) {
062: /*
063: ** get a specific cookie by its name, parse the cookie.
064: */
065: String myCookie = getCookie();
066: String search = name + "=";
067: if (myCookie.length() > 0) {
068: int offset = myCookie.indexOf(search);
069: if (offset != -1) {
070: offset += search.length();
071: int end = myCookie.indexOf(";", offset);
072: if (end == -1)
073: end = myCookie.length();
074: return myCookie.substring(offset, end);
075: } else {
076: System.out.println("-----------> Did not find cookie: "
077: + name);
078: }
079: }
080: return "";
081: }
082:
083: private String getCookie() {
084: /*
085: ** get all cookies for a document
086: */
087: try {
088:
089: Class classJSObject = Class
090: .forName("netscape.javascript.JSObject");
091:
092: Method meth_getWindow = classJSObject.getMethod(
093: "getWindow", new Class[] { Applet.class });
094: Object myBrowser = meth_getWindow.invoke(null,
095: new Object[] { parentApplet });
096:
097: Method meth_getMember = classJSObject.getMethod(
098: "getMember", new Class[] { String.class });
099: Object myDocument = meth_getMember.invoke(myBrowser,
100: new Object[] { "document" });
101:
102: String myCookie = (String) meth_getMember.invoke(
103: myDocument, new Object[] { "cookie" });
104: System.out.println("------------> getCookie = " + myCookie);
105: /*
106: JSObject myBrowser = (JSObject) JSObject.getWindow(parentApplet);
107: JSObject myDocument = (JSObject) myBrowser.getMember("document");
108: String myCookie = (String)myDocument.getMember("cookie");
109: */
110: if (myCookie.length() > 0)
111: return myCookie;
112: } catch (Exception e) {
113: e.printStackTrace();
114: }
115: return "?";
116: }
117:
118: /********************* Public *******************/
119: public void closeWindow() {
120: callWindwowsMethode("close", null);
121: //System.out.println(callWindwowsMethode("confirm",new Object[] {"Confirmer"}));
122: //confirm('Chaîne de caractères');
123: }
124:
125: public Object callWindwowsMethode(String meth, Object[] arg) {
126: Object ret = null;
127: try {
128: //Class classJSObject = Class.forName("sun.plugin.javascript.JSObject");
129: Class classJSObject = Class
130: .forName("netscape.javascript.JSObject");
131: Method meth_getWindow = classJSObject.getMethod(
132: "getWindow", new Class[] { Applet.class });
133: Object myBrowser = meth_getWindow.invoke(null,
134: new Object[] { parentApplet });
135: //System.out.println("pJSObject = " + pJSObject);
136: Method meth_call = classJSObject.getMethod("call",
137: new Class[] { String.class, Object[].class });
138: ret = meth_call.invoke(myBrowser,
139: new Object[] { meth, arg });
140: /*Method[] tabMeth = classJSObject.getMethods();
141: for (int i=0 ; i < tabMeth.length; i++){
142: Method pMethod = tabMeth[i];
143: System.out.println("Méthode disponible = " + pMethod);
144: }*/
145: } catch (Exception e) {
146: e.printStackTrace();
147: }
148: return ret;
149: }
150:
151: public void setCookies(String project, String login,
152: String MD5Password) throws Exception {
153: setCookies(SALOME_TMF, project + "=" + login);
154: setCookies(SALOME_TMF_MD5, MD5paswd
155: .getEncodedPassword(MD5Password));
156: }
157:
158: public void setCookies(String name, String value) throws Exception {
159: // String name = SALOME_TMF;
160: // String value = project+ "=" + login;
161:
162: Class classJSObject = Class
163: .forName("netscape.javascript.JSObject");
164: Method meth_getWindow = classJSObject.getMethod("getWindow",
165: new Class[] { Applet.class });
166: Object myBrowser = meth_getWindow.invoke(null,
167: new Object[] { parentApplet });
168:
169: Method meth_getMember = classJSObject.getMethod("getMember",
170: new Class[] { String.class });
171: Object myDocument = meth_getMember.invoke(myBrowser,
172: new Object[] { "document" });
173:
174: if (myDocument == null) {
175: System.out
176: .println("Can't set cookie: no document reference");
177: throw new Exception(
178: "Can't set cookie: no document reference");
179: }
180:
181: try {
182:
183: String cookieText = name + "=" + value;
184: System.out.println("Setting document.cookie = \""
185: + cookieText + '"');
186: Method meth_setMember = classJSObject.getMethod(
187: "setMember", new Class[] { String.class,
188: Object.class });
189: meth_setMember.invoke(myDocument, new Object[] { "cookie",
190: cookieText });
191: } catch (Exception ex) {
192: ex.printStackTrace();
193: }
194:
195: }
196:
197: public String getLoginCookies() {
198: if (cookiesLoaded == false) {
199: loadingCookies();
200: }
201: return strLogin;
202: }
203:
204: public String getProjectCookies() {
205: if (cookiesLoaded == false) {
206: loadingCookies();
207: }
208: return strProject;
209: }
210:
211: public String getMD5PasswordCookies() {
212: if (!MD5CookiesLoaded) {
213: strMD5Password = getCookie(SALOME_TMF_MD5);
214: MD5CookiesLoaded = true;
215: }
216: return strMD5Password;
217: }
218:
219: }
|