solved s11 ex03
This commit is contained in:
parent
5d33f94e75
commit
a8a4b074d7
95
11/src/main/java/ch/zhaw/ads/TelNrSearchServer.java
Normal file
95
11/src/main/java/ch/zhaw/ads/TelNrSearchServer.java
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.security.cert.Certificate;
|
||||||
|
import java.util.regex.*;
|
||||||
|
|
||||||
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
import javax.net.ssl.SSLPeerUnverifiedException;
|
||||||
|
|
||||||
|
|
||||||
|
public class TelNrSearchServer implements CommandExecutor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String execute(String command) {
|
||||||
|
return findInPage(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String findInPage(String url_string) {
|
||||||
|
URL url;
|
||||||
|
try {
|
||||||
|
|
||||||
|
url = new URL(url_string);
|
||||||
|
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
||||||
|
|
||||||
|
// dumpl all cert info
|
||||||
|
print_https_cert(con);
|
||||||
|
|
||||||
|
// dump all the content
|
||||||
|
return print_content(con);
|
||||||
|
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void print_https_cert(HttpsURLConnection con) {
|
||||||
|
if (con != null) {
|
||||||
|
try {
|
||||||
|
System.out.println("Response Code : " + con.getResponseCode());
|
||||||
|
System.out.println("Cipher Suite : " + con.getCipherSuite());
|
||||||
|
System.out.println("\n");
|
||||||
|
|
||||||
|
Certificate[] certs = con.getServerCertificates();
|
||||||
|
for (Certificate cert : certs) {
|
||||||
|
System.out.println("Cert Type : " + cert.getType());
|
||||||
|
System.out.println("Cert Hash Code : " + cert.hashCode());
|
||||||
|
System.out.println(
|
||||||
|
"Cert Public Key Algorithm : "
|
||||||
|
+ cert.getPublicKey().getAlgorithm());
|
||||||
|
System.out.println(
|
||||||
|
"Cert Public Key Format : "
|
||||||
|
+ cert.getPublicKey().getFormat());
|
||||||
|
System.out.println("\n");
|
||||||
|
}
|
||||||
|
} catch (SSLPeerUnverifiedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String print_content(HttpsURLConnection con) {
|
||||||
|
if (con == null) return "";
|
||||||
|
|
||||||
|
StringBuffer result = new StringBuffer(100);
|
||||||
|
try {
|
||||||
|
System.out.println("****** Content of the URL ********");
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||||
|
String input;
|
||||||
|
Pattern pattern = Pattern.compile("(0041|\\+41|0\\d{2})\\s?\\d{3}\\s?\\d{2}\\s?\\d{2}");
|
||||||
|
Matcher matcher;
|
||||||
|
while ((input = br.readLine()) != null) {
|
||||||
|
matcher = pattern.matcher(input);
|
||||||
|
while(matcher.find()) {
|
||||||
|
result.append(matcher.group() + "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
br.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
20
11/src/test/java/ch/zhaw/ads/TelNrSearchServerTest.java
Normal file
20
11/src/test/java/ch/zhaw/ads/TelNrSearchServerTest.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.Before;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class TelNrSearchServerTest {
|
||||||
|
|
||||||
|
TelNrSearchServer serverToTest;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
serverToTest = new TelNrSearchServer();
|
||||||
|
System.out.println(serverToTest.execute("https://tel.search.ch/?was=Meier"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user