use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method resetLogLevels.
private void resetLogLevels(HttpRequest request, HttpResponder responder, String namespace, String appName, String appVersion, String type, String programName, @Nullable String component, String runId) throws Exception {
ProgramType programType = getProgramType(type);
if (programType == null) {
throw new BadRequestException("Invalid program type provided");
}
try {
Set<String> loggerNames = parseBody(request, SET_STRING_TYPE);
lifecycleService.resetProgramLogLevels(new ApplicationId(namespace, appName, appVersion).program(programType, programName), loggerNames == null ? Collections.<String>emptySet() : loggerNames, component, runId);
responder.sendStatus(HttpResponseStatus.OK);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Invalid JSON in body");
} catch (SecurityException e) {
throw new UnauthorizedException("Unauthorized to reset the log levels");
}
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method updateLogLevels.
private void updateLogLevels(HttpRequest request, HttpResponder responder, String namespace, String appName, String appVersion, String type, String programName, @Nullable String component, String runId) throws Exception {
ProgramType programType = getProgramType(type);
if (programType == null) {
throw new BadRequestException("Invalid program type provided");
}
try {
// we are decoding the body to Map<String, String> instead of Map<String, LogEntry.Level> here since Gson will
// serialize invalid enum values to null, which is allowed for log level, instead of throw an Exception.
lifecycleService.updateProgramLogLevels(new ApplicationId(namespace, appName, appVersion).program(programType, programName), transformLogLevelsMap(decodeArguments(request)), component, runId);
responder.sendStatus(HttpResponseStatus.OK);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Invalid JSON in body");
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage());
} catch (SecurityException e) {
throw new UnauthorizedException("Unauthorized to update the log levels");
}
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method startPrograms.
/**
* Starts all programs that are passed into the data. The data is an array of JSON objects
* where each object must contain the following three elements: appId, programType, and programId
* (flow name, service name, etc.). In additional, each object can contain an optional runtimeargs element,
* which is a map of arguments to start the program with.
* <p>
* Example input:
* <pre><code>
* [{"appId": "App1", "programType": "Service", "programId": "Service1"},
* {"appId": "App1", "programType": "Mapreduce", "programId": "MapReduce2", "runtimeargs":{"arg1":"val1"}},
* {"appId": "App2", "programType": "Flow", "programId": "Flow1"}]
* </code></pre>
* </p><p>
* The response will be an array of JsonObjects each of which will contain the three input parameters
* as well as a "statusCode" field which maps to the status code for the data in that JsonObjects.
* </p><p>
* If an error occurs in the input (for the example above, App2 does not exist), then all JsonObjects for which the
* parameters have a valid status will have the status field but all JsonObjects for which the parameters do not have
* a valid status will have an error message and statusCode.
* </p><p>
* For example, if there is no App2 in the data above, then the response would be 200 OK with following possible data:
* </p>
* <pre><code>
* [{"appId": "App1", "programType": "Service", "programId": "Service1", "statusCode": 200},
* {"appId": "App1", "programType": "Mapreduce", "programId": "Mapreduce2", "statusCode": 200},
* {"appId":"App2", "programType":"Flow", "programId":"Flow1", "statusCode":404, "error": "App: App2 not found"}]
* </code></pre>
*/
@POST
@Path("/start")
@AuditPolicy({ AuditDetail.REQUEST_BODY, AuditDetail.RESPONSE_BODY })
public void startPrograms(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
List<BatchProgramStart> programs = validateAndGetBatchInput(request, BATCH_STARTS_TYPE);
List<BatchProgramResult> output = new ArrayList<>(programs.size());
for (BatchProgramStart program : programs) {
ProgramId programId = new ProgramId(namespaceId, program.getAppId(), program.getProgramType(), program.getProgramId());
try {
ProgramController programController = lifecycleService.start(programId, program.getRuntimeargs(), false);
output.add(new BatchProgramResult(program, HttpResponseStatus.OK.getCode(), null, programController.getRunId().getId()));
} catch (NotFoundException e) {
output.add(new BatchProgramResult(program, HttpResponseStatus.NOT_FOUND.getCode(), e.getMessage()));
} catch (BadRequestException e) {
output.add(new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.getCode(), e.getMessage()));
} catch (ConflictException e) {
output.add(new BatchProgramResult(program, HttpResponseStatus.CONFLICT.getCode(), e.getMessage()));
}
}
responder.sendJson(HttpResponseStatus.OK, output);
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class MonitorHandler method resetServiceLogLevels.
/**
* Reset the log levels of the service.
* All loggers will be reset to the level when the service started.
*/
@Path("system/services/{service-name}/resetloglevels")
@POST
public void resetServiceLogLevels(HttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
if (!serviceManagementMap.containsKey(serviceName)) {
throw new NotFoundException(String.format("Invalid service name %s", serviceName));
}
MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
if (!masterServiceManager.isServiceEnabled()) {
throw new ForbiddenException(String.format("Failed to reset log levels for service %s " + "because the service is not enabled", serviceName));
}
try {
Set<String> loggerNames = parseBody(request, SET_STRING_TYPE);
masterServiceManager.resetServiceLogLevels(loggerNames == null ? Collections.<String>emptySet() : loggerNames);
responder.sendStatus(HttpResponseStatus.OK);
} catch (IllegalStateException ise) {
throw new ServiceUnavailableException(String.format("Failed to reset log levels for service %s " + "because the service may not be ready yet", serviceName));
} catch (JsonSyntaxException e) {
throw new BadRequestException("Invalid Json in the body");
}
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class MonitorHandler method monitor.
@Path("/system/services/{service-name}/status")
@GET
public void monitor(HttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
if (!serviceManagementMap.containsKey(serviceName)) {
throw new NotFoundException(String.format("Invalid service name %s", serviceName));
}
MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
if (!masterServiceManager.isServiceEnabled()) {
throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
}
if (masterServiceManager.canCheckStatus() && masterServiceManager.isServiceAvailable()) {
JsonObject json = new JsonObject();
json.addProperty("status", STATUSOK);
responder.sendJson(HttpResponseStatus.OK, json);
} else if (masterServiceManager.canCheckStatus()) {
JsonObject json = new JsonObject();
json.addProperty("status", STATUSNOTOK);
responder.sendJson(HttpResponseStatus.OK, json);
} else {
throw new BadRequestException("Operation not valid for this service");
}
}
Aggregations