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