Search in sources :

Example 1 with HttpOperationInvoker

use of org.apache.geode.management.internal.web.shell.HttpOperationInvoker in project geode by apache.

the class ShellCommands method httpConnect.

private Result httpConnect(Map<String, String> sslConfigProps, boolean useSsl, String url, String userName, String passwordToUse) {
    Gfsh gfsh = getGfsh();
    try {
        Map<String, String> securityProperties = new HashMap<String, String>();
        // at this point, if userName is not empty, password should not be empty either
        if (userName != null && userName.length() > 0) {
            securityProperties.put("security-username", userName);
            securityProperties.put("security-password", passwordToUse);
        }
        if (useSsl) {
            configureHttpsURLConnection(sslConfigProps);
            if (url.startsWith("http:")) {
                url = url.replace("http:", "https:");
            }
        }
        Iterator<String> it = sslConfigProps.keySet().iterator();
        while (it.hasNext()) {
            String secKey = it.next();
            securityProperties.put(secKey, sslConfigProps.get(secKey));
        }
        // This is so that SSL termination results in https URLs being returned
        String query = (url.startsWith("https")) ? "?scheme=https" : "";
        LogWrapper.getInstance().warning(String.format("Sending HTTP request for Link Index at (%1$s)...", url.concat("/index").concat(query)));
        LinkIndex linkIndex = new SimpleHttpRequester(gfsh, CONNECT_LOCATOR_TIMEOUT_MS, securityProperties).exchange(url.concat("/index").concat(query), LinkIndex.class);
        LogWrapper.getInstance().warning(String.format("Received Link Index (%1$s)", linkIndex.toString()));
        HttpOperationInvoker operationInvoker = new RestHttpOperationInvoker(linkIndex, gfsh, url, securityProperties);
        Initializer.init(operationInvoker);
        gfsh.setOperationInvoker(operationInvoker);
        LogWrapper.getInstance().info(CliStrings.format(CliStrings.CONNECT__MSG__SUCCESS, operationInvoker.toString()));
        return ResultBuilder.createInfoResult(CliStrings.format(CliStrings.CONNECT__MSG__SUCCESS, operationInvoker.toString()));
    } catch (Exception e) {
        // all other exceptions, just logs it and returns a connection error
        if (!(e instanceof SecurityException) && !(e instanceof AuthenticationFailedException)) {
            return handleExcpetion(e, null);
        }
        // connection error
        if (userName != null) {
            return handleExcpetion(e, null);
        }
        // otherwise, prompt for username and password and retry the conenction
        try {
            userName = gfsh.readText(CliStrings.CONNECT__USERNAME + ": ");
            passwordToUse = gfsh.readPassword(CliStrings.CONNECT__PASSWORD + ": ");
            return httpConnect(sslConfigProps, useSsl, url, userName, passwordToUse);
        } catch (IOException ioe) {
            return handleExcpetion(ioe, null);
        }
    } finally {
        Gfsh.redirectInternalJavaLoggers();
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) IOException(java.io.IOException) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) LinkIndex(org.apache.geode.management.internal.web.domain.LinkIndex) RestHttpOperationInvoker(org.apache.geode.management.internal.web.shell.RestHttpOperationInvoker) HttpOperationInvoker(org.apache.geode.management.internal.web.shell.HttpOperationInvoker) RestHttpOperationInvoker(org.apache.geode.management.internal.web.shell.RestHttpOperationInvoker) SimpleHttpRequester(org.apache.geode.management.internal.web.http.support.SimpleHttpRequester) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh)

Aggregations

IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Gfsh (org.apache.geode.management.internal.cli.shell.Gfsh)1 LinkIndex (org.apache.geode.management.internal.web.domain.LinkIndex)1 SimpleHttpRequester (org.apache.geode.management.internal.web.http.support.SimpleHttpRequester)1 HttpOperationInvoker (org.apache.geode.management.internal.web.shell.HttpOperationInvoker)1 RestHttpOperationInvoker (org.apache.geode.management.internal.web.shell.RestHttpOperationInvoker)1 AuthenticationFailedException (org.apache.geode.security.AuthenticationFailedException)1