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