/Users/lyon/j4p/src/net/server/sendmail/SearchThread.java
|
1 package net.server.sendmail;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.Vector;
9
10 public class SearchThread extends Thread {
11
12 String m_sSenderEmail = "lyon@docjava.com";
13
14 String m_sServerHostName = "www.docjava.com";
15 String m_sRecipientEmail = null;
16 String m_sSearchString = null;
17
18 Thread searchThread;
19
20
21 /**************************************************************
22 * SearchThread Constructor
23 * @param search - the string containing the search criteria
24 * @param email - the recipient email address
25 * Sets the search string and email address properties and
26 * starts a new thread
27 **************************************************************/
28 SearchThread(String search, String email) {
29 System.out.println("\nCreated new SearchThred;");
30 this.m_sSearchString = search;
31 this.m_sRecipientEmail = email;
32
33 //-- Create and start the thread
34 searchThread = new Thread(this);
35 searchThread.start();
36 }
37
38
39 /**************************************************************
40 * Overidden run() method
41 * This contains the code we want to run inside the new thread
42 **************************************************************/
43 public void run() {
44 System.out.println("SearchThread started.");
45 String msg = searchAllTheWeb(m_sSearchString);
46 msg = msg +
47 "<x-html><!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html>"
48 + "<head><title>DocJavaSearch results!</title></head>";
49
50 email(msg);
51 }
52
53
54 /**************************************************************
55 * Method getSearch
56 * @param s - the string containing the search criteria
57 * @return String - the String containing the search results
58 * Queries each search engine, passing in the query string
59 * Acccessor method used only to return the search results
60 * Not used in normal search process
61 **************************************************************/
62 public String getSearch(String s) {
63 return searchAllTheWeb(s);
64 }// end method
65
66
67 /**************************************************************
68 * Method searchAllTheWeb
69 * @param search - the string containing the search criteria
70 * @return String - the String containing the search results
71 * Queries each search engine, passing in the query string
72 **************************************************************/
73 public String searchAllTheWeb(String search) {
74
75 //-- alltheweb.com
76 String s0 = ("http://www.alltheweb.com"
77 + "/cgi-bin/search?exec=FAST+Search&type=all&"
78 + "query=" + search);
79
80 //-- go.com
81 String s1 = ("http://www.go.com/Split?pat=go&col=WW&qt="
82 + search);
83
84 //-- google.com
85 String s2 = ("http://google.com/search?q="
86 + search);
87
88 //-- northernlight.com
89 String s3 = ("http://www.northernlight.com/"
90 + "nlquery.fcg?cb=0&qr="
91 + search + "&search.x=28&search.y=8");
92
93 //-- webcrawler.com
94 String s4 = ("http://www.webcrawler.com/"
95 + "cgi-bin/WebQuery?searchText=" + search);
96
97 return getUrlResult(s0) +
98 getUrlResult(s1) +
99 getUrlResult(s2) +
100 getUrlResult(s3) +
101 getUrlResult(s4);
102
103 }// end method
104
105
106 /**************************************************************
107 * Prints out the message to command line
108 **************************************************************/
109 public void print(String s) {
110 System.out.println(s);
111
112 }// end method
113
114
115 /**************************************************************
116 * Invokes the email applet and sends required info to
117 * send the email
118 **************************************************************/
119 public void email(String msg) {
120 //System.out.println ("\n*** Sending email info to email applet.");
121
122 //System.out.println ("ServerHostName = " + m_sServerHostName);
123 //System.out.println ("Recipient Email = " + m_sRecipientEmail);
124 //System.out.println ("Sender Email = " + m_sSenderEmail);
125 //System.out.println ("Message = " + m_sMessage);
126
127 net.server.sendmail.SendMailApplet mail = new net.server.sendmail.SendMailApplet();
128
129 mail.setSenderEmail(m_sSenderEmail);
130 mail.setRecipientEmail(m_sRecipientEmail);
131 mail.setMailServerHostName(m_sServerHostName);
132 mail.setMessage(msg);
133
134 try {
135 mail.send();
136 } catch (IOException e) {
137 System.err.println(
138 "Error sending email:\n"
139 + e.getMessage());
140 }
141
142 }// end method
143
144
145
146 /**************************************************************
147 * Method getUrlResult
148 * Queries a website with the url provided and returns the results
149 * @param search - the string representing the search query
150 * for a particular search engine
151 **************************************************************/
152 // sendmail.SearchThread.getUrlResult(searchString);
153 public static String getUrlResult(String search) {
154 System.out.println("*** Getting search results...");
155
156 try {
157 URL url = new URL(search);
158 String nextLine;
159 Vector v = new Vector();
160
161 BufferedReader br = new BufferedReader(
162 new InputStreamReader(
163 url.openStream()));
164 while ((nextLine = br.readLine()) != null)
165 v.addElement(nextLine);
166
167 String s[] = new String[v.size()];
168
169 for (int i = 0; i < s.length; i++)
170 s[i] = (String) v.elementAt(i);
171
172
173 String w = "";
174 for (int i = 0; i < s.length; i++)
175 w = w + s[i] + "\n";
176
177 return w;
178
179 } catch (MalformedURLException e) {
180 System.out.println(e.getMessage());
181
182 } catch (IOException e) {
183 System.out.println(e.getMessage());
184 }
185
186 return null;
187
188 }// end method
189
190
191 }// end class doWebSearch
192