View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
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   * Provides the Operating Status blurb on the gui  see
42   * fgsmsstatus.jsp
43   *
44   * @author AO
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       * determines if an fgsms service is currently accessible. not for use
66       * with other services.
67       *
68       * @param endpoint
69       * @return
70       */
71      public String sendGetRequest(String endpoint) {
72          //   String result = null;
73          if (endpoint.startsWith("http")) {
74              // Send a GET request to the servlet
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 }