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  
22  package org.miloss.fgsms.presentation;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Properties;
27  import javax.servlet.ServletContext;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.xml.ws.BindingProvider;
31  import org.miloss.fgsms.services.interfaces.common.GetOperatingStatusRequestMessage;
32  import org.miloss.fgsms.services.interfaces.common.GetOperatingStatusResponseMessage;
33  import org.miloss.fgsms.services.interfaces.common.SecurityWrapper;
34  import org.miloss.fgsms.services.interfaces.status.OpStatusService;
35  import org.apache.log4j.Level;
36  import org.miloss.fgsms.common.Logger;;
37  import org.miloss.fgsms.common.Utility;
38  
39  /**
40   *
41   * @author AO
42   */
43  public class OpStatHelper {
44  
45      static Logger log = Logger.getLogger("fgsms.OpStat");
46  
47      public static List<OpStatWrapper> GetStatusAll(IProxyLoader pl, ServletContext context, HttpServletRequest req, HttpServletResponse res) {
48          List<OpStatWrapper> list = new ArrayList<OpStatWrapper>();
49          try {
50              OpStatusService GetOpStat = pl.GetOpStat(context, req, res);
51              SecurityWrapper w = (SecurityWrapper) req.getSession().getAttribute("currentclassification");
52              List<String> urls = new ArrayList<String>();
53              Properties rawConfiguration = pl.getRawConfiguration();
54              String s = rawConfiguration.getProperty(IProxyLoader.ARS);
55              String[] ss = null;
56              if (s.contains("|")) {
57                  ss = s.split("|");
58                  TransferToList(urls, ss);
59              } else {
60                  urls.add(s);
61              }
62  
63              s = rawConfiguration.getProperty(IProxyLoader.DATAACCESS);
64              if (s.contains("|")) {
65                  ss = s.split("|");
66  
67                  TransferToList(urls, ss);
68              } else {
69                  urls.add(s);
70              }
71              s = rawConfiguration.getProperty(IProxyLoader.DCS);
72              if (s.contains("|")) {
73                  ss = s.split("|");
74                  TransferToList(urls, ss);
75              } else {
76                  urls.add(s);
77              }
78              s = rawConfiguration.getProperty(IProxyLoader.POLICYCONFIG);
79              if (s.contains("|")) {
80                  ss = s.split("|");
81                  TransferToList(urls, ss);
82              } else {
83                  urls.add(s);
84              }
85              s = rawConfiguration.getProperty(IProxyLoader.REPORTING);
86              if (s.contains("|")) {
87                  ss = s.split("|");
88  
89                  TransferToList(urls, ss);
90              } else {
91                  urls.add(s);
92              }
93              s = rawConfiguration.getProperty(IProxyLoader.STATUS);
94              if (s.contains("|")) {
95                  ss = s.split("|");
96                  TransferToList(urls, ss);
97              } else {
98                  urls.add(s);
99              }
100             GetOperatingStatusRequestMessage reqmsg = new GetOperatingStatusRequestMessage();
101             reqmsg.setClassification(w);
102 
103             for (int i = 0; i < urls.size(); i++) {
104                 log.log(Level.INFO, (i + 1) + "/" + urls.size() + " requesting opstatus for " + urls.get(i)+ "-opstat");
105                 BindingProvider bp = (BindingProvider) GetOpStat;
106                 bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, urls.get(i) + "-opstat");
107                 try {
108                     GetOperatingStatusResponseMessage operatingStatus = GetOpStat.getOperatingStatus(reqmsg);
109                     OpStatWrapper x = new OpStatWrapper();
110                     x.uri = urls.get(i);
111                     x.msg = operatingStatus;
112                     list.add(x);
113                 } catch (Exception ex) {
114                     GetOperatingStatusResponseMessage resr = new GetOperatingStatusResponseMessage();
115                     resr.setStatus(false);
116                     resr.setStatusMessage("Unable to connect " + ex.getMessage());
117                     OpStatWrapper x = new OpStatWrapper();
118                     x.uri = urls.get(i);
119                     x.msg = resr;
120                     list.add(x);
121                 }
122             }
123         } catch (Exception ex) {
124             log.log(Level.WARN, null, ex);
125         }
126         return list;
127     }
128 
129     private static void TransferToList(List<String> destination, String[] source) {
130         if (source == null) {
131             return;
132         }
133         for (int i = 0; i < source.length; i++) {
134             destination.add(source[i]);
135         }
136     }
137 
138     public static String toHtmlFormatedString(GetOperatingStatusResponseMessage item, String url) {
139         StringBuilder sb = new StringBuilder();
140 
141         sb.append("<div style=\"border-style: dotted; border-width: 1px\">");
142         sb.append("URL: ");
143         sb.append(url);
144         sb.append("<br>Is Running?:  ");
145         sb.append(Boolean.toString(item.isStatus()));
146         sb.append("<br>Status Message: ");
147         sb.append(item.getStatusMessage());
148         sb.append("<br>Started at: ");
149         if (item.getStartedAt() != null) {
150             sb.append(Utility.formatDateTime(item.getStartedAt()));
151         }
152         if (item.getVersionInfo()!=null){
153           sb.append("<br> Version: ");
154           sb.append(item.getVersionInfo().getVersionData());
155           sb.append("<br>Version Source: ");
156           sb.append(item.getVersionInfo().getVersionSource());
157         }
158 
159         sb.append("<br>: Data Sent Success: ");
160         sb.append(Long.toString(item.getDataSentSuccessfully()));
161         sb.append("<br>: Data Sent Failure: ");
162         sb.append(Long.toString(item.getDataNotSentSuccessfully()));
163 
164 
165         sb.append("</div>");
166         return sb.toString();
167     }
168 }