Child: [r23] (diff)

Download this file

internet.java    310 lines (251 with data), 10.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package utils;
import GUI.MetaContainer;
import definitions.is;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import org.simpleframework.http.Request;
/**
*
* @author Nuno Brito, 10th August 2010 in Coimbra, Portugal.
*/
public class internet {
static internet action = new internet();
String[] test_connectivity = {
"http://google.com",
"http://cnn.com",
"http://digg.com",
"http://wordpress.com",
"http://www.w3.org",
"http://yahoo.com",
};
Boolean debug = true;
//checks for connection to the internet through dummy request
public static String getTextFile(String TextFileURL)
{
String text_out = "";
try {
//make a valid URL to get ninja.txt
URL url = new URL(TextFileURL);
//open a connection to that source
HttpURLConnection urlConnect =
(HttpURLConnection)url.openConnection();
// ensure that there is no cache to give old outdated files
urlConnect.setDoInput(true);
urlConnect.setUseCaches(false);
//urlConnect.setConnectTimeout(1000);
//urlConnect.setDoInput(false);
//urlConnect.setDoOutput(false);
DataInputStream dis;
String s;
dis = new DataInputStream(urlConnect.getInputStream());
while ((s = dis.readLine()) != null)
{
//text_out = text_out +"\r\n"+s;
if(s.length() == 0)
continue;
text_out = text_out.concat(s + "\n");
}
dis.close();
} catch (UnknownHostException e) {
// e.printStackTrace();
return "";
}
catch (IOException e) {
// e.printStackTrace();
return "";
}
return text_out;
}
/** sends email using an external PHP script, useful to bypass proxies */
public static String sendEmail(String emailAddress,
String subject, String body){
String website = "http://nunobrito.eu/email/egos.php"
+ "?address=" + emailAddress
+ "&subject=" + subject
+ "&body=" + body
;
String result = webget(website);
//String result = webget("http://google.com");
System.out.println(website);
return result;
}
/**
* <b>Bare Bones Browser Launch for Java</b><br>
* Utility class to open a web page from a Swing application
* in the user's default browser.<br>
* Supports: Mac OS X, GNU/Linux, Unix, Windows XP/Vista/7<br>
* Example Usage:<code><br> &nbsp; &nbsp;
* String url = "http://www.google.com/";<br> &nbsp; &nbsp;
* BareBonesBrowserLaunch.openURL(url);<br></code>
* Latest Version: <a href="http://www.centerkey.com/java/browser/">www.centerkey.com/java/browser</a><br>
* Author: Dem Pilafian<br>
* Public Domain Software -- Free to Use as You Like
* @version 3.0, February 7, 2010
*/
static final String[] browsers = { "google-chrome", "firefox", "opera",
"konqueror", "epiphany", "seamonkey", "galeon", "kazehakase", "mozilla" };
static final String errMsg = "Error attempting to launch web browser";
/**
* Opens the specified web page in the user's default browser
* @param url A web address (URL) of a web page (ex: "http://www.google.com/")
*/
public static void openURL(String url) {
try { //attempt to use Desktop library from JDK 1.6+ (even if on 1.5)
Class<?> d = Class.forName("java.awt.Desktop");
d.getDeclaredMethod("browse", new Class[] {java.net.URI.class}).invoke(
d.getDeclaredMethod("getDesktop").invoke(null),
new Object[] {java.net.URI.create(url)});
//above code mimics:
// java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (Exception ignore) { //library not available or failed
String osName = System.getProperty("os.name");
try {
if (osName.startsWith("Mac OS")) {
Class.forName("com.apple.eio.FileManager").getDeclaredMethod(
"openURL", new Class[] {String.class}).invoke(null,
new Object[] {url});
}
else if (osName.startsWith("Windows"))
Runtime.getRuntime().exec(
"rundll32 url.dll,FileProtocolHandler " + url);
else { //assume Unix or Linux
boolean found = false;
for (String browser : browsers)
if (!found) {
found = Runtime.getRuntime().exec(
new String[] {"which", browser}).waitFor() == 0;
if (found)
Runtime.getRuntime().exec(new String[] {browser, url});
}
if (!found)
throw new Exception(Arrays.toString(browsers));
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, errMsg + "\n" + e.toString());
}
}
}
/** Get the login status */
static public boolean isLogged(final String URL){
// add our specific URL for the component and required parameter
String target = URL + "/user?action=isLogged";
// get the file
String result = utils.internet.getTextFile(target);
// output the result
boolean out = result.equalsIgnoreCase("true");
return out;
}
/** get a given page from a given URL address */
static public String webget(String address) {
// perhaps in the future we can use something like http://goo.gl/03WQp
// provide a holder for the reply
String result = "";
try {
URL webpage = new URL(address);
BufferedReader in = new BufferedReader(
new InputStreamReader(webpage.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
// Process each line.
result = result.concat(inputLine);
}
in.close();
} catch (MalformedURLException me) {
// System.err.println("webGet operation has failed, MalFormedException: "
// + me.toString());
return me.toString();
} catch (IOException ioe) {
// System.err.println("webGet operation has failed, IOException: "
// + ioe.toString());
return ioe.toString();
}
return result;
}
private void log(String gender, String message){
System.out.println
("internet ["+gender+"] "+message);
}
static private void debug(String message){
if(action.debug)
action.log("debug",message);
}
/** Do a threaded webget to prevent congestions.
* This method is not working as originally intended but at least is still
* useful for making requests that do not require a reply back.
*/
public static void threadedWebGet(String URL){
// start a new thread for this purpose
WebGetThread a = new WebGetThread(URL);
a.start();
}
// derived from http://stackoverflow.com/questions/8765578/get-local-ip-address-without-connecting-to-the-internet
static public String getLocalIP(){
String result = "";
try{
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()){
NetworkInterface current = interfaces.nextElement();
//System.out.println(current);
if (!current.isUp() || current.isLoopback() || current.isVirtual())
continue;
Enumeration<InetAddress> addresses = current.getInetAddresses();
while (addresses.hasMoreElements()){
InetAddress current_addr = addresses.nextElement();
if (current_addr.isLoopbackAddress())
continue;
if (current_addr instanceof Inet4Address)
result = result.concat(current_addr.getHostAddress() + "\n");
//else if (current_addr instanceof Inet6Address)
// System.out.println(current_addr.getHostAddress());
//System.out.println(current_addr.getHostAddress());
}
}
}catch (Exception e){
}
return result;
}
/** Get the value from an HTML provided on command line
* @param request the request from our web server
* @param parameter the key title for the value that we need acquire
* @return a simple string with the value for the requested key
*/
public static String getHTMLparameter(Request request, String parameter){
String result = "";
try { // should we show a specific section?
result = request.getParameter(parameter);
} catch (IOException ex) {
}
if(result == null)
result = ""; // we can't afford null values,
// clean up the result
result = java.net.URLDecoder.decode(result);
return result;
}
}
class WebGetThread extends Thread{
private String URL = ""; // the target URL address
/** Public constructor */
public WebGetThread(String URL){
this.URL = URL;
}
@Override
public void run(){
// get the page onto a string
utils.internet.getTextFile(URL);
}
}