Établissement d'une liste de cartes nommées du référentiel

Cette application Java appelle l'interface REST Mapping Service pour obtenir une liste des cartes nommées du référentiel. La liste de cartes nommées renvoyée dans la réponse est ensuite émise dans la ligne de commande.

Voici le code :



public static void main(String[] args)
{
    java.io.InputStream is = null;

    try
    {
        // Create the REST request URL
        String serverUrl = "http://MyServer:8080/rest/Spatial/MappingService/maps.json";
        java.net.URL url = new java.net.URL(serverUrl);
        java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();

        String user = "user";
        String password = "pass";

        // Credentials need to be a base64 encoded string of the nature "user:pass"
        String credentials = user + ":" + password;
        byte[] encodedBytes  = org.apache.commons.codec.binary.Base64.encodeBase64(credentials.getBytes());     
        String base64EncodedString = new String(encodedBytes);

        // Apply the authorization header - a string of nature "Basic <base64EncodedCredentialsString>"
        String authHeader = "Basic" + " " + base64EncodedString; 
        conn.addRequestProperty("Authorization", authHeader);

        // Get response for the request
        is = conn.getInputStream();
        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
        org.apache.commons.io.IOUtils.copy(is, bos);
        is.close();

        // Display the response 
        System.out.println(new String(bos.toByteArray()));
    }
    catch (Exception e)
    {
        System.out.println(e.getMessage());
    }
    finally
    {
        org.apache.commons.io.IOUtils.closeQuietly(is);
    }
}