001: package com.sun.portal.search.admin.mbeans.tasks;
002:
003: import java.io.*;
004: import java.net.*;
005: import java.util.*;
006: import java.util.logging.*;
007:
008: import com.sun.portal.util.Platform;
009:
010: public class FilterSimulator {
011:
012: class HostResult {
013:
014: String h_cname;
015: int h_type;
016: int h_errno;
017: int h_length;
018: String[] h_ips;
019: String[] h_aliases;
020: String org_url;
021:
022: public HostResult(String url, int code, String host,
023: int length, int type, String[] ps, String[] aliases) {
024: org_url = url;
025: h_cname = host;
026: h_length = length;
027: h_type = type;
028: h_ips = ps;
029: h_aliases = aliases;
030: h_errno = code;
031: }
032: }
033:
034: class RedirectResult {
035: String redirect_url;
036: String server_type;
037: int content_length;
038: int return_code;
039:
040: public RedirectResult(int code, String host, String type,
041: int length) {
042: redirect_url = host;
043: server_type = type;
044: content_length = length;
045: return_code = code;
046: }
047: }
048:
049: String trim_fr(String s) {
050: int h;
051: int i;
052: s = s.trim();
053: if (s.startsWith("<URL:")) {
054: String host = s.substring(5, s.length() - 1); // the closing >
055: s = host;
056: } else if ((h = s.indexOf("<URL:")) > 0) {
057: String host = s.substring(0, h - 1) + " "
058: + s.substring(h + 5, s.length() - 1);
059: s = host;
060: } else if ((i = s.indexOf(" -- ")) > 0) {
061: s = s.substring(0, i);
062: }
063: return s;
064: }
065:
066: int getInt(BufferedReader in) throws Exception {
067: String s = in.readLine();
068: try {
069: return Integer.parseInt(s);
070: } catch (Exception e) {
071: return -1;
072: }
073: }
074:
075: String[] increaseArray(String[] src) {
076: if (src == null)
077: return new String[10];
078: String[] tmp = new String[src.length + 10];
079: System.arraycopy(src, 0, tmp, 0, src.length);
080: return tmp;
081: }
082:
083: public FilterSimulator() {
084:
085: }
086:
087: public HashMap runProb(boolean isSmartHostHeuristics, String root,
088: boolean showDNS, String url) throws Exception {
089: HashMap result = new HashMap();
090: URL urlo;
091: String host = "", protocol = "", uri = "";
092: int port = 0;
093:
094: try {
095: urlo = new URL(url);
096: host = urlo.getHost();
097: protocol = urlo.getProtocol();
098: uri = urlo.getFile();
099: port = urlo.getPort();
100: } catch (Exception e) {
101: //malformed url
102: result.put("URLMalformed", "");
103: return result;
104: //throw new Exception(e);
105: }
106:
107: if (port < 1)
108: port = 80;
109:
110: HostResult hr = doGetHostByName(root, host);
111:
112: LinkedList host_result = new LinkedList();
113: host_result.addLast(hr.h_cname);
114: host_result.addLast(new Integer(hr.h_type));
115: host_result.addLast(new Integer(hr.h_errno));
116: host_result.addLast(new Integer(hr.h_length));
117: host_result.addLast(hr.h_ips);
118: host_result.addLast(hr.h_aliases);
119: host_result.addLast(hr.org_url);
120:
121: result.put("HostResult", host_result);
122:
123: if (!protocol.startsWith("http"))
124: return result;
125:
126: RedirectResult r = doRedirect(root, false, url);
127:
128: LinkedList r_result = new LinkedList();
129: r_result.addLast(r.redirect_url);
130: r_result.addLast(r.server_type);
131: r_result.addLast(new Integer(r.content_length));
132: r_result.addLast(new Integer(r.return_code));
133:
134: result.put("RedirectResult", r_result);
135:
136: if (r.return_code >= 0) {
137:
138: RedirectResult r1 = doRedirect(root, true, url);
139:
140: LinkedList r1_result = new LinkedList();
141: r1_result.addLast(r1.redirect_url);
142: r1_result.addLast(r1.server_type);
143: r1_result.addLast(new Integer(r1.content_length));
144: r1_result.addLast(new Integer(r1.return_code));
145:
146: result.put("RedirectResult1", r1_result);
147:
148: String url2 = protocol + "://" + hr.h_cname + ":" + port
149: + uri;
150: RedirectResult r2 = doRedirect(root, true, url2);
151:
152: LinkedList r2_result = new LinkedList();
153: r2_result.addLast(r2.redirect_url);
154: r2_result.addLast(r2.server_type);
155: r2_result.addLast(new Integer(r2.content_length));
156: r2_result.addLast(new Integer(r2.return_code));
157:
158: result.put("RedirectResult2", r2_result);
159:
160: result.put("RedirectResult2URL", url2);
161:
162: } else {
163: result.put("RedirectResult1", null);
164: result.put("RedirectResult2", null);
165: }
166:
167: return result;
168:
169: }
170:
171: /*
172: public void getDomainByName(String root, String host) throws Exception
173: {
174: HostResult r = doGetHostByName(root, host);
175: switch(r.h_errno) {
176: case 0:
177: throw new Exception(getI18n("simulator.sameashostname"));
178: case 4: // Valid name, no data record of requested type
179: break;
180: case 1:
181: case 2:
182: case 3:
183: throw new Exception(getI18n("simulator.namenotfound"));
184: default:
185: throw new Exception(getI18n("simulator.unknownerror"));
186: }
187: return;
188: }
189: */
190:
191: public String getHostByName(String root, String host)
192: throws Exception {
193: HostResult r = doGetHostByName(root, host);
194: if (r.h_errno != 0) {
195: if (r.h_errno > 0 && r.h_errno < h_errmsg.length)
196: throw new Exception(h_errmsg[r.h_errno]);
197: else
198: throw new Exception("simulator.unknownerror");
199: }
200: return r.h_cname;
201: }
202:
203: public HostResult doGetHostByName(String root, String host)
204: throws Exception {
205: String runCsCli = null;
206: runCsCli = Platform.getCommand("run-cs-cli");
207: String cmd = root + File.separator + runCsCli + " hostinfo "
208: + host;
209: Process proc = Runtime.getRuntime().exec(cmd);
210: BufferedReader in = new BufferedReader(new InputStreamReader(
211: proc.getInputStream()));
212:
213: String cname = null;
214: String[] aliases = null;
215: String[] ps = null;
216: int h_type = 0, h_len = 0;
217: int code = -1;
218: String s;
219: boolean is_ok = false;
220:
221: // always return a code
222: code = getInt(in);
223: if (code != 0) {
224: if (code == 4)
225: cname = host;
226: while ((s = in.readLine()) != null) {
227: }
228: in.close();
229: return new HostResult(host, code, cname, 0, 0, null, null);
230: }
231:
232: // then cname, type, len
233: cname = in.readLine();
234: h_type = getInt(in);
235: h_len = getInt(in);
236:
237: // now the ps if any
238: int count = 0;
239: int len = 0;
240: while ((s = in.readLine()) != null) {
241: if (s.equalsIgnoreCase("ALIASES"))
242: break;
243:
244: if (len < ++count) { // need more space
245: ps = increaseArray(ps);
246: len = ps.length;
247: }
248: ps[count - 1] = s;
249: }
250: if (s == null)
251: return new HostResult(host, code, cname, h_type, h_len, ps,
252: null);
253:
254: count = 0;
255: len = 0;
256: while ((s = in.readLine()) != null) {
257: if (len < ++count) { // need more space
258: aliases = increaseArray(aliases);
259: len = aliases.length;
260: }
261: aliases[count - 1] = s;
262: }
263:
264: return new HostResult(host, code, cname, h_type, h_len, ps,
265: aliases);
266: }
267:
268: public RedirectResult doRedirect(String root,
269: boolean is_visual_server, String url) throws Exception {
270: String runCsCli = null;
271: runCsCli = Platform.getCommand("run-cs-cli");
272: String cmd = root + File.separator + runCsCli + " ufetch ";
273: if (!is_visual_server) // plus header
274: cmd += "-d ";
275: cmd += url;
276:
277: Process proc = Runtime.getRuntime().exec(cmd);
278: BufferedReader in = new BufferedReader(new InputStreamReader(
279: proc.getInputStream()));
280: String s;
281: String host = null, type = null;
282: int comp_len = 0, len = 0;
283: boolean is_ok = false;
284: boolean isredirect = false;
285:
286: while ((s = in.readLine()) != null) {
287: is_ok = true;
288:
289: if (!is_visual_server) {
290: // ufetch return head last
291: if (s.length() > 7
292: && s.substring(0, 7)
293: .equalsIgnoreCase("Server:")) {
294: type = s.substring(7).trim();
295: }
296: if (s.length() > 15
297: && s.substring(0, 15).equalsIgnoreCase(
298: "Content-length:")) {
299: try {
300: len = Integer.parseInt(s.substring(7).trim());
301: } catch (Exception e) {
302: }
303: }
304: if (s.length() > 9
305: && s.substring(0, 9).equalsIgnoreCase(
306: "Location:")) {
307: host = s.substring(9).trim();
308: if (!(host.equals(url + "/") || (url.equals(host
309: + "/"))))
310: isredirect = true;
311: }
312: }
313: comp_len += s.length();
314: }
315: in.close();
316: if (len == 0)
317: len = comp_len;
318: if (!is_ok)
319: return new RedirectResult(-1, "", "", len);
320: else if (isredirect)
321: return new RedirectResult(1, host, type, len);
322: else
323: return new RedirectResult(0, null, type, len);
324: }
325:
326: public HashMap runSimu(boolean isSmartHostHeuristics, String root,
327: String url, boolean do_dns, boolean do_redirect)
328: throws Exception {
329: HashMap result_map = new HashMap();
330:
331: String runCsCli = null;
332: runCsCli = Platform.getCommand("run-cs-cli");
333: String cmd = root + File.separator + runCsCli + " frsim -l "
334: + root + File.separator + "config" + File.separator
335: + "filterrules.conf";
336:
337: Process proc;
338: BufferedReader in;
339: PrintWriter out;
340: String s;
341: String result = "";
342: boolean is_ok = false;
343:
344: result_map = runProb(isSmartHostHeuristics, root, do_dns, url);
345:
346: proc = Runtime.getRuntime().exec(cmd);
347: in = new BufferedReader(new InputStreamReader(proc
348: .getInputStream()));
349: out = new PrintWriter(proc.getOutputStream());
350: out.println(url); //input ur
351: out.flush();
352: out.close();
353:
354: while ((s = in.readLine()) != null) {
355: result += trim_fr(s);
356: result += "\n";
357: }
358: in.close();
359:
360: result_map.put("frsimResult", result);
361:
362: return result_map;
363: }
364:
365: static public boolean isWindows() {
366: if (System.getProperty("os.name").startsWith("windows")) {
367: return true;
368: } else {
369: return false;
370: }
371: }
372:
373: static String h_errmsg[] = { "robot.simulator.successdns",
374: "robot.simulator.namenotfound",
375: "robot.simulator.dnsserverfail",
376: "robot.simulator.norecovery",
377: "robot.simulator.dns.noaddress",
378: "robot.simulator.unknownerror" };
379:
380: }
|