001: package com.sun.portal.proxylet.client.proxyletcmdlistener;
002:
003: import netscape.javascript.JSObject;
004:
005: import java.net.*;
006: import java.io.*;
007: import java.applet.*;
008:
009: public class ProxyletCommandListenerApplet extends Applet implements
010: Runnable {
011: private ServerSocket serversocket;
012:
013: private final String READ_PROXY_SETTINGS_COMMAND = "read_proxy_settings_command";
014: private final String SET_PROXY_SETTINGS_COMMAND = "set_proxy_settings_command";
015: private final String RESTORE_PROXY_SETTINGS_COMMAND = "restore_proxy_settings_command";
016: private final String GET_PROXY_MODE_COMMAND = "get_proxy_mode_command";
017: private final String GET_PROXY_MODE_INFO_COMMAND = "get_proxy_mode_info_command";
018: private final String GET_AUTO_CONFIG_URL_COMMAND = "get_auto_config_url_command";
019: private final String INVOKE_HELP_COMMAND = "invoke_help_command";
020: private final String DUMMY_RESPONSE = "dummy_response";
021:
022: private JSObject win;
023:
024: private boolean listening = true;
025:
026: public void init() {
027: try {
028: serversocket = new ServerSocket(58084);
029: System.out.println("Server listening on 58084");
030: win = JSObject.getWindow(this );
031: Thread t = new Thread(this );
032: t.start();
033: } catch (Exception e) {
034: e.printStackTrace();
035: }
036: }
037:
038: public void start() {
039:
040: }
041:
042: public void run() {
043: //hide the page containing this applet
044: win.call("hideParentWindow", null);
045:
046: InputStream in = null;
047: OutputStream out = null;
048: Socket connect = null;
049: //Send a message to downloadmgrlite to closeitself
050: try {
051: connect = new Socket("127.0.0.1", 58083);
052: out = connect.getOutputStream();
053: in = connect.getInputStream();
054: String data = "GET closesocket HTTP/1.0";
055: out.write(data.getBytes());
056: out.write("\n\n".getBytes());
057: out.flush();
058:
059: } catch (Exception e) {
060: } finally {
061: try {
062: if (connect != null) {
063: in.close();
064: out.close();
065: connect.close();
066: }
067: } catch (Exception ignore) {
068: }
069: }
070:
071: Socket client = null;
072:
073: while (listening) {
074: try {
075: client = serversocket.accept();
076: processrequest(client);
077: } catch (Exception e) {
078: e.printStackTrace();
079: }
080: }
081:
082: // win.call("closeParentWindow", null);
083: }
084:
085: public void stop() {
086: try {
087: serversocket.close();
088: } catch (Exception e) {
089: }
090: }
091:
092: public void processrequest(Socket client) {
093: System.out.println("processrequest for " + client);
094: try {
095: InputStream inputStream = client.getInputStream();
096: BufferedReader in = new BufferedReader(
097: new InputStreamReader(inputStream));
098:
099: OutputStream outputStream = client.getOutputStream();
100: PrintWriter out = new PrintWriter(outputStream);
101:
102: String line = in.readLine();
103: System.out.println(line);
104:
105: if (line.indexOf("pp1.0") == -1) {
106: System.out.println("Invalid format");
107: return;
108: }
109:
110: String action = line.substring(line.indexOf(":") + 1, line
111: .length());
112: System.out.println("action = " + action);
113:
114: if (action.indexOf(READ_PROXY_SETTINGS_COMMAND) != -1) {
115: win.call("readProxySetting", null);
116: win.call("hideParentWindow", null);
117: out.println(DUMMY_RESPONSE);
118: } else if (action.indexOf(SET_PROXY_SETTINGS_COMMAND) != -1) {
119: String configLocation = action.substring(action
120: .indexOf("?") + 1, action.length());
121: Object arg[] = new Object[1];
122: arg[0] = configLocation;
123: win.call("setProxySetting", arg);
124: win.call("hideParentWindow", null);
125: out.println(DUMMY_RESPONSE);
126: } else if (action.indexOf(RESTORE_PROXY_SETTINGS_COMMAND) != -1) {
127: win.call("resetProxySetting", null);
128: win.call("hideParentWindow", null);
129: out.println(DUMMY_RESPONSE);
130: } else if (action.indexOf(GET_PROXY_MODE_COMMAND) != -1) {
131: Object proxyMode = win.call("getProxyMode", null);
132: out.println(proxyMode.toString());
133: } else if (action.indexOf(GET_PROXY_MODE_INFO_COMMAND) != -1) {
134: Object proxyModeInfo = win.call("getProxyModeInfo",
135: null);
136: out.println(proxyModeInfo.toString());
137: } else if (action.indexOf(GET_AUTO_CONFIG_URL_COMMAND) != -1) {
138: Object autoConfigURL = win.call("getAutoConfigURL",
139: null);
140: out.println(autoConfigURL.toString());
141: } else if (action.indexOf(INVOKE_HELP_COMMAND) != -1) {
142: String helpUrl = action.substring(
143: action.indexOf("?") + 1, action.length());
144: Object arg[] = new Object[1];
145: arg[0] = helpUrl;
146: win.call("invokeHelp", arg);
147: out.println(DUMMY_RESPONSE);
148: } else if (action.indexOf("close") != -1) {
149: win.call("closeParentWindow", null);
150: out.println(DUMMY_RESPONSE);
151: out.flush();
152:
153: listening = false;
154: serversocket.close();
155: }
156:
157: out.flush();
158: in.close();
159: out.close();
160: client.close();
161: } catch (Exception e) {
162: e.printStackTrace();
163: } finally {
164: try {
165: client.close();
166: } catch (Exception e) {
167: }
168: }
169: }
170: }
|