Search in sources :

Example 11 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class RemoveNodeSourceCommand method execute.

public void execute(ApplicationContext currentContext) throws CLIException {
    if (currentContext.isForced()) {
        preempt = true;
    }
    HttpPost request = new HttpPost(currentContext.getResourceUrl("nodesource/remove"));
    QueryStringBuilder queryStringBuilder = new QueryStringBuilder();
    queryStringBuilder.add("name", nodeSource).add("preempt", Boolean.toString(preempt));
    request.setEntity(queryStringBuilder.buildEntity(APPLICATION_FORM_URLENCODED));
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(response) == statusCode(OK)) {
        boolean success = readValue(response, Boolean.TYPE, currentContext);
        resultStack(currentContext).push(success);
        if (success) {
            writeLine(currentContext, "Node source '%s' deleted successfully.", nodeSource);
        } else {
            writeLine(currentContext, "Cannot delete node source: %s.", nodeSource);
        }
    } else {
        handleError(String.format("An error occurred while deleting node source: %s", nodeSource), response, currentContext);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) QueryStringBuilder(org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder)

Example 12 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class SetPolicyCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    Map<String, PluginView> knownPolicies = currentContext.getPolicies();
    if (knownPolicies == null) {
        HttpGet request = new HttpGet(currentContext.getResourceUrl("policies"));
        HttpResponseWrapper response = execute(request, currentContext);
        if (statusCode(OK) == statusCode(response)) {
            knownPolicies = new HashMap<>();
            List<PluginView> pluginViewList = readValue(response, new TypeReference<List<PluginView>>() {
            }, currentContext);
            for (PluginView pluginView : pluginViewList) {
                knownPolicies.put(pluginView.getPluginName(), pluginView);
            }
            currentContext.setPolicies(knownPolicies);
        } else {
            handleError("Unable to retrieve known policy types:", response, currentContext);
        }
    }
    if (knownPolicies != null) {
        PluginView pluginView = knownPolicies.get(policyType);
        if (pluginView == null) {
            throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("Unknown policy type: %s", policyType));
        }
        ConfigurableFieldView[] configurableFields = pluginView.getConfigurableFields();
        if (configurableFields.length != policyArgs.length) {
            throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("Invalid number of arguments specified for '%s' type.", policyType));
        }
        QueryStringBuilder queryStringBuilder = new QueryStringBuilder();
        queryStringBuilder.add("policyType", policyType);
        for (int index = 0; index < configurableFields.length; index++) {
            ConfigurableFieldView cf = configurableFields[index];
            Type ft = cf.getMeta().type();
            if (FILEBROWSER.equals(ft) || CREDENTIAL.equals(ft)) {
                String contents = FileUtility.readFileToString(new File(policyArgs[index]));
                queryStringBuilder.add("policyFileParameters", contents);
            } else {
                queryStringBuilder.add("policyParameters", policyArgs[index]);
            }
        }
        currentContext.setProperty(SET_POLICY, queryStringBuilder);
    }
}
Also used : HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) HttpGet(org.apache.http.client.methods.HttpGet) ConfigurableFieldView(org.ow2.proactive_grid_cloud_portal.cli.json.ConfigurableFieldView) QueryStringBuilder(org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder) Type(org.ow2.proactive_grid_cloud_portal.cli.json.FieldMetaDataView.Type) PluginView(org.ow2.proactive_grid_cloud_portal.cli.json.PluginView) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) List(java.util.List) File(java.io.File)

Example 13 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class AbstractCommand method execute.

protected HttpResponseWrapper execute(HttpUriRequest request, ApplicationContext currentContext) {
    String sessionId = currentContext.getSessionId();
    if (sessionId != null) {
        request.setHeader("sessionid", sessionId);
    }
    CommonHttpClientBuilder httpClientBuilder = new HttpClientBuilder().useSystemProperties();
    try {
        if ("https".equals(request.getURI().getScheme()) && currentContext.canInsecureAccess()) {
            httpClientBuilder.insecure(true);
        }
        HttpResponse response = httpClientBuilder.build().execute(request);
        return new HttpResponseWrapper(response);
    } catch (SSLPeerUnverifiedException sslException) {
        throw new CLIException(CLIException.REASON_OTHER, "SSL error. Perhaps HTTPS certificate could not be validated, " + "you can try with -k or insecure() for insecure SSL connection.", sslException);
    } catch (Exception e) {
        throw new CLIException(CLIException.REASON_OTHER, e.getMessage(), e);
    } finally {
        ((HttpRequestBase) request).releaseConnection();
    }
}
Also used : HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HttpResponse(org.apache.http.HttpResponse) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) CommonHttpClientBuilder(org.ow2.proactive.http.CommonHttpClientBuilder) HttpClientBuilder(org.ow2.proactive.http.HttpClientBuilder) IOException(java.io.IOException) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) NotConnectedRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) CommonHttpClientBuilder(org.ow2.proactive.http.CommonHttpClientBuilder)

Example 14 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class LoginCommand method login.

@Override
protected String login(ApplicationContext currentContext) throws CLIException {
    String password = currentContext.getProperty(PASSWORD, String.class);
    if (password == null) {
        password = new String(readPassword(currentContext, "password:"));
    }
    HttpPost request = new HttpPost(currentContext.getResourceUrl("login"));
    StringEntity entity = new StringEntity("username=" + username + "&password=" + password, APPLICATION_FORM_URLENCODED);
    request.setEntity(entity);
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(OK) == statusCode(response)) {
        return StringUtility.responseAsString(response).trim();
    } else {
        throw buildCLIException(REASON_OTHER, response, currentContext);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper)

Example 15 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class LoginWithCredentialsCommand method login.

@Override
protected String login(ApplicationContext currentContext) throws CLIException {
    File credentials = new File(pathname);
    if (!credentials.exists()) {
        throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("File does not exist: %s", credentials.getAbsolutePath()));
    }
    if (warn) {
        writeLine(currentContext, "Using the default credentials file: %s", credentials.getAbsolutePath());
    }
    HttpPost request = new HttpPost(currentContext.getResourceUrl("login"));
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("credential", new ByteArrayBody(FileUtility.byteArray(credentials), APPLICATION_OCTET_STREAM.getMimeType()));
    request.setEntity(entity);
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(OK) == statusCode(response)) {
        return StringUtility.responseAsString(response).trim();
    } else {
        handleError("An error occurred while logging: ", response, currentContext);
        throw new CLIException(REASON_OTHER, "An error occurred while logging.");
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) File(java.io.File)

Aggregations

HttpResponseWrapper (org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper)22 QueryStringBuilder (org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder)12 HttpGet (org.apache.http.client.methods.HttpGet)10 HttpPost (org.apache.http.client.methods.HttpPost)9 CLIException (org.ow2.proactive_grid_cloud_portal.cli.CLIException)7 List (java.util.List)5 NSStateView (org.ow2.proactive_grid_cloud_portal.cli.json.NSStateView)4 PluginView (org.ow2.proactive_grid_cloud_portal.cli.json.PluginView)4 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)3 File (java.io.File)3 RmStateView (org.ow2.proactive_grid_cloud_portal.cli.json.RmStateView)3 ArrayList (java.util.ArrayList)2 HttpPut (org.apache.http.client.methods.HttpPut)2 ConfigurableFieldView (org.ow2.proactive_grid_cloud_portal.cli.json.ConfigurableFieldView)2 ErrorView (org.ow2.proactive_grid_cloud_portal.cli.json.ErrorView)2 Type (org.ow2.proactive_grid_cloud_portal.cli.json.FieldMetaDataView.Type)2 NodeEventView (org.ow2.proactive_grid_cloud_portal.cli.json.NodeEventView)2 IOException (java.io.IOException)1 Stack (java.util.Stack)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1