55985
|
1 |
/*
|
|
2 |
* To change this license header, choose License Headers in Project Properties.
|
|
3 |
* To change this template file, choose Tools | Templates
|
|
4 |
* and open the template in the editor.
|
|
5 |
*/
|
|
6 |
package com.oracle.jmx.remote.rest.http;
|
|
7 |
|
|
8 |
import javax.management.*;
|
|
9 |
import com.oracle.jmx.remote.rest.json.JSONArray;
|
|
10 |
import com.oracle.jmx.remote.rest.json.JSONObject;
|
|
11 |
import com.oracle.jmx.remote.rest.json.JSONPrimitive;
|
|
12 |
import com.oracle.jmx.remote.rest.mapper.JSONMappingException;
|
|
13 |
import com.oracle.jmx.remote.rest.mapper.JSONMappingFactory;
|
|
14 |
import java.net.HttpURLConnection;
|
|
15 |
import java.util.*;
|
|
16 |
|
|
17 |
import static javax.management.MBeanOperationInfo.*;
|
|
18 |
|
|
19 |
/**
|
|
20 |
* @author harsha
|
|
21 |
*/
|
|
22 |
public class GetRequestHandler {
|
|
23 |
|
|
24 |
private final MBeanServer mbeanServer;
|
|
25 |
private final List<String> allowedMBeans;
|
|
26 |
public GetRequestHandler(MBeanServer mServer, List<String> allowedMBeans) {
|
|
27 |
this.mbeanServer = mServer;
|
|
28 |
this.allowedMBeans = allowedMBeans;
|
|
29 |
}
|
|
30 |
|
|
31 |
public synchronized JSONObject handle(String resource, String query) {
|
|
32 |
|
|
33 |
System.out.println("Resource = " + resource + ", Body = " + query);
|
|
34 |
|
|
35 |
try {
|
|
36 |
if ((query == null || query.isEmpty()) && (resource == null || resource.isEmpty())) {
|
|
37 |
return HttpResponse.getJsonObject(HttpURLConnection.HTTP_OK,
|
|
38 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
39 |
new JSONPrimitive("Nothing to see here.. move along"));
|
|
40 |
}
|
|
41 |
if (query != null && resource.isEmpty()) { // Handle default domain
|
|
42 |
String[] tokens = query.split(Tokens.FORWARD_SLASH);
|
|
43 |
if (tokens.length == 1 && tokens[0].equalsIgnoreCase(Tokens.DEFAULT_DOMAIN)) {
|
|
44 |
// Get default domain
|
|
45 |
return HttpResponse.getJsonObject(HttpURLConnection.HTTP_OK,
|
|
46 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
47 |
new JSONPrimitive(mbeanServer.getDefaultDomain()));
|
|
48 |
|
|
49 |
} else { // Get mbeans belonging to a domain
|
|
50 |
|
|
51 |
}
|
|
52 |
} else {
|
|
53 |
// handle string escaping for '/'
|
|
54 |
String[] tokens = resource.split("/");
|
|
55 |
switch (tokens[0]) {
|
|
56 |
case Tokens.DOMAINS:
|
|
57 |
String[] domains = mbeanServer.getDomains();
|
|
58 |
JSONArray jarr = new JSONArray();
|
|
59 |
Arrays.stream(domains).forEach(a -> jarr.add(new JSONPrimitive(a)));
|
|
60 |
return HttpResponse.getJsonObject(HttpURLConnection.HTTP_OK,
|
|
61 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
62 |
jarr);
|
|
63 |
case Tokens.MBEANS:
|
|
64 |
|
|
65 |
//Set<ObjectInstance> mbeans = mbeanServer.queryMBeans(null, null);
|
|
66 |
jarr = new JSONArray();
|
|
67 |
//mbeans.stream()
|
|
68 |
//.map(objIns -> objIns.getObjectName().toString())
|
|
69 |
allowedMBeans.stream().forEach(a -> jarr.add(new JSONPrimitive(a)));
|
|
70 |
return HttpResponse.getJsonObject(HttpURLConnection.HTTP_OK,
|
|
71 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
72 |
jarr);
|
|
73 |
default:
|
|
74 |
if (tokens.length == 2) {
|
|
75 |
if (!allowedMBeans.contains(tokens[0])) {
|
|
76 |
throw new InstanceNotFoundException("Invalid MBean");
|
|
77 |
}
|
|
78 |
ObjectName mbean = ObjectName.getInstance(tokens[0]);
|
|
79 |
JSONObject jsonObject = getJSONObject(readAttributes(mbeanServer, mbean, tokens[1]));
|
|
80 |
return HttpResponse.getJsonObject(HttpURLConnection.HTTP_OK,
|
|
81 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
82 |
jsonObject);
|
|
83 |
} else if (tokens.length == 1 && query != null && !query.isEmpty()) {
|
|
84 |
if (!allowedMBeans.contains(tokens[0])) {
|
|
85 |
throw new InstanceNotFoundException("Invalid MBean");
|
|
86 |
}
|
|
87 |
ObjectName mbean = ObjectName.getInstance(tokens[0]);
|
|
88 |
if (query.startsWith(Tokens.ATTRS)) {
|
|
89 |
String attrs = query.split(Tokens.EQUALS)[1];
|
|
90 |
JSONObject jsonObject = getJSONObject(readAttributes(mbeanServer, mbean, attrs));
|
|
91 |
return HttpResponse.getJsonObject(HttpURLConnection.HTTP_OK,
|
|
92 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
93 |
jsonObject);
|
|
94 |
}
|
|
95 |
} else if (tokens.length == 1 && (query == null || query.isEmpty())) {
|
|
96 |
if (!allowedMBeans.contains(tokens[0])) {
|
|
97 |
throw new InstanceNotFoundException("Invalid MBean");
|
|
98 |
}
|
|
99 |
|
|
100 |
// We get MBeanInfo
|
|
101 |
ObjectName mbeanObj = ObjectName.getInstance(tokens[0]);
|
|
102 |
return HttpResponse.getJsonObject(HttpURLConnection.HTTP_OK,
|
|
103 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
104 |
getMBeanInfo(mbeanServer, mbeanObj));
|
|
105 |
}
|
|
106 |
System.out.println("Unrecognized token : " + tokens[0]);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
} catch (MBeanException | JSONMappingException | IntrospectionException ex) {
|
|
110 |
return HttpResponse.getJsonObject(HttpResponse.getHttpErrorCode(ex),
|
|
111 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
112 |
new JSONPrimitive("Invalid Mbean attribute"));
|
|
113 |
|
|
114 |
} catch (AttributeNotFoundException ex) {
|
|
115 |
return HttpResponse.getJsonObject(HttpResponse.getHttpErrorCode(ex),
|
|
116 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
117 |
new JSONPrimitive(ex.getMessage()));
|
|
118 |
} catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException ex) {
|
|
119 |
return HttpResponse.getJsonObject(HttpResponse.getHttpErrorCode(ex),
|
|
120 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
121 |
new JSONPrimitive("Invalid Mbean"));
|
|
122 |
}
|
|
123 |
return HttpResponse.getJsonObject(HttpURLConnection.HTTP_OK,
|
|
124 |
new JSONPrimitive(resource + (query == null ? "" : query)),
|
|
125 |
new JSONPrimitive("Nothing to see here.. move along"));
|
|
126 |
}
|
|
127 |
|
|
128 |
private JSONObject getMBeanInfo(MBeanServer mbeanServer, ObjectName mbean) throws InstanceNotFoundException, IntrospectionException, ReflectionException {
|
|
129 |
JSONObject jobj = new JSONObject();
|
|
130 |
MBeanInfo mBeanInfo = mbeanServer.getMBeanInfo(mbean);
|
|
131 |
if (mBeanInfo == null) {
|
|
132 |
return jobj;
|
|
133 |
}
|
|
134 |
jobj.put("description", mBeanInfo.getDescription());
|
|
135 |
|
|
136 |
// Populate Attribute Info
|
|
137 |
MBeanAttributeInfo[] attributes = mBeanInfo.getAttributes();
|
|
138 |
JSONArray jarr = new JSONArray();
|
|
139 |
for (MBeanAttributeInfo attr : attributes) {
|
|
140 |
String access;
|
|
141 |
if (attr.isReadable()) {
|
|
142 |
if (attr.isWritable()) {
|
|
143 |
access = "read/write";
|
|
144 |
} else {
|
|
145 |
access = "read-only";
|
|
146 |
}
|
|
147 |
} else if (attr.isWritable()) {
|
|
148 |
access = "write-only";
|
|
149 |
} else {
|
|
150 |
access = "no-access";
|
|
151 |
}
|
|
152 |
JSONObject jobj1 = new JSONObject();
|
|
153 |
jobj1.put("description", attr.getDescription());
|
|
154 |
jobj1.put("name", attr.getName());
|
|
155 |
jobj1.put("type", attr.getType());
|
|
156 |
jobj1.put("access", access);
|
|
157 |
jobj1.put("descriptor", getDescriptorJSON(attr.getDescriptor()));
|
|
158 |
jarr.add(jobj1);
|
|
159 |
}
|
|
160 |
jobj.put("attributeInfo", jarr);
|
|
161 |
|
|
162 |
// Add constructor Info
|
|
163 |
MBeanConstructorInfo[] constructorInfo = mBeanInfo.getConstructors();
|
|
164 |
jarr = new JSONArray();
|
|
165 |
for (MBeanConstructorInfo constructor : constructorInfo) {
|
|
166 |
JSONObject jobj1 = new JSONObject();
|
|
167 |
jobj1.put("description", constructor.getDescription());
|
|
168 |
jobj1.put("name", constructor.getName());
|
|
169 |
JSONArray jarr1 = new JSONArray();
|
|
170 |
for (MBeanParameterInfo paramInfo : constructor.getSignature()) {
|
|
171 |
jarr1.add(getParamJSON(paramInfo));
|
|
172 |
}
|
|
173 |
jobj1.put("signature", jarr1);
|
|
174 |
if (constructor.getDescriptor().getFieldNames().length > 1) {
|
|
175 |
jobj1.put("descriptor", getDescriptorJSON(constructor.getDescriptor()));
|
|
176 |
}
|
|
177 |
jarr.add(jobj1);
|
|
178 |
}
|
|
179 |
jobj.put("constructorInfo", jarr);
|
|
180 |
|
|
181 |
MBeanOperationInfo[] opInfo = mBeanInfo.getOperations();
|
|
182 |
jarr = new JSONArray();
|
|
183 |
|
|
184 |
for (MBeanOperationInfo op : opInfo) {
|
|
185 |
String impactString;
|
|
186 |
switch (op.getImpact()) {
|
|
187 |
case ACTION:
|
|
188 |
impactString = "action";
|
|
189 |
break;
|
|
190 |
case ACTION_INFO:
|
|
191 |
impactString = "action/info";
|
|
192 |
break;
|
|
193 |
case INFO:
|
|
194 |
impactString = "info";
|
|
195 |
break;
|
|
196 |
case UNKNOWN:
|
|
197 |
impactString = "unknown";
|
|
198 |
break;
|
|
199 |
default:
|
|
200 |
impactString = "(" + op.getImpact() + ")";
|
|
201 |
}
|
|
202 |
|
|
203 |
JSONObject jobj1 = new JSONObject();
|
|
204 |
jobj1.put("description", op.getDescription());
|
|
205 |
jobj1.put("name", op.getName());
|
|
206 |
jobj1.put("returnType", op.getReturnType());
|
|
207 |
JSONArray jarr1 = new JSONArray();
|
|
208 |
for (MBeanParameterInfo paramInfo : op.getSignature()) {
|
|
209 |
jarr1.add(getParamJSON(paramInfo));
|
|
210 |
}
|
|
211 |
jobj1.put("signature", jarr1);
|
|
212 |
jobj1.put("impact", impactString);
|
|
213 |
if (op.getDescriptor().getFieldNames().length > 1) {
|
|
214 |
jobj1.put("descriptor", getDescriptorJSON(op.getDescriptor()));
|
|
215 |
}
|
|
216 |
jarr.add(jobj1);
|
|
217 |
}
|
|
218 |
jobj.put("operationInfo", jarr);
|
|
219 |
|
|
220 |
MBeanNotificationInfo[] notifications = mBeanInfo.getNotifications();
|
|
221 |
jarr = new JSONArray();
|
|
222 |
|
|
223 |
for (MBeanNotificationInfo notification : notifications) {
|
|
224 |
|
|
225 |
JSONObject jobj1 = new JSONObject();
|
|
226 |
jobj1.put("description", notification.getDescription());
|
|
227 |
jobj1.put("name", notification.getName());
|
|
228 |
|
|
229 |
JSONArray jarr1 = new JSONArray();
|
|
230 |
for (String notifType : notification.getNotifTypes()) {
|
|
231 |
jarr1.add(new JSONPrimitive(notifType));
|
|
232 |
}
|
|
233 |
jobj1.put("notifTypes", jarr1);
|
|
234 |
if (notification.getDescriptor().getFieldNames().length > 1) {
|
|
235 |
jobj1.put("descriptor", getDescriptorJSON(notification.getDescriptor()));
|
|
236 |
}
|
|
237 |
jarr.add(jobj1);
|
|
238 |
}
|
|
239 |
jobj.put("notificationInfo", jarr);
|
|
240 |
|
|
241 |
jobj.put("descriptor", getDescriptorJSON(mBeanInfo.getDescriptor()));
|
|
242 |
return jobj;
|
|
243 |
}
|
|
244 |
|
|
245 |
private JSONObject getParamJSON(MBeanParameterInfo mParamInfo) {
|
|
246 |
JSONObject jobj1 = new JSONObject();
|
|
247 |
if (mParamInfo.getDescription() != null && !mParamInfo.getDescription().isEmpty()) {
|
|
248 |
jobj1.put("description", mParamInfo.getDescription());
|
|
249 |
}
|
|
250 |
jobj1.put("name", mParamInfo.getName());
|
|
251 |
jobj1.put("type", mParamInfo.getType());
|
|
252 |
if (mParamInfo.getDescriptor() != null && mParamInfo.getDescriptor().getFieldNames().length > 1) {
|
|
253 |
jobj1.put("descriptor", getDescriptorJSON(mParamInfo.getDescriptor()));
|
|
254 |
}
|
|
255 |
return jobj1;
|
|
256 |
}
|
|
257 |
|
|
258 |
private JSONObject getDescriptorJSON(Descriptor descriptor) {
|
|
259 |
JSONObject jobj2 = new JSONObject();
|
|
260 |
try {
|
|
261 |
String[] descNames = descriptor.getFieldNames();
|
|
262 |
for (String descName : descNames) {
|
|
263 |
Object fieldValue = descriptor.getFieldValue(descName);
|
|
264 |
jobj2.put(descName, fieldValue != null ? fieldValue.toString() : null);
|
|
265 |
}
|
|
266 |
} catch (Throwable t) {
|
|
267 |
t.printStackTrace();
|
|
268 |
}
|
|
269 |
return jobj2;
|
|
270 |
}
|
|
271 |
|
|
272 |
private Map<String, Object> readAttributes(MBeanServer mbeanServer,
|
|
273 |
ObjectName objName, String requestStr)
|
|
274 |
throws InstanceNotFoundException, IntrospectionException,
|
|
275 |
ReflectionException, MBeanException, AttributeNotFoundException {
|
|
276 |
requestStr = requestStr.trim();
|
|
277 |
Map<String, Object> result = new HashMap<>();
|
|
278 |
if (requestStr.trim().equalsIgnoreCase("all")) {
|
|
279 |
MBeanInfo mInfo = mbeanServer.getMBeanInfo(objName);
|
|
280 |
String[] attrs = Arrays.stream(mInfo.getAttributes())
|
|
281 |
.map(MBeanAttributeInfo::getName)
|
|
282 |
.toArray(String[]::new);
|
|
283 |
AttributeList attrVals = mbeanServer.getAttributes(objName, attrs);
|
|
284 |
if (attrVals.size() != attrs.length) {
|
|
285 |
List<String> missingAttrs = new ArrayList<>(Arrays.asList(attrs));
|
|
286 |
for (Attribute a : attrVals.asList()) {
|
|
287 |
missingAttrs.remove(a.getName());
|
|
288 |
result.put(a.getName(), a.getValue());
|
|
289 |
}
|
|
290 |
for (String attr : missingAttrs) {
|
|
291 |
try {
|
|
292 |
Object attribute = mbeanServer.getAttribute(objName, attr);
|
|
293 |
} catch (RuntimeException ex) {
|
|
294 |
if (ex.getCause() instanceof UnsupportedOperationException) {
|
|
295 |
result.put(attr, "< Attribute not supported >");
|
|
296 |
} else if (ex.getCause() instanceof IllegalArgumentException) {
|
|
297 |
result.put(attr, "< Invalid attributes >");
|
|
298 |
}
|
|
299 |
continue;
|
|
300 |
}
|
|
301 |
result.put(attr, "< Error: No such attribute >");
|
|
302 |
}
|
|
303 |
} else {
|
|
304 |
attrVals.asList().forEach((a) -> {
|
|
305 |
result.put(a.getName(), a.getValue());
|
|
306 |
});
|
|
307 |
}
|
|
308 |
} else {
|
|
309 |
String[] attrs = Arrays.stream(requestStr.split(Tokens.COMMA))
|
|
310 |
.map(String::trim)
|
|
311 |
.toArray(String[]::new);
|
|
312 |
|
|
313 |
if (attrs.length == 1) {
|
|
314 |
result.put(attrs[0], mbeanServer.getAttribute(objName, attrs[0]));
|
|
315 |
} else {
|
|
316 |
AttributeList attrVals = mbeanServer.getAttributes(objName, attrs);
|
|
317 |
if (attrVals.size() != attrs.length) {
|
|
318 |
List<String> missingAttrs = new ArrayList<>(Arrays.asList(attrs));
|
|
319 |
for (Attribute a : attrVals.asList()) {
|
|
320 |
missingAttrs.remove(a.getName());
|
|
321 |
result.put(a.getName(), a.getValue());
|
|
322 |
}
|
|
323 |
for (String attr : missingAttrs) {
|
|
324 |
result.put(attr, "< Error: No such attribute >");
|
|
325 |
}
|
|
326 |
} else {
|
|
327 |
attrVals.asList().forEach((a) -> {
|
|
328 |
result.put(a.getName(), a.getValue());
|
|
329 |
});
|
|
330 |
}
|
|
331 |
}
|
|
332 |
}
|
|
333 |
return result;
|
|
334 |
}
|
|
335 |
|
|
336 |
private JSONObject getJSONObject(Map<String, Object> attributeMap) throws JSONMappingException {
|
|
337 |
JSONObject jobject = new JSONObject();
|
|
338 |
JSONMappingFactory mappingFactory = JSONMappingFactory.INSTANCE;
|
|
339 |
for (String key : attributeMap.keySet()) {
|
|
340 |
Object attrVal = attributeMap.get(key);
|
|
341 |
if (attrVal == null) {
|
|
342 |
jobject.put(key, new JSONPrimitive());
|
|
343 |
} else if (mappingFactory.getTypeMapper(attrVal) != null) {
|
|
344 |
jobject.put(key, mappingFactory.getTypeMapper(attrVal).toJsonValue(attrVal));
|
|
345 |
}
|
|
346 |
}
|
|
347 |
return jobject;
|
|
348 |
}
|
|
349 |
|
|
350 |
private interface Tokens {
|
|
351 |
|
|
352 |
public static final String DOMAINS = "domains";
|
|
353 |
public static final String MBEANS = "mbeans";
|
|
354 |
public static final String ATTRS = "attributes";
|
|
355 |
public static final String DOMAIN = "domain";
|
|
356 |
public static final String DEFAULT_DOMAIN = "domain=default";
|
|
357 |
public static final String ALL = "all";
|
|
358 |
public static final String EQUALS = "=";
|
|
359 |
public static final String COMMA = ",";
|
|
360 |
public static final String FORWARD_SLASH = "/";
|
|
361 |
}
|
|
362 |
}
|