1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.miloss.fgsms.presentation;
22
23 import java.net.URL;
24 import org.apache.http.HttpHost;
25 import org.apache.http.auth.AuthScope;
26 import org.apache.http.auth.UsernamePasswordCredentials;
27 import org.apache.http.client.CredentialsProvider;
28 import org.apache.http.client.HttpClient;
29 import org.apache.http.client.methods.CloseableHttpResponse;
30 import org.apache.http.client.methods.HttpGet;
31 import org.apache.http.impl.client.BasicCredentialsProvider;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.impl.client.HttpClientBuilder;
34 import org.apache.http.impl.client.HttpClients;
35 import org.miloss.fgsms.common.Constants;
36 import org.miloss.fgsms.common.Utility;
37 import org.apache.log4j.Level;
38 import org.miloss.fgsms.common.Logger;;
39
40
41
42
43
44
45
46 public class StatusHelper {
47
48 public StatusHelper(String username, String password, org.miloss.fgsms.common.Constants.AuthMode mode, String keystore, String keystorepassword, String truststore, String truststorepassword) {
49 try {
50 this.username = username;
51 this.password = password;
52 this.mode = mode;
53
54 } catch (Exception ex) {
55 log.log(Level.WARN, "error initializing ssl sockets ", ex);
56 }
57 }
58 private static final Logger log = LogHelper.getLog();
59
60 private String username;
61 private String password;
62 private org.miloss.fgsms.common.Constants.AuthMode mode = org.miloss.fgsms.common.Constants.AuthMode.None;
63
64
65
66
67
68
69
70
71 public String sendGetRequest(String endpoint) {
72
73 if (endpoint.startsWith("http")) {
74
75 try {
76
77 URL url = new URL(endpoint);
78 int port = url.getPort();
79 if (port <= 0) {
80 if (endpoint.startsWith("https:")) {
81 port = 443;
82 } else {
83 port = 80;
84 }
85 }
86
87 HttpClientBuilder create = HttpClients.custom();
88
89 if (mode == org.miloss.fgsms.common.Constants.AuthMode.UsernamePassword) {
90 CredentialsProvider credsProvider = new BasicCredentialsProvider();
91 credsProvider.setCredentials(
92 new AuthScope(url.getHost(), port),
93 new UsernamePasswordCredentials(username, Utility.DE(password)));
94 create.setDefaultCredentialsProvider(credsProvider);;
95
96
97 }
98 CloseableHttpClient c = create.build();
99
100 CloseableHttpResponse response = c.execute(new HttpHost(url.getHost(), port), new HttpGet(endpoint));
101
102 c.close();
103 int status=response.getStatusLine().getStatusCode();
104 if (status == 200) {
105 return "OK";
106 }
107 return String.valueOf(status);
108
109 } catch (Exception ex) {
110 Logger.getLogger(Helper.class).log(Level.WARN, "error fetching http doc from " + endpoint + ex.getLocalizedMessage());
111 return "offline";
112 }
113 } else {
114 return "undeterminable";
115 }
116 }
117 }