Search in sources :

Example 36 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException 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 37 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException 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 38 with CLIException

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

the class AbstractCommand method errorView.

protected HttpErrorView errorView(String responseContent) {
    try {
        HttpErrorView errorView = new HttpErrorView();
        BufferedReader reader = new BufferedReader(new StringReader(responseContent));
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.startsWith("errorMessage:")) {
                errorView.errorMessage = line.substring(line.indexOf(':')).trim();
                break;
            }
        }
        while ((line = reader.readLine()) != null) {
            if (line.startsWith("httpErrorCode:")) {
                errorView.errorCode = line.substring(line.indexOf(':')).trim();
                break;
            }
        }
        while ((line = reader.readLine()) != null) {
            if (line.startsWith("stackTrace:")) {
                StringBuilder buffer = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                errorView.stackTrace = buffer.toString();
                break;
            }
        }
        return errorView;
    } catch (IOException ioe) {
        throw new CLIException(REASON_IO_ERROR, ioe);
    }
}
Also used : BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) IOException(java.io.IOException)

Example 39 with CLIException

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

the class AbstractCommand method handleError.

@SuppressWarnings("unchecked")
protected void handleError(String errorMessage, Exception error, ApplicationContext currentContext) {
    Stack resultStack = resultStack(currentContext);
    resultStack.push(error);
    if (error instanceof NotConnectedRestException) {
        throw new CLIException(REASON_UNAUTHORIZED_ACCESS, errorMessage, error);
    }
    writeLine(currentContext, errorMessage);
    Throwable cause = error.getCause();
    String message = error.getMessage();
    if (cause != null) {
        message = cause.getMessage();
    }
    writeLine(currentContext, "Error message: %s", message);
    if (isDebugModeEnabled(currentContext)) {
        writeLine(currentContext, "Stack trace: %s", getStackTraceAsString((cause == null) ? error : cause));
    } else {
        writeDebugModeUsage(currentContext);
    }
}
Also used : CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) NotConnectedRestException(org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) Stack(java.util.Stack)

Example 40 with CLIException

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

the class AbstractJsHelpCommand method printHelp.

public void printHelp(ApplicationContext currentContext, CommandSet.Entry[]... entrySet) throws CLIException {
    ObjectArrayFormatter formatter = new ObjectArrayFormatter();
    formatter.setMaxColumnLength(100);
    formatter.setSpace(2);
    formatter.setTitle(ImmutableList.of("Command", "Description"));
    formatter.addEmptyLine();
    boolean versionEntryAdded = false;
    List<CommandSet.Entry> entries = new ArrayList<>();
    for (CommandSet.Entry[] es : entrySet) {
        for (CommandSet.Entry entry : es) {
            if (entry.jsCommand() != null && entry.jsCommand().equals(CommandSet.VERSION.jsCommand())) {
                if (!versionEntryAdded) {
                    entries.add(entry);
                    versionEntryAdded = true;
                }
            } else {
                entries.add(entry);
            }
        }
    }
    Collections.sort(entries);
    for (CommandSet.Entry entry : entries) {
        if (entry.jsCommand() != null) {
            formatter.addLine(ImmutableList.of(entry.jsCommand(), entry.description()));
        }
    }
    writeLine(currentContext, "%s", StringUtility.objectArrayFormatterAsString(formatter));
}
Also used : ArrayList(java.util.ArrayList) CommandSet(org.ow2.proactive_grid_cloud_portal.cli.CommandSet) ObjectArrayFormatter(org.ow2.proactive.utils.ObjectArrayFormatter)

Aggregations

CLIException (org.ow2.proactive_grid_cloud_portal.cli.CLIException)49 SchedulerRestInterface (org.ow2.proactive_grid_cloud_portal.common.SchedulerRestInterface)30 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 IOException (java.io.IOException)8 File (java.io.File)6 List (java.util.List)5 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)4 NSStateView (org.ow2.proactive_grid_cloud_portal.cli.json.NSStateView)4 PluginView (org.ow2.proactive_grid_cloud_portal.cli.json.PluginView)4 ArrayList (java.util.ArrayList)3 RmStateView (org.ow2.proactive_grid_cloud_portal.cli.json.RmStateView)3 FileInputStream (java.io.FileInputStream)2 Stack (java.util.Stack)2 HttpPut (org.apache.http.client.methods.HttpPut)2 ConfigurableFieldView (org.ow2.proactive_grid_cloud_portal.cli.json.ConfigurableFieldView)2 TaskResultData (org.ow2.proactive_grid_cloud_portal.scheduler.dto.TaskResultData)2 NotConnectedRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)2