src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/http/MBeanResource.java
author hb
Wed, 27 Dec 2017 16:05:53 +0530
branchjmx-rest-api
changeset 55996 e8d4ccaf6877
parent 55995 a798bdd52997
child 55997 f881344569d9
permissions -rw-r--r--
Bulk operation at MBeanServer level
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     1
package com.oracle.jmx.remote.rest.http;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     2
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     3
import com.oracle.jmx.remote.rest.json.JSONArray;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     4
import com.oracle.jmx.remote.rest.json.JSONElement;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     5
import com.oracle.jmx.remote.rest.json.JSONObject;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     6
import com.oracle.jmx.remote.rest.json.JSONPrimitive;
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
     7
import com.oracle.jmx.remote.rest.json.parser.JSONParser;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
     8
import com.oracle.jmx.remote.rest.json.parser.ParseException;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
     9
import com.oracle.jmx.remote.rest.mapper.JSONDataException;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    10
import com.oracle.jmx.remote.rest.mapper.JSONMapper;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    11
import com.oracle.jmx.remote.rest.mapper.JSONMappingException;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    12
import com.oracle.jmx.remote.rest.mapper.JSONMappingFactory;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    13
import com.sun.net.httpserver.HttpExchange;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    14
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    15
import javax.management.*;
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    16
import javax.management.openmbean.OpenMBeanAttributeInfo;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    17
import javax.management.openmbean.OpenMBeanParameterInfo;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    18
import javax.management.openmbean.OpenType;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    19
import javax.management.remote.rest.PlatformRestAdapter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    20
import java.io.IOException;
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    21
import java.net.HttpURLConnection;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    22
import java.util.*;
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    23
import java.util.regex.Matcher;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    24
import java.util.regex.Pattern;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    25
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    26
import static javax.management.MBeanOperationInfo.*;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    27
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    28
public class MBeanResource implements RestResource {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    29
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    30
    private final ObjectName objectName;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    31
    private final MBeanServer mBeanServer;
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    32
    private static final String pathPrefix = "^/?jmx/servers/[^/]+/mbeans/[^/]+";
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    33
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    34
    private final static Map<String, Class<?>> primitiveToObject = new HashMap<>();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    35
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    36
    static {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    37
        primitiveToObject.put("int", Integer.TYPE);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    38
        primitiveToObject.put("long", Long.TYPE);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    39
        primitiveToObject.put("double", Double.TYPE);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    40
        primitiveToObject.put("float", Float.TYPE);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    41
        primitiveToObject.put("boolean", Boolean.TYPE);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    42
        primitiveToObject.put("char", Character.TYPE);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    43
        primitiveToObject.put("byte", Byte.TYPE);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    44
        primitiveToObject.put("void", Void.TYPE);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    45
        primitiveToObject.put("short", Short.TYPE);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
    46
    }
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    47
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    48
    public MBeanResource(MBeanServer mBeanServer, ObjectName objectName) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    49
        this.mBeanServer = mBeanServer;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    50
        this.objectName = objectName;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    51
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    52
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    53
    private JSONObject getMBeanInfo(MBeanServer mbeanServer, ObjectName mbean) throws InstanceNotFoundException, IntrospectionException, ReflectionException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    54
        JSONObject jobj = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    55
        MBeanInfo mBeanInfo = mbeanServer.getMBeanInfo(mbean);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    56
        if (mBeanInfo == null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    57
            return jobj;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    58
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    59
        jobj.put("description", mBeanInfo.getDescription());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    60
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    61
        // Populate Attribute Info
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    62
        MBeanAttributeInfo[] attributes = mBeanInfo.getAttributes();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    63
        JSONArray jarr = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    64
        for (MBeanAttributeInfo attr : attributes) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    65
            String access;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    66
            if (attr.isReadable()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    67
                if (attr.isWritable()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    68
                    access = "read/write";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    69
                } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    70
                    access = "read-only";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    71
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    72
            } else if (attr.isWritable()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    73
                access = "write-only";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    74
            } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    75
                access = "no-access";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    76
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    77
            JSONObject jobj1 = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    78
            jobj1.put("name", attr.getName());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    79
            jobj1.put("type", attr.getType());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    80
            jobj1.put("access", access);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    81
            jobj1.put("description", attr.getDescription());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    82
            jobj1.put("descriptor", getDescriptorJSON(attr.getDescriptor()));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    83
            jarr.add(jobj1);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    84
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    85
        jobj.put("attributeInfo", jarr);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    86
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    87
        // Add constructor Info
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    88
        MBeanConstructorInfo[] constructorInfo = mBeanInfo.getConstructors();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    89
        jarr = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    90
        for (MBeanConstructorInfo constructor : constructorInfo) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    91
            JSONObject jobj1 = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    92
            jobj1.put("name", constructor.getName());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    93
            JSONArray jarr1 = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    94
            for (MBeanParameterInfo paramInfo : constructor.getSignature()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    95
                jarr1.add(getParamJSON(paramInfo));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    96
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    97
            jobj1.put("signature", jarr1);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    98
            jobj1.put("description", constructor.getDescription());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    99
            if (constructor.getDescriptor().getFieldNames().length > 1) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   100
                jobj1.put("descriptor", getDescriptorJSON(constructor.getDescriptor()));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   101
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   102
            jarr.add(jobj1);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   103
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   104
        jobj.put("constructorInfo", jarr);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   105
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   106
        MBeanOperationInfo[] opInfo = mBeanInfo.getOperations();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   107
        jarr = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   108
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   109
        for (MBeanOperationInfo op : opInfo) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   110
            String impactString;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   111
            switch (op.getImpact()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   112
                case ACTION:
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   113
                    impactString = "action";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   114
                    break;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   115
                case ACTION_INFO:
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   116
                    impactString = "action/info";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   117
                    break;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   118
                case INFO:
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   119
                    impactString = "info";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   120
                    break;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   121
                case UNKNOWN:
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   122
                    impactString = "unknown";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   123
                    break;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   124
                default:
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   125
                    impactString = "(" + op.getImpact() + ")";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   126
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   127
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   128
            JSONObject jobj1 = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   129
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   130
            jobj1.put("name", op.getName());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   131
            JSONArray jarr1 = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   132
            for (MBeanParameterInfo paramInfo : op.getSignature()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   133
                jarr1.add(getParamJSON(paramInfo));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   134
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   135
            jobj1.put("signature", jarr1);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   136
            jobj1.put("returnType", op.getReturnType());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   137
            jobj1.put("impact", impactString);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   138
            jobj1.put("description", op.getDescription());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   139
            if (op.getDescriptor().getFieldNames().length > 1) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   140
                jobj1.put("descriptor", getDescriptorJSON(op.getDescriptor()));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   141
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   142
            jarr.add(jobj1);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   143
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   144
        jobj.put("operationInfo", jarr);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   145
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   146
        MBeanNotificationInfo[] notifications = mBeanInfo.getNotifications();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   147
        jarr = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   148
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   149
        for (MBeanNotificationInfo notification : notifications) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   150
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   151
            JSONObject jobj1 = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   152
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   153
            jobj1.put("name", notification.getName());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   154
            JSONArray jarr1 = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   155
            for (String notifType : notification.getNotifTypes()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   156
                jarr1.add(new JSONPrimitive(notifType));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   157
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   158
            jobj1.put("notifTypes", jarr1);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   159
            jobj1.put("description", notification.getDescription());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   160
            if (notification.getDescriptor().getFieldNames().length > 1) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   161
                jobj1.put("descriptor", getDescriptorJSON(notification.getDescriptor()));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   162
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   163
            jarr.add(jobj1);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   164
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   165
        jobj.put("notificationInfo", jarr);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   166
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   167
        jobj.put("descriptor", getDescriptorJSON(mBeanInfo.getDescriptor()));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   168
        return jobj;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   169
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   170
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   171
    private JSONObject getParamJSON(MBeanParameterInfo mParamInfo) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   172
        JSONObject jobj1 = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   173
        if (mParamInfo.getDescription() != null && !mParamInfo.getDescription().isEmpty()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   174
            jobj1.put("description", mParamInfo.getDescription());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   175
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   176
        jobj1.put("name", mParamInfo.getName());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   177
        jobj1.put("type", mParamInfo.getType());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   178
        if (mParamInfo.getDescriptor() != null && mParamInfo.getDescriptor().getFieldNames().length > 1) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   179
            jobj1.put("descriptor", getDescriptorJSON(mParamInfo.getDescriptor()));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   180
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   181
        return jobj1;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   182
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   183
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   184
    private JSONObject getDescriptorJSON(Descriptor descriptor) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   185
        JSONObject jobj2 = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   186
        try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   187
            String[] descNames = descriptor.getFieldNames();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   188
            for (String descName : descNames) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   189
                Object fieldValue = descriptor.getFieldValue(descName);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   190
                jobj2.put(descName, fieldValue != null ? fieldValue.toString() : null);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   191
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   192
        } catch (Throwable t) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   193
            t.printStackTrace();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   194
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   195
        return jobj2;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   196
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   197
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   198
    private Map<String, Object> getAttributes(String[] attrs) throws InstanceNotFoundException,
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   199
            ReflectionException {
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   200
        Map<String, Object> result = new LinkedHashMap<>();
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   201
        if (attrs == null || attrs.length == 0) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   202
            return result;
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   203
        }
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   204
        AttributeList attrVals = mBeanServer.getAttributes(objectName, attrs);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   205
        if (attrVals.size() != attrs.length) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   206
            List<String> missingAttrs = new ArrayList<>(Arrays.asList(attrs));
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   207
            for (Attribute a : attrVals.asList()) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   208
                missingAttrs.remove(a.getName());
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   209
                result.put(a.getName(), a.getValue());
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   210
            }
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   211
            for (String attr : missingAttrs) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   212
                result.put(attr, "< Error: No such attribute >");
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   213
            }
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   214
        } else {
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   215
            attrVals.asList().forEach((a) -> result.put(a.getName(), a.getValue()));
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   216
        }
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   217
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   218
        return result;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   219
    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   220
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   221
    private Map<String, Object> getAllAttributes() throws IntrospectionException,
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   222
            InstanceNotFoundException, ReflectionException, AttributeNotFoundException, MBeanException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   223
        Map<String, Object> result = new HashMap<>();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   224
        MBeanInfo mInfo = mBeanServer.getMBeanInfo(objectName);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   225
        String[] attrs = Arrays.stream(mInfo.getAttributes())
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   226
                .map(MBeanAttributeInfo::getName)
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   227
                .toArray(String[]::new);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   228
        AttributeList attrVals = mBeanServer.getAttributes(objectName, attrs);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   229
        if (attrVals.size() != attrs.length) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   230
            List<String> missingAttrs = new ArrayList<>(Arrays.asList(attrs));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   231
            for (Attribute a : attrVals.asList()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   232
                missingAttrs.remove(a.getName());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   233
                result.put(a.getName(), a.getValue());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   234
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   235
            for (String attr : missingAttrs) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   236
                try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   237
                    Object attribute = mBeanServer.getAttribute(objectName, attr);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   238
                } catch (RuntimeException ex) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   239
                    if (ex.getCause() instanceof UnsupportedOperationException) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   240
                        result.put(attr, "< Attribute not supported >");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   241
                    } else if (ex.getCause() instanceof IllegalArgumentException) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   242
                        result.put(attr, "< Invalid attributes >");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   243
                    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   244
                    continue;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   245
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   246
                result.put(attr, "< Error: No such attribute >");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   247
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   248
        } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   249
            attrVals.asList().forEach((a) -> {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   250
                result.put(a.getName(), a.getValue());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   251
            });
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   252
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   253
        return result;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   254
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   255
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   256
    private Map<String, Object> setAttributes(JSONObject attrMap) throws JSONDataException,
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   257
            IntrospectionException, InstanceNotFoundException, ReflectionException {
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   258
        if (attrMap == null || attrMap.isEmpty()) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   259
            throw new JSONDataException("Null arguments for set attribute");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   260
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   261
        Map<String, Object> result = new HashMap<>();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   262
        for (String attrName : attrMap.keySet()) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   263
            MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(objectName);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   264
            MBeanAttributeInfo attrInfo = Arrays.stream(mBeanInfo.getAttributes()).filter(a -> a.getName().equals(attrName)).findFirst().orElse(null);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   265
            if (attrInfo == null) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   266
                result.put(attrName, "<Attribute not found>");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   267
            } else if (!attrInfo.isWritable()) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   268
                result.put(attrName, "<Attribute is read-only>");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   269
            } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   270
                JSONMapper mapper;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   271
                if (attrInfo instanceof OpenMBeanAttributeInfo) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   272
                    OpenType<?> type = ((OpenMBeanAttributeInfo) attrInfo).getOpenType();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   273
                    mapper = JSONMappingFactory.INSTANCE.getTypeMapper(type);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   274
                } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   275
                    Class<?> inputCls = primitiveToObject.get(attrInfo.getType());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   276
                    try {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   277
                        if (inputCls == null) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   278
                            inputCls = Class.forName(attrInfo.getType());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   279
                        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   280
                    } catch (ClassNotFoundException | ClassCastException ex) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   281
                        throw new IllegalArgumentException("Invalid parameters : " + attrMap.get(attrName).toJsonString() + " cannot be mapped to : " + attrInfo.getType());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   282
                    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   283
                    mapper = JSONMappingFactory.INSTANCE.getTypeMapper(inputCls);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   284
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   285
                try {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   286
                    Object attrValue = mapper.toJavaObject(attrMap.get(attrName));
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   287
                    Attribute attrObj = new Attribute(attrName, attrValue);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   288
                    mBeanServer.setAttribute(objectName, attrObj);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   289
                    result.put(attrName, "success");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   290
                } catch (InvalidAttributeValueException | JSONDataException e) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   291
                    result.put(attrName, "<Invalid value for the attribute>");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   292
                } catch (AttributeNotFoundException e) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   293
                    result.put(attrName, "<Attribute not found>");
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   294
                } catch (ReflectionException | InstanceNotFoundException | MBeanException e) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   295
                    result.put(attrName, "<ERROR: Unable to retrieve value>");
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   296
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   297
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   298
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   299
        return result;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   300
    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   301
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   302
    private Object mapJsonToType(JSONElement jsonElement, MBeanParameterInfo type) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   303
        if (type instanceof OpenMBeanParameterInfo) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   304
            OpenType<?> openType = ((OpenMBeanParameterInfo) type).getOpenType();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   305
            JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(openType);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   306
            try {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   307
                return typeMapper.toJavaObject(jsonElement);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   308
            } catch (JSONDataException ex) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   309
                throw new IllegalArgumentException("Invalid JSON String : " + jsonElement.toJsonString() + " for arguments");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   310
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   311
        } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   312
            Class<?> inputCls = primitiveToObject.get(type.getType());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   313
            try {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   314
                if (inputCls == null) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   315
                    inputCls = Class.forName(type.getType());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   316
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   317
            } catch (ClassNotFoundException | ClassCastException ex) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   318
                throw new IllegalArgumentException("Invalid parameters : " + jsonElement.toJsonString() + " cannot be mapped to : " + type.getType());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   319
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   320
            JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(inputCls);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   321
            if (typeMapper == null) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   322
                throw new IllegalArgumentException("Invalid parameters : " + jsonElement.toJsonString() + " cannot be mapped to : " + type.getType());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   323
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   324
            try {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   325
                return typeMapper.toJavaObject(jsonElement);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   326
            } catch (JSONDataException ex) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   327
                throw new IllegalArgumentException("Invalid JSON String : " + jsonElement.toJsonString() + " for arguments");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   328
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   329
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   330
    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   331
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   332
    private Map<String, Object> getParameters(Map<String, JSONElement> jsonValues, Map<String, MBeanParameterInfo> typeMap) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   333
        if (jsonValues.size() != typeMap.size()) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   334
            throw new IllegalArgumentException("Invalid parameters : expected - " + typeMap.size() + " parameters, got - " + jsonValues.size());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   335
        }
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   336
        if (!jsonValues.keySet().equals(typeMap.keySet())) {
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   337
            throw new IllegalArgumentException("Invalid parameters - expected : " + Arrays.toString(typeMap.keySet().toArray()));
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   338
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   339
        Map<String, Object> parameters = new LinkedHashMap<>();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   340
        if (typeMap.size() == 0 && jsonValues.isEmpty()) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   341
            return parameters;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   342
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   343
        for (String name : typeMap.keySet()) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   344
            MBeanParameterInfo type = typeMap.get(name);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   345
            JSONElement jsonVal = jsonValues.get(name);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   346
            Object obj = mapJsonToType(jsonVal, type);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   347
            parameters.put(name, obj);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   348
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   349
        return parameters;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   350
    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   351
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   352
    private JSONElement execOperation(String opstr, JSONObject params)
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   353
            throws MBeanException, IntrospectionException, ReflectionException, InstanceNotFoundException {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   354
        if (params == null) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   355
            params = new JSONObject();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   356
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   357
        MBeanInfo mBeanInfo;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   358
        try {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   359
            mBeanInfo = mBeanServer.getMBeanInfo(objectName);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   360
        } catch (InstanceNotFoundException ex) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   361
            throw new IllegalArgumentException("MBean does not exist");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   362
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   363
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   364
        MBeanOperationInfo[] opinfos = Arrays.stream(mBeanInfo.getOperations()).
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   365
                filter(a -> a.getName().equals(opstr)).toArray(MBeanOperationInfo[]::new);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   366
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   367
        if (opinfos.length == 0) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   368
            throw new IllegalArgumentException("Invalid Operation String");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   369
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   370
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   371
        String[] signature = null;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   372
        Object[] parameters = null;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   373
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   374
        if (opinfos.length == 1) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   375
            MBeanParameterInfo[] sig = opinfos[0].getSignature();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   376
            Map<String, MBeanParameterInfo> typeMap = new LinkedHashMap<>();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   377
            Arrays.stream(sig).forEach(e -> typeMap.put(e.getName(), e));
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   378
            parameters = getParameters(params, typeMap).values().toArray();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   379
            signature = Arrays.asList(sig).stream().map(a -> a.getType()).toArray(a -> new String[a]);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   380
        } else if (opinfos.length > 1) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   381
            IllegalArgumentException exception = null;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   382
            for (MBeanOperationInfo opInfo : opinfos) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   383
                MBeanParameterInfo[] sig = opInfo.getSignature();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   384
                try {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   385
                    Map<String, MBeanParameterInfo> typeMap = new LinkedHashMap<>();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   386
                    Arrays.stream(sig).forEach(e -> typeMap.put(e.getName(), e));
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   387
                    parameters = getParameters(params, typeMap).values().toArray();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   388
                    signature = Arrays.asList(sig).stream().map(a -> a.getType()).toArray(a -> new String[a]);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   389
                    exception = null;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   390
                    break;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   391
                } catch (IllegalArgumentException ex) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   392
                    exception = ex;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   393
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   394
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   395
            if (exception != null) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   396
                throw exception;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   397
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   398
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   399
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   400
        Object invoke = null;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   401
        try {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   402
            invoke = mBeanServer.invoke(objectName, opstr, parameters, signature);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   403
            if (invoke != null) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   404
                JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(invoke);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   405
                if (typeMapper != null) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   406
                    return typeMapper.toJsonValue(invoke);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   407
                } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   408
                    return new JSONPrimitive("<Unable to map result to JSON>");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   409
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   410
            } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   411
                return new JSONPrimitive("void");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   412
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   413
        } catch (JSONMappingException e) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   414
            return new JSONPrimitive("<Unable to map result to JSON>");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   415
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   416
    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   417
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   418
    private HttpResponse doMBeanInfo(HttpExchange exchange) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   419
        try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   420
            JSONObject mBeanInfo = getMBeanInfo(mBeanServer, objectName);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   421
            return new HttpResponse(200, mBeanInfo.toJsonString());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   422
        } catch (RuntimeOperationsException | IntrospectionException | ReflectionException e) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   423
            return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   424
        } catch (InstanceNotFoundException e) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   425
            return new HttpResponse(HttpResponse.BAD_REQUEST, "Specified MBean does not exist");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   426
        } catch (Exception e) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   427
            return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   428
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   429
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   430
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   431
    @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   432
    public void handle(HttpExchange exchange) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   433
        String path = exchange.getRequestURI().getPath();
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   434
        if (path.matches(pathPrefix + "/?$")) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   435
            RestResource.super.handle(exchange);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   436
        } else if (path.matches(pathPrefix + "/info$") && exchange.getRequestMethod().equalsIgnoreCase("GET")) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   437
            RestResource.super.handle(exchange);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   438
        } else if (path.matches(pathPrefix + "/[^/]+/?$") && exchange.getRequestMethod().equalsIgnoreCase("POST")) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   439
            RestResource.super.handle(exchange);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   440
        } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   441
            HttpUtil.sendResponse(exchange, new HttpResponse(404, "Not found"));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   442
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   443
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   444
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   445
    @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   446
    public HttpResponse doGet(HttpExchange exchange) {
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   447
        if (exchange.getRequestURI().getPath().endsWith("info")) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   448
            return doMBeanInfo(exchange);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   449
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   450
        String path = PlatformRestAdapter.getAuthority() + exchange.getRequestURI().getPath().replaceAll("\\/$", "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   451
        String info = path + "/info";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   452
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   453
        try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   454
            Map<String, Object> allAttributes = getAllAttributes();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   455
            Map<String, String> _links = new LinkedHashMap<>();
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   456
            _links.put("info", HttpUtil.escapeUrl(info));
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   457
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   458
            MBeanOperationInfo[] opInfo = mBeanServer.getMBeanInfo(objectName).getOperations();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   459
            JSONArray jarr = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   460
            for (MBeanOperationInfo op : opInfo) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   461
                JSONObject jobj1 = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   462
                JSONArray jarr1 = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   463
                jobj1.put("name", op.getName());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   464
                jobj1.put("href", HttpUtil.escapeUrl(path + "/" + op.getName()));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   465
                jobj1.put("method", "POST");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   466
                for (MBeanParameterInfo paramInfo : op.getSignature()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   467
                    JSONObject jobj = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   468
                    jobj.put("name", paramInfo.getName());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   469
                    jobj.put("type", paramInfo.getType());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   470
                    jarr1.add(jobj);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   471
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   472
                jobj1.put("arguments", jarr1);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   473
                jobj1.put("returnType", op.getReturnType());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   474
                jarr.add(jobj1);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   475
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   476
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   477
            JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(allAttributes);
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   478
            if (typeMapper != null) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   479
                JSONElement jsonElement1 = typeMapper.toJsonValue(allAttributes);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   480
                JSONElement jsonElement2 = typeMapper.toJsonValue(_links);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   481
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   482
                JSONObject jobj = new JSONObject();
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   483
                jobj.put("attributes", jsonElement1);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   484
                jobj.put("operations", jarr);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   485
                jobj.put("_links", jsonElement2);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   486
                return new HttpResponse(200, jobj.toJsonString());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   487
            } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   488
                return new HttpResponse(HttpResponse.SERVER_ERROR, "Unable to find JSONMapper");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   489
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   490
        } catch (RuntimeOperationsException | IntrospectionException | ReflectionException | JSONMappingException e) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   491
            return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   492
        } catch (InstanceNotFoundException e) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   493
            return new HttpResponse(HttpResponse.BAD_REQUEST, "Specified MBean does not exist");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   494
        } catch (AttributeNotFoundException e) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   495
            return new HttpResponse(HttpResponse.BAD_REQUEST, "Specified Attribute does not exist");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   496
        } catch (MBeanException e) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   497
            Throwable cause = e.getCause();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   498
            return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   499
        } catch (Exception e) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   500
            return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   501
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   502
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   503
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   504
    /*
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   505
    HTTP POST for this MBean's URL allows setting of attributes and execution of operations.
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   506
    POST request body can follow one of the below formats
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   507
    1. { name : value}
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   508
    Set a single attribute
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   509
    2. { name1 : value1, name2 : value2 }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   510
    Sets multiple attributes
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   511
    3. {attributes : {read : [name]} , {write : {name : value}}, operations : {op_name : {param_name:name, param_value:value}}}
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   512
    This bulk operation request sets multiple attributes and executes multiple
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   513
    operations on the MBean.
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   514
     */
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   515
    @Override
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   516
    public HttpResponse doPost(HttpExchange exchange) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   517
        String path = exchange.getRequestURI().getPath();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   518
        String reqBody = null;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   519
        try {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   520
            if (path.matches(pathPrefix + "/?$")) { // POST to current URL
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   521
                reqBody = HttpUtil.readRequestBody(exchange);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   522
                if (reqBody == null || reqBody.isEmpty()) { // No Parameters
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   523
                    return HttpResponse.BAD_REQUEST;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   524
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   525
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   526
                JSONParser parser = new JSONParser(reqBody);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   527
                JSONElement jsonElement = parser.parse();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   528
                if (!(jsonElement instanceof JSONObject)) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   529
                    return new HttpResponse(HttpResponse.BAD_REQUEST,
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   530
                            "Invalid parameters : [" + reqBody + "]");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   531
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   532
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   533
                JSONObject jsonObject = (JSONObject) jsonElement;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   534
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   535
                if (jsonObject.keySet().contains("attributes") | jsonObject.keySet().contains("operations")) {
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   536
                    return new HttpResponse(HttpURLConnection.HTTP_OK, handleBulkRequest(exchange, jsonObject).toJsonString());
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   537
                } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   538
                    Map<String, Object> stringObjectMap = setAttributes(jsonObject);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   539
                    JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(stringObjectMap);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   540
                    if (typeMapper != null) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   541
                        return new HttpResponse(HttpURLConnection.HTTP_OK, typeMapper.toJsonValue(stringObjectMap).toJsonString());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   542
                    } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   543
                        return new HttpResponse(HttpResponse.SERVER_ERROR, "Unable to find JSON Mapper");
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   544
                    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   545
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   546
            } else if (path.matches(pathPrefix + "/[^/]+/?$")) {  // POST to MBeanOperation
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   547
                Matcher matcher = Pattern.compile(pathPrefix + "/").matcher(path);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   548
                String operation;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   549
                if (matcher.find()) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   550
                    String ss = path.substring(matcher.end());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   551
                    operation = ss;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   552
                } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   553
                    return HttpResponse.BAD_REQUEST;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   554
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   555
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   556
                reqBody = HttpUtil.readRequestBody(exchange);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   557
                JSONElement result;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   558
                if (reqBody == null || reqBody.isEmpty()) { // No Parameters
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   559
                    result = execOperation(operation, null);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   560
                } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   561
                    JSONParser parser = new JSONParser(reqBody);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   562
                    JSONElement jsonElement = parser.parse();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   563
                    if (!(jsonElement instanceof JSONObject)) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   564
                        return new HttpResponse(HttpResponse.BAD_REQUEST,
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   565
                                "Invalid parameters : [" + reqBody + "] for operation - " + operation);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   566
                    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   567
                    result = execOperation(operation, (JSONObject) jsonElement);
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   568
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   569
                return new HttpResponse(HttpURLConnection.HTTP_OK, result.toJsonString());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   570
            } else {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   571
                return HttpResponse.REQUEST_NOT_FOUND;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   572
            }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   573
        } catch (InstanceNotFoundException e) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   574
            // Should never happen
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   575
        } catch (JSONDataException | ParseException e) {
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   576
            return new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, "Invalid JSON : " + reqBody, e.getMessage());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   577
        } catch (IntrospectionException | JSONMappingException | MBeanException | ReflectionException | IOException e) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   578
            return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e));
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   579
        } catch (IllegalArgumentException e) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   580
            return new HttpResponse(HttpResponse.BAD_REQUEST, e.getMessage());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   581
        } catch (Exception e) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   582
            return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e));
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   583
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   584
        return HttpResponse.REQUEST_NOT_FOUND;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   585
    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   586
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   587
    public JSONElement handleBulkRequest(HttpExchange exchange, JSONObject reqObject) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   588
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   589
        JSONObject result = new JSONObject();
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   590
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   591
        // Handle attributes
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   592
        JSONElement element = reqObject.get("attributes");
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   593
        if (element != null && element instanceof JSONObject) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   594
            JSONObject attrInfo = (JSONObject) element;
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   595
            JSONObject attrNode = new JSONObject();
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   596
            // Read attributes
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   597
            JSONElement read = attrInfo.get("get");
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   598
            if (read != null && read instanceof JSONArray) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   599
                JSONArray jattrs = (JSONArray) read;
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   600
                JSONElement jAttrRead;
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   601
                Map<String, Object> attrRead = null;
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   602
                try {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   603
                    String[] attributes = getStrings(jattrs);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   604
                    attrRead = getAttributes(attributes);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   605
                    JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(attrRead);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   606
                    jAttrRead = typeMapper.toJsonValue(attrRead);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   607
                } catch (InstanceNotFoundException | ReflectionException | JSONMappingException e) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   608
                    jAttrRead = new JSONPrimitive("<ERROR: Unable to retrieve value>");
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   609
                } catch (JSONDataException e) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   610
                    jAttrRead = new JSONPrimitive("Invalid JSON : " + read.toJsonString());
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   611
                }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   612
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   613
                attrNode.put("get", jAttrRead);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   614
            }
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   615
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   616
            // Write attributes
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   617
            JSONElement write = attrInfo.get("set");
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   618
            JSONElement jAttrRead;
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   619
            if (write != null && write instanceof JSONObject) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   620
                JSONObject jattrs = (JSONObject) write;
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   621
                try {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   622
                    Map<String, Object> attrMap = setAttributes(jattrs);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   623
                    JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(attrMap);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   624
                    jAttrRead = typeMapper.toJsonValue(attrMap);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   625
                } catch (JSONDataException ex) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   626
                    jAttrRead = new JSONPrimitive("Invalid JSON : " + write.toJsonString());
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   627
                } catch (JSONMappingException | IntrospectionException | InstanceNotFoundException | ReflectionException e) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   628
                    jAttrRead = new JSONPrimitive("<ERROR: Unable to retrieve value>");
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   629
                }
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   630
                attrNode.put("set", jAttrRead);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   631
            }
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   632
            result.put("attributes", attrNode);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   633
        }
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   634
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   635
        // Execute operations
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   636
        element = reqObject.get("operations");
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   637
        if (element != null) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   638
            JSONArray operationList;
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   639
            if (element instanceof JSONPrimitive             // Single no-arg operation
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   640
                    || element instanceof JSONObject) {     // single/mulitple operations
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   641
                operationList = new JSONArray();
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   642
                operationList.add(element);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   643
            } else if (element instanceof JSONArray) {  // List of no-arg/with-arg operation
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   644
                operationList = (JSONArray) element;
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   645
            } else {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   646
                operationList = new JSONArray();
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   647
            }
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   648
            JSONObject opResult = new JSONObject();
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   649
            for (JSONElement elem : operationList) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   650
                if (elem instanceof JSONPrimitive
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   651
                        && ((JSONPrimitive) elem).getValue() instanceof String) { // no-arg operation
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   652
                    String opName = (String) ((JSONPrimitive) elem).getValue();
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   653
                    try {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   654
                        JSONElement obj = execOperation(opName, null);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   655
                        opResult.put(opName, obj);
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   656
                    } catch (IllegalArgumentException e) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   657
                        opResult.put(opName, e.getMessage());
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   658
                    } catch (IntrospectionException | InstanceNotFoundException | MBeanException | ReflectionException e) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   659
                        opResult.put(opName, "<ERROR while executing operation>");
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   660
                    }
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   661
                } else if (elem instanceof JSONObject) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   662
                    Set<String> opNames = ((JSONObject) element).keySet();
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   663
                    for (String opName : opNames) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   664
                        try {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   665
                            JSONElement obj = execOperation(opName, (JSONObject) ((JSONObject) element).get(opName));
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   666
                            opResult.put(opName, obj);
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   667
                        } catch (IllegalArgumentException e) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   668
                            opResult.put(opName, e.getMessage());
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   669
                        } catch (IntrospectionException | InstanceNotFoundException | MBeanException | ReflectionException e) {
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   670
                            opResult.put(opName, "<ERROR while executing operation>");
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   671
                        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   672
                    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   673
                }
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   674
            }
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   675
            result.put("operations", opResult);
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   676
        }
55996
e8d4ccaf6877 Bulk operation at MBeanServer level
hb
parents: 55995
diff changeset
   677
        return result;
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   678
    }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   679
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   680
    private String[] getStrings(JSONArray jsonArray) throws JSONDataException {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   681
        List<String> attributes = new ArrayList<>();
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   682
        for (JSONElement element : jsonArray) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   683
            if (element instanceof JSONPrimitive && ((JSONPrimitive) element).getValue() instanceof String) {
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   684
                JSONPrimitive val = (JSONPrimitive) element;
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   685
                attributes.add((String) val.getValue());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   686
            } else throw new JSONDataException("Expecting String, got " + element.toJsonString());
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   687
        }
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   688
        return attributes.toArray(new String[0]);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   689
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   690
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   691
    @Override
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   692
    public HttpResponse doPut(HttpExchange exchange) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   693
        return null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   694
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   695
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   696
    @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   697
    public HttpResponse doDelete(HttpExchange exchange) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   698
        return null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   699
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   700
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   701
    @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   702
    public HttpResponse doHead(HttpExchange exchange) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   703
        return null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   704
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   705
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   706
}