]> WPIA git - gigi.git/blob - src/org/cacert/gigi/ping/HTTPFetch.java
upd: move external keywords to own class
[gigi.git] / src / org / cacert / gigi / ping / HTTPFetch.java
1 package org.cacert.gigi.ping;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.net.HttpURLConnection;
7 import java.net.URL;
8
9 import org.cacert.gigi.dbObjects.CertificateOwner;
10 import org.cacert.gigi.dbObjects.Domain;
11 import org.cacert.gigi.util.SystemKeywords;
12
13 public class HTTPFetch extends DomainPinger {
14
15     @Override
16     public void ping(Domain domain, String expToken, CertificateOwner user, int confId) {
17         try {
18             String[] tokenParts = expToken.split(":", 2);
19             URL u = new URL("http://" + domain.getSuffix() + "/" + SystemKeywords.HTTP_CHALLENGE_PREFIX + tokenParts[0] + ".txt");
20             HttpURLConnection huc = (HttpURLConnection) u.openConnection();
21             if (huc.getResponseCode() != 200) {
22                 enterPingResult(confId, "error", "Invalid status code " + huc.getResponseCode() + ".", null);
23                 return;
24             }
25             BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream(), "UTF-8"));
26             String line = br.readLine();
27             if (line == null) {
28                 enterPingResult(confId, "error", "Empty document.", null);
29                 return;
30             }
31             if (line.trim().equals(tokenParts[1])) {
32                 enterPingResult(confId, PING_SUCCEDED, "", null);
33                 return;
34             }
35             enterPingResult(confId, "error", "Challenge tokens differed.", null);
36             return;
37         } catch (IOException e) {
38             e.printStackTrace();
39             enterPingResult(confId, "error", "Exception: connection closed.", null);
40             return;
41         }
42     }
43 }