test/jdk/javax/management/remote/rest/RestAdapterTest.java
author hb
Mon, 25 Dec 2017 20:42:05 +0530
branchjmx-rest-api
changeset 55994 9721e36abeb0
child 56002 60ab3b595a8e
permissions -rw-r--r--
Implementation of GET for new APIs Implemented paging and filtering for GET
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55994
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     1
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     2
import java.io.BufferedInputStream;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     3
import java.io.BufferedReader;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     4
import java.io.BufferedWriter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     5
import java.io.File;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     6
import java.io.FileInputStream;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     7
import java.io.FileWriter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     8
import java.io.IOException;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
     9
import java.io.InputStreamReader;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    10
import java.net.URL;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    11
import java.net.URLDecoder;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    12
import java.net.URLEncoder;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    13
import java.security.KeyStore;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    14
import java.util.Base64;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    15
import java.util.Properties;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    16
import java.util.logging.Level;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    17
import java.util.logging.Logger;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    18
import javax.management.remote.rest.PlatformRestAdapter;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    19
import javax.net.ssl.HttpsURLConnection;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    20
import javax.net.ssl.KeyManagerFactory;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    21
import javax.net.ssl.SSLContext;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    22
import javax.net.ssl.TrustManagerFactory;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    23
import org.testng.annotations.Test;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    24
import org.testng.Assert;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    25
import org.testng.annotations.AfterClass;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    26
import org.testng.annotations.BeforeClass;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    27
import org.testng.annotations.DataProvider;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    28
import org.testng.annotations.Test;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    29
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    30
/**
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    31
 * @test @modules java.logging java.management java.management.rest
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    32
 * @run testng/othervm RestAdapterTest
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    33
 *
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    34
 */
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    35
@Test
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    36
public class RestAdapterTest {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    37
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    38
    private static final String CHARSET = "UTF-8";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    39
    private static String sslAgentConfig;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    40
    private static String sslClientConfig;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    41
    private static String passwordFile;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    42
    private static String configFile;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    43
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    44
    private void createAgentSslConfigFile(String fileName) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    45
        File f = new File(fileName);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    46
        if (f.exists()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    47
            return;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    48
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    49
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    50
        String testDir = System.getProperty("test.src");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    51
        props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreAgent");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    52
        props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    53
        props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreAgent");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    54
        props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    55
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    56
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    57
            props.store(writer, "");
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
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    61
    private void createClientSslConfigFile(String fileName) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    62
        File f = new File(fileName);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    63
        if (f.exists()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    64
            return;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    65
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    66
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    67
        String testDir = System.getProperty("test.src");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    68
        props.setProperty("javax.net.ssl.keyStore", testDir + File.separator + "keystoreClient");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    69
        props.setProperty("javax.net.ssl.keyStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    70
        props.setProperty("javax.net.ssl.trustStore", testDir + File.separator + "truststoreClient");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    71
        props.setProperty("javax.net.ssl.trustStorePassword", "glopglop");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    72
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    73
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    74
            props.store(writer, "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    75
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    76
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    77
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    78
    private void setupMgmtConfig(String fileName) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    79
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    80
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    81
        props.setProperty("com.sun.management.jmxremote.ssl", "true");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    82
        props.setProperty("com.sun.management.jmxremote.ssl.config.file", sslAgentConfig);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    83
        props.setProperty("com.sun.management.jmxremote.password.file", passwordFile);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    84
        props.setProperty("com.sun.management.jmxremote.rest.port", "0");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    85
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    86
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    87
            props.store(writer, "");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    88
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    89
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    90
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    91
    private void setupConfig() throws Exception {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    92
        String testSrcRoot = System.getProperty("test.src") + File.separator;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    93
        sslAgentConfig = testSrcRoot + "sslConfigAgent";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    94
        sslClientConfig = testSrcRoot + "sslConfigClient";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    95
        passwordFile = testSrcRoot + "password.properties";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    96
        configFile = testSrcRoot + "mgmt.properties";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    97
        createAgentSslConfigFile(sslAgentConfig);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    98
        createClientSslConfigFile(sslClientConfig);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
    99
        setupMgmtConfig(configFile);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   100
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   101
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   102
    @BeforeClass
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   103
    public void setupAdapter() throws Exception {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   104
        setupConfig();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   105
        File file = new File(configFile);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   106
        Properties props = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   107
        props.load(new FileInputStream(file));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   108
        if (props.get("com.sun.management.jmxremote.rest.port") != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   109
            PlatformRestAdapter.init((String) props.get("com.sun.management.jmxremote.rest.port"), props);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   110
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   111
        SSLContext ctx = getSSlContext(sslClientConfig);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   112
        HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   113
        HttpsURLConnection.setDefaultHostnameVerifier(
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   114
                (String hostname, javax.net.ssl.SSLSession sslSession) -> hostname.equals("Harsha-Wardhana-B"));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   115
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   116
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   117
    @AfterClass
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   118
    public void tearDown() {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   119
        PlatformRestAdapter.stop();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   120
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   121
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   122
    private static SSLContext getSSlContext(String sslConfigFileName) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   123
        final String keyStore, keyStorePassword, trustStore, trustStorePassword;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   124
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   125
        try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   126
            Properties p = new Properties();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   127
            BufferedInputStream bin = new BufferedInputStream(new FileInputStream(sslConfigFileName));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   128
            p.load(bin);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   129
            keyStore = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_KEYSTORE_FILE);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   130
            keyStorePassword = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_KEYSTORE_PASSWORD);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   131
            trustStore = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_TRUSTSTORE_FILE);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   132
            trustStorePassword = p.getProperty(PlatformRestAdapter.PropertyNames.SSL_TRUSTSTORE_PASSWORD);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   133
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   134
            char[] keyStorePasswd = null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   135
            if (keyStorePassword.length() != 0) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   136
                keyStorePasswd = keyStorePassword.toCharArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   137
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   138
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   139
            char[] trustStorePasswd = null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   140
            if (trustStorePassword.length() != 0) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   141
                trustStorePasswd = trustStorePassword.toCharArray();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   142
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   143
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   144
            KeyStore ks = null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   145
            if (keyStore != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   146
                ks = KeyStore.getInstance(KeyStore.getDefaultType());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   147
                FileInputStream ksfis = new FileInputStream(keyStore);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   148
                ks.load(ksfis, keyStorePasswd);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   149
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   150
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   151
                    KeyManagerFactory.getDefaultAlgorithm());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   152
            kmf.init(ks, keyStorePasswd);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   153
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   154
            KeyStore ts = null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   155
            if (trustStore != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   156
                ts = KeyStore.getInstance(KeyStore.getDefaultType());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   157
                FileInputStream tsfis = new FileInputStream(trustStore);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   158
                ts.load(tsfis, trustStorePasswd);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   159
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   160
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   161
                    TrustManagerFactory.getDefaultAlgorithm());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   162
            tmf.init(ts);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   163
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   164
            SSLContext ctx = SSLContext.getInstance("SSL");
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   165
            ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   166
            return ctx;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   167
        } catch (Exception ex) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   168
            Logger.getLogger(PlatformRestAdapter.class.getName()).log(Level.SEVERE, null, ex);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   169
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   170
        return null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   171
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   172
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   173
    private String executeHttpRequest(String inputUrl, String charset) throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   174
        if (inputUrl != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   175
            URL url = new URL(PlatformRestAdapter.getBaseURL() + (charset != null ? URLEncoder.encode(inputUrl, charset) : inputUrl));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   176
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   177
            con.setDoOutput(false);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   178
            String userCredentials = "username1:password1";
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   179
            String basicAuth = "Basic " + Base64.getEncoder().encodeToString(userCredentials.getBytes());
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   180
            con.setRequestProperty("Authorization", basicAuth);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   181
            if (charset != null && !charset.isEmpty()) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   182
                con.setRequestProperty("Content-Type", "application/json; charset=" + charset);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   183
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   184
            try {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   185
                int status = con.getResponseCode();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   186
                if (status == 200) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   187
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   188
                    StringBuilder sbuf;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   189
                    try (BufferedReader br = new BufferedReader(
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   190
                            new InputStreamReader(con.getInputStream()))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   191
                        sbuf = new StringBuilder();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   192
                        String input;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   193
                        while ((input = br.readLine()) != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   194
                            sbuf.append(charset != null ? URLDecoder.decode(input, charset) : input);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   195
                        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   196
                    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   197
                    return sbuf.toString();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   198
                } else {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   199
                    StringBuilder sbuf;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   200
                    try (BufferedReader br = new BufferedReader(
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   201
                            new InputStreamReader(con.getErrorStream()))) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   202
                        sbuf = new StringBuilder();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   203
                        String input;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   204
                        while ((input = br.readLine()) != null) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   205
                            sbuf.append(charset != null ? URLDecoder.decode(input, charset) : input);
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   206
                        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   207
                    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   208
                    return sbuf.toString();
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   209
                }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   210
            } catch (IOException e) {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   211
            }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   212
        }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   213
        return null;
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   214
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   215
    
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   216
    @Test
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   217
    public void testMBeanServerCollection() throws IOException {
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   218
        System.out.println(executeHttpRequest("/platform/mbeans/java.lang:type=Runtime",null));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   219
//        System.out.println(executeHttpRequest("/platform/mbeans/java.lang:name=CodeHeap%20'non-nmethods',type=MemoryPool/info", null));
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   220
    }
9721e36abeb0 Implementation of GET for new APIs
hb
parents:
diff changeset
   221
}