193 t.printStackTrace(); |
193 t.printStackTrace(); |
194 } |
194 } |
195 return jobj2; |
195 return jobj2; |
196 } |
196 } |
197 |
197 |
198 private Map<String, Object> getAttributes(String[] attrs) throws IntrospectionException, |
198 private Map<String, Object> getAttributes(String[] attrs) throws InstanceNotFoundException, |
199 InstanceNotFoundException, ReflectionException, AttributeNotFoundException, MBeanException { |
199 ReflectionException { |
200 Map<String, Object> result = new LinkedHashMap<>(); |
200 Map<String, Object> result = new LinkedHashMap<>(); |
201 if (attrs.length == 1) { |
201 if (attrs == null || attrs.length == 0) { |
202 result.put(attrs[0], mBeanServer.getAttribute(objectName, attrs[0])); |
202 return result; |
|
203 } |
|
204 AttributeList attrVals = mBeanServer.getAttributes(objectName, attrs); |
|
205 if (attrVals.size() != attrs.length) { |
|
206 List<String> missingAttrs = new ArrayList<>(Arrays.asList(attrs)); |
|
207 for (Attribute a : attrVals.asList()) { |
|
208 missingAttrs.remove(a.getName()); |
|
209 result.put(a.getName(), a.getValue()); |
|
210 } |
|
211 for (String attr : missingAttrs) { |
|
212 result.put(attr, "< Error: No such attribute >"); |
|
213 } |
203 } else { |
214 } else { |
204 AttributeList attrVals = mBeanServer.getAttributes(objectName, attrs); |
215 attrVals.asList().forEach((a) -> result.put(a.getName(), a.getValue())); |
205 if (attrVals.size() != attrs.length) { |
216 } |
206 List<String> missingAttrs = new ArrayList<>(Arrays.asList(attrs)); |
217 |
207 for (Attribute a : attrVals.asList()) { |
|
208 missingAttrs.remove(a.getName()); |
|
209 result.put(a.getName(), a.getValue()); |
|
210 } |
|
211 for (String attr : missingAttrs) { |
|
212 result.put(attr, "< Error: No such attribute >"); |
|
213 } |
|
214 } else { |
|
215 attrVals.asList().forEach((a) -> result.put(a.getName(), a.getValue())); |
|
216 } |
|
217 } |
|
218 return result; |
218 return result; |
219 } |
219 } |
220 |
220 |
221 private Map<String, Object> getAllAttributes() throws IntrospectionException, |
221 private Map<String, Object> getAllAttributes() throws IntrospectionException, |
222 InstanceNotFoundException, ReflectionException, AttributeNotFoundException, MBeanException { |
222 InstanceNotFoundException, ReflectionException, AttributeNotFoundException, MBeanException { |
580 return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e)); |
582 return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e)); |
581 } |
583 } |
582 return HttpResponse.REQUEST_NOT_FOUND; |
584 return HttpResponse.REQUEST_NOT_FOUND; |
583 } |
585 } |
584 |
586 |
585 private HttpResponse handleBulkRequest(HttpExchange exchange, JSONObject reqObject) { |
587 public JSONElement handleBulkRequest(HttpExchange exchange, JSONObject reqObject) { |
586 try { |
588 |
587 JSONObject result = new JSONObject(); |
589 JSONObject result = new JSONObject(); |
588 |
590 |
589 do { |
591 // Handle attributes |
590 // Handle attributes |
592 JSONElement element = reqObject.get("attributes"); |
591 JSONElement element = reqObject.get("attributes"); |
593 if (element != null && element instanceof JSONObject) { |
592 if (element != null && !(element instanceof JSONObject)) // input validation |
594 JSONObject attrInfo = (JSONObject) element; |
593 break; |
595 JSONObject attrNode = new JSONObject(); |
594 if (element != null && element instanceof JSONObject) { |
596 // Read attributes |
595 JSONObject attrInfo = (JSONObject) element; |
597 JSONElement read = attrInfo.get("get"); |
596 JSONObject attrNode = new JSONObject(); |
598 if (read != null && read instanceof JSONArray) { |
597 // atleast one of get/set must be present |
599 JSONArray jattrs = (JSONArray) read; |
598 if (attrInfo.get("get") == null && attrInfo.get("set") == null) |
600 JSONElement jAttrRead; |
599 break; |
601 Map<String, Object> attrRead = null; |
600 // Read attributes |
602 try { |
601 JSONElement read = attrInfo.get("get"); |
603 String[] attributes = getStrings(jattrs); |
602 if (read != null && !(read instanceof JSONArray)) |
604 attrRead = getAttributes(attributes); |
603 break; |
605 JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(attrRead); |
604 if (read != null && read instanceof JSONArray) { |
606 jAttrRead = typeMapper.toJsonValue(attrRead); |
605 JSONArray jattrs = (JSONArray) read; |
607 } catch (InstanceNotFoundException | ReflectionException | JSONMappingException e) { |
606 String[] attributes = getStrings(jattrs); |
608 jAttrRead = new JSONPrimitive("<ERROR: Unable to retrieve value>"); |
607 Map<String, Object> attrRead = getAttributes(attributes); |
609 } catch (JSONDataException e) { |
608 JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(attrRead); |
610 jAttrRead = new JSONPrimitive("Invalid JSON : " + read.toJsonString()); |
609 JSONElement jAttrRead = typeMapper.toJsonValue(attrRead); |
611 } |
610 attrNode.put("get", jAttrRead); |
612 |
|
613 attrNode.put("get", jAttrRead); |
|
614 } |
|
615 |
|
616 // Write attributes |
|
617 JSONElement write = attrInfo.get("set"); |
|
618 JSONElement jAttrRead; |
|
619 if (write != null && write instanceof JSONObject) { |
|
620 JSONObject jattrs = (JSONObject) write; |
|
621 try { |
|
622 Map<String, Object> attrMap = setAttributes(jattrs); |
|
623 JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(attrMap); |
|
624 jAttrRead = typeMapper.toJsonValue(attrMap); |
|
625 } catch (JSONDataException ex) { |
|
626 jAttrRead = new JSONPrimitive("Invalid JSON : " + write.toJsonString()); |
|
627 } catch (JSONMappingException | IntrospectionException | InstanceNotFoundException | ReflectionException e) { |
|
628 jAttrRead = new JSONPrimitive("<ERROR: Unable to retrieve value>"); |
|
629 } |
|
630 attrNode.put("set", jAttrRead); |
|
631 } |
|
632 result.put("attributes", attrNode); |
|
633 } |
|
634 |
|
635 // Execute operations |
|
636 element = reqObject.get("operations"); |
|
637 if (element != null) { |
|
638 JSONArray operationList; |
|
639 if (element instanceof JSONPrimitive // Single no-arg operation |
|
640 || element instanceof JSONObject) { // single/mulitple operations |
|
641 operationList = new JSONArray(); |
|
642 operationList.add(element); |
|
643 } else if (element instanceof JSONArray) { // List of no-arg/with-arg operation |
|
644 operationList = (JSONArray) element; |
|
645 } else { |
|
646 operationList = new JSONArray(); |
|
647 } |
|
648 JSONObject opResult = new JSONObject(); |
|
649 for (JSONElement elem : operationList) { |
|
650 if (elem instanceof JSONPrimitive |
|
651 && ((JSONPrimitive) elem).getValue() instanceof String) { // no-arg operation |
|
652 String opName = (String) ((JSONPrimitive) elem).getValue(); |
|
653 try { |
|
654 JSONElement obj = execOperation(opName, null); |
|
655 opResult.put(opName, obj); |
|
656 } catch (IllegalArgumentException e) { |
|
657 opResult.put(opName, e.getMessage()); |
|
658 } catch (IntrospectionException | InstanceNotFoundException | MBeanException | ReflectionException e) { |
|
659 opResult.put(opName, "<ERROR while executing operation>"); |
611 } |
660 } |
612 |
661 } else if (elem instanceof JSONObject) { |
613 // Write attributes |
662 Set<String> opNames = ((JSONObject) element).keySet(); |
614 JSONElement write = attrInfo.get("set"); |
663 for (String opName : opNames) { |
615 |
664 try { |
616 if (write != null && !(write instanceof JSONObject)) |
665 JSONElement obj = execOperation(opName, (JSONObject) ((JSONObject) element).get(opName)); |
617 break; |
|
618 |
|
619 if (write != null && write instanceof JSONObject) { |
|
620 JSONObject jattrs = (JSONObject) write; |
|
621 Map<String, Object> attrMap = setAttributes(jattrs); |
|
622 JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(attrMap); |
|
623 JSONElement jAttrRead = typeMapper.toJsonValue(attrMap); |
|
624 attrNode.put("set", jAttrRead); |
|
625 } |
|
626 result.put("attributes", attrNode); |
|
627 } |
|
628 |
|
629 // Execute operations |
|
630 element = reqObject.get("operations"); |
|
631 if (element != null) { |
|
632 JSONArray operationList; |
|
633 if (element instanceof JSONPrimitive // Single no-arg operation |
|
634 || element instanceof JSONObject) { // single/mulitple operations |
|
635 operationList = new JSONArray(); |
|
636 operationList.add(element); |
|
637 } else if (element instanceof JSONArray) { // List of no-arg/with-arg operation |
|
638 operationList = (JSONArray) element; |
|
639 } else { |
|
640 // Should never happen |
|
641 return HttpResponse.BAD_REQUEST; |
|
642 } |
|
643 JSONObject opResult = new JSONObject(); |
|
644 for (JSONElement elem : operationList) { |
|
645 if (elem instanceof JSONPrimitive |
|
646 && ((JSONPrimitive) elem).getValue() instanceof String) { // no-arg operation |
|
647 String opName = (String) ((JSONPrimitive) elem).getValue(); |
|
648 JSONElement obj = execOperation(opName, null); |
|
649 opResult.put(opName, obj); |
666 opResult.put(opName, obj); |
650 } else if (elem instanceof JSONObject) { |
667 } catch (IllegalArgumentException e) { |
651 Set<String> opNames = ((JSONObject) element).keySet(); |
668 opResult.put(opName, e.getMessage()); |
652 for (String opName : opNames) { |
669 } catch (IntrospectionException | InstanceNotFoundException | MBeanException | ReflectionException e) { |
653 JSONElement obj = execOperation(opName, (JSONObject) ((JSONObject) element).get(opName)); |
670 opResult.put(opName, "<ERROR while executing operation>"); |
654 opResult.put(opName, obj); |
|
655 } |
|
656 } |
671 } |
657 } |
672 } |
658 result.put("operations", opResult); |
673 } |
659 } |
674 } |
660 return new HttpResponse(HttpURLConnection.HTTP_OK, result.toJsonString()); |
675 result.put("operations", opResult); |
661 } while (false); |
676 } |
662 throw new JSONDataException("Invalid request body : " + reqObject.toJsonString()); |
677 return result; |
663 } catch (InstanceNotFoundException e) { |
|
664 // Should never happen |
|
665 } catch (ClassNotFoundException | JSONDataException e) { |
|
666 return new HttpResponse(HttpResponse.BAD_REQUEST, "Invalid JSON : " + e.getMessage()); |
|
667 } catch (IntrospectionException | JSONMappingException | MBeanException | ReflectionException e) { |
|
668 return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e)); |
|
669 } catch (IllegalArgumentException e) { |
|
670 return new HttpResponse(HttpResponse.BAD_REQUEST, e.getMessage()); |
|
671 } catch (Exception e) { |
|
672 return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e)); |
|
673 } |
|
674 return HttpResponse.REQUEST_NOT_FOUND; |
|
675 } |
678 } |
676 |
679 |
677 private String[] getStrings(JSONArray jsonArray) throws JSONDataException { |
680 private String[] getStrings(JSONArray jsonArray) throws JSONDataException { |
678 List<String> attributes = new ArrayList<>(); |
681 List<String> attributes = new ArrayList<>(); |
679 for (JSONElement element : jsonArray) { |
682 for (JSONElement element : jsonArray) { |