1 | package com.bowman.cardserv.web;
|
---|
2 |
|
---|
3 | import com.bowman.cardserv.*;
|
---|
4 | import com.bowman.cardserv.cws.ServiceMapping;
|
---|
5 | import com.bowman.cardserv.interfaces.CommandManager;
|
---|
6 | import com.bowman.cardserv.rmi.*;
|
---|
7 | import com.bowman.cardserv.session.*;
|
---|
8 | import com.bowman.cardserv.tv.TvService;
|
---|
9 | import com.bowman.cardserv.util.*;
|
---|
10 | import com.bowman.xml.XMLConfig;
|
---|
11 |
|
---|
12 | import java.io.*;
|
---|
13 | import java.rmi.RemoteException;
|
---|
14 | import java.text.SimpleDateFormat;
|
---|
15 | import java.util.*;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Created by IntelliJ IDEA.
|
---|
19 | * User: bowman
|
---|
20 | * Date: Feb 6, 2008
|
---|
21 | * Time: 9:44:41 PM
|
---|
22 | */
|
---|
23 | public class XmlHelper implements CommandManager {
|
---|
24 |
|
---|
25 | private static final SimpleDateFormat rfc822fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
|
---|
26 | private static final String[] CWS_STATES = {"disconnected", "connected", "connecting", "", "unresponsive", "disabled"};
|
---|
27 | private static boolean checkFileDescriptors = true;
|
---|
28 |
|
---|
29 | private RemoteProxy proxy;
|
---|
30 | private WebBackend webBackend;
|
---|
31 |
|
---|
32 | Map ctrlCommands = new LinkedHashMap();
|
---|
33 | Map statusCommands = new LinkedHashMap();
|
---|
34 |
|
---|
35 | public XmlHelper(WebBackend webBackend) {
|
---|
36 | this.proxy = webBackend.proxy;
|
---|
37 | this.webBackend = webBackend;
|
---|
38 |
|
---|
39 | registerCommands();
|
---|
40 | }
|
---|
41 |
|
---|
42 | void registerCommands() {
|
---|
43 | Command.setManager(this);
|
---|
44 | // built in control commands
|
---|
45 | try {
|
---|
46 | registerControlCommands(WebBackend.class.getResourceAsStream("ctrl-commands.xml"), this, "Internal");
|
---|
47 | } catch (Exception e) {
|
---|
48 | webBackend.logger.severe("Failed to load/parse internal control commands (ctrl-commands.xml).", e);
|
---|
49 | }
|
---|
50 | // built in status commands
|
---|
51 | try {
|
---|
52 | registerStatusCommands(WebBackend.class.getResourceAsStream("status-commands.xml"), this, "Internal");
|
---|
53 | } catch (Exception e) {
|
---|
54 | webBackend.logger.severe("Failed to load/parse internal status commands (status-commands.xml).", e);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static Set registerStatusCommands(InputStream is, Object handler, String label) throws Exception {
|
---|
59 | ProxyXmlConfig cmdDefs = new ProxyXmlConfig(new XMLConfig(is, false, "UTF-8"));
|
---|
60 | StatusCommand cmd; Set commands = new HashSet();
|
---|
61 | for(Iterator iter = cmdDefs.getMultipleSubConfigs("command"); iter.hasNext(); ) {
|
---|
62 | cmd = StatusCommand.createFromXml((ProxyXmlConfig)iter.next());
|
---|
63 | cmd.register(handler, label);
|
---|
64 | commands.add(cmd);
|
---|
65 | }
|
---|
66 | return commands;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static Set registerControlCommands(InputStream is, Object handler, String label) throws Exception {
|
---|
70 | ProxyXmlConfig cmdDefs = new ProxyXmlConfig(new XMLConfig(is, false, "UTF-8"));
|
---|
71 | CtrlCommand cmd; Set commands = new HashSet();
|
---|
72 | for(Iterator iter = cmdDefs.getMultipleSubConfigs("command"); iter.hasNext(); ) {
|
---|
73 | cmd = CtrlCommand.createFromXml((ProxyXmlConfig)iter.next());
|
---|
74 | cmd.register(handler, label);
|
---|
75 | commands.add(cmd);
|
---|
76 | }
|
---|
77 | return commands;
|
---|
78 | }
|
---|
79 |
|
---|
80 | Set getServices(String[] profiles) throws RemoteException {
|
---|
81 | CwsStatus[] connectors = proxy.getMultiCwsStatus(profiles);
|
---|
82 | Set all = new HashSet();
|
---|
83 | TvService[] services;
|
---|
84 | for(int i = 0; i < connectors.length; i++) {
|
---|
85 | services = proxy.getServices(connectors[i].getName(), false);
|
---|
86 | if(services != null) all.addAll(Arrays.asList(services));
|
---|
87 | }
|
---|
88 | return all;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public String onQryStatusCommand(String cmd, Map params, String authUser) throws RemoteException {
|
---|
92 | XmlStringBuffer xb = new XmlStringBuffer();
|
---|
93 | xb.appendElement("cws-status-resp", "ver", "1.0");
|
---|
94 | String profile = (String)params.get("profile");
|
---|
95 | ProfileStatus[] ps = proxy.getUserProfiles(authUser);
|
---|
96 | if(ps == null) ps = proxy.getProfiles();
|
---|
97 | String[] profiles = getProfileNames(ps, profile);
|
---|
98 | params.put("ps", ps);
|
---|
99 | params.put("profiles", profiles);
|
---|
100 | runStatusCmd(cmd, xb, params, authUser);
|
---|
101 | xb.closeElement("cws-status-resp");
|
---|
102 | return xb.toString();
|
---|
103 | }
|
---|
104 |
|
---|
105 | public String onQryControlCommand(String cmd, Map params, String authUser) throws RemoteException {
|
---|
106 | XmlStringBuffer xb = new XmlStringBuffer();
|
---|
107 | xb.appendElement("cws-command-resp", "ver", "1.0");
|
---|
108 | if(!proxy.isAdmin(authUser)) appendCmdResult(cmd, new CtrlCommandResult(false, "Admin user required.", null), xb);
|
---|
109 | else appendCmdResult(cmd, runCtrlCmd(cmd, params, authUser), xb);
|
---|
110 | xb.closeElement("cws-command-resp");
|
---|
111 | return xb.toString();
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void appendCmdResult(String cmd, CtrlCommandResult res, XmlStringBuffer xb) {
|
---|
115 | xb.appendElement("cmd-result", "command", cmd);
|
---|
116 | xb.appendAttr("success", res.success);
|
---|
117 | if(res.data != null) xb.appendAttr("data", res.data.toString());
|
---|
118 | xb.endElement(false);
|
---|
119 | xb.appendText(res.message);
|
---|
120 | xb.closeElement("cmd-result");
|
---|
121 | }
|
---|
122 |
|
---|
123 | public CtrlCommandResult runCtrlCmdShutdown() throws RemoteException {
|
---|
124 | proxy.shutdown();
|
---|
125 | return new CtrlCommandResult(true, "Shutdown initiated.", null);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public CtrlCommandResult runCtrlCmdResetConnector(Map params) throws RemoteException {
|
---|
129 | return runCtrlCmdReset(params);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public CtrlCommandResult runCtrlCmdResetService(Map params) throws RemoteException {
|
---|
133 | return runCtrlCmdReset(params);
|
---|
134 | }
|
---|
135 |
|
---|
136 | private CtrlCommandResult runCtrlCmdReset(Map params) throws RemoteException {
|
---|
137 | boolean result = false; int count = -1;
|
---|
138 | String resultMsg;
|
---|
139 | String name = (String)params.get("name");
|
---|
140 | String idStr = (String)params.get("id");
|
---|
141 | String profile = (String)params.get("profile");
|
---|
142 | boolean full = "true".equalsIgnoreCase((String)params.get("full"));
|
---|
143 | if(name != null) {
|
---|
144 | if("ALL".equals(name)) {
|
---|
145 | CwsStatus[] connectors = proxy.getMultiCwsStatus(null);
|
---|
146 | count = 0;
|
---|
147 | for(int i = 0; i < connectors.length; i++)
|
---|
148 | count += proxy.resetStatus(connectors[i].getName(), full);
|
---|
149 | resultMsg = "Service map for 'ALL' reset (" + count + " entries cleared).";
|
---|
150 | result = true;
|
---|
151 | } else {
|
---|
152 | count = proxy.resetStatus(name, full);
|
---|
153 | result = count != -1;
|
---|
154 | resultMsg = result?"Service map for '" + name + "' reset (" + count + " cleared).":"No such connector: " + name;
|
---|
155 | }
|
---|
156 | } else if(idStr != null) {
|
---|
157 | if(profile == null) resultMsg = "Missing parameter: profile";
|
---|
158 | else {
|
---|
159 | try {
|
---|
160 | Set services = ProxyConfig.getServiceTokens("id", idStr, false);
|
---|
161 | ServiceMapping sm = (ServiceMapping)services.iterator().next();
|
---|
162 | result = proxy.resetStatus(profile, sm.serviceId, sm.getCustomData());
|
---|
163 | resultMsg = result?"Service status for '" + profile + ":" + idStr + "' reset.":"No match found for: " + idStr;
|
---|
164 | } catch (ConfigException e) {
|
---|
165 | resultMsg = e.getMessage();
|
---|
166 | }
|
---|
167 | }
|
---|
168 | } else resultMsg = "Missing parameter: name or id";
|
---|
169 | return new CtrlCommandResult(result, resultMsg, count>-1?new Integer(count):null);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public CtrlCommandResult runCtrlCmdRetryConnector(Map params) throws RemoteException {
|
---|
173 | boolean result = false;
|
---|
174 | String resultMsg;
|
---|
175 | String name = (String)params.get("name");
|
---|
176 | if(name != null) {
|
---|
177 | proxy.retryConnector(name);
|
---|
178 | result = true;
|
---|
179 | resultMsg = "Connector notified, attempting reconnect.";
|
---|
180 | } else resultMsg = "Missing parameter: name";
|
---|
181 | return new CtrlCommandResult(result, resultMsg, null);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public CtrlCommandResult runCtrlCmdDisableConnector(Map params) throws RemoteException {
|
---|
185 | boolean result = false;
|
---|
186 | String resultMsg;
|
---|
187 | String name = (String)params.get("name");
|
---|
188 | if(name != null) {
|
---|
189 | proxy.disableConnector(name);
|
---|
190 | result = true;
|
---|
191 | resultMsg = "Connector disabled.";
|
---|
192 | } else resultMsg = "Missing parameter: name";
|
---|
193 | return new CtrlCommandResult(result, resultMsg, null);
|
---|
194 | }
|
---|
195 |
|
---|
196 | public CtrlCommandResult runCtrlCmdSetConnectorMetric(Map params) throws RemoteException {
|
---|
197 | boolean result = false;
|
---|
198 | String resultMsg;
|
---|
199 | String name = (String)params.get("name");
|
---|
200 | String metricStr = (String)params.get("metric");
|
---|
201 | if(metricStr == null) return new CtrlCommandResult(false, "Missing parameter: metric", null);
|
---|
202 | int metric = 1;
|
---|
203 | try {
|
---|
204 | metric = Integer.parseInt(metricStr);
|
---|
205 | } catch (NumberFormatException e) {
|
---|
206 | return new CtrlCommandResult(false, "Bad metric value: " + metricStr, null);
|
---|
207 | }
|
---|
208 | if(name != null) {
|
---|
209 | proxy.setConnectorMetric(name, metric);
|
---|
210 | result = true;
|
---|
211 | resultMsg = "Metric set for: " + name;
|
---|
212 | } else resultMsg = "Missing parameter: name";
|
---|
213 | return new CtrlCommandResult(result, resultMsg, null);
|
---|
214 | }
|
---|
215 |
|
---|
216 | public CtrlCommandResult runCtrlCmdSetAuUser(Map params) throws RemoteException {
|
---|
217 | boolean result = false;
|
---|
218 | String resultMsg;
|
---|
219 | String name = (String)params.get("name");
|
---|
220 | String user = (String)params.get("user");
|
---|
221 | if(user == null) return new CtrlCommandResult(false, "Missing parameter: user", null);
|
---|
222 | if(name != null) {
|
---|
223 | result = proxy.setAuUser(name, user);
|
---|
224 | resultMsg = result?"Temp au-user '" + user + "' toggled for: " + name:"Invalid connector/user";
|
---|
225 | } else resultMsg = "Missing parameter: name";
|
---|
226 | return new CtrlCommandResult(result, resultMsg, null);
|
---|
227 | }
|
---|
228 |
|
---|
229 | public CtrlCommandResult runCtrlCmdSetProfileDebug(Map params) throws RemoteException {
|
---|
230 | boolean value = "true".equalsIgnoreCase((String)params.get("value"));
|
---|
231 | String profile = (String)params.get("profile");
|
---|
232 | if("ALL".equals(profile)) profile = null;
|
---|
233 |
|
---|
234 | proxy.setProfileDebug(value, profile);
|
---|
235 | if(!value) webBackend.clearTransactions(profile);
|
---|
236 | return new CtrlCommandResult(true, "Debug flag set for: " + (profile == null?"ALL":profile), null);
|
---|
237 | }
|
---|
238 |
|
---|
239 | public CtrlCommandResult runCtrlCmdSetUserDebug(Map params) throws RemoteException {
|
---|
240 | boolean value = "true".equalsIgnoreCase((String)params.get("value"));
|
---|
241 | String user = (String)params.get("name");
|
---|
242 |
|
---|
243 | if(proxy.setUserDebug(value, user)) return new CtrlCommandResult(true, "Debug flag set for: " + user);
|
---|
244 | else return new CtrlCommandResult(false, "No such user connected: " + user);
|
---|
245 | }
|
---|
246 |
|
---|
247 | public CtrlCommandResult runCtrlCmdKickUser(Map params) throws RemoteException {
|
---|
248 | boolean result = false; int count = 0;
|
---|
249 | String resultMsg;
|
---|
250 | String name = (String)params.get("name");
|
---|
251 | if(name != null) {
|
---|
252 | count = proxy.kickUser(name);
|
---|
253 | if(count != -1) result = true;
|
---|
254 | if(count == 0) resultMsg = "User '" + name + "' not connected.";
|
---|
255 | else resultMsg = result?"User '" + name + "' kicked: " + count + " sessions closed.":"No such user: " + name;
|
---|
256 | } else resultMsg = "Missing parameter: name";
|
---|
257 | return new CtrlCommandResult(result, resultMsg, count>-1?new Integer(count):null);
|
---|
258 | }
|
---|
259 |
|
---|
260 | public CtrlCommandResult runCtrlCmdOsdMessage(Map params) throws RemoteException {
|
---|
261 | boolean result = false; int count = -1;
|
---|
262 | String resultMsg;
|
---|
263 | String text = (String)params.get("text");
|
---|
264 | String name = (String)params.get("name");
|
---|
265 | if("ALL".equalsIgnoreCase(name)) name = null;
|
---|
266 | if(text == null) resultMsg = "Missing parameter: text";
|
---|
267 | else {
|
---|
268 | count = proxy.sendOsdMessage(name, text);
|
---|
269 | if(count > 0) result = true;
|
---|
270 | resultMsg = result?"Message sent to " + count + " active and compatible newcamd sessions.":"No active/compatible newcamd sessions found";
|
---|
271 | }
|
---|
272 | return new CtrlCommandResult(result, resultMsg, count>-1?new Integer(count):null);
|
---|
273 | }
|
---|
274 |
|
---|
275 | public CtrlCommandResult runCtrlCmdRemoveSeen(Map params) throws RemoteException {
|
---|
276 | String name = (String)params.get("name");
|
---|
277 | if("ALL".equalsIgnoreCase(name)) name = null;
|
---|
278 | int count = proxy.removeSeenUser(name);
|
---|
279 | return new CtrlCommandResult(count > 0, "Removed " + count + " matching entries from the seen log.", new Integer(count));
|
---|
280 | }
|
---|
281 |
|
---|
282 | public CtrlCommandResult runCtrlCmdRemoveFailed(Map params) throws RemoteException {
|
---|
283 | String mask = (String)params.get("mask");
|
---|
284 | if(mask == null) mask = "*";
|
---|
285 | int count = proxy.removeLoginFailure(mask);
|
---|
286 | return new CtrlCommandResult(count > 0, "Removed " + count + " matching entries from the failure log.", new Integer(count));
|
---|
287 | }
|
---|
288 |
|
---|
289 |
|
---|
290 | public CtrlCommandResult runCtrlCmdClearWarnings() throws RemoteException {
|
---|
291 | webBackend.warningLog.clear();
|
---|
292 | return new CtrlCommandResult(true, "Warnings cleared.");
|
---|
293 | }
|
---|
294 |
|
---|
295 | public CtrlCommandResult runCtrlCmdClearEvents() throws RemoteException {
|
---|
296 | webBackend.eventLog.clear();
|
---|
297 | return new CtrlCommandResult(true, "Events cleared.");
|
---|
298 | }
|
---|
299 |
|
---|
300 | public CtrlCommandResult runCtrlCmdClearFileLog() throws RemoteException {
|
---|
301 | webBackend.fileLog.clear();
|
---|
302 | return new CtrlCommandResult(true, "File log events cleared.");
|
---|
303 | }
|
---|
304 |
|
---|
305 | public CtrlCommandResult runCtrlCmdGenKeystore(Map params) throws RemoteException {
|
---|
306 | String password = (String)params.get("password");
|
---|
307 | String host = (String)params.get("host");
|
---|
308 | String validity = (String)params.get("validity");
|
---|
309 | if(password == null || password.length() < 2) return new CtrlCommandResult(false, "Missing parameter: password");
|
---|
310 | if(host == null || host.length() < 2) return new CtrlCommandResult(false, "Missing parameter: host");
|
---|
311 | if(validity == null || validity.length() == 0) validity = "1000";
|
---|
312 | File binDir = new File(System.getProperty("java.home"), "bin");
|
---|
313 | if(binDir.exists() && binDir.isDirectory()) {
|
---|
314 | File keyTool = new File(binDir, "keytool");
|
---|
315 | if(!keyTool.exists()) keyTool = new File(binDir, "keytool.exe");
|
---|
316 | if(keyTool.exists()) {
|
---|
317 | try {
|
---|
318 | String cmdLine = keyTool.getAbsolutePath();
|
---|
319 | cmdLine = cmdLine + " -keystore csp_keystore -genkey -alias Cardservproxy -keyalg RSA";
|
---|
320 | cmdLine = cmdLine + " -storepass " + password + " -keypass " + password;
|
---|
321 | cmdLine = cmdLine + " -dname cn=" + host + " -validity " + validity;
|
---|
322 |
|
---|
323 | Process p = Runtime.getRuntime().exec(cmdLine);
|
---|
324 | BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
---|
325 | String line; StringBuffer sb = new StringBuffer();
|
---|
326 | while((line = br.readLine()) != null) {
|
---|
327 | sb.append(line).append(" ");
|
---|
328 | webBackend.logger.warning(line);
|
---|
329 | }
|
---|
330 | int exitValue = p.waitFor();
|
---|
331 | if(exitValue != 0) {
|
---|
332 | webBackend.logger.warning("Keytool exit value: " + exitValue + " CmdLine: " + cmdLine);
|
---|
333 | return new CtrlCommandResult(false, "Keystore generation failed: " + sb, new Integer(exitValue));
|
---|
334 | } else {
|
---|
335 | return new CtrlCommandResult(true, "Keystore file 'csp_keystore' successfully created in proxy dir (probably). Enabling ssl requires restart.");
|
---|
336 | }
|
---|
337 |
|
---|
338 | } catch(Exception e) {
|
---|
339 | e.printStackTrace();
|
---|
340 | return new CtrlCommandResult(false, "Error occured: " + e);
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 | return new CtrlCommandResult(false, "Couldn't find keytool executable.");
|
---|
345 | }
|
---|
346 |
|
---|
347 | String getCfgXml() throws IOException {
|
---|
348 | File cfgFile = ProxyConfig.getInstance().getCfgFile();
|
---|
349 | byte[] buf = new byte[(int)cfgFile.length()];
|
---|
350 | DataInputStream dis = new DataInputStream(new FileInputStream(cfgFile));
|
---|
351 | dis.readFully(buf);
|
---|
352 | dis.close();
|
---|
353 | return new String(buf, "UTF-8");
|
---|
354 | }
|
---|
355 |
|
---|
356 | CtrlCommandResult runCtrlCmd(String cmd, Map params, String user) throws RemoteException {
|
---|
357 | CtrlCommand cmdDef = (CtrlCommand)ctrlCommands.get(cmd);
|
---|
358 | CtrlCommandResult cmdRes = cmdDef.invoke(params, user);
|
---|
359 | webBackend.logger.info("CtrlCommand '" + cmd + "' executed by user: " + user + " (result: " + cmdRes.message + ")");
|
---|
360 | return cmdRes;
|
---|
361 | }
|
---|
362 |
|
---|
363 | public void runStatusCmdCaProfiles(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
364 | String profileName = (String)params.get("name");
|
---|
365 | ProfileStatus[] ps = (ProfileStatus[])params.get("ps");
|
---|
366 | ProfileStatus[] psShow = null;
|
---|
367 | if(profileName != null) {
|
---|
368 | for(int i = 0; i < ps.length; i++) {
|
---|
369 | if(profileName.equals(ps[i].getName())) {
|
---|
370 | psShow = new ProfileStatus[] {ps[i]};
|
---|
371 | break;
|
---|
372 | }
|
---|
373 | }
|
---|
374 | } else psShow = ps;
|
---|
375 | xmlFormatProfiles(psShow, xb, proxy.isAdmin(user));
|
---|
376 | }
|
---|
377 |
|
---|
378 | public void runStatusCmdCwsConnectors(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
379 | String[] profiles = (String[])params.get("profiles");
|
---|
380 | CwsStatus[] connectors = null;
|
---|
381 | String cwsName = (String)params.get("name");
|
---|
382 | if(cwsName != null) {
|
---|
383 | CwsStatus temp = proxy.getCwsStatus(cwsName);
|
---|
384 | if(temp != null) connectors = new CwsStatus[] {temp};
|
---|
385 | } else {
|
---|
386 | connectors = proxy.getMultiCwsStatus(profiles);
|
---|
387 | }
|
---|
388 | xmlFormatConnectors(connectors, xb, proxy.isAdmin(user), webBackend.cwsTransactions, proxy, profiles);
|
---|
389 | }
|
---|
390 |
|
---|
391 | public void runStatusCmdProxyUsers(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
392 | String[] profiles = (String[])params.get("profiles");
|
---|
393 | UserStatus[] users = null;
|
---|
394 | String userName = (String)params.get("name");
|
---|
395 | boolean activeOnly = "true".equalsIgnoreCase((String)params.get("hide-inactive"));
|
---|
396 | if(!proxy.isAdmin(user)) userName = user;
|
---|
397 | if(userName != null) {
|
---|
398 | UserStatus temp = proxy.getUserStatus(userName, activeOnly);
|
---|
399 | if(temp != null) users = new UserStatus[] {temp};
|
---|
400 | } else {
|
---|
401 | users = proxy.getUsersStatus(profiles, activeOnly);
|
---|
402 | }
|
---|
403 | SeenEntry[] seen = proxy.getSeenUsers(null, userName, true);
|
---|
404 | xmlFormatUsers(users, seen.length, xb);
|
---|
405 | }
|
---|
406 |
|
---|
407 | public void runStatusCmdProxyPlugins(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
408 | String name = (String)params.get("name");
|
---|
409 | xmlFormatProxyPlugins(xb, name, proxy.isAdmin(user));
|
---|
410 | }
|
---|
411 |
|
---|
412 | public void runStatusCmdProxyStatus(XmlStringBuffer xb, Map params) throws RemoteException {
|
---|
413 | xmlFormatProxyStatus(xb, (String[])params.get("profiles"));
|
---|
414 | }
|
---|
415 |
|
---|
416 | public void runStatusCmdCacheStatus(XmlStringBuffer xb) throws RemoteException {
|
---|
417 | CacheStatus cs = proxy.getCacheStatus();
|
---|
418 | xmlFormatCacheStatus(cs, xb);
|
---|
419 | }
|
---|
420 |
|
---|
421 | public void runStatusCmdErrorLog(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
422 | xmlFormatErrorLog(xb, (String[])params.get("profiles"), proxy.isAdmin(user));
|
---|
423 | }
|
---|
424 |
|
---|
425 | public void runStatusCmdFileLog(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
426 | if(!proxy.isAdmin(user) || !ProxyConfig.getInstance().isIncludeFileEvents()) {
|
---|
427 | xb.appendElement("file-log", "size", "-1", true);
|
---|
428 | } else {
|
---|
429 | xmlFormatFileLog(xb);
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | public void runStatusCmdUserLog(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
434 | String userName = (String)params.get("name");
|
---|
435 | if(userName == null || !proxy.isAdmin(user)) userName = user;
|
---|
436 | xmlFormatUserLog(xb, userName, (String[])params.get("profiles"));
|
---|
437 | }
|
---|
438 |
|
---|
439 | public void runStatusCmdUserWarningLog(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
440 | String[] profiles = (String[])params.get("profiles");
|
---|
441 | if(proxy.isAdmin(user)) xmlFormatUserWarningLog(xb, null, profiles);
|
---|
442 | else xmlFormatUserWarningLog(xb, user, profiles);
|
---|
443 | }
|
---|
444 |
|
---|
445 | public void runStatusCmdCwsLog(XmlStringBuffer xb, Map params) throws RemoteException {
|
---|
446 | xmlFormatCwsLog(xb, (String)params.get("name"));
|
---|
447 | }
|
---|
448 |
|
---|
449 | public void runStatusCmdAllServices(XmlStringBuffer xb, Map params) throws RemoteException {
|
---|
450 | String[] profiles = (String[])params.get("profiles");
|
---|
451 | Set all = getServices(profiles);
|
---|
452 | List sorted = new ArrayList(all);
|
---|
453 | Collections.sort(sorted);
|
---|
454 | xb.appendElement("all-services", "count", sorted.size());
|
---|
455 | xmlFormatServices((TvService[])sorted.toArray(new TvService[sorted.size()]), xb, false, true, true, null, profiles);
|
---|
456 | xb.closeElement("all-services");
|
---|
457 | }
|
---|
458 |
|
---|
459 | public void runStatusCmdWatchedServices(XmlStringBuffer xb, Map params) throws RemoteException {
|
---|
460 | String[] profiles = (String[])params.get("profiles");
|
---|
461 | List watched = Arrays.asList(proxy.getWatchedServices(profiles));
|
---|
462 | Collections.sort(watched);
|
---|
463 | xb.appendElement("watched-services", "count", watched.size());
|
---|
464 | xmlFormatServices((TvService[])watched.toArray(new TvService[watched.size()]), xb, true, true, true, null, profiles);
|
---|
465 | xb.closeElement("watched-services");
|
---|
466 | }
|
---|
467 |
|
---|
468 | public void runStatusCmdExportServices(XmlStringBuffer xb, Map params) throws RemoteException {
|
---|
469 | String[] profiles = (String[])params.get("profiles");
|
---|
470 | String format = (String)params.get("format");
|
---|
471 | CwsStatus[] connectors;
|
---|
472 | String cwsName = (String)params.get("name");
|
---|
473 | if(cwsName != null) {
|
---|
474 | CwsStatus temp = proxy.getCwsStatus(cwsName);
|
---|
475 | if(temp != null) connectors = new CwsStatus[] {temp};
|
---|
476 | else connectors = new CwsStatus[0];
|
---|
477 | } else {
|
---|
478 | connectors = proxy.getMultiCwsStatus(profiles);
|
---|
479 | }
|
---|
480 | xb.appendElement("export-services", "connectors", connectors.length);
|
---|
481 | TvService[] services;
|
---|
482 | for(int i = 0; i < connectors.length; i++) {
|
---|
483 | xb.appendElement("connector", "name", connectors[i].getName());
|
---|
484 | services = proxy.getServices(connectors[i].getName(), false);
|
---|
485 | if(services != null) {
|
---|
486 | xb.appendElement("can-decode-services", "count", services.length);
|
---|
487 | if("hex".equalsIgnoreCase(format)) xmlFormatHexTokenList(services, xb);
|
---|
488 | else xmlFormatServices(services, xb, false, true, null);
|
---|
489 | xb.closeElement("can-decode-services");
|
---|
490 | }
|
---|
491 | services = proxy.getCannotDecodeServices(connectors[i].getName());
|
---|
492 | if(services != null) {
|
---|
493 | xb.appendElement("cannot-decode-services", "count", services.length);
|
---|
494 | if("hex".equalsIgnoreCase(format)) xmlFormatHexTokenList(services, xb);
|
---|
495 | else xmlFormatServices(services, xb, false, true, null);
|
---|
496 | xb.closeElement("cannot-decode-services");
|
---|
497 | }
|
---|
498 | xb.closeElement("connector");
|
---|
499 | }
|
---|
500 | xb.closeElement("export-services");
|
---|
501 | }
|
---|
502 |
|
---|
503 | private void xmlFormatHexTokenList(TvService[] services, XmlStringBuffer xb) {
|
---|
504 | Map perProfile = new HashMap(); Set tokens; String token;
|
---|
505 | for(int i = 0; i < services.length; i++) {
|
---|
506 | if(perProfile.containsKey(services[i].getProfileName())) tokens = (Set)perProfile.get(services[i].getProfileName());
|
---|
507 | else {
|
---|
508 | tokens = new LinkedHashSet();
|
---|
509 | perProfile.put(services[i].getProfileName(), tokens);
|
---|
510 | }
|
---|
511 | token = new ServiceMapping(services[i]).toString();
|
---|
512 | tokens.add(token);
|
---|
513 | }
|
---|
514 | String profile;
|
---|
515 | for(Iterator iter = perProfile.keySet().iterator(); iter.hasNext(); ) {
|
---|
516 | profile = (String)iter.next();
|
---|
517 | xb.appendElement("profile", "name", profile);
|
---|
518 | for(Iterator i = ((Set)perProfile.get(profile)).iterator(); i.hasNext(); ) {
|
---|
519 | xb.appendText((String)i.next());
|
---|
520 | if(i.hasNext()) xb.appendText(" ");
|
---|
521 | }
|
---|
522 | xb.closeElement("profile");
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | public void runStatusCmdLastSeen(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
527 | String userName = (String)params.get("name");
|
---|
528 | if(!proxy.isAdmin(user)) userName = user;
|
---|
529 | SeenEntry[] seen = proxy.getSeenUsers((String[])params.get("profiles"), userName, false);
|
---|
530 | xmlFormatLastSeen(seen, xb);
|
---|
531 | }
|
---|
532 |
|
---|
533 | public void runStatusCmdLoginFailures(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
534 | String userName = (String)params.get("name");
|
---|
535 | if(!proxy.isAdmin(user)) userName = user;
|
---|
536 | SeenEntry[] seen = proxy.getSeenUsers((String[])params.get("profiles"), userName, true);
|
---|
537 | xmlFormatLoginFailures(seen, xb);
|
---|
538 | }
|
---|
539 |
|
---|
540 | public void runStatusCmdFetchCfg(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
541 | if(!webBackend.isSuperUser(user)) {
|
---|
542 | xb.appendElement("error", "description", "Super-user required.", true);
|
---|
543 | } else {
|
---|
544 | try {
|
---|
545 | xb.setContents(getCfgXml());
|
---|
546 | } catch (Exception e) {
|
---|
547 | xb.setContents(getError(e.toString()));
|
---|
548 | }
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | public void runStatusCmdCwsBouquet(XmlStringBuffer xb, Map params) throws RemoteException {
|
---|
553 | if(!params.containsKey("xml")) return;
|
---|
554 | Set all = getServices((String[])params.get("profiles"));
|
---|
555 | Map serviceMap = new HashMap();
|
---|
556 | TvService service;
|
---|
557 | for(Iterator iter = all.iterator(); iter.hasNext();) {
|
---|
558 | service = (TvService)iter.next();
|
---|
559 | serviceMap.put(new Integer(service.getId()), service);
|
---|
560 | }
|
---|
561 | StringBuffer bqFile = new StringBuffer();
|
---|
562 | bqFile.append("#NAME Favourites (TV)\n");
|
---|
563 | int srvId;
|
---|
564 | XMLConfig bqXml = (XMLConfig)params.get("xml");
|
---|
565 | XMLConfig fileXml = bqXml.getSubConfig("file");
|
---|
566 | boolean enigma2 = false;
|
---|
567 | if(fileXml != null) enigma2 = "enigma2".equalsIgnoreCase(fileXml.getString("format"));
|
---|
568 | String prefix = enigma2?"#SERVICE 1:0:":"#SERVICE: 1:0:";
|
---|
569 | for(Enumeration en = bqXml.getMultipleSubConfigs("service"); en.hasMoreElements(); ) {
|
---|
570 | try {
|
---|
571 | srvId = Integer.parseInt(((XMLConfig)en.nextElement()).getString("id"));
|
---|
572 | service = (TvService)serviceMap.get(new Integer(srvId));
|
---|
573 | if(service != null && service.getTransponder() != -1) {
|
---|
574 | bqFile.append(prefix).append(Integer.toHexString(service.getType())).append(":");
|
---|
575 | bqFile.append(Integer.toHexString(srvId)).append(":");
|
---|
576 | bqFile.append(Integer.toHexString((int)service.getTransponder())).append(":");
|
---|
577 | bqFile.append(Integer.toHexString((int)service.getNetworkId())).append(":");
|
---|
578 | bqFile.append(Long.toHexString(service.getNamespace())).append(":0:0:0:\n");
|
---|
579 | }
|
---|
580 | } catch (NumberFormatException e) {
|
---|
581 | // e.printStackTrace();
|
---|
582 | }
|
---|
583 | }
|
---|
584 | String id = enigma2?"favourites":Long.toString(System.currentTimeMillis(), Character.MAX_RADIX);
|
---|
585 | String fileName = "/userbouquet." + id + ".tv";
|
---|
586 | String data = bqFile.toString();
|
---|
587 | if(enigma2) data = data.toUpperCase();
|
---|
588 | webBackend.bouquets.put(fileName, data);
|
---|
589 | xb.appendElement("cws-bouquet", "url", fileName, true);
|
---|
590 | }
|
---|
591 |
|
---|
592 | public void runStatusCmdSystemProperties(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
593 | if(!webBackend.isSuperUser(user)) {
|
---|
594 | xb.appendElement("error", "description", "Super-user required.", true);
|
---|
595 | } else {
|
---|
596 | Properties p = System.getProperties();
|
---|
597 | xb.appendElement("system-properties", "count", p.size());
|
---|
598 | String key;
|
---|
599 | for(Enumeration e = p.propertyNames(); e.hasMoreElements(); ) {
|
---|
600 | key = (String)e.nextElement();
|
---|
601 | xb.appendElement("property", "name", key);
|
---|
602 | xb.appendAttr("value", p.getProperty(key)).endElement(true);
|
---|
603 | }
|
---|
604 | xb.closeElement("system-properties");
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | public void runStatusCmdSystemThreads(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
609 | if(!webBackend.isSuperUser(user)) {
|
---|
610 | xb.appendElement("error", "description", "Super-user required.", true);
|
---|
611 | } else {
|
---|
612 | Thread[] threads = new Thread[Thread.activeCount()];
|
---|
613 | Thread.enumerate(threads);
|
---|
614 | xb.appendElement("system-threads", "count", threads.length);
|
---|
615 | for(int i = 0; i < threads.length; i++) {
|
---|
616 | if(threads[i] != null) xb.appendElement("thread", "name", threads[i].toString(), true);
|
---|
617 | }
|
---|
618 | xb.closeElement("system-threads");
|
---|
619 | }
|
---|
620 | }
|
---|
621 |
|
---|
622 | public void runStatusCmdCtrlCommands(XmlStringBuffer xb, Map params) throws RemoteException {
|
---|
623 | String cmdName = (String)params.get("name");
|
---|
624 | String grpName = (String)params.get("group");
|
---|
625 | xmlFormatCtrlCommands(xb, cmdName, grpName);
|
---|
626 | }
|
---|
627 |
|
---|
628 | public void runStatusCmdStatusCommands(XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
629 | String cmdName = (String)params.get("name");
|
---|
630 | String grpName = (String)params.get("group");
|
---|
631 | xmlFormatStatusCommands(xb, cmdName, grpName, proxy.isAdmin(user));
|
---|
632 | }
|
---|
633 |
|
---|
634 | void runStatusCmd(String cmd, XmlStringBuffer xb, Map params, String user) throws RemoteException {
|
---|
635 | StatusCommand cmdDef = (StatusCommand)statusCommands.get(cmd);
|
---|
636 | if(cmdDef == null) xb.appendElement("error", "description", "Command unregistered", true);
|
---|
637 | else {
|
---|
638 | if(cmdDef.adminOnly && !proxy.isAdmin(user)) xb.appendElement("error", "description", "Admin user required.", true);
|
---|
639 | else cmdDef.invoke(xb, params, user);
|
---|
640 | }
|
---|
641 | }
|
---|
642 |
|
---|
643 | String onXMLInput(XMLConfig xml, String preAuthUser, String ip) throws RemoteException {
|
---|
644 | XmlStringBuffer xb = new XmlStringBuffer();
|
---|
645 | String authUser = preAuthUser;
|
---|
646 |
|
---|
647 | if("cws-status-req".equals(xml.getName())) {
|
---|
648 | xb.appendElement("cws-status-resp", "ver", "1.0");
|
---|
649 | } else if("cws-command-req".equals(xml.getName())) {
|
---|
650 | xb.appendElement("cws-command-resp", "ver", "1.0");
|
---|
651 | }
|
---|
652 |
|
---|
653 | XMLConfig loginXml = xml.getSubConfig("cws-login");
|
---|
654 | if(loginXml != null) {
|
---|
655 | loginXml = loginXml.getSubConfig("user");
|
---|
656 | if(loginXml != null) {
|
---|
657 | String user = loginXml.getString("name");
|
---|
658 | String passwd = loginXml.getString("password");
|
---|
659 | if(webBackend.authUser(user, passwd)) {
|
---|
660 | String sessionId = webBackend.createSession(user);
|
---|
661 | xb.appendElement("status", "state", "loggedIn");
|
---|
662 | xb.appendAttr("user", user).appendAttr("admin", proxy.isAdmin(user));
|
---|
663 | xb.appendAttr("super-user", webBackend.isSuperUser(user));
|
---|
664 | xb.appendAttr("session-id", sessionId).endElement(true);
|
---|
665 | } else {
|
---|
666 | xb.appendElement("status", "state", "failed", true);
|
---|
667 | webBackend.logger.warning("User '" + user + "' (" + com.bowman.cardserv.util.CustomFormatter.formatAddress(ip) +
|
---|
668 | ") login failure: invalid password");
|
---|
669 | SessionManager.getInstance().fireUserLoginFailed(user, "Web/" + CaProfile.MULTIPLE.getName(), ip,
|
---|
670 | "xml api post auth failed (bad password)");
|
---|
671 | }
|
---|
672 | }
|
---|
673 | } else {
|
---|
674 | XMLConfig sessionXml = xml.getSubConfig("session");
|
---|
675 | if(sessionXml != null) {
|
---|
676 | String sessionId = sessionXml.getString("session-id");
|
---|
677 | if(sessionId != null) authUser = (String)webBackend.sessions.get(sessionId);
|
---|
678 | }
|
---|
679 | }
|
---|
680 |
|
---|
681 | if(authUser != null) {
|
---|
682 | if("cws-status-req".equals(xml.getName())) {
|
---|
683 | onXMLStatus(xml, authUser, xb);
|
---|
684 | } else if("cws-command-req".equals(xml.getName())) {
|
---|
685 | onXMLCtrlCmd(xml, authUser, xb);
|
---|
686 | } else throw new IllegalArgumentException("Malformed request. Expected root element cws-status-req or cws-command-req.");
|
---|
687 | } else if(loginXml == null) xb.appendElement("error", "description", "Not logged in.", true);
|
---|
688 |
|
---|
689 | if("cws-status-req".equals(xml.getName())) {
|
---|
690 | xb.closeElement("cws-status-resp");
|
---|
691 | } else if("cws-command-req".equals(xml.getName())) {
|
---|
692 | xb.closeElement("cws-command-resp");
|
---|
693 | }
|
---|
694 |
|
---|
695 | return xb.toString();
|
---|
696 | }
|
---|
697 |
|
---|
698 | private void onXMLCtrlCmd(XMLConfig xml, String preAuthUser, XmlStringBuffer reply) throws RemoteException {
|
---|
699 | if(!webBackend.isSuperUser(preAuthUser))
|
---|
700 | reply.appendElement("error", "description", "Super-user required.", true);
|
---|
701 | else {
|
---|
702 | XMLConfig cmdXml = xml.getSubConfig("command");
|
---|
703 | if(cmdXml == null) throw new IllegalArgumentException("Malformed request. No command element in cws-command-req.");
|
---|
704 | else {
|
---|
705 | String cmd = cmdXml.getString("command");
|
---|
706 | if(cmd == null) xml.getString("command");
|
---|
707 | if(cmd == null || !ctrlCommands.containsKey(cmd.toLowerCase()))
|
---|
708 | throw new IllegalArgumentException("Malformed request. Unknown command: " + cmd);
|
---|
709 |
|
---|
710 | appendCmdResult(cmd, runCtrlCmd(cmd, cmdXml.flatten(true), preAuthUser), reply);
|
---|
711 | }
|
---|
712 | }
|
---|
713 | }
|
---|
714 |
|
---|
715 | private void onXMLStatus(XMLConfig xml, String authUser, XmlStringBuffer xb) throws RemoteException {
|
---|
716 | String profile = xml.getString("profile");
|
---|
717 | ProfileStatus[] ps = proxy.getUserProfiles(authUser);
|
---|
718 | String[] profiles = getProfileNames(ps, profile);
|
---|
719 |
|
---|
720 | XMLConfig statusCmd;
|
---|
721 | for(Enumeration e = xml.getAllSubConfigs(); e.hasMoreElements(); ) {
|
---|
722 | statusCmd = (XMLConfig)e.nextElement();
|
---|
723 | if("true".equalsIgnoreCase(statusCmd.getString("include"))) {
|
---|
724 | Map params = statusCmd.flatten(true);
|
---|
725 | params.put("ps", ps);
|
---|
726 | params.put("profiles", profiles);
|
---|
727 | params.put("xml", statusCmd);
|
---|
728 | runStatusCmd(statusCmd.getName(), xb, params, authUser);
|
---|
729 | }
|
---|
730 | }
|
---|
731 | }
|
---|
732 |
|
---|
733 | private void xmlFormatProfiles(ProfileStatus[] profiles, XmlStringBuffer xb, boolean admin) throws RemoteException {
|
---|
734 | if(profiles != null) {
|
---|
735 | xb.appendElement("ca-profiles");
|
---|
736 | int mappedServices, capacity;
|
---|
737 | for(int i = 0; i < profiles.length; i++) {
|
---|
738 | mappedServices = getServices(new String[] {profiles[i].getName()}).size();
|
---|
739 | capacity = proxy.getCwsCapacity(new String[] {profiles[i].getName()});
|
---|
740 |
|
---|
741 | xb.appendElement("profile");
|
---|
742 | xb.appendAttr("name", profiles[i].getName());
|
---|
743 | xb.appendAttr("enabled", profiles[i].isEnabled());
|
---|
744 | if(!CaProfile.MULTIPLE.getName().equals(profiles[i].getName())) {
|
---|
745 | xb.appendAttr("cache-only", profiles[i].isCacheOnly());
|
---|
746 | xb.appendAttr("ca-id", profiles[i].getCaId());
|
---|
747 | xb.appendAttr("network-id", profiles[i].getNetworkId());
|
---|
748 | if(profiles[i].getProviderIdents() != null)
|
---|
749 | xb.appendAttr("provider-idents", profiles[i].getProviderIdents());
|
---|
750 | xb.appendAttr("mismatched-cards", profiles[i].isMismatchedCards());
|
---|
751 | xb.appendAttr("provider-match", profiles[i].isRequiresProviderMatch());
|
---|
752 | xb.appendAttr("parsed-services", profiles[i].getServices());
|
---|
753 | xb.appendAttr("parsed-conflicts", profiles[i].getConflicts());
|
---|
754 | if(profiles[i].getResetStr() != null)
|
---|
755 | xb.appendAttr("reset-services", profiles[i].getResetStr());
|
---|
756 | if(profiles[i].getBlockedStr() != null)
|
---|
757 | xb.appendAttr("blocked-services", profiles[i].getBlockedStr());
|
---|
758 | if(profiles[i].getAllowedStr() != null)
|
---|
759 | xb.appendAttr("allowed-services", profiles[i].getAllowedStr());
|
---|
760 | }
|
---|
761 | xb.appendAttr("mapped-services", mappedServices);
|
---|
762 | xb.appendAttr("debug", profiles[i].isDebug());
|
---|
763 | xb.appendAttr("capacity", capacity);
|
---|
764 | xb.appendAttr("sessions", profiles[i].getSessions());
|
---|
765 | xb.appendAttr("max-cw-wait", profiles[i].getMaxCwWait());
|
---|
766 | xb.appendAttr("max-cache-wait", profiles[i].getMaxCacheWait());
|
---|
767 | if(profiles[i].getCongestionLimit() != profiles[i].getMaxCwWait())
|
---|
768 | xb.appendAttr("congestion-limit", profiles[i].getCongestionLimit());
|
---|
769 |
|
---|
770 | xb.endElement(false);
|
---|
771 |
|
---|
772 | PortStatus[] ps = profiles[i].getListenPorts();
|
---|
773 | for(int n = 0; n < ps.length; n++) {
|
---|
774 | if(ps[n].getPort() > 0) {
|
---|
775 | xb.appendElement("listen-port", "name", ps[n].getLabel());
|
---|
776 | xb.appendAttr("protocol", ps[n].getProtocol());
|
---|
777 | xb.appendAttr("port-number", ps[n].getPort());
|
---|
778 | xb.appendAttr("alive", ps[n].isAlive());
|
---|
779 | if(!"".equals(ps[n].getProperties()) && admin) xb.appendAttr("properties", ps[n].getProperties());
|
---|
780 | xb.endElement(true);
|
---|
781 | }
|
---|
782 | }
|
---|
783 |
|
---|
784 | xb.closeElement("profile");
|
---|
785 | }
|
---|
786 | xb.closeElement("ca-profiles");
|
---|
787 | }
|
---|
788 | }
|
---|
789 |
|
---|
790 | public static void xmlFormatConnectors(CwsStatus[] connectors, XmlStringBuffer xb, boolean admin, Map cwsLog,
|
---|
791 | RemoteProxy proxy, String[] profiles) throws RemoteException
|
---|
792 | {
|
---|
793 | if(connectors != null) {
|
---|
794 |
|
---|
795 | xb.appendElement("cws-connectors", "count", connectors.length);
|
---|
796 |
|
---|
797 | CwsStatus conn;
|
---|
798 | for(int i = 0; i < connectors.length; i++) {
|
---|
799 | conn = connectors[i];
|
---|
800 | xb.appendElement("connector");
|
---|
801 | xb.appendAttr("name", conn.getName());
|
---|
802 | xb.appendAttr("protocol", conn.getProtocol());
|
---|
803 | xb.appendAttr("profile", conn.getProfileName());
|
---|
804 | xb.appendAttr("metric", conn.getMetric());
|
---|
805 | xb.appendAttr("status", CWS_STATES[conn.getStatus()]);
|
---|
806 |
|
---|
807 | if(conn.getStatus() == CwsStatus.CWS_CONNECTED || conn.getStatus() == CwsStatus.CWS_UNRESPONSIVE) {
|
---|
808 | TvService[] services = proxy.getServices(conn.getName(), true);
|
---|
809 | if(admin) xb.appendAttr("host", conn.getRemoteHost());
|
---|
810 | if(!CaProfile.MULTIPLE.getName().equals(conn.getProfileName()))
|
---|
811 | xb.appendAttr("provider-idents", conn.getProviderIdents());
|
---|
812 | xb.appendAttr("connected", formatTimeStamp(conn.getConnectTimeStamp()));
|
---|
813 | xb.appendAttr("duration", formatDurationFrom(conn.getConnectTimeStamp()));
|
---|
814 | xb.appendAttr("service-count", services.length);
|
---|
815 | xb.appendAttr("sendq", conn.getSendQ());
|
---|
816 | xb.appendAttr("utilization", conn.getUtilization());
|
---|
817 | xb.appendAttr("avgutilization", conn.getAvgUtilization());
|
---|
818 | xb.appendAttr("ecm-count", conn.getEcmCount());
|
---|
819 | xb.appendAttr("ecm-load", conn.getEcmLoad());
|
---|
820 | xb.appendAttr("emm-count", conn.getEmmCount());
|
---|
821 | xb.appendAttr("timeout-count", conn.getTimeoutCount());
|
---|
822 | xb.appendAttr("capacity", conn.getCapacity());
|
---|
823 | xb.appendAttr("cutime", conn.getCurrentEcmTime());
|
---|
824 | xb.appendAttr("avgtime", conn.getAverageEcmTime());
|
---|
825 | if(admin && conn.getCardData1() != null) xb.appendAttr("card-data1", conn.getCardData1());
|
---|
826 | if(admin && conn.getCardData2() != null) xb.appendAttr("card-data2", conn.getCardData2());
|
---|
827 | if(cwsLog != null && cwsLog.containsKey(conn.getName())) {
|
---|
828 | xb.appendAttr("cws-log", ((List)cwsLog.get(conn.getName())).size());
|
---|
829 | }
|
---|
830 | xb.endElement(false);
|
---|
831 |
|
---|
832 | boolean includeProfile = CaProfile.MULTIPLE.getName().equals(conn.getProfileName());
|
---|
833 | xmlFormatServices(services, xb, false, includeProfile, false, conn.getRecentSids(), profiles);
|
---|
834 | if(admin) xmlFormatRemoteInfo(conn, xb);
|
---|
835 |
|
---|
836 | xb.closeElement("connector");
|
---|
837 | } else if(conn.getStatus() == CwsStatus.CWS_DISCONNECTED) {
|
---|
838 | long secsLeft = (conn.getNextAttemptTimeStamp() - System.currentTimeMillis()) / 1000;
|
---|
839 | String nextAttempt = formatDuration(secsLeft);
|
---|
840 | if(nextAttempt.length() > 0) xb.appendAttr("next-attempt", nextAttempt);
|
---|
841 | if(conn.getDisconnectTimeStamp() > 0)
|
---|
842 | xb.appendAttr("disconnected", formatTimeStamp(conn.getDisconnectTimeStamp()));
|
---|
843 | xb.endElement(true);
|
---|
844 | } else xb.endElement(true);
|
---|
845 | }
|
---|
846 |
|
---|
847 | xb.closeElement("cws-connectors");
|
---|
848 | }
|
---|
849 | }
|
---|
850 |
|
---|
851 | public static void xmlFormatRemoteInfo(CwsStatus connector, XmlStringBuffer xb) {
|
---|
852 | Properties p = connector.getRemoteInfo();
|
---|
853 | if(p == null || p.isEmpty()) return;
|
---|
854 | xb.appendElement("remote-info", "count", p.size());
|
---|
855 | List sorted = new ArrayList(p.keySet());
|
---|
856 | Collections.sort(sorted);
|
---|
857 | String key;
|
---|
858 | for(Iterator iter = sorted.iterator(); iter.hasNext(); ) {
|
---|
859 | key = (String)iter.next();
|
---|
860 | xb.appendElement("cws-param", "name", key);
|
---|
861 | xb.appendAttr("value", p.getProperty(key)).endElement(true);
|
---|
862 | }
|
---|
863 | xb.closeElement("remote-info");
|
---|
864 | }
|
---|
865 |
|
---|
866 | public static void xmlFormatUsers(UserStatus[] users, int loginFailures, XmlStringBuffer xb) {
|
---|
867 | if(users == null) users = new UserStatus[0];
|
---|
868 |
|
---|
869 | xb.appendElement("proxy-users", "count", users.length);
|
---|
870 | xb.appendAttr("login-failures", loginFailures).endElement(false);
|
---|
871 | for(int i = 0; i < users.length; i++) {
|
---|
872 | xb.appendElement("user", "name", users[i].getUserName());
|
---|
873 | xb.appendAttr("display-name", users[i].getDisplayName());
|
---|
874 | xb.appendAttr("start-date", users[i].getStartDate());
|
---|
875 | xb.appendAttr("expire-date", users[i].getExpireDate());
|
---|
876 | if(users[i].isAdmin()) xb.appendAttr("admin", users[i].isAdmin());
|
---|
877 |
|
---|
878 | xb.appendAttr("sessions", users[i].getSessionCount(null));
|
---|
879 | // xb.appendAttr("max-sessions", users[i].getMaxSessions());
|
---|
880 |
|
---|
881 | String name;
|
---|
882 | for(Iterator iter = users[i].getPropertyNames(); iter.hasNext(); ) {
|
---|
883 | name = (String)iter.next();
|
---|
884 | xb.appendAttr(name, users[i].getProperty(name));
|
---|
885 | }
|
---|
886 | xb.endElement(false);
|
---|
887 |
|
---|
888 | SessionStatus[] sessions = users[i].getSessions();
|
---|
889 | SessionStatus ss;
|
---|
890 | for(int n = 0; n < sessions.length; n++) {
|
---|
891 | ss = sessions[n];
|
---|
892 | xb.appendElement("session", "host", ss.getRemoteHost());
|
---|
893 | xb.appendAttr("id", ss.getSessionId());
|
---|
894 | xb.appendAttr("count", users[i].getSessionCount(ss.getProfileName()) + "/" + ss.getMaxSessions());
|
---|
895 | xb.appendAttr("active", ss.isActive());
|
---|
896 | xb.appendAttr("profile", ss.getProfileName());
|
---|
897 | xb.appendAttr("client-id", ss.getClientId());
|
---|
898 | xb.appendAttr("protocol", ss.getProtocol());
|
---|
899 | xb.appendAttr("context", ss.getContext());
|
---|
900 | // hack to indicate au
|
---|
901 | int idx = ss.getContext().indexOf("No (");
|
---|
902 | if(idx != -1) xb.appendAttr("au", ss.getContext().substring(idx + 4, ss.getContext().lastIndexOf(")")));
|
---|
903 | xb.appendAttr("connected", formatTimeStamp(ss.getConnectTimeStamp()));
|
---|
904 | xb.appendAttr("duration", formatDurationFrom(ss.getConnectTimeStamp()));
|
---|
905 | xb.appendAttr("ecm-count", ss.getEcmCount());
|
---|
906 | xb.appendAttr("emm-count", ss.getEmmCount());
|
---|
907 | xb.appendAttr("pending-count", ss.getPendingCount());
|
---|
908 | if(ss.getKaCount() > 0) xb.appendAttr("keepalive-count", ss.getKaCount());
|
---|
909 | xb.appendAttr("last-transaction", ss.getLastTransactionTime());
|
---|
910 | if(ss.getLastZapTimeStamp() > 0) xb.appendAttr("last-zap", formatDurationFrom(ss.getLastZapTimeStamp()));
|
---|
911 | if(ss.getIdleTime() > -1) xb.appendAttr("idle-time", formatDuration(ss.getIdleTime() / 1000));
|
---|
912 |
|
---|
913 | xb.appendAttr("flags", ss.getFlags());
|
---|
914 | xb.appendAttr("avg-ecm-interval", ss.getAvgEcmInterval());
|
---|
915 | xb.endElement(false);
|
---|
916 | xmlFormatServices(new TvService[] {ss.getLastService()}, xb, false, CaProfile.MULTIPLE.getName().equals(ss.getProfileName()), null);
|
---|
917 | xb.closeElement("session");
|
---|
918 | }
|
---|
919 | xb.closeElement("user");
|
---|
920 | }
|
---|
921 | xb.closeElement("proxy-users");
|
---|
922 | }
|
---|
923 |
|
---|
924 | private void xmlFormatLastSeen(SeenEntry[] seen, XmlStringBuffer xb) {
|
---|
925 | xb.appendElement("last-seen", "count", seen.length);
|
---|
926 | for(int i = 0; i < seen.length; i++) {
|
---|
927 | xb.appendElement("entry", "name", seen[i].getName());
|
---|
928 | xb.appendAttr("profile", seen[i].getProfile());
|
---|
929 | xb.appendAttr("last-login", formatTimeStamp(seen[i].getLastLogin()));
|
---|
930 | xb.appendAttr("last-seen", formatTimeStamp(seen[i].getLastSeen()));
|
---|
931 | xb.appendAttr("host", seen[i].getHostAddr());
|
---|
932 | List log = (List)webBackend.userTransactions.get(seen[i].getName());
|
---|
933 | if(log == null) log = new ArrayList();
|
---|
934 | xb.appendAttr("user-log", log.size());
|
---|
935 | /*
|
---|
936 | xb.appendAttr("total-time", formatDuration(seen[i].getTotalTime()));
|
---|
937 | xb.appendAttr("total-ecm-count", seen[i].getTotalEcmCount());
|
---|
938 | */
|
---|
939 | xb.endElement(true);
|
---|
940 | }
|
---|
941 | xb.closeElement("last-seen");
|
---|
942 | }
|
---|
943 |
|
---|
944 | private void xmlFormatLoginFailures(SeenEntry[] seen, XmlStringBuffer xb) {
|
---|
945 | xb.appendElement("login-failures", "count", seen.length);
|
---|
946 | for(int i = 0; i < seen.length; i++) {
|
---|
947 | xb.appendElement("entry", "name", seen[i].getName());
|
---|
948 | xb.appendAttr("context", seen[i].getProfile());
|
---|
949 | xb.appendAttr("last-failure", formatTimeStamp(seen[i].getLastLogin()));
|
---|
950 | xb.appendAttr("first-failure", formatTimeStamp(seen[i].getLastLogout()));
|
---|
951 | xb.appendAttr("failure-count", seen[i].getCount());
|
---|
952 | xb.appendAttr("host", seen[i].getHostAddr());
|
---|
953 | xb.appendAttr("reason", seen[i].getLastReason());
|
---|
954 | xb.endElement(true);
|
---|
955 | }
|
---|
956 | xb.closeElement("login-failures");
|
---|
957 | }
|
---|
958 |
|
---|
959 | public static void xmlFormatServices(TvService[] services, XmlStringBuffer xb, boolean includeCount, boolean includeProfile,
|
---|
960 | int[] recentSids)
|
---|
961 | {
|
---|
962 | xmlFormatServices(services, xb, includeCount, includeProfile, true, recentSids, null);
|
---|
963 | }
|
---|
964 |
|
---|
965 | public static void xmlFormatServices(TvService[] services, XmlStringBuffer xb, boolean includeCount, boolean includeProfile,
|
---|
966 | boolean includeCid, int[] recentSids, String[] profileNames)
|
---|
967 | {
|
---|
968 | if(services != null) {
|
---|
969 | Set sids = null;
|
---|
970 | if(recentSids != null && recentSids.length > 0) {
|
---|
971 | sids = new HashSet();
|
---|
972 | for(int i = 0; i < recentSids.length; i++) sids.add(new Integer(recentSids[i]));
|
---|
973 | }
|
---|
974 | Set profiles = new HashSet();
|
---|
975 | if(profileNames != null) profiles.addAll(Arrays.asList(profileNames));
|
---|
976 | for(int n = 0; n < services.length; n++) {
|
---|
977 | if(services[n] == null || services[n].getId() == -1) continue;
|
---|
978 | if(!profiles.isEmpty())
|
---|
979 | if(!profiles.contains(services[n].getProfileName())) continue;
|
---|
980 | xb.appendElement("service", "id", services[n].getId());
|
---|
981 | if(includeCid) xb.appendAttr("cdata", new ServiceMapping(services[n]).toString());
|
---|
982 | xb.appendAttr("name", services[n].getDisplayName());
|
---|
983 | if(includeCount) xb.appendAttr("watchers", services[n].getWatchers());
|
---|
984 | if(includeProfile) xb.appendAttr("profile", services[n].getProfileName());
|
---|
985 | if(sids != null && sids.contains(new Integer(services[n].getId()))) xb.appendAttr("hit", "true");
|
---|
986 | xb.endElement(true);
|
---|
987 | }
|
---|
988 | }
|
---|
989 | }
|
---|
990 |
|
---|
991 | private void xmlFormatProxyPlugins(XmlStringBuffer xb, String name, boolean admin) throws RemoteException {
|
---|
992 | PluginStatus[] plugins = admin?proxy.getPlugins():new PluginStatus[0];
|
---|
993 | xb.appendElement("proxy-plugins", "count", plugins.length);
|
---|
994 | for(int i = 0; i < plugins.length; i++) {
|
---|
995 | if(name == null || name.equals(plugins[i].getName())) {
|
---|
996 | xb.appendElement("plugin", "name", plugins[i].getName());
|
---|
997 | xb.appendAttr("description", plugins[i].getDescription());
|
---|
998 | xb.appendAttr("class-name", plugins[i].getClassName());
|
---|
999 | xb.endElement(false);
|
---|
1000 |
|
---|
1001 | String key;
|
---|
1002 | for(Iterator iter = plugins[i].getPropertyNames(); iter.hasNext(); ) {
|
---|
1003 | key = (String)iter.next();
|
---|
1004 | xb.appendElement("plugin-param", "name", key);
|
---|
1005 | xb.appendAttr("value", plugins[i].getProperty(key)).endElement(true);
|
---|
1006 | }
|
---|
1007 | xb.closeElement("plugin");
|
---|
1008 | }
|
---|
1009 | }
|
---|
1010 | xb.closeElement("proxy-plugins");
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | private void xmlFormatProxyStatus(XmlStringBuffer xb, String[] profiles) throws RemoteException {
|
---|
1014 | int sessions = proxy.getSessionCount(profiles, false);
|
---|
1015 | int connectors = proxy.getCwsCount(profiles);
|
---|
1016 | long startTime = proxy.getProxyStartTime();
|
---|
1017 | if(sessions == -1 && connectors == -1 && startTime == -1) {
|
---|
1018 | xb.appendElement("proxy-status", "state", "down", true);
|
---|
1019 | } else {
|
---|
1020 | int[] counters = proxy.getCounters();
|
---|
1021 |
|
---|
1022 | xb.appendElement("proxy-status", "state", "up");
|
---|
1023 | xb.appendAttr("name", proxy.getName());
|
---|
1024 | xb.appendAttr("version", CardServProxy.APP_VERSION);
|
---|
1025 | xb.appendAttr("build", CardServProxy.APP_BUILD);
|
---|
1026 | xb.appendAttr("ecm-count", counters[RemoteListener.C_ECMCOUNT]);
|
---|
1027 | xb.appendAttr("ecm-forwards", counters[RemoteListener.C_ECMFORWARDS]);
|
---|
1028 | xb.appendAttr("ecm-cache-hits", counters[RemoteListener.C_ECMCACHEHITS]);
|
---|
1029 | xb.appendAttr("ecm-failures", counters[RemoteListener.C_ECMFAILURES]);
|
---|
1030 | xb.appendAttr("ecm-denied", counters[RemoteListener.C_ECMDENIED]);
|
---|
1031 | xb.appendAttr("ecm-filtered", counters[RemoteListener.C_ECMFILTERED]);
|
---|
1032 | xb.appendAttr("emm-count", counters[RemoteListener.C_EMMCOUNT]);
|
---|
1033 | xb.appendAttr("ecm-rate", counters[RemoteListener.C_ECMRATE]);
|
---|
1034 | xb.appendAttr("probeq", counters[RemoteListener.C_PROBEQ]);
|
---|
1035 | xb.appendAttr("started", formatTimeStamp(startTime));
|
---|
1036 | xb.appendAttr("duration", formatDurationFrom(startTime));
|
---|
1037 | xb.appendAttr("connectors", connectors);
|
---|
1038 | xb.appendAttr("capacity", proxy.getCwsCapacity(profiles));
|
---|
1039 | xb.appendAttr("active-sessions", proxy.getSessionCount(profiles, true));
|
---|
1040 | xb.appendAttr("sessions", sessions);
|
---|
1041 | xb.endElement(false);
|
---|
1042 |
|
---|
1043 | String os = System.getProperty("os.name", "undetermined") + " " + System.getProperty("os.version", "")
|
---|
1044 | + " (" + System.getProperty("os.arch", "unknown") + ")";
|
---|
1045 | Runtime rt = Runtime.getRuntime();
|
---|
1046 |
|
---|
1047 | xb.appendElement("jvm", "name", System.getProperty("java.vm.name", "Unknown"));
|
---|
1048 | xb.appendAttr("version", System.getProperty("java.runtime.version", "0.0.0"));
|
---|
1049 | xb.appendAttr("heap-total", rt.totalMemory() / 1024);
|
---|
1050 | xb.appendAttr("heap-free", rt.freeMemory() / 1024);
|
---|
1051 | xb.appendAttr("threads", Thread.activeCount());
|
---|
1052 |
|
---|
1053 | if(checkFileDescriptors) {
|
---|
1054 | try { // java6+ unix specific jmx info
|
---|
1055 | long openFd = UnixUtil.getOpenFileDescriptorCount();
|
---|
1056 | long maxFd = UnixUtil.getMaxFileDescriptorCount();
|
---|
1057 | if(openFd > 0) xb.appendAttr("filedesc-open", openFd);
|
---|
1058 | if(maxFd > 0) xb.appendAttr("filedesc-max", maxFd);
|
---|
1059 | } catch(Throwable e) {
|
---|
1060 | webBackend.logger.fine("No unix management instrumentation available: " + e);
|
---|
1061 | checkFileDescriptors = false;
|
---|
1062 | }
|
---|
1063 | }
|
---|
1064 | xb.appendAttr("time", formatTimeStamp(System.currentTimeMillis()));
|
---|
1065 | xb.appendAttr("os", os).endElement(true);
|
---|
1066 |
|
---|
1067 | xb.closeElement("proxy-status");
|
---|
1068 | }
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | public static void xmlFormatCacheStatus(CacheStatus cs, XmlStringBuffer xb) {
|
---|
1072 | xb.appendElement("cache-status", "type", cs.getType());
|
---|
1073 | Properties p = cs.getUsageStats();
|
---|
1074 | if(p == null) p = new Properties();
|
---|
1075 | List sorted = new ArrayList(p.keySet());
|
---|
1076 | Collections.sort(sorted);
|
---|
1077 | String key;
|
---|
1078 | for(Iterator iter = sorted.iterator(); iter.hasNext(); ) {
|
---|
1079 | key = (String)iter.next();
|
---|
1080 | xb.appendElement("cache-param", "name", key);
|
---|
1081 | xb.appendAttr("value", p.getProperty(key)).endElement(true);
|
---|
1082 | }
|
---|
1083 | xb.closeElement("cache-status");
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | private void xmlFormatErrorLog(XmlStringBuffer xb, String[] profiles, boolean admin) {
|
---|
1087 | RemoteEvent event;
|
---|
1088 | Set ps = profiles == null ? null : new HashSet(Arrays.asList(profiles));
|
---|
1089 | xb.appendElement("error-log", "size", webBackend.eventLog.size());
|
---|
1090 |
|
---|
1091 | for(Iterator iter = new ArrayList(webBackend.eventLog).iterator(); iter.hasNext(); ) {
|
---|
1092 | event = (RemoteEvent)iter.next();
|
---|
1093 | if(ps == null || event.getProfile() == null || ps.contains(event.getProfile())) {
|
---|
1094 | xb.appendElement("event");
|
---|
1095 | xb.appendAttr("timestamp", formatTimeStamp(event.getTimeStamp()));
|
---|
1096 | xb.appendAttr("type", event.getType());
|
---|
1097 | xb.appendAttr("profile", event.getProfile());
|
---|
1098 | xb.appendAttr("label", event.getLabel());
|
---|
1099 | xb.appendAttr("msg", admin?event.getMessage():"");
|
---|
1100 | xb.endElement(true);
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | xb.closeElement("error-log");
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | private void xmlFormatFileLog(XmlStringBuffer xb) {
|
---|
1107 | RemoteEvent event;
|
---|
1108 | xb.appendElement("file-log", "size", webBackend.fileLog.size());
|
---|
1109 |
|
---|
1110 | for(Iterator iter = new ArrayList(webBackend.fileLog).iterator(); iter.hasNext(); ) {
|
---|
1111 | event = (RemoteEvent)iter.next();
|
---|
1112 | xb.appendElement("event");
|
---|
1113 | xb.appendAttr("timestamp", formatTimeStamp(event.getTimeStamp()));
|
---|
1114 | xb.appendAttr("log-level", event.getProperty("log-level"));
|
---|
1115 | xb.appendAttr("label", event.getLabel());
|
---|
1116 | xb.appendAttr("msg", event.getMessage());
|
---|
1117 | xb.endElement(true);
|
---|
1118 | }
|
---|
1119 | xb.closeElement("file-log");
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 | private void xmlFormatUserLog(XmlStringBuffer xb, String userName, String[] profiles) {
|
---|
1123 | List ecmLog = (List)webBackend.userTransactions.get(userName);
|
---|
1124 | if(ecmLog == null) ecmLog = new ArrayList();
|
---|
1125 | xb.appendElement("user-log", "size", ecmLog.size()).appendAttr("name", userName).endElement(false);
|
---|
1126 | xmlFormatEcmTransactions(xb, ecmLog, profiles, null, false, false);
|
---|
1127 | xb.closeElement("user-log");
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | private void xmlFormatUserWarningLog(XmlStringBuffer xb, String userName, String[] profiles) {
|
---|
1131 | xb.appendElement("user-warning-log");
|
---|
1132 | if(userName != null) xb.appendAttr("name", userName).endElement(false);
|
---|
1133 | xmlFormatEcmTransactions(xb, webBackend.warningLog, profiles, userName, true, false);
|
---|
1134 | xb.closeElement("user-warning-log");
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | private void xmlFormatCwsLog(XmlStringBuffer xb, String cwsName) {
|
---|
1138 | List ecmLog = (List)webBackend.cwsTransactions.get(cwsName);
|
---|
1139 | if(ecmLog == null) ecmLog = new ArrayList();
|
---|
1140 | xb.appendElement("cws-log", "size", ecmLog.size()).appendAttr("name", cwsName).endElement(false);
|
---|
1141 | xmlFormatEcmTransactions(xb, ecmLog, null, null, false, true);
|
---|
1142 | xb.closeElement("cws-log");
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | public static void xmlFormatEcmTransactions(XmlStringBuffer xb, Collection ecmLog, String[] profiles, String userName,
|
---|
1146 | boolean warningsOnly, boolean cwsLog)
|
---|
1147 | {
|
---|
1148 | Set ps = profiles == null ? null : new HashSet(Arrays.asList(profiles));
|
---|
1149 | RemoteEvent event;
|
---|
1150 | for(Iterator iter = new ArrayList(ecmLog).iterator(); iter.hasNext(); ) {
|
---|
1151 | event = (RemoteEvent)iter.next();
|
---|
1152 | if(warningsOnly && !"true".equalsIgnoreCase(event.getProperty("warning"))) continue; // filter out non-warnings
|
---|
1153 | if(warningsOnly && userName != null && !userName.equals(event.getMessage())) continue; // filter out other users
|
---|
1154 | if(ps == null || event.getProfile() == null || ps.contains(event.getProfile())) {
|
---|
1155 | xb.appendElement("ecm");
|
---|
1156 | try {
|
---|
1157 | if(warningsOnly) {
|
---|
1158 | xb.appendAttr("name", event.getMessage());
|
---|
1159 | if(event.getProperty("time-cache") != null) xb.appendAttr("time-cache", event.getProperty("time-cache"));
|
---|
1160 | if(event.getProperty("time-queue") != null) xb.appendAttr("time-queue", event.getProperty("time-queue"));
|
---|
1161 | if(event.getProperty("time-cws") != null) xb.appendAttr("time-cws", event.getProperty("time-cws"));
|
---|
1162 | if(event.getProperty("time-client") != null) xb.appendAttr("time-client", event.getProperty("time-client"));
|
---|
1163 | } else {
|
---|
1164 | if("true".equalsIgnoreCase(event.getProperty("warning"))) xb.appendAttr("warning", "true");
|
---|
1165 | }
|
---|
1166 | if(event.getProperty("count") != null) xb.appendAttr("count", event.getProperty("count"));
|
---|
1167 |
|
---|
1168 | xb.appendAttr("timestamp", formatTimeStamp(Long.parseLong(event.getProperty("timestamp"))));
|
---|
1169 | xb.appendAttr("request-hash", event.getProperty("request-hash"));
|
---|
1170 | xb.appendAttr("ecm-size", event.getProperty("ecm-size"));
|
---|
1171 | if(event.getProperty("cw") != null) xb.appendAttr("cw", event.getProperty("cw"));
|
---|
1172 | if(event.getProperty("ext-newcamd") != null) xb.appendAttr("ext-newcamd", event.getProperty("ext-newcamd"));
|
---|
1173 | xb.appendAttr("session-id", event.getProperty("id"));
|
---|
1174 | xb.appendAttr("service-name", event.getProperty("service"));
|
---|
1175 | if(event.getProperty("provider-ident") != null) xb.appendAttr("provider-ident", event.getProperty("provider-ident"));
|
---|
1176 | if(event.getProperty("ca-id") != null) xb.appendAttr("ca-id", event.getProperty("ca-id"));
|
---|
1177 | if(event.getProperty("network-id") != null) xb.appendAttr("network-id", event.getProperty("network-id"));
|
---|
1178 | if(event.getProperty("origin-id") != null) xb.appendAttr("origin-id", event.getProperty("origin-id"));
|
---|
1179 | if(event.getProperty("reply-sid") != null) xb.appendAttr("reply-sid", event.getProperty("reply-sid"));
|
---|
1180 | xb.appendAttr("time", event.getProperty("time"));
|
---|
1181 | xb.appendAttr("flags", event.getProperty("flags"));
|
---|
1182 | if(event.getProperty("filtered-by") != null) xb.appendAttr("filtered-by", event.getProperty("filtered-by"));
|
---|
1183 |
|
---|
1184 | if(cwsLog) xb.appendAttr("user-name", event.getMessage());
|
---|
1185 | else if(event.getProperty("cws-name") != null) xb.appendAttr("cws-name", event.getProperty("cws-name"));
|
---|
1186 |
|
---|
1187 | xb.endElement(true);
|
---|
1188 | } catch (Exception e) {
|
---|
1189 | e.printStackTrace();
|
---|
1190 | xb.endElement(true);
|
---|
1191 | }
|
---|
1192 | }
|
---|
1193 | }
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | private void xmlFormatCtrlCommands(XmlStringBuffer xb, String cmdName, String grpName) throws RemoteException {
|
---|
1197 | xb.appendElement("ctrl-commands", "count", ctrlCommands.size());
|
---|
1198 | xmlFormatCommands(xb, cmdName, grpName, ctrlCommands, true);
|
---|
1199 | xb.closeElement("ctrl-commands");
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | private void xmlFormatStatusCommands(XmlStringBuffer xb, String cmdName, String grpName, boolean admin) throws RemoteException {
|
---|
1203 | xb.appendElement("status-commands", "count", statusCommands.size());
|
---|
1204 | xmlFormatCommands(xb, cmdName, grpName, statusCommands, admin);
|
---|
1205 | xb.closeElement("status-commands");
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | private void xmlFormatCommands(XmlStringBuffer xb, String cmdName, String grpName, Map commands, boolean admin) throws RemoteException {
|
---|
1209 | Map optionLists = new HashMap(); // merge named lists of options that may be used by multiple commands
|
---|
1210 |
|
---|
1211 | // group by handler
|
---|
1212 | Map groups = new LinkedHashMap();
|
---|
1213 | Command cmd;
|
---|
1214 | for(Iterator iter = commands.values().iterator(); iter.hasNext(); ) {
|
---|
1215 | cmd = (Command)iter.next();
|
---|
1216 | if(!groups.containsKey(cmd.groupLabel)) groups.put(cmd.groupLabel, cmd.handler);
|
---|
1217 | }
|
---|
1218 | String group;
|
---|
1219 | for(Iterator iter = groups.keySet().iterator(); iter.hasNext(); ) {
|
---|
1220 | group = (String)iter.next();
|
---|
1221 | if(grpName == null || grpName.equals(group)) {
|
---|
1222 | xb.appendElement("command-group", "name", group);
|
---|
1223 | xb.appendAttr("handler", groups.get(group).getClass().getName()).endElement(false);
|
---|
1224 | xmlFormatCommandGroup(xb, group, cmdName, commands, optionLists, admin);
|
---|
1225 | xb.closeElement("command-group");
|
---|
1226 | }
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | xmlFormatOptionLists(xb, optionLists);
|
---|
1230 | }
|
---|
1231 |
|
---|
1232 | public static void xmlFormatOptionLists(XmlStringBuffer xb, Map optionLists) {
|
---|
1233 | String name;
|
---|
1234 | for(Iterator iter = optionLists.keySet().iterator(); iter.hasNext(); ) {
|
---|
1235 | name = (String)iter.next();
|
---|
1236 | xb.appendElement("option-list", "name", name);
|
---|
1237 | for(Iterator i = ((Set)optionLists.get(name)).iterator(); i.hasNext(); ) {
|
---|
1238 | xb.appendElement("option", "value", i.next().toString(), true);
|
---|
1239 | }
|
---|
1240 | xb.closeElement("option-list");
|
---|
1241 | }
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | private void xmlFormatCommandGroup(XmlStringBuffer xb, String groupName, String cmdName, Map commands, Map optionLists, boolean admin)
|
---|
1245 | throws RemoteException
|
---|
1246 | {
|
---|
1247 | Command cmd; Command.CommandParam prm;
|
---|
1248 | for(Iterator iter = commands.values().iterator(); iter.hasNext(); ) {
|
---|
1249 | cmd = (Command)iter.next();
|
---|
1250 | if(cmdName != null && !cmd.name.equals(cmdName)) continue;
|
---|
1251 | if(groupName != null && !groupName.equals(cmd.groupLabel)) continue;
|
---|
1252 | if(cmd instanceof StatusCommand && ((StatusCommand)cmd).adminOnly && !admin) continue;
|
---|
1253 | xb.appendElement("command", "name", cmd.name);
|
---|
1254 | xb.appendAttr("label", cmd.label);
|
---|
1255 | xb.appendAttr("description", cmd.description);
|
---|
1256 | if(cmd instanceof CtrlCommand) xb.appendAttr("confirm", ((CtrlCommand)cmd).confirm);
|
---|
1257 | if(cmd instanceof StatusCommand) xb.appendAttr("admin-only", ((StatusCommand)cmd).adminOnly);
|
---|
1258 | xb.endElement(false);
|
---|
1259 | for(Iterator prms = cmd.params.values().iterator(); prms.hasNext(); ) {
|
---|
1260 | prm = (CtrlCommand.CommandParam)prms.next();
|
---|
1261 | if(!admin && prm.adminOnly) continue;
|
---|
1262 | xb.appendElement("command-param", "name", prm.name);
|
---|
1263 | xb.appendAttr("label", prm.label);
|
---|
1264 | if(prm.value != null) xb.appendAttr("value", prm.value);
|
---|
1265 | if(prm.size > 0) xb.appendAttr("size", prm.size);
|
---|
1266 | if(cmd instanceof CtrlCommand) {
|
---|
1267 | xb.appendAttr("allow-arbitrary", prm.allowArbitrary);
|
---|
1268 | if(!prm.allowArbitrary && prm.options.contains("true")) xb.appendAttr("boolean", true);
|
---|
1269 | }
|
---|
1270 | if(cmd instanceof StatusCommand) {
|
---|
1271 | xb.appendAttr("optional", prm.optional);
|
---|
1272 | xb.appendAttr("admin-only", prm.adminOnly);
|
---|
1273 | }
|
---|
1274 | xb.endElement(false);
|
---|
1275 | String[] options = prm.getOptions();
|
---|
1276 | if(options != null)
|
---|
1277 | for(int i = 0; i < options.length; i++) {
|
---|
1278 | if(options[i].startsWith("@")) {
|
---|
1279 | if(!optionLists.containsKey(options[i]))
|
---|
1280 | optionLists.put(options[i], getParamOptions(options[i].substring(1)));
|
---|
1281 | }
|
---|
1282 | xb.appendElement("option", "value", options[i], true);
|
---|
1283 | }
|
---|
1284 | xb.closeElement("command-param");
|
---|
1285 | }
|
---|
1286 | xb.closeElement("command");
|
---|
1287 | }
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | private Set getParamOptions(String source) throws RemoteException {
|
---|
1291 | Set set = new TreeSet();
|
---|
1292 | if("connected-users".equals(source)) {
|
---|
1293 | UserStatus[] users = proxy.getUsersStatus(null, false);
|
---|
1294 | for(int i = 0; i < users.length; i++) set.add(users[i].getUserName());
|
---|
1295 | } else if("active-users".equals(source)) {
|
---|
1296 | UserStatus[] users = proxy.getUsersStatus(null, true);
|
---|
1297 | for(int i = 0; i < users.length; i++) set.add(users[i].getUserName());
|
---|
1298 | } else if("known-users".equals(source)) {
|
---|
1299 | UserStatus[] users = proxy.getUsersStatus(null, false);
|
---|
1300 | for(int i = 0; i < users.length; i++) set.add(users[i].getUserName());
|
---|
1301 | SeenEntry[] seen = proxy.getSeenUsers(null, null, false);
|
---|
1302 | for(int i = 0; i < seen.length; i++) set.add(seen[i].getName());
|
---|
1303 | } else if("offline-users".equals(source)) {
|
---|
1304 | SeenEntry[] seen = proxy.getSeenUsers(null, null, false);
|
---|
1305 | for(int i = 0; i < seen.length; i++) set.add(seen[i].getName());
|
---|
1306 | } else if("profiles".equals(source)) {
|
---|
1307 | ProfileStatus[] profiles = proxy.getProfiles();
|
---|
1308 | for(int i = 0; i < profiles.length; i++)
|
---|
1309 | if(!CaProfile.MULTIPLE.getName().equals(profiles[i].getName())) set.add(profiles[i].getName());
|
---|
1310 | } else if("connectors".equals(source)) {
|
---|
1311 | CwsStatus[] connectors = proxy.getMultiCwsStatus(null);
|
---|
1312 | for(int i = 0; i < connectors.length; i++) set.add(connectors[i].getName());
|
---|
1313 | }
|
---|
1314 | return set;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | public static String formatTimeStamp(long timeStamp) {
|
---|
1318 | synchronized(rfc822fmt) { // incredible - SimpleDateFormat isn't threadsafe
|
---|
1319 | return rfc822fmt.format(new Date(timeStamp));
|
---|
1320 | }
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | public static String formatDuration(long s) {
|
---|
1324 | if(s == 0) return "0s";
|
---|
1325 | int i = 0;
|
---|
1326 | String res = "";
|
---|
1327 | while(s > 0) {
|
---|
1328 | switch(i++) {
|
---|
1329 | case 0:
|
---|
1330 | res += s % 60 + "s";
|
---|
1331 | s = s / 60;
|
---|
1332 | case 1:
|
---|
1333 | if(s % 60 > 0) res = s % 60 + "m " + res;
|
---|
1334 | s = s / 60;
|
---|
1335 | case 2:
|
---|
1336 | if(s % 24 > 0) res = s % 24 + "h " + res;
|
---|
1337 | s = s / 24;
|
---|
1338 | case 3:
|
---|
1339 | if(s % 7 > 0) res = s % 7 + "d " + res;
|
---|
1340 | s = s / 7;
|
---|
1341 | case 4:
|
---|
1342 | if(s > 0) res = s + "w " + res;
|
---|
1343 | s = 0;
|
---|
1344 | }
|
---|
1345 | }
|
---|
1346 | return res;
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | public static String formatDurationFrom(long timeStamp) {
|
---|
1350 | long s = (System.currentTimeMillis() - timeStamp) / 1000;
|
---|
1351 | return formatDuration(s);
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | public static String[] getProfileNames(ProfileStatus[] profiles, String selected) {
|
---|
1355 | String[] names = new String[profiles.length];
|
---|
1356 | for(int i = 0; i < profiles.length; i++) names[i] = profiles[i].getName();
|
---|
1357 | Set profileSet = new HashSet(Arrays.asList(names));
|
---|
1358 | if(selected != null) {
|
---|
1359 | if(profileSet.contains(selected)) names = new String[] {selected};
|
---|
1360 | else names = new String[0]; // selected unknown or not allowed profile
|
---|
1361 | }
|
---|
1362 | return names;
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | public static String getError(String descr) {
|
---|
1366 | return new XmlStringBuffer().appendElement("error", "description", descr, true).toString();
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | public static String getCfgResult(String message) {
|
---|
1370 | return new XmlStringBuffer().appendElement("cfg-result", "message", message, true).toString();
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | private void addCommand(Map commands, Command command, boolean override) {
|
---|
1374 | Command old;
|
---|
1375 | if(commands.containsKey(command.name) && override) {
|
---|
1376 | old = (Command)commands.get(command.name);
|
---|
1377 | old.setOverride(command);
|
---|
1378 | } else {
|
---|
1379 | commands.put(command.name, command);
|
---|
1380 | }
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | private void removeCommand(Map commands, Command command) {
|
---|
1384 | Command cmd = (Command)commands.get(command.name);
|
---|
1385 | if(cmd == command) commands.remove(command.name);
|
---|
1386 | else cmd.setOverride(null);
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | public void registerCommand(Command command) {
|
---|
1390 | registerCommand(command, false);
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | public void registerCommand(Command command, boolean override) {
|
---|
1394 | if(command instanceof CtrlCommand) addCommand(ctrlCommands, command, override);
|
---|
1395 | if(command instanceof StatusCommand) addCommand(statusCommands, command, override);
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | public void unregisterCommand(Command command) {
|
---|
1399 | if(command instanceof CtrlCommand) removeCommand(ctrlCommands, command);
|
---|
1400 | if(command instanceof StatusCommand) removeCommand(statusCommands, command);
|
---|
1401 | }
|
---|
1402 | }
|
---|