src/jdk.management.rest/share/classes/jdk/internal/management/remote/rest/http/MBeanCollectionResource.java
author hb
Fri, 19 Jan 2018 13:56:36 +0530
branchjmx-rest-api
changeset 56027 81372436b79e
parent 56026 bd531f08d7c7
permissions -rw-r--r--
Fixed double string escaping, corrected type
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56026
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
     1
/*
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
     4
 *
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    10
 *
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    15
 * accompanied this code).
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    16
 *
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    20
 *
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    23
 * questions.
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    24
 */
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    25
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    26
package jdk.internal.management.remote.rest.http;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    27
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    28
import jdk.internal.management.remote.rest.json.JSONArray;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    29
import jdk.internal.management.remote.rest.json.JSONElement;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    30
import jdk.internal.management.remote.rest.json.JSONObject;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    31
import jdk.internal.management.remote.rest.json.JSONPrimitive;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    32
import jdk.internal.management.remote.rest.json.parser.JSONParser;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    33
import jdk.internal.management.remote.rest.json.parser.ParseException;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    34
import jdk.internal.management.remote.rest.mapper.JSONMapper;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    35
import jdk.internal.management.remote.rest.mapper.JSONMappingException;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    36
import jdk.internal.management.remote.rest.mapper.JSONMappingFactory;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    37
import com.sun.net.httpserver.HttpExchange;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    38
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    39
import javax.management.*;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    40
import jdk.internal.management.remote.rest.PlatformRestAdapter;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    41
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    42
import java.io.IOException;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    43
import java.io.UnsupportedEncodingException;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    44
import java.net.URLDecoder;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    45
import java.nio.charset.StandardCharsets;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    46
import java.util.*;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    47
import java.util.concurrent.ConcurrentHashMap;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    48
import java.util.concurrent.CopyOnWriteArrayList;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    49
import java.util.regex.Matcher;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    50
import java.util.regex.Pattern;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    51
import java.util.stream.Collectors;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    52
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    53
public class MBeanCollectionResource implements RestResource, NotificationListener {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    54
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    55
    private List<ObjectName> allowedMbeans;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    56
    private final MBeanServer mBeanServer;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    57
    private final Map<ObjectName, MBeanResource> mBeanResourceMap = new ConcurrentHashMap<>();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    58
    private static final int pageSize = 10;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    59
    private static final String pathPrefix = "^/?jmx/servers/[a-zA-Z0-9\\-\\.]+/mbeans";
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    60
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    61
    // Only MXBean or any other MBean that uses types
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    62
    // that have a valid mapper functions
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    63
    private boolean isMBeanAllowed(ObjectName objName) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    64
        try {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    65
            MBeanInfo mInfo = mBeanServer.getMBeanInfo(objName);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    66
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    67
            // Return true for MXbean
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    68
            Descriptor desc = mInfo.getDescriptor();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    69
            String isMxBean = (String) desc.getFieldValue("mxbean");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    70
            if (isMxBean != null && isMxBean.equalsIgnoreCase("true"))
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    71
                return true;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    72
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    73
            // Check attribute types
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    74
            MBeanAttributeInfo[] attrsInfo = mInfo.getAttributes();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    75
            for (MBeanAttributeInfo attrInfo : attrsInfo) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    76
                String type = attrInfo.getType();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    77
                if (!JSONMappingFactory.INSTANCE.isTypeMapped(type)) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    78
                    return false;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    79
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    80
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    81
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    82
            // Check operation parameters and return types
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    83
            MBeanOperationInfo[] operations = mInfo.getOperations();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    84
            for (MBeanOperationInfo opInfo : operations) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    85
                MBeanParameterInfo[] signature = opInfo.getSignature();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    86
                for (MBeanParameterInfo sig : signature) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    87
                    if (!JSONMappingFactory.INSTANCE.isTypeMapped(sig.getType())) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    88
                        return false;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    89
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    90
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    91
                if (!JSONMappingFactory.INSTANCE.isTypeMapped(opInfo.getReturnType())) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    92
                    return false;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    93
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    94
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    95
            return true;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    96
        } catch (InstanceNotFoundException | IntrospectionException |
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    97
                ReflectionException | ClassNotFoundException ex) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    98
            ex.printStackTrace();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
    99
            return false;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   100
        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   101
    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   102
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   103
    private void introspectMBeanTypes(MBeanServer server) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   104
        if (allowedMbeans.isEmpty()) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   105
            Set<ObjectInstance> allMBeans = server.queryMBeans(null, null); // get all Mbeans
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   106
            allMBeans.stream().filter((objIns) -> (isMBeanAllowed(objIns.getObjectName())))
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   107
                    .forEachOrdered(objIns -> allowedMbeans.add(objIns.getObjectName()));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   108
        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   109
    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   110
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   111
    @Override
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   112
    public void handleNotification(Notification notification, Object handback) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   113
        try {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   114
            MBeanServerNotification mbs = (MBeanServerNotification) notification;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   115
            if (MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(mbs.getType())) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   116
                ObjectName mBeanName = mbs.getMBeanName();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   117
                if (isMBeanAllowed(mBeanName)) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   118
                    allowedMbeans.add(mBeanName);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   119
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   120
            } else if (MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals(mbs.getType())) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   121
                if (allowedMbeans.contains(mbs.getMBeanName())) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   122
                    allowedMbeans.remove(mbs.getMBeanName());
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   123
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   124
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   125
        } catch (Exception e) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   126
        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   127
    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   128
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   129
    public MBeanCollectionResource(MBeanServer mBeanServer) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   130
        this.mBeanServer = mBeanServer;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   131
        allowedMbeans = new ArrayList<>();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   132
        introspectMBeanTypes(mBeanServer);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   133
        allowedMbeans = new CopyOnWriteArrayList<>(allowedMbeans);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   134
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   135
        // Create a REST handler for each MBean
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   136
        allowedMbeans.forEach(objectName -> mBeanResourceMap.put(objectName,
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   137
                new MBeanResource(mBeanServer, objectName)));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   138
    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   139
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   140
    @Override
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   141
    public void handle(HttpExchange exchange) throws IOException {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   142
        String path = URLDecoder.decode(exchange.getRequestURI().getPath(), StandardCharsets.UTF_8.name());
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   143
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   144
        if (path.matches(pathPrefix + "/?$")) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   145
            RestResource.super.handle(exchange);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   146
        } else if (path.matches(pathPrefix + "/[^/]+/?.*")) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   147
            // Extract mbean name
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   148
            // Forward the request to its corresponding rest resource
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   149
            Pattern mbeans = Pattern.compile(pathPrefix + "/");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   150
            Matcher matcher = mbeans.matcher(path);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   151
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   152
            if (matcher.find()) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   153
                String ss = path.substring(matcher.end());
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   154
                String mBeanName = ss;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   155
                if (ss.indexOf('/') != -1) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   156
                    mBeanName = ss.substring(0, ss.indexOf('/'));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   157
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   158
                try {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   159
                    MBeanResource mBeanResource = mBeanResourceMap.get(new ObjectName(mBeanName));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   160
                    if (mBeanResource == null) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   161
                        HttpUtil.sendResponse(exchange, HttpResponse.REQUEST_NOT_FOUND);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   162
                        return;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   163
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   164
                    mBeanResource.handle(exchange);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   165
                } catch (MalformedObjectNameException e) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   166
                    HttpUtil.sendResponse(exchange, HttpResponse.BAD_REQUEST);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   167
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   168
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   169
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   170
        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   171
    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   172
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   173
    @Override
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   174
    public HttpResponse doGet(HttpExchange exchange) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   175
        try {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   176
            final String path = PlatformRestAdapter.getDomain()
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   177
                    + URLDecoder.decode(exchange.getRequestURI().getPath(), StandardCharsets.UTF_8.displayName())
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   178
                    .replaceAll("/$", "");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   179
            List<ObjectName> filteredMBeans = allowedMbeans;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   180
            Map<String, String> queryMap = HttpUtil.getGetRequestQueryMap(exchange);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   181
            String query = exchange.getRequestURI().getQuery();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   182
            if (query != null && queryMap.isEmpty()) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   183
                return new HttpResponse(HttpResponse.BAD_REQUEST,
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   184
                        "Invalid query params : Allowed query keys [objectname,page]");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   185
            } else if (query != null && !queryMap.isEmpty()) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   186
                Map<String, String> newMap = new HashMap<>(queryMap);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   187
                newMap.remove("objectname");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   188
                newMap.remove("page");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   189
                if (!newMap.isEmpty()) { // Invalid query params
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   190
                    return new HttpResponse(HttpResponse.BAD_REQUEST,
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   191
                            "Invalid query params : Allowed query keys [objectname,page]");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   192
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   193
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   194
            if (queryMap.containsKey("objectname")) {        // Filter based on ObjectName query
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   195
                Set<ObjectName> queryMBeans = mBeanServer
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   196
                        .queryNames(new ObjectName(queryMap.get("objectname")), null);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   197
                queryMBeans.retainAll(allowedMbeans);   // Intersection of two lists
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   198
                filteredMBeans = new ArrayList<>(queryMBeans);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   199
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   200
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   201
            JSONObject _links = HttpUtil.getPaginationLinks(exchange, filteredMBeans, pageSize);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   202
            List<ObjectName> mbeanPage = HttpUtil.filterByPage(exchange, filteredMBeans, pageSize);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   203
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   204
            List<Map<String, String>> items = new ArrayList<>(filteredMBeans.size());
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   205
            for (ObjectName objectName : mbeanPage) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   206
                Map<String, String> item = new LinkedHashMap<>();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   207
                item.put("name", objectName.getCanonicalName());
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   208
                MBeanResource mBeanResource = mBeanResourceMap.get(objectName);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   209
                try {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   210
                    JSONObject mBeanInfo = mBeanResource.getMBeanInfo(mBeanServer, objectName);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   211
                    JSONElement element = mBeanInfo.get("descriptor");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   212
                    if (element != null) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   213
                        JSONElement element1 = ((JSONObject) element).get("interfaceClassName");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   214
                        if (element1 != null) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   215
                            String intfName = (String) ((JSONPrimitive) element1).getValue();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   216
                            item.put("interfaceClassName", intfName);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   217
                        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   218
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   219
                    element = mBeanInfo.get("className");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   220
                    if (element != null) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   221
                        String className = (String) ((JSONPrimitive) element).getValue();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   222
                        item.put("className", className);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   223
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   224
                    element = mBeanInfo.get("description");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   225
                    if (element != null) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   226
                        String description = (String) ((JSONPrimitive) element).getValue();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   227
                        item.put("description", description);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   228
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   229
                    element = mBeanInfo.get("attributeInfo");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   230
                    if (element != null) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   231
                        item.put("attributeCount", ((JSONArray) element).size() + "");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   232
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   233
                    element = mBeanInfo.get("operationInfo");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   234
                    if (element != null) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   235
                        item.put("operationCount", ((JSONArray) element).size() + "");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   236
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   237
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   238
                } catch (InstanceNotFoundException | IntrospectionException | ReflectionException e) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   239
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   240
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   241
                String href = path + "/" + objectName.toString();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   242
                href = HttpUtil.escapeUrl(href);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   243
                item.put("href", href);
56027
81372436b79e Fixed double string escaping, corrected type
hb
parents: 56026
diff changeset
   244
                String info = HttpUtil.escapeUrl(path + "/" + objectName.toString() + "/info");
81372436b79e Fixed double string escaping, corrected type
hb
parents: 56026
diff changeset
   245
                item.put("info", info);
56026
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   246
                items.add(item);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   247
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   248
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   249
            Map<String, String> properties = new HashMap<>();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   250
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   251
            properties.put("mbeanCount", Integer.toString(filteredMBeans.size()));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   252
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   253
            JSONMapper typeMapper1 = JSONMappingFactory.INSTANCE.getTypeMapper(items);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   254
            JSONMapper typeMapper2 = JSONMappingFactory.INSTANCE.getTypeMapper(properties);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   255
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   256
            JSONElement linkElem = typeMapper1.toJsonValue(items);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   257
            JSONElement propElem = typeMapper2.toJsonValue(properties);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   258
            JSONObject jobj = new JSONObject();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   259
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   260
            jobj.putAll((JSONObject) propElem);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   261
            jobj.put("mbeans", linkElem);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   262
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   263
            if (_links != null && !_links.isEmpty()) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   264
                jobj.put("_links", _links);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   265
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   266
            return new HttpResponse(jobj.toJsonString());
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   267
        } catch (JSONMappingException e) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   268
            return HttpResponse.SERVER_ERROR;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   269
        } catch (UnsupportedEncodingException e) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   270
            return HttpResponse.BAD_REQUEST;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   271
        } catch (MalformedObjectNameException e) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   272
            return new HttpResponse(HttpResponse.BAD_REQUEST, "Invalid query string");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   273
        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   274
    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   275
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   276
    @Override
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   277
    public HttpResponse doPost(HttpExchange exchange) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   278
        try {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   279
            String path = URLDecoder.decode(exchange.getRequestURI().getPath(),StandardCharsets.UTF_8.displayName());
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   280
            String reqBody = null;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   281
            if (path.matches(pathPrefix + "/?$")) { // POST to current URL
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   282
                reqBody = HttpUtil.readRequestBody(exchange);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   283
                if (reqBody == null || reqBody.isEmpty()) { // No Parameters
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   284
                    return HttpResponse.BAD_REQUEST;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   285
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   286
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   287
                JSONParser parser = new JSONParser(reqBody);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   288
                JSONElement jsonElement = parser.parse();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   289
                if (!(jsonElement instanceof JSONObject)) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   290
                    return new HttpResponse(HttpResponse.BAD_REQUEST,
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   291
                            "Invalid parameters : [" + reqBody + "]");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   292
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   293
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   294
                JSONObject jsonObject = (JSONObject) jsonElement;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   295
                JSONObject normalizedObject = new JSONObject(jsonObject);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   296
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   297
                // Normalize the input MBean names
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   298
                for (String objectNameString : jsonObject.keySet()) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   299
                    if (!objectNameString.startsWith("?")) { // Ignore object name patterns
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   300
                        JSONElement element = jsonObject.get(objectNameString);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   301
                        normalizedObject.remove(objectNameString);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   302
                        normalizedObject.put(new ObjectName(objectNameString).getCanonicalName(), element);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   303
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   304
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   305
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   306
                jsonObject.clear();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   307
                jsonObject = normalizedObject;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   308
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   309
                Set<String> objectNamePatterns = jsonObject.keySet()
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   310
                        .stream()
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   311
                        .filter(a -> a.startsWith("?"))
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   312
                        .collect(Collectors.toSet());
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   313
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   314
                if (!objectNamePatterns.isEmpty()) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   315
                    for (String pattern : objectNamePatterns) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   316
                        Set<ObjectName> queryMBeans = mBeanServer
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   317
                                .queryNames(new ObjectName(pattern.substring(1)), null);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   318
                        queryMBeans.retainAll(allowedMbeans);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   319
                        JSONElement patternNode = jsonObject.get(pattern);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   320
                        jsonObject.remove(pattern);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   321
                        for (ObjectName queryMBean : queryMBeans) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   322
                            String name = queryMBean.getCanonicalName();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   323
                            if (jsonObject.containsKey(name)) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   324
                                JSONObject obj = new JSONObject();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   325
                                obj.put(name, patternNode);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   326
                                deepMerge(jsonObject, obj);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   327
                            } else {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   328
                                jsonObject.put(name, patternNode);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   329
                            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   330
                        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   331
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   332
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   333
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   334
                JSONObject result = new JSONObject();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   335
                for (String mBeanName : jsonObject.keySet()) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   336
                    MBeanResource mBeanResource = mBeanResourceMap.get(new ObjectName(mBeanName));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   337
                    if (mBeanResource != null) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   338
                        JSONElement element = jsonObject.get(mBeanName);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   339
                        if (element instanceof JSONObject) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   340
                            JSONElement res = mBeanResource.handleBulkRequest
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   341
                                    ((JSONObject) element);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   342
                            result.put(mBeanName, res);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   343
                        } else {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   344
                            result.put(mBeanName, "Invalid input");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   345
                        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   346
                    } else {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   347
                        result.put(mBeanName, "Invalid MBean");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   348
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   349
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   350
                return new HttpResponse(result.toJsonString());
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   351
            } else {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   352
                return HttpResponse.METHOD_NOT_ALLOWED;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   353
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   354
        } catch (ParseException e) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   355
            return new HttpResponse(HttpResponse.BAD_REQUEST, "Invalid JSON String for request body");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   356
        } catch (IOException e) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   357
            return HttpResponse.BAD_REQUEST;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   358
        } catch (MalformedObjectNameException e) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   359
            return new HttpResponse(HttpResponse.BAD_REQUEST, "Invalid query string");
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   360
        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   361
    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   362
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   363
    private JSONObject deepMerge(JSONObject jsonObject1, JSONObject jsonObject2) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   364
        for (String key : jsonObject2.keySet()) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   365
            if (jsonObject1.containsKey(key)) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   366
                if (jsonObject2.get(key) instanceof JSONObject && jsonObject1.get(key) instanceof JSONObject) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   367
                    JSONObject jobj1 = (JSONObject) jsonObject1.get(key);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   368
                    JSONObject jobj2 = (JSONObject) jsonObject2.get(key);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   369
                    jsonObject1.put(key, deepMerge(jobj1, jobj2));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   370
                } else if (jsonObject2.get(key) instanceof JSONArray && jsonObject1.get(key) instanceof JSONArray) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   371
                    JSONArray array1 = (JSONArray) jsonObject1.get(key);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   372
                    JSONArray array2 = (JSONArray) jsonObject2.get(key);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   373
                    for (JSONElement each : array2) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   374
                        if (!array1.contains(each)) {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   375
                            array1.add(each);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   376
                        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   377
                    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   378
                } else {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   379
                    JSONArray array = new JSONArray();
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   380
                    array.add(jsonObject1.get(key));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   381
                    array.add(jsonObject2.get(key));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   382
                    jsonObject1.put(key, array);
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   383
                }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   384
            } else {
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   385
                jsonObject1.put(key, jsonObject2.get(key));
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   386
            }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   387
        }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   388
        return jsonObject1;
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   389
    }
bd531f08d7c7 1. Removed all changes to Java SE APIs
hb
parents:
diff changeset
   390
}