use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class SecureStoreHandler method create.
@Path("/{key-name}")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void create(FullHttpRequest httpRequest, HttpResponder httpResponder, @PathParam("namespace-id") String namespace, @PathParam("key-name") String name) throws Exception {
SecureKeyId secureKeyId = new SecureKeyId(namespace, name);
SecureKeyCreateRequest secureKeyCreateRequest;
try {
secureKeyCreateRequest = parseBody(httpRequest);
} catch (IOException e) {
SecureKeyCreateRequest dummy = new SecureKeyCreateRequest("<description>", "<data>", ImmutableMap.of("key", "value"));
throw new BadRequestException("Unable to parse the request. The request body should be of the following format." + " \n" + GSON.toJson(dummy));
}
if (Strings.isNullOrEmpty(secureKeyCreateRequest.getData()) || secureKeyCreateRequest.getData().trim().isEmpty()) {
throw new BadRequestException("The data field must not be null or empty. The data will be stored securely " + "under provided key name.");
}
secureStoreManager.put(namespace, name, secureKeyCreateRequest.getData(), secureKeyCreateRequest.getDescription(), secureKeyCreateRequest.getProperties());
httpResponder.sendStatus(HttpResponseStatus.OK);
}
use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.
the class MonitorClient method setSystemServiceInstances.
/**
* Sets the number of instances the system service is running on.
*
* @param serviceName name of the system service
* @param instances number of instances the system service is running on
* @throws IOException if a network error occurred
* @throws NotFoundException if the system service with the specified name was not found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void setSystemServiceInstances(String serviceName, int instances) throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveURL(String.format("system/services/%s/instances", serviceName));
HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(new SystemServiceId(serviceName));
} else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(new String(response.getResponseBody()));
}
}
use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.
the class MonitorClient method getSystemServiceStatus.
/**
* Gets the status of a system service.
*
* @param serviceName Name of the system service
* @return status of the system service
* @throws IOException if a network error occurred
* @throws NotFoundException if the system service with the specified name could not be found
* @throws BadRequestException if the operation was not valid for the system service
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public String getSystemServiceStatus(String serviceName) throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveURL(String.format("system/services/%s/status", serviceName));
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
String responseBody = new String(response.getResponseBody());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(new SystemServiceId(serviceName));
} else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(responseBody);
}
Map<String, String> status = GSON.fromJson(responseBody, new TypeToken<Map<String, String>>() {
}.getType());
return status.get("status");
}
use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.
the class ProgramClient method stop.
/**
* Stops a program.
*
* @param programId the program to stop
* @throws IOException if a network error occurred
* @throws ProgramNotFoundException if the program with specified name could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
* @throws BadRequestException if an attempt is made to stop a program that is either not running or
* was started by a workflow
*/
public void stop(ProgramId programId) throws IOException, ProgramNotFoundException, UnauthenticatedException, UnauthorizedException, BadRequestException {
String path = String.format("apps/%s/versions/%s/%s/%s/stop", programId.getApplication(), programId.getVersion(), programId.getType().getCategoryName(), programId.getProgram());
URL url = config.resolveNamespacedURLV3(programId.getNamespaceId(), path);
// Required to add body even if runtimeArgs is null to avoid 411 error for Http POST
HttpRequest.Builder request = HttpRequest.post(url).withBody("");
HttpResponse response = restClient.execute(request.build(), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ProgramNotFoundException(programId);
}
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(response.getResponseCode() + ": " + response.getResponseBodyAsString());
}
}
use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.
the class SecureStoreHandler method create.
@Path("/{key-name}")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void create(FullHttpRequest httpRequest, HttpResponder httpResponder, @PathParam("namespace-id") String namespace, @PathParam("key-name") String name) throws Exception {
SecureKeyId secureKeyId = new SecureKeyId(namespace, name);
SecureKeyCreateRequest secureKeyCreateRequest;
try {
secureKeyCreateRequest = parseBody(httpRequest);
} catch (IOException e) {
SecureKeyCreateRequest dummy = new SecureKeyCreateRequest("<description>", "<data>", ImmutableMap.of("key", "value"));
throw new BadRequestException("Unable to parse the request. The request body should be of the following format." + " \n" + GSON.toJson(dummy));
}
if (Strings.isNullOrEmpty(secureKeyCreateRequest.getData()) || secureKeyCreateRequest.getData().trim().isEmpty()) {
throw new BadRequestException("The data field must not be null or empty. The data will be stored securely " + "under provided key name.");
}
secureStoreManager.put(namespace, name, secureKeyCreateRequest.getData(), secureKeyCreateRequest.getDescription(), secureKeyCreateRequest.getProperties());
httpResponder.sendStatus(HttpResponseStatus.OK);
}
Aggregations