src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/http/MBeanCollectionResource.java
branchjmx-rest-api
changeset 56006 352a4f213fc6
parent 56002 60ab3b595a8e
child 56007 d6cbabcaf518
equal deleted inserted replaced
56005:90cff2ac77b8 56006:352a4f213fc6
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package com.oracle.jmx.remote.rest.http;
    26 package com.oracle.jmx.remote.rest.http;
    27 
    27 
       
    28 import com.oracle.jmx.remote.rest.json.JSONArray;
    28 import com.oracle.jmx.remote.rest.json.JSONElement;
    29 import com.oracle.jmx.remote.rest.json.JSONElement;
    29 import com.oracle.jmx.remote.rest.json.JSONObject;
    30 import com.oracle.jmx.remote.rest.json.JSONObject;
       
    31 import com.oracle.jmx.remote.rest.json.JSONPrimitive;
    30 import com.oracle.jmx.remote.rest.json.parser.JSONParser;
    32 import com.oracle.jmx.remote.rest.json.parser.JSONParser;
    31 import com.oracle.jmx.remote.rest.json.parser.ParseException;
    33 import com.oracle.jmx.remote.rest.json.parser.ParseException;
    32 import com.oracle.jmx.remote.rest.mapper.JSONMapper;
    34 import com.oracle.jmx.remote.rest.mapper.JSONMapper;
    33 import com.oracle.jmx.remote.rest.mapper.JSONMappingException;
    35 import com.oracle.jmx.remote.rest.mapper.JSONMappingException;
    34 import com.oracle.jmx.remote.rest.mapper.JSONMappingFactory;
    36 import com.oracle.jmx.remote.rest.mapper.JSONMappingFactory;
    36 
    38 
    37 import javax.management.*;
    39 import javax.management.*;
    38 import javax.management.remote.rest.PlatformRestAdapter;
    40 import javax.management.remote.rest.PlatformRestAdapter;
    39 import java.io.IOException;
    41 import java.io.IOException;
    40 import java.io.UnsupportedEncodingException;
    42 import java.io.UnsupportedEncodingException;
    41 import java.net.HttpURLConnection;
    43 import java.net.URLDecoder;
       
    44 import java.nio.charset.StandardCharsets;
    42 import java.util.*;
    45 import java.util.*;
    43 import java.util.concurrent.ConcurrentHashMap;
    46 import java.util.concurrent.ConcurrentHashMap;
    44 import java.util.concurrent.CopyOnWriteArrayList;
    47 import java.util.concurrent.CopyOnWriteArrayList;
    45 import java.util.regex.Matcher;
    48 import java.util.regex.Matcher;
    46 import java.util.regex.Pattern;
    49 import java.util.regex.Pattern;
   132                 new MBeanResource(mBeanServer, objectName)));
   135                 new MBeanResource(mBeanServer, objectName)));
   133     }
   136     }
   134 
   137 
   135     @Override
   138     @Override
   136     public void handle(HttpExchange exchange) throws IOException {
   139     public void handle(HttpExchange exchange) throws IOException {
   137         String path = exchange.getRequestURI().getPath();
   140         String path = URLDecoder.decode(exchange.getRequestURI().getPath(), StandardCharsets.UTF_8.displayName());
       
   141 
   138         if (path.matches(pathPrefix + "/?$")) {
   142         if (path.matches(pathPrefix + "/?$")) {
   139             RestResource.super.handle(exchange);
   143             RestResource.super.handle(exchange);
   140         } else if (path.matches(pathPrefix + "/[^/]+/?.*")) {
   144         } else if (path.matches(pathPrefix + "/[^/]+/?.*")) {
   141             // Extract mbean name
   145             // Extract mbean name
   142             // Forward the request to its corresponding rest resource
   146             // Forward the request to its corresponding rest resource
   159         }
   163         }
   160     }
   164     }
   161 
   165 
   162     @Override
   166     @Override
   163     public HttpResponse doGet(HttpExchange exchange) {
   167     public HttpResponse doGet(HttpExchange exchange) {
   164         final String path = PlatformRestAdapter.getDomain()
       
   165                 + exchange.getRequestURI().getPath().replaceAll("/$", "");
       
   166         try {
   168         try {
       
   169             final String path = PlatformRestAdapter.getDomain()
       
   170                     + URLDecoder.decode(exchange.getRequestURI().getPath(), StandardCharsets.UTF_8.displayName())
       
   171                     .replaceAll("/$", "");
   167             List<ObjectName> filteredMBeans = allowedMbeans;
   172             List<ObjectName> filteredMBeans = allowedMbeans;
   168             Map<String, String> queryMap = HttpUtil.getGetRequestQueryMap(exchange);
   173             Map<String, String> queryMap = HttpUtil.getGetRequestQueryMap(exchange);
   169             String query = exchange.getRequestURI().getQuery();
   174             String query = exchange.getRequestURI().getQuery();
   170             if(query != null && queryMap.isEmpty()) {
   175             if (query != null && queryMap.isEmpty()) {
   171                 return new HttpResponse(HttpResponse.BAD_REQUEST,
   176                 return new HttpResponse(HttpResponse.BAD_REQUEST,
   172                         "Invalid query params : Allowed query keys [query,page]");
   177                         "Invalid query params : Allowed query keys [objectname,page]");
   173             }else if(query != null && !queryMap.isEmpty()) {
   178             } else if (query != null && !queryMap.isEmpty()) {
   174                 Map<String, String> newMap = new HashMap<>(queryMap);
   179                 Map<String, String> newMap = new HashMap<>(queryMap);
   175                 newMap.remove("query");
   180                 newMap.remove("objectname");
   176                 newMap.remove("page");
   181                 newMap.remove("page");
   177                 if(!newMap.isEmpty()) { // Invalid query params
   182                 if (!newMap.isEmpty()) { // Invalid query params
   178                     return new HttpResponse(HttpResponse.BAD_REQUEST,
   183                     return new HttpResponse(HttpResponse.BAD_REQUEST,
   179                             "Invalid query params : Allowed query keys [query,page]");
   184                             "Invalid query params : Allowed query keys [objectname,page]");
   180                 }
   185                 }
   181             }
   186             }
   182             if (queryMap.containsKey("query")) {        // Filter based on ObjectName query
   187             if (queryMap.containsKey("objectname")) {        // Filter based on ObjectName query
   183                 Set<ObjectName> queryMBeans = mBeanServer
   188                 Set<ObjectName> queryMBeans = mBeanServer
   184                         .queryNames(new ObjectName(queryMap.get("query")), null);
   189                         .queryNames(new ObjectName(queryMap.get("objectname")), null);
   185                 queryMBeans.retainAll(allowedMbeans);   // Intersection of two lists
   190                 queryMBeans.retainAll(allowedMbeans);   // Intersection of two lists
   186                 filteredMBeans = new ArrayList<>(queryMBeans);
   191                 filteredMBeans = new ArrayList<>(queryMBeans);
   187             }
   192             }
   188 
   193 
   189             JSONObject _links = HttpUtil.getPaginationLinks(exchange, filteredMBeans, pageSize);
   194             JSONObject _links = HttpUtil.getPaginationLinks(exchange, filteredMBeans, pageSize);
   190             List<ObjectName> mbeanPage = HttpUtil.filterByPage(exchange, filteredMBeans, pageSize);
   195             List<ObjectName> mbeanPage = HttpUtil.filterByPage(exchange, filteredMBeans, pageSize);
   191 
   196 
   192             List<Map<String, String>> items = new ArrayList<>(filteredMBeans.size());
   197             List<Map<String, String>> items = new ArrayList<>(filteredMBeans.size());
   193             mbeanPage.forEach(objectName -> {
   198             for (ObjectName objectName : mbeanPage) {
   194                 Map<String, String> item = new LinkedHashMap<>(2);
   199                 Map<String, String> item = new LinkedHashMap<>();
   195                 item.put("name", objectName.toString());
   200                 item.put("name", objectName.toString());
       
   201                 MBeanResource mBeanResource = mBeanResourceMap.get(objectName.toString());
       
   202                 try {
       
   203                     JSONObject mBeanInfo = mBeanResource.getMBeanInfo(mBeanServer, objectName);
       
   204                     JSONElement element = mBeanInfo.get("descriptor");
       
   205                     if (element != null) {
       
   206                         JSONElement element1 = ((JSONObject) element).get("interfaceClassName");
       
   207                         if (element1 != null) {
       
   208                             String intfName = (String) ((JSONPrimitive) element1).getValue();
       
   209                             item.put("interfaceClassName", intfName);
       
   210                         }
       
   211                     }
       
   212                     element = mBeanInfo.get("className");
       
   213                     if (element != null) {
       
   214                         String className = (String) ((JSONPrimitive) element).getValue();
       
   215                         item.put("className", className);
       
   216                     }
       
   217                     element = mBeanInfo.get("description");
       
   218                     if (element != null) {
       
   219                         String description = (String) ((JSONPrimitive) element).getValue();
       
   220                         item.put("description", description);
       
   221                     }
       
   222                     element = mBeanInfo.get("attributeInfo");
       
   223                     if(element != null) {
       
   224                         item.put("attributeCount", ((JSONArray)element).size() + "");
       
   225                     }
       
   226                     element = mBeanInfo.get("operationInfo");
       
   227                     if(element != null) {
       
   228                         item.put("operationCount", ((JSONArray)element).size() + "");
       
   229                     }
       
   230 
       
   231                 } catch (InstanceNotFoundException | IntrospectionException | ReflectionException e) {
       
   232                 }
       
   233 
   196                 String href = path + "/" + objectName.toString();
   234                 String href = path + "/" + objectName.toString();
   197                 href = HttpUtil.escapeUrl(href);
   235                 href = HttpUtil.escapeUrl(href);
   198                 item.put("href", href);
   236                 item.put("href", href);
   199                 items.add(item);
   237                 items.add(item);
   200             });
   238                 String info = HttpUtil.escapeUrl(href + "/info");
       
   239                 item.put("info", info);
       
   240             }
   201 
   241 
   202             Map<String, String> properties = new HashMap<>();
   242             Map<String, String> properties = new HashMap<>();
   203 
   243 
   204             properties.put("mbeanCount", Integer.toString(filteredMBeans.size()));
   244             properties.put("mbeanCount", Integer.toString(filteredMBeans.size()));
   205 
   245 
   226         }
   266         }
   227     }
   267     }
   228 
   268 
   229     @Override
   269     @Override
   230     public HttpResponse doPost(HttpExchange exchange) {
   270     public HttpResponse doPost(HttpExchange exchange) {
   231         String path = exchange.getRequestURI().getPath();
       
   232         String reqBody = null;
       
   233         try {
   271         try {
       
   272             String path = URLDecoder.decode(exchange.getRequestURI().getPath(),StandardCharsets.UTF_8.displayName());
       
   273             String reqBody = null;
   234             if (path.matches(pathPrefix + "/?$")) { // POST to current URL
   274             if (path.matches(pathPrefix + "/?$")) { // POST to current URL
   235                 reqBody = HttpUtil.readRequestBody(exchange);
   275                 reqBody = HttpUtil.readRequestBody(exchange);
   236                 if (reqBody == null || reqBody.isEmpty()) { // No Parameters
   276                 if (reqBody == null || reqBody.isEmpty()) { // No Parameters
   237                     return HttpResponse.BAD_REQUEST;
   277                     return HttpResponse.BAD_REQUEST;
   238                 }
   278                 }