Search in sources :

Example 76 with BadRequestException

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");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Example 77 with BadRequestException

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");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Example 78 with BadRequestException

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);
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) ConflictException(co.cask.cdap.common.ConflictException) ArrayList(java.util.ArrayList) BatchProgramResult(co.cask.cdap.proto.BatchProgramResult) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) BatchProgramStart(co.cask.cdap.proto.BatchProgramStart) ProgramId(co.cask.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 79 with BadRequestException

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");
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) JsonSyntaxException(com.google.gson.JsonSyntaxException) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 80 with BadRequestException

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");
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) NotFoundException(co.cask.cdap.common.NotFoundException) JsonObject(com.google.gson.JsonObject) BadRequestException(co.cask.cdap.common.BadRequestException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

BadRequestException (co.cask.cdap.common.BadRequestException)82 Path (javax.ws.rs.Path)29 NotFoundException (co.cask.cdap.common.NotFoundException)25 HttpResponse (co.cask.common.http.HttpResponse)17 JsonSyntaxException (com.google.gson.JsonSyntaxException)17 URL (java.net.URL)17 POST (javax.ws.rs.POST)17 NamespaceId (co.cask.cdap.proto.id.NamespaceId)16 IOException (java.io.IOException)15 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)13 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)12 HttpRequest (co.cask.common.http.HttpRequest)12 InputStreamReader (java.io.InputStreamReader)11 Reader (java.io.Reader)11 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)9 ApplicationId (co.cask.cdap.proto.id.ApplicationId)8 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)7 ProgramType (co.cask.cdap.proto.ProgramType)7 ProgramId (co.cask.cdap.proto.id.ProgramId)6 Test (org.junit.Test)6