1 package org.metricshub.wmi;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.regex.Pattern;
27 import java.util.stream.Collectors;
28 import org.metricshub.wmi.exceptions.WqlQuerySyntaxException;
29
30 public abstract class WmiHelper {
31
32
33
34
35 private WmiHelper() {}
36
37 public static final String DEFAULT_NAMESPACE = "ROOT\\CIMV2";
38
39
40
41
42 private static final Pattern WQL_SIMPLE_SELECT_PATTERN = Pattern.compile(
43 "^\\s*SELECT\\s+(\\*|(?!SELECT|FROM|WHERE)[a-z0-9._]+" +
44 "|((?!SELECT|FROM|WHERE)[a-z0-9._]+" +
45 "\\s*,\\s*)+((?!SELECT|FROM|WHERE)[a-z0-9._]+))\\s+" +
46 "FROM\\s+((?!WHERE|FROM)\\w+)\\s*(WHERE\\s+.*)?$",
47 Pattern.CASE_INSENSITIVE
48 );
49
50
51
52
53
54
55
56
57
58 public static boolean isValidWql(final String wqlQuery) {
59 return WQL_SIMPLE_SELECT_PATTERN.matcher(wqlQuery).find();
60 }
61
62
63
64
65
66
67
68
69 public static String createNetworkResource(final String hostname, final String namespace) {
70 Utils.checkNonNull(namespace, "namespace");
71 return hostname == null || hostname.isEmpty() ? namespace : String.format("\\\\%s\\%s", hostname, namespace);
72 }
73
74
75
76
77
78 public static boolean isLocalNetworkResource(final String networkResource) {
79 Utils.checkNonNull(networkResource, "networkResource");
80
81
82 return (
83 !networkResource.startsWith("\\\\") ||
84 networkResource.startsWith("\\\\localhost\\") ||
85 networkResource.startsWith("\\\\127.0.0.1\\") ||
86 networkResource.startsWith("\\\\0:0:0:0:0:0:0:1\\") ||
87 networkResource.startsWith("\\\\::1\\") ||
88 networkResource.startsWith("\\\\0000:0000:0000:0000:0000:0000:0000:0001\\") ||
89 networkResource.toLowerCase().startsWith("\\\\" + Utils.getComputerName().toLowerCase() + "\\")
90 );
91
92
93 }
94
95
96
97
98
99
100
101
102
103
104 public static List<String> extractPropertiesFromResult(final List<Map<String, Object>> resultRows, final String wql) {
105 try {
106 return extractPropertiesFromResult(resultRows, WqlQuery.newInstance(wql));
107 } catch (WqlQuerySyntaxException e) {
108 throw new IllegalStateException(e);
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121
122 public static List<String> extractPropertiesFromResult(
123 final List<Map<String, Object>> resultRows,
124 final WqlQuery wqlQuery
125 ) {
126
127
128
129 if (resultRows.isEmpty()) {
130 return wqlQuery.getSelectedProperties();
131 }
132
133
134 final String[] resultPropertyArray = resultRows.get(0).keySet().toArray(new String[0]);
135
136
137
138 if (wqlQuery.getSelectedProperties().isEmpty()) {
139 Arrays.sort(resultPropertyArray, String.CASE_INSENSITIVE_ORDER);
140 return Arrays.asList(resultPropertyArray);
141 }
142
143
144
145 final List<String> queryProperties = wqlQuery.getSelectedProperties();
146 final Map<String, String> resultProperties = Arrays
147 .asList(resultPropertyArray)
148 .stream()
149 .collect(Collectors.toMap(String::toLowerCase, property -> property));
150 return queryProperties
151 .stream()
152 .map(property -> resultProperties.getOrDefault(property.toLowerCase(), property))
153 .collect(Collectors.toList());
154 }
155 }