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