mailreader-lr62-hook/src/main/java/org/gcube/portal/GetCaller.java

89 lines
2.7 KiB
Java
Raw Normal View History

package org.gcube.portal;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.gcube.portal.SSLUtilities;
import javax.net.ssl.HttpsURLConnection;
public class GetCaller extends Thread {
private final int MINUTES_4_MILLISECONDS = 180000;
private final static String USER_AGENT = "Mozilla/5.0";
private List<String> urlsToContact;
public GetCaller(List<String> urlsToContact) {
super();
this.urlsToContact = urlsToContact;
}
@Override
public void run() {
try {
System.out.println("********** READING MAILS THREAD Waiting ... starting in " + MINUTES_4_MILLISECONDS/60000 + " minutes");
System.out.println("********** LDAP EXPORT THREAD Waiting ... starting in " + MINUTES_4_MILLISECONDS/60000 + " minutes");
Thread.sleep(MINUTES_4_MILLISECONDS);
for (String url : urlsToContact) {
String response = sendHTTPsGet(url);
System.out.println("**********\n\n"+ "Called URL=" + url + " correctly, response:" + response);
}
String response_ldap = sendGet(ServletTrigger.SERVLET_URL_LDAP);
System.out.println("**********\n\n"+ "Called URL=" + ServletTrigger.SERVLET_URL_LDAP + " correctly, response: " + response_ldap);
} catch (Exception e) {
e.printStackTrace();
}
}
private String sendHTTPsGet(String url) throws Exception {
URL obj = new URL(null, url, new sun.net.www.protocol.https.Handler());
SSLUtilities.trustAllHostnames();
SSLUtilities.trustAllHttpsCertificates();
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
private String sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
}