author | hb |
Thu, 28 Dec 2017 14:43:14 +0530 | |
branch | jmx-rest-api |
changeset 55998 | 54779691e11f |
parent 55997 | f881344569d9 |
child 56006 | 352a4f213fc6 |
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; |
55994 | 36 |
import java.util.List; |
37 |
||
55997 | 38 |
/** |
39 |
* This class handles all the HTTP requests for the base URL |
|
40 |
* for REST adapter. |
|
41 |
*/ |
|
55994 | 42 |
public class MBeanServerCollectionResource implements RestResource { |
43 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
44 |
private final List<MBeanServerResource> restAdapters; |
55994 | 45 |
private final int pageSize = 5; |
46 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
47 |
public MBeanServerCollectionResource(List<MBeanServerResource> adapters, HttpServer server) { |
55994 | 48 |
this.restAdapters = adapters; |
49 |
server.createContext("/jmx/servers", this); |
|
50 |
} |
|
51 |
||
52 |
@Override |
|
53 |
public HttpResponse doGet(HttpExchange exchange) { |
|
54 |
try { |
|
55 |
JSONObject _links = HttpUtil.getPaginationLinks(exchange, restAdapters, pageSize); |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
56 |
List<MBeanServerResource> filteredList = HttpUtil.filterByPage(exchange, restAdapters, pageSize); |
55994 | 57 |
if (filteredList == null) { |
55997 | 58 |
return HttpResponse.OK; |
55994 | 59 |
} |
60 |
||
55997 | 61 |
final String path = PlatformRestAdapter.getDomain() + |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
62 |
exchange.getRequestURI().getPath().replaceAll("/$", ""); |
55994 | 63 |
|
64 |
JSONObject root = new JSONObject(); |
|
55997 | 65 |
if (_links != null && !_links.isEmpty()) { |
66 |
root.put("_links", _links); |
|
55994 | 67 |
} |
68 |
||
55997 | 69 |
root.put("mBeanServerCount", Integer.toString(restAdapters.size())); |
55994 | 70 |
|
71 |
JSONArray list = new JSONArray(); |
|
72 |
filteredList.stream().map((adapter) -> { |
|
73 |
JSONObject result = new JSONObject(); |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
74 |
result.put("name", adapter.getContext()); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
75 |
result.put("href", path + "/" + adapter.getContext()); |
55994 | 76 |
return result; |
77 |
}).forEachOrdered((result) -> { |
|
78 |
list.add(result); |
|
79 |
}); |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
80 |
root.put("mBeanServers", list); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
81 |
return new HttpResponse(HttpURLConnection.HTTP_OK, root.toJsonString()); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
82 |
} catch (UnsupportedEncodingException e) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
83 |
return new HttpResponse(HttpResponse.BAD_REQUEST, |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
84 |
HttpUtil.getRequestCharset(exchange) + " is not supported"); |
55994 | 85 |
} |
86 |
} |
|
87 |
} |