WebUtil improvements

This commit is contained in:
Olof Larsson 2015-09-11 13:29:01 +02:00
parent 0ab5ac4c84
commit 92d1501fa5
2 changed files with 115 additions and 25 deletions

View File

@ -67,7 +67,7 @@ public class CmdMassiveCoreCmdurl extends MassiveCommand
{ {
try try
{ {
final List<String> lines = WebUtil.getUrlLines(url, 5000); final List<String> lines = WebUtil.getLines(url);
sync(new Runnable() sync(new Runnable()
{ {
@Override @Override

View File

@ -7,24 +7,42 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class WebUtil public class WebUtil
{ {
// -------------------------------------------- // // -------------------------------------------- //
// ASYNC GET TOUCH // CONSTANTS
// -------------------------------------------- // // -------------------------------------------- //
private static final ExecutorService executor = Executors.newSingleThreadExecutor(); public final static String USER_AGENT = "MassiveCore/1.2.3";
public final static int CONNECT_TIMEOUT = 10000;
public static void asyncGetTouch(String url) // -------------------------------------------- //
// TOUCH
// -------------------------------------------- //
private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
public static void touch(String url, boolean synchronous)
{ {
try try
{ {
asyncGetTouch(new URL(url)); touch(new URL(url), synchronous);
} }
catch (MalformedURLException e) catch (MalformedURLException e)
{ {
@ -32,26 +50,41 @@ public class WebUtil
} }
} }
public static void asyncGetTouch(final URL url) public static void touch(final URL url, boolean synchronous)
{ {
executor.execute(new Runnable() if (synchronous)
{
touch(url);
}
else
{
EXECUTOR.execute(new Runnable()
{ {
@Override @Override
public void run() public void run()
{ {
touch(url);
}
});
}
}
protected static void touch(final URL url)
{
HttpURLConnection uc = null;
try try
{ {
InputStream inputStream = null; uc = openHttpUrlConnection(url);
uc.connect();
inputStream = url.openStream();
inputStream.close();
} }
catch (IOException e) catch (IOException e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
finally
{
try { uc.disconnect(); } catch (Exception ignored) {}
} }
});
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -59,7 +92,7 @@ public class WebUtil
// -------------------------------------------- // // -------------------------------------------- //
// This one should be run async since it's very slow. // This one should be run async since it's very slow.
public static List<String> getUrlLines(URL url, int timeoutMillis) throws IOException public static List<String> getLines(URL url) throws IOException
{ {
HttpURLConnection uc = null; HttpURLConnection uc = null;
InputStream is = null; InputStream is = null;
@ -68,10 +101,7 @@ public class WebUtil
try try
{ {
uc = (HttpURLConnection) url.openConnection(); uc = openHttpUrlConnection(url);
uc.setConnectTimeout(timeoutMillis);
uc.setRequestMethod("GET");
uc.addRequestProperty("User-Agent", "Mozilla/4.76");
uc.connect(); uc.connect();
is = uc.getInputStream(); is = uc.getInputStream();
@ -96,5 +126,65 @@ public class WebUtil
} }
} }
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static HttpURLConnection openHttpUrlConnection(URL url) throws IOException
{
// Create Ret
HttpURLConnection ret = (HttpURLConnection) url.openConnection();
// Fill Ret
trustConnection(ret);
ret.setConnectTimeout(CONNECT_TIMEOUT);
ret.setRequestMethod("GET");
ret.addRequestProperty("User-Agent", USER_AGENT);
// Return Ret
return ret;
}
// -------------------------------------------- //
// SSL TRUST
// -------------------------------------------- //
public static final HostnameVerifier TRUSTING_HOSTNAME_VERIFIER = new HostnameVerifier()
{
@Override public boolean verify(String hostname, SSLSession session) { return true; }
};
public static final X509TrustManager TRUSTING_TRUST_MANAGER = new X509TrustManager()
{
@Override public X509Certificate[] getAcceptedIssuers() { return null; }
@Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
@Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
};
public static final SSLSocketFactory TRUSTING_SOCKET_FACTORY;
static
{
SSLSocketFactory socketFactory = null;
try
{
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[]{ TRUSTING_TRUST_MANAGER }, null);
socketFactory = ctx.getSocketFactory();
}
catch (Exception e)
{
e.printStackTrace();
}
TRUSTING_SOCKET_FACTORY = socketFactory;
}
public static void trustConnection(URLConnection connection)
{
if ( ! (connection instanceof HttpsURLConnection)) return;
HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
httpsConnection.setSSLSocketFactory(TRUSTING_SOCKET_FACTORY);
httpsConnection.setHostnameVerifier(TRUSTING_HOSTNAME_VERIFIER);
}
} }