src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/http/MBeanServerCollectionResource.java
author hb
Thu, 04 Jan 2018 14:39:04 +0530
branchjmx-rest-api
changeset 56006 352a4f213fc6
parent 55998 54779691e11f
permissions -rw-r--r--
1. URL decoding of URL 2. MBean info availble in MBeanCollection page 3. MBeanCollection post supports objectname filtering 4. Tests now use reflection instead of Methodhndles 3. couple of bug fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55997
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     1
/*
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     4
 *
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    10
 *
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    15
 * accompanied this code).
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    16
 *
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    20
 *
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    23
 * questions.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    24
 */
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    25
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    26
package com.oracle.jmx.remote.rest.http;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    27
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    28
import com.oracle.jmx.remote.rest.json.JSONArray;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    29
import com.oracle.jmx.remote.rest.json.JSONObject;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    30
import com.sun.net.httpserver.HttpExchange;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    31
import com.sun.net.httpserver.HttpServer;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    32
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    33
import javax.management.remote.rest.PlatformRestAdapter;
55998
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    34
import java.io.UnsupportedEncodingException;
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    35
import java.net.HttpURLConnection;
56006
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    36
import java.net.URLDecoder;
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    37
import java.nio.charset.StandardCharsets;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    38
import java.util.List;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    39
55997
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    40
/**
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    41
 * This class handles all the HTTP requests for the base URL
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    42
 * for REST adapter.
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    43
 */
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    44
public class MBeanServerCollectionResource implements RestResource {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    45
55998
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    46
    private final List<MBeanServerResource> restAdapters;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    47
    private final int pageSize = 5;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    48
55998
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    49
    public MBeanServerCollectionResource(List<MBeanServerResource> adapters, HttpServer server) {
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    50
        this.restAdapters = adapters;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    51
        server.createContext("/jmx/servers", this);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    52
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    53
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    54
    @Override
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    55
    public HttpResponse doGet(HttpExchange exchange) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    56
        try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    57
            JSONObject _links = HttpUtil.getPaginationLinks(exchange, restAdapters, pageSize);
55998
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    58
            List<MBeanServerResource> filteredList = HttpUtil.filterByPage(exchange, restAdapters, pageSize);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    59
            if (filteredList == null) {
55997
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    60
                return HttpResponse.OK;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    61
            }
56006
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    62
            String query = exchange.getRequestURI().getQuery();
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    63
            if (query != null) {
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    64
                return HttpResponse.BAD_REQUEST;
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    65
            }
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    66
            String exchangePath = URLDecoder.decode(exchange.getRequestURI().getPath(), StandardCharsets.UTF_8.displayName())
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    67
                    .replaceAll("/$", "");
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    68
            if (!exchangePath.equalsIgnoreCase("/jmx/servers")) {
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    69
                return HttpResponse.REQUEST_NOT_FOUND;
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    70
            }
352a4f213fc6 1. URL decoding of URL
hb
parents: 55998
diff changeset
    71
            final String path = PlatformRestAdapter.getDomain() + exchangePath;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    72
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    73
            JSONObject root = new JSONObject();
55997
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    74
            if (_links != null && !_links.isEmpty()) {
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    75
                root.put("_links", _links);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    76
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    77
55997
f881344569d9 Added copyright header
hb
parents: 55994
diff changeset
    78
            root.put("mBeanServerCount", Integer.toString(restAdapters.size()));
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    79
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    80
            JSONArray list = new JSONArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    81
            filteredList.stream().map((adapter) -> {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    82
                JSONObject result = new JSONObject();
55998
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    83
                result.put("name", adapter.getContext());
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    84
                result.put("href", path + "/" + adapter.getContext());
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    85
                return result;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    86
            }).forEachOrdered((result) -> {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    87
                list.add(result);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    88
            });
55998
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    89
            root.put("mBeanServers", list);
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    90
            return new HttpResponse(HttpURLConnection.HTTP_OK, root.toJsonString());
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    91
        } catch (UnsupportedEncodingException e) {
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    92
            return new HttpResponse(HttpResponse.BAD_REQUEST,
54779691e11f Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents: 55997
diff changeset
    93
                    HttpUtil.getRequestCharset(exchange) + " is not supported");
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    94
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    95
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    96
}