001: package com.sun.portal.proxylet.client.proxyletcmdlistener;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import java.net.InetAddress;
006: import java.net.ServerSocket;
007: import java.net.Socket;
008: import java.util.Enumeration;
009: import java.applet.Applet;
010: import java.io.*;
011:
012: import netscape.javascript.JSObject;
013:
014: /**
015: * This applet runs as a server in the browser to cater to requests from html pages
016: * for loading 'proxyletscript.jar' and 'proxyletCommandListener.html' (which loads
017: * the applet 'ProxyletCommandListenerApplet') files.
018: * This is created for the reason that when an applet and a signed script (as .html file)
019: * reside in the same html page, in different frames, the browser allows applet to call
020: * signed script's those functions which require extra privileges only if they both have
021: * been loaded from the same server. Thus we bundle both 'proxyletscripts.jar' and
022: * 'proxyletCommandListener.html' files in 'proxyletcmdlistener.jar'. When the
023: * browser requests this 'DownloadManagerLite' class for loading both these files, this
024: * class extracts them to a tmp directory and returns them as the response for the 'GET'
025: * request from the browser. Thus, for the browser, both the files are from the same host,
026: * ie, 'localhost' and it allows them to communicate.
027: */
028: public class DownloadManagerLite extends JApplet {
029: private static DownloadManagerLite applet;
030: private String tempDirectory;
031:
032: public void init() {
033: getContentPane().setBackground(Color.white);
034: String temp = this .getParameter("DEBUGLEVEL");
035: cleanupPreviousSessionsDownloadMgrLiteFiles();
036: tempDirectory = createTempDirectory();
037:
038: synchronized (getClass()) {
039: if (applet == null)
040: applet = this ;
041: }
042: }
043:
044: public void start() {
045: try {
046: String version = System.getProperty("java.version");
047: if (version.charAt(2) < '3'
048: || (version.charAt(2) <= '3' && (version.length() == 3 || version
049: .charAt(4) < '1'))) {
050: call("loadJVM", 0);
051: return;
052: }
053: ServerSocket serverSocket = new ServerSocket(58083);
054: (new ServerSocketThread(serverSocket)).start();
055: call("loadsignedscript", 0);
056: call("loadsignedapplet", 0);
057: } catch (Exception e) {
058: e.printStackTrace();
059: return;
060: }
061: }
062:
063: public void stop() {
064: try {
065: super .stop();
066: } catch (Exception e) {
067: e.printStackTrace();
068: }
069: }
070:
071: public void addItem(String newWord) {
072: repaint();
073: }
074:
075: public void paint(Graphics g) {
076: super .paint(g);
077: }
078:
079: /**
080: * callJSFinish
081: * This function invokes a JS function in the downloadmgr page, that
082: * completes the downloadmgr loading.
083: */
084: private void call(String funcName, int localport) {
085: JSObject win;
086:
087: String hostname = null;
088: try {
089: hostname = InetAddress.getLocalHost().getHostAddress();
090: } catch (Exception ignore) {
091: }
092:
093: Object[] arg = { hostname, Integer.toString(localport) };
094:
095: win = getWindow();
096: win.call(funcName, arg);
097: }
098:
099: public JSObject getWindow() {
100: JSObject win = null;
101: String vmversion = System.getProperty("java.version");
102:
103: // get the applets represented by this applet context
104: Enumeration applets = getAppletContext().getApplets();
105:
106: Applet _applet = null;
107: while (applets.hasMoreElements()) {
108: _applet = (Applet) applets.nextElement();
109: }
110:
111: //
112: // This was required in Netscape for Java 1.3.0
113: //
114: if (vmversion.indexOf("1.3.0") != -1)
115: win = JSObject.getWindow(getAppletContext().getApplet(
116: "downapp"));
117: else
118: win = JSObject.getWindow(this );
119:
120: return win;
121: }
122:
123: public String extractResource(String resourceName) throws Exception {
124: ClassLoader c1 = DownloadManagerLite.class.getClassLoader();
125: InputStream is = c1.getResourceAsStream(resourceName);
126: File file = null;
127:
128: if (is != null) {
129: //generate a temporary name for the resource and write to temp file
130: BufferedInputStream istream = new BufferedInputStream(is);
131:
132: file = new File(tempDirectory + resourceName);
133: if (file.exists()) {
134: file.delete();
135: file.createNewFile();
136: }
137:
138: System.out.println("Extracting resource " + resourceName
139: + " to directory " + tempDirectory);
140: file.deleteOnExit();
141: BufferedOutputStream ostream = new BufferedOutputStream(
142: new FileOutputStream(file));
143: int bsize = 2048;
144: int n = 0;
145: byte[] buffer = new byte[bsize];
146: while ((n = istream.read(buffer, 0, bsize)) != -1) {
147: ostream.write(buffer, 0, n);
148: }
149: istream.close();
150: ostream.close();
151: }
152: System.out.println("file Path = " + file.getAbsolutePath());
153: return file.getAbsolutePath();
154: }
155:
156: private String createTempDirectory() {
157: String tmpDirectory = System.getProperty("java.io.tmpdir");
158: boolean fileNotCreated = true;
159:
160: while (fileNotCreated) {
161: String randomSuffix = (new Integer(
162: (int) (Math.random() * 1000))).toString();
163: tmpDirectory = System.getProperty("java.io.tmpdir")
164: + File.separator + "downloadmgrlite_proxylet"
165: + randomSuffix + File.separator;
166: try {
167: File tmpDir = new File(tmpDirectory);
168: if (!tmpDir.exists()) {
169: tmpDir.mkdir();
170: break;
171: }
172: } catch (Exception e) {
173: System.out
174: .println("could not create tmp directory with "
175: + "downloadmgrlite_proxylet suffix.. "
176: + "using default tmp dir.. "
177: + e.getMessage());
178: break;
179: }
180: }
181:
182: return tmpDirectory;
183: }
184:
185: public static void cleanupPreviousSessionsDownloadMgrLiteFiles() {
186: System.out.println("inside Cleanup routine");
187: try {
188: String tempdirectory = System.getProperty("java.io.tmpdir");
189:
190: //get all the downloadmanagerlite related files from previous session..
191: //all these files/directories start with 'downloadmgrlite_proxylet' prefix..
192:
193: File file = new File(tempdirectory);
194: if (file.isDirectory()) {
195: File[] flist = file.listFiles();
196: for (int i = 0; i < flist.length; i++) {
197: File fle = flist[i];
198: if (fle.exists()
199: && fle.getName().toLowerCase().startsWith(
200: "downloadmgrlite_proxylet"))
201: recursivelyCleanupDirectory(fle);
202: }
203: }
204: } catch (Exception ignore) {
205: //ignore
206: }
207: }
208:
209: private static void recursivelyCleanupDirectory(File file) {
210: if (file.isDirectory()) {
211: File[] flist = file.listFiles();
212: for (int i = 0; i < flist.length; i++) {
213: File f = flist[i];
214: if (f.exists())
215: recursivelyCleanupDirectory(f);
216: }
217: }
218:
219: System.out.println("deleting file = " + file.getAbsolutePath());
220: file.delete();
221: }
222:
223: class ServerSocketThread extends Thread {
224: private ServerSocket serverSocket = null;
225:
226: ServerSocketThread(ServerSocket socket) {
227: super ("ServerSocketThread");
228: this .serverSocket = socket;
229: }
230:
231: public void run() {
232: boolean running = true;
233: boolean done = false;
234: String proxyletscriptjarFileLocation = "";
235: String proxyletCommandListenerFileLocation = "";
236: String proxyletCmdListenerclassFileLocation = "";
237: String proxyletCmdListenerjarFileLocation = "";
238:
239: try {
240: while (running) {
241: Socket client = serverSocket.accept();
242:
243: System.out.println("process request for " + client);
244:
245: InputStream inputStream = client.getInputStream();
246: BufferedReader in = new BufferedReader(
247: new InputStreamReader(inputStream));
248:
249: OutputStream outputStream = client
250: .getOutputStream();
251: BufferedOutputStream out = new BufferedOutputStream(
252: outputStream);
253:
254: proxyletscriptjarFileLocation = extractResource("proxyletscript.jar");
255: File proxyletscriptjarFile = new File(
256: proxyletscriptjarFileLocation);
257:
258: if (done == false) {
259: proxyletCommandListenerFileLocation = extractResource("proxyletCommandListener.html");
260:
261: proxyletCmdListenerjarFileLocation = tempDirectory
262: + "com"
263: + File.separator
264: + "sun"
265: + File.separator
266: + "portal"
267: + File.separator
268: + "proxylet"
269: + File.separator
270: + "client"
271: + File.separator
272: + "proxyletcmdlistener";
273: File fmkdirs = new File(
274: proxyletCmdListenerjarFileLocation);
275: if (!fmkdirs.exists()) {
276: System.out
277: .println("Creating directory "
278: + proxyletCmdListenerjarFileLocation);
279: fmkdirs.mkdirs();
280: }
281:
282: proxyletCmdListenerclassFileLocation = extractResource("com/sun/portal/proxylet/client/proxyletcmdlistener/ProxyletCommandListenerApplet.class");
283: done = true;
284: }
285:
286: String line = in.readLine();
287: System.out.println(line);
288:
289: if (line.indexOf("GET") != -1) {
290: if (line.indexOf("proxyletscript.jar") != -1) {
291: out.write("HTTP/1.1 200 OK \n".getBytes());
292: out
293: .write("Content-Type: application/java-archive \n"
294: .getBytes());
295: out.write("\n".getBytes());
296:
297: BufferedInputStream proxyletscriptjarFileIn = new BufferedInputStream(
298: new FileInputStream(
299: proxyletscriptjarFile));
300: byte[] buffer = new byte[4096];
301: int bytesRead;
302: while ((bytesRead = proxyletscriptjarFileIn
303: .read(buffer)) != -1)
304: out.write(buffer, 0, bytesRead); // write
305:
306: out.flush();
307: } else if (line
308: .indexOf("proxyletcommandlistener") != -1) {
309: PrintWriter pWriter = new PrintWriter(out);
310: pWriter.println("HTTP/1.1 200 OK");
311: pWriter.println("Content-Type: text/html");
312: pWriter.println();
313:
314: FileReader fileReader = new FileReader(
315: proxyletCommandListenerFileLocation);
316: BufferedReader reader = new BufferedReader(
317: fileReader);
318: String fileLine = "";
319: while ((fileLine = reader.readLine()) != null)
320: pWriter.println(fileLine);
321:
322: pWriter.flush();
323: pWriter.close();
324: } else if (line
325: .indexOf("ProxyletCommandListenerApplet") != -1) {
326: out.write("HTTP/1.1 200 OK \n".getBytes());
327: out
328: .write("Content-Type: application/java-class \n"
329: .getBytes());
330: out.write("\n".getBytes());
331:
332: BufferedInputStream proxyletscriptjarFileIn = new BufferedInputStream(
333: new FileInputStream(
334: proxyletCmdListenerclassFileLocation));
335: byte[] buffer = new byte[4096];
336: int bytesRead;
337: while ((bytesRead = proxyletscriptjarFileIn
338: .read(buffer)) != -1)
339: out.write(buffer, 0, bytesRead); // write
340:
341: out.flush();
342: } else if (line.indexOf("closesocket") != -1) {
343: try {
344: System.out
345: .println("Closing downloadmgr lite..");
346: serverSocket.close();
347: } catch (Exception e) {
348: }
349: }
350: }
351:
352: in.close();
353: out.close();
354: client.close();
355: }
356: } catch (Exception e) {
357: e.printStackTrace();
358: }
359: }
360: }
361: }
|