src/java.management.rest/share/classes/com/oracle/jmx/remote/rest/http/HttpUtil.java
author hb
Fri, 05 Jan 2018 13:42:53 +0530
branchjmx-rest-api
changeset 56007 d6cbabcaf518
parent 56006 352a4f213fc6
permissions -rw-r--r--
1. MBeanCollection Bulk operation via objectname query 2. All MBeans identified by getCanonicalName instead of toString
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55997
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
     1
/*
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
     4
 *
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    10
 *
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    15
 * accompanied this code).
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    16
 *
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    20
 *
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    23
 * questions.
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
    24
 */
f881344569d9 Added copyright header
hb
parents: 55995
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.JSONObject;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    29
import com.sun.net.httpserver.Headers;
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
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    32
import javax.management.remote.rest.PlatformRestAdapter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    33
import java.io.*;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    34
import java.net.URI;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    35
import java.net.URL;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    36
import java.net.URLDecoder;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    37
import java.net.URLEncoder;
56006
352a4f213fc6 1. URL decoding of URL
hb
parents: 56002
diff changeset
    38
import java.nio.charset.StandardCharsets;
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    39
import java.util.Base64;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    40
import java.util.LinkedHashMap;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    41
import java.util.List;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    42
import java.util.Map;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    43
import java.util.stream.Collectors;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    44
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    45
public class HttpUtil {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    46
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    47
    public static String getRequestCharset(HttpExchange ex) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    48
        String charset = null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    49
        List<String> contentType = ex.getRequestHeaders().get("Content-type");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    50
        if (contentType != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    51
            for (String kv : contentType) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    52
                for (String value : kv.split(";")) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    53
                    value = value.trim();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    54
                    if (value.toLowerCase().startsWith("charset=")) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    55
                        charset = value.substring("charset=".length());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    56
                    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    57
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    58
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    59
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    60
        return charset;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    61
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    62
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    63
    public static String getAcceptCharset(HttpExchange ex) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    64
        List<String> acceptCharset = ex.getRequestHeaders().get("Accept-Charset");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    65
        if (acceptCharset != null && acceptCharset.size() > 0) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    66
            return acceptCharset.get(0);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    67
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    68
        return null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    69
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    70
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    71
    public static String getGetRequestResource(HttpExchange ex) throws UnsupportedEncodingException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    72
        String charset = getRequestCharset(ex);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    73
        String httpHandlerPath = ex.getHttpContext().getPath();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    74
        String requestURIpath = ex.getRequestURI().getPath();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    75
        String getRequestPath = requestURIpath.substring(httpHandlerPath.length());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    76
        if (charset != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    77
            return URLDecoder.decode(getRequestPath, charset);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    78
        } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    79
            return getRequestPath;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    80
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    81
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    82
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    83
    public static String getGetRequestQuery(HttpExchange ex) throws UnsupportedEncodingException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    84
        String charset = getRequestCharset(ex);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    85
        String query = ex.getRequestURI().getQuery();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    86
        if (charset != null && query != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    87
            return URLDecoder.decode(query, charset);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    88
        } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    89
            return query;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    90
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    91
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    92
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    93
    public static Map<String, String> getGetRequestQueryMap(HttpExchange ex)
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    94
            throws UnsupportedEncodingException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    95
        String query = ex.getRequestURI().getQuery();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    96
        Map<String, String> queryParams = new LinkedHashMap<>();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    97
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    98
        if (query == null || query.isEmpty()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    99
            return queryParams;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   100
        }
56006
352a4f213fc6 1. URL decoding of URL
hb
parents: 56002
diff changeset
   101
        query = URLDecoder.decode(query, StandardCharsets.UTF_8.displayName());
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   102
        String[] params = query.trim().split("&");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   103
        for (String param : params) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   104
            int idx = param.indexOf('=');
56007
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   105
            if (idx != -1) {
56002
60ab3b595a8e Lot of bug fixes
hb
parents: 55998
diff changeset
   106
                queryParams.put(param.substring(0, idx), param.substring(idx + 1));
60ab3b595a8e Lot of bug fixes
hb
parents: 55998
diff changeset
   107
            }
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   108
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   109
        return queryParams;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   110
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   111
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   112
    public static String getCredentials(HttpExchange exchange) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   113
        Headers rmap = exchange.getRequestHeaders();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   114
        String auth = rmap.getFirst("Authorization");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   115
        if (auth != null && !auth.isEmpty()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   116
            int sp = auth.indexOf(' ');
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   117
            byte[] b = Base64.getDecoder().decode(auth.substring(sp + 1));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   118
            String authCredentials = new String(b);
56007
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   119
            int colon = authCredentials.indexOf(':');
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   120
            return authCredentials.substring(0, colon);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   121
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   122
        return "";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   123
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   124
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   125
    public static String readRequestBody(HttpExchange he) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   126
        String charset = getRequestCharset(he);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   127
        StringBuilder stringBuilder = new StringBuilder();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   128
        InputStreamReader in = charset != null ? new InputStreamReader(he.getRequestBody(), charset) : new InputStreamReader(he.getRequestBody());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   129
        BufferedReader br = new BufferedReader(in);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   130
        String line;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   131
        while ((line = br.readLine()) != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   132
            String decode = charset != null ? URLDecoder.decode(line, charset) : line;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   133
            stringBuilder.append(decode);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   134
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   135
        return stringBuilder.toString();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   136
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   137
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   138
    public static void sendResponse(HttpExchange exchange, HttpResponse response) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   139
        String charset = getRequestCharset(exchange);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   140
        String acceptCharset = HttpUtil.getAcceptCharset(exchange);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   141
        if (acceptCharset != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   142
            charset = acceptCharset;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   143
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   144
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   145
        // Set response headers explicitly
55995
a798bdd52997 POST : Attribute update - working
hb
parents: 55994
diff changeset
   146
        String msg = charset == null ? response.getBody() : URLEncoder.encode(response.getBody(), charset);
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   147
        byte[] bytes = msg.getBytes();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   148
        Headers resHeaders = exchange.getResponseHeaders();
56007
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   149
        if (charset != null && !charset.isEmpty()) {
56006
352a4f213fc6 1. URL decoding of URL
hb
parents: 56002
diff changeset
   150
            resHeaders.add("Content-Type", "application/json; charset=" + charset);
352a4f213fc6 1. URL decoding of URL
hb
parents: 56002
diff changeset
   151
        } else {
352a4f213fc6 1. URL decoding of URL
hb
parents: 56002
diff changeset
   152
            resHeaders.add("Content-Type", "application/json;");
352a4f213fc6 1. URL decoding of URL
hb
parents: 56002
diff changeset
   153
        }
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   154
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   155
        exchange.sendResponseHeaders(response.getCode(), bytes.length);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   156
        try (OutputStream os = exchange.getResponseBody()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   157
            os.write(bytes);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   158
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   159
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   160
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   161
    public static <T> List<T> filterByPage(HttpExchange exchange, List<T> input, int pageSize) throws UnsupportedEncodingException {
56007
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   162
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   163
        Map<String, String> queryParams = HttpUtil.getGetRequestQueryMap(exchange);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   164
        int currPage = 1;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   165
        if (queryParams != null && !queryParams.isEmpty()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   166
            String pageStr = queryParams.get("page");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   167
            if (pageStr != null && !pageStr.isEmpty()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   168
                currPage = Integer.parseInt(pageStr);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   169
                currPage = currPage > 1 ? currPage : 1;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   170
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   171
        }
56007
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   172
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   173
        if (input.size() <= pageSize) {
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   174
            return input;
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   175
        }
d6cbabcaf518 1. MBeanCollection Bulk operation via objectname query
hb
parents: 56006
diff changeset
   176
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   177
        int start = (currPage - 1) * pageSize;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   178
        int end = Math.min(input.size(), start + pageSize);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   179
        if (start < end) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   180
            return input.subList(start, end);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   181
        } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   182
            return null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   183
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   184
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   185
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   186
    public static <T> JSONObject getPaginationLinks(HttpExchange exchange, List<T> input, int pageSize) throws UnsupportedEncodingException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   187
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   188
        if (pageSize >= input.size()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   189
            return null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   190
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   191
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   192
        Map<String, String> queryParams = HttpUtil.getGetRequestQueryMap(exchange);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   193
        int currPage = 1;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   194
        if (queryParams != null && !queryParams.isEmpty()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   195
            String pageStr = queryParams.get("page");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   196
            if (pageStr != null && !pageStr.isEmpty()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   197
                currPage = Integer.parseInt(pageStr);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   198
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   199
        }
55997
f881344569d9 Added copyright header
hb
parents: 55995
diff changeset
   200
        String path = PlatformRestAdapter.getDomain() + exchange.getRequestURI().getPath() + "?";
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   201
        Map<String, String> queryMap = getGetRequestQueryMap(exchange);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   202
        if (queryMap != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   203
            queryMap.remove("page");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   204
            if (!queryMap.isEmpty()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   205
                String query = queryMap.keySet().stream()
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   206
                        .map(k -> k + "=" + queryMap.get(k))
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   207
                        .collect(Collectors.joining("&"));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   208
                path = path + query + "&";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   209
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   210
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   211
        int totalPages = (input.size() % pageSize == 0) ? input.size() / pageSize : input.size() / pageSize + 1;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   212
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   213
        JSONObject jobj = new JSONObject();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   214
        jobj.put("first", path.replaceAll(".$", ""));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   215
        if (currPage == 2) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   216
            jobj.put("prev", path.replaceAll(".$", ""));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   217
        } else if (currPage > 2) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   218
            jobj.put("prev", path + "page=" + (currPage - 1));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   219
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   220
        if (currPage < totalPages) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   221
            jobj.put("next", path + "page=" + (currPage + 1));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   222
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   223
        jobj.put("last", path + "page=" + totalPages);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   224
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   225
        return jobj;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   226
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   227
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   228
    public static String escapeUrl(String input) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   229
        try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   230
            URL url = new URL(input);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   231
            URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   232
            return uri.toURL().toString();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   233
        } catch (Exception ex) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   234
            return null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   235
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   236
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   237
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   238
}