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);
}
}
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();
}
}
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);
}
}
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);
}
}
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));
}
Aggregations