forked from D-Net/dnet-hadoop
213 lines
6.6 KiB
Java
Executable File
213 lines
6.6 KiB
Java
Executable File
|
|
package eu.dnetlib.oa.graph.usagestats.export;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.DataOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.io.Reader;
|
|
import java.net.Authenticator;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.PasswordAuthentication;
|
|
import java.net.URL;
|
|
import java.net.URLConnection;
|
|
import java.security.KeyManagementException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.cert.X509Certificate;
|
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.SSLSession;
|
|
import javax.net.ssl.TrustManager;
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
public class Simple {
|
|
|
|
public static void main(final String[] args) throws Exception {
|
|
|
|
System.out.println("=============================================");
|
|
System.out.println("Hello world");
|
|
System.out.println("=============================================");
|
|
|
|
String url = "https://analytics.openaire.eu/?module=API&method=Live.getLastVisitsDetails&idSite=13&period=day&date=2016-03-15&format=json&expanded=5&filter_limit=1000&token_auth=703bd17d845acdaf795e01bb1e0895b9";
|
|
// String url = "https://www.ntua.gr/media/djmediatools/cache/images/home-showcase/1170x460-crop-100-1_averof.jpg";
|
|
// String url = "https://www.ntua.gr";
|
|
|
|
// simpleConnection(url);
|
|
// ignoreSslConnection(url);
|
|
// getJson(url);
|
|
getJson2(url);
|
|
}
|
|
|
|
public static void simpleConnection(String givenUrl) throws IOException {
|
|
|
|
Authenticator.setDefault(new Authenticator() {
|
|
protected PasswordAuthentication getPasswordAuthentication() {
|
|
return new PasswordAuthentication("spyros", "RU78N9sqQndnH3SQ".toCharArray());
|
|
}
|
|
});
|
|
|
|
URL url = new URL(givenUrl);
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
|
conn.setRequestMethod("GET");
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
|
|
// wr.writeBytes(params);
|
|
wr.flush();
|
|
wr.close();
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(
|
|
conn.getInputStream()));
|
|
String line = null;
|
|
while ((line = br.readLine()) != null) {
|
|
System.out.println(line);
|
|
}
|
|
}
|
|
|
|
public static String getJson(String url) throws Exception {
|
|
try {
|
|
|
|
Authenticator.setDefault(new Authenticator() {
|
|
protected PasswordAuthentication getPasswordAuthentication() {
|
|
return new PasswordAuthentication("spyros", "RU78N9sqQndnH3SQ".toCharArray());
|
|
}
|
|
});
|
|
|
|
System.out.println("===> Connecting to: " + url);
|
|
URL website = new URL(url);
|
|
System.out.println("Connection url -----> " + url);
|
|
URLConnection connection = website.openConnection();
|
|
|
|
// connection.setRequestProperty ("Authorization", "Basic "+encoded);
|
|
StringBuilder response;
|
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
|
response = new StringBuilder();
|
|
String inputLine;
|
|
while ((inputLine = in.readLine()) != null) {
|
|
response.append(inputLine);
|
|
response.append("\n");
|
|
}
|
|
}
|
|
return response.toString();
|
|
} catch (Exception e) {
|
|
System.out.println("Failed to get URL: " + e);
|
|
throw new Exception("Failed to get URL: " + e.toString(), e);
|
|
}
|
|
}
|
|
|
|
public static void ignoreSslConnection(String givenUrl)
|
|
throws IOException, NoSuchAlgorithmException, KeyManagementException {
|
|
/* Start of Fix */
|
|
TrustManager[] trustAllCerts = new TrustManager[] {
|
|
new X509TrustManager() {
|
|
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
return null;
|
|
}
|
|
|
|
public void checkClientTrusted(X509Certificate[] certs, String authType) {
|
|
}
|
|
|
|
public void checkServerTrusted(X509Certificate[] certs, String authType) {
|
|
}
|
|
|
|
}
|
|
};
|
|
|
|
Authenticator.setDefault(new Authenticator() {
|
|
protected PasswordAuthentication getPasswordAuthentication() {
|
|
return new PasswordAuthentication("spyros", "RU78N9sqQndnH3SQ".toCharArray());
|
|
}
|
|
});
|
|
|
|
SSLContext sc = SSLContext.getInstance("SSL");
|
|
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
|
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
|
|
|
// Create all-trusting host name verifier
|
|
HostnameVerifier allHostsValid = new HostnameVerifier() {
|
|
public boolean verify(String hostname, SSLSession session) {
|
|
return true;
|
|
}
|
|
};
|
|
// Install the all-trusting host verifier
|
|
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
|
|
/* End of the fix */
|
|
|
|
URL url = new URL(givenUrl);
|
|
URLConnection con = url.openConnection();
|
|
Reader reader = new InputStreamReader(con.getInputStream());
|
|
while (true) {
|
|
int ch = reader.read();
|
|
if (ch == -1)
|
|
break;
|
|
System.out.print((char) ch);
|
|
}
|
|
}
|
|
|
|
public static String getJson2(String url) throws Exception {
|
|
try {
|
|
|
|
// Trust all certificates
|
|
TrustManager[] trustAllCerts = new TrustManager[] {
|
|
new X509TrustManager() {
|
|
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
return null;
|
|
}
|
|
|
|
public void checkClientTrusted(X509Certificate[] certs, String authType) {
|
|
}
|
|
|
|
public void checkServerTrusted(X509Certificate[] certs, String authType) {
|
|
}
|
|
}
|
|
};
|
|
|
|
// Provide username & password until there is IP authentication
|
|
Authenticator.setDefault(new Authenticator() {
|
|
protected PasswordAuthentication getPasswordAuthentication() {
|
|
return new PasswordAuthentication("spyros", "RU78N9sqQndnH3SQ".toCharArray());
|
|
}
|
|
});
|
|
|
|
SSLContext sc = SSLContext.getInstance("SSL");
|
|
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
|
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
|
|
|
// Create all-trusting host name verifier
|
|
HostnameVerifier allHostsValid = new HostnameVerifier() {
|
|
public boolean verify(String hostname, SSLSession session) {
|
|
return true;
|
|
}
|
|
};
|
|
// Install the all-trusting host verifier
|
|
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
|
|
/* End of the fix */
|
|
|
|
System.out.println("===> Connecting to: " + url);
|
|
URL website = new URL(url);
|
|
System.out.println("Connection url -----> " + url);
|
|
URLConnection connection = website.openConnection();
|
|
|
|
// connection.setRequestProperty ("Authorization", "Basic "+encoded);
|
|
StringBuilder response;
|
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
|
response = new StringBuilder();
|
|
String inputLine;
|
|
while ((inputLine = in.readLine()) != null) {
|
|
response.append(inputLine);
|
|
response.append("\n");
|
|
}
|
|
}
|
|
return response.toString();
|
|
} catch (Exception e) {
|
|
System.out.println("Failed to get URL: " + e);
|
|
throw new Exception("Failed to get URL: " + e.toString(), e);
|
|
}
|
|
}
|
|
|
|
}
|