]> WPIA git - gigi.git/blob - src/org/cacert/gigi/ping/HTTPFetch.java
a5755844a47636bc24396d7dd64af392f2772f75
[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
12 public class HTTPFetch extends DomainPinger {
13
14     @Override
15     public void ping(Domain domain, String expToken, CertificateOwner user, int confId) {
16         try {
17             String[] tokenParts = expToken.split(":", 2);
18             URL u = new URL("http://" + domain.getSuffix() + "/cacert-" + tokenParts[0] + ".txt");
19             HttpURLConnection huc = (HttpURLConnection) u.openConnection();
20             if (huc.getResponseCode() != 200) {
21                 enterPingResult(confId, "error", "Invaild status code " + huc.getResponseCode() + ".", null);
22                 return;
23             }
24             BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream(), "UTF-8"));
25             String line = br.readLine();
26             if (line == null) {
27                 enterPingResult(confId, "error", "Empty document.", null);
28                 return;
29             }
30             if (line.trim().equals(tokenParts[1])) {
31                 enterPingResult(confId, PING_SUCCEDED, "", null);
32                 return;
33             }
34             enterPingResult(confId, "error", "Challange tokens differed.", null);
35             return;
36         } catch (IOException e) {
37             e.printStackTrace();
38             enterPingResult(confId, "error", "Exception: connection closed.", null);
39             return;
40         }
41     }
42 }