Search in sources :

Example 21 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method performRunLevelStop.

/**
   * Stops the particular run of the Workflow or MapReduce program.
   */
@POST
@Path("/apps/{app-id}/{program-type}/{program-id}/runs/{run-id}/stop")
public void performRunLevelStop(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("program-type") String type, @PathParam("program-id") String programId, @PathParam("run-id") String runId) throws Exception {
    ProgramType programType;
    try {
        programType = ProgramType.valueOfCategoryName(type);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    }
    ProgramId program = new ProgramId(namespaceId, appId, programType, programId);
    lifecycleService.stop(program, runId);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType) ProgramId(co.cask.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 22 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method combineForUpdate.

private ProgramSchedule combineForUpdate(ScheduleDetail scheduleDetail, ProgramSchedule existing) throws BadRequestException {
    String description = Objects.firstNonNull(scheduleDetail.getDescription(), existing.getDescription());
    ProgramId programId = scheduleDetail.getProgram() == null ? existing.getProgramId() : existing.getProgramId().getParent().program(scheduleDetail.getProgram().getProgramType() == null ? existing.getProgramId().getType() : ProgramType.valueOfSchedulableType(scheduleDetail.getProgram().getProgramType()), Objects.firstNonNull(scheduleDetail.getProgram().getProgramName(), existing.getProgramId().getProgram()));
    if (!programId.equals(existing.getProgramId())) {
        throw new BadRequestException(String.format("Must update the schedule '%s' with the same program as '%s'. " + "To change the program in a schedule, please delete the schedule and create a new one.", existing.getName(), existing.getProgramId().toString()));
    }
    Map<String, String> properties = Objects.firstNonNull(scheduleDetail.getProperties(), existing.getProperties());
    Trigger trigger = Objects.firstNonNull(scheduleDetail.getTrigger(), existing.getTrigger());
    List<? extends Constraint> constraints = Objects.firstNonNull(scheduleDetail.getConstraints(), existing.getConstraints());
    Long timeoutMillis = Objects.firstNonNull(scheduleDetail.getTimeoutMillis(), existing.getTimeoutMillis());
    return new ProgramSchedule(existing.getName(), description, programId, properties, trigger, constraints, timeoutMillis);
}
Also used : StreamSizeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.StreamSizeTrigger) TimeTrigger(co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) Trigger(co.cask.cdap.internal.schedule.trigger.Trigger) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 23 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class RouteConfigHttpHandler method storeRouteConfig.

@PUT
@Path("/routeconfig")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void storeRouteConfig(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("service-id") String serviceId) throws Exception {
    NamespaceId namespace = new NamespaceId(namespaceId);
    ProgramId programId = namespace.app(appId).service(serviceId);
    Map<String, Integer> routes = parseBody(request, ROUTE_CONFIG_TYPE);
    if (routes == null || routes.isEmpty()) {
        throw new BadRequestException("Route config contains invalid format or empty content.");
    }
    List<ProgramId> nonExistingServices = new ArrayList<>();
    for (String version : routes.keySet()) {
        ProgramId routeProgram = namespace.app(appId, version).service(serviceId);
        if (lifecycleService.getProgramSpecification(routeProgram) == null) {
            nonExistingServices.add(routeProgram);
        }
    }
    if (nonExistingServices.size() > 0) {
        throw new BadRequestException("The following versions of the application/service could not be found : " + nonExistingServices);
    }
    RouteConfig routeConfig = new RouteConfig(routes);
    if (!routeConfig.isValid()) {
        throw new BadRequestException("Route Percentage needs to add up to 100.");
    }
    routeStore.store(programId, routeConfig);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : ArrayList(java.util.ArrayList) BadRequestException(co.cask.cdap.common.BadRequestException) RouteConfig(co.cask.cdap.route.store.RouteConfig) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ProgramId(co.cask.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 24 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class MonitorHandler method restartInstances.

private void restartInstances(HttpResponder responder, String serviceName, int instanceId, boolean restartAll) throws Exception {
    long startTimeMs = System.currentTimeMillis();
    boolean isSuccess = true;
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
    try {
        if (!masterServiceManager.isServiceEnabled()) {
            String message = String.format("Failed to restart instance for %s because the service is not enabled.", serviceName);
            LOG.debug(message);
            isSuccess = false;
            throw new ForbiddenException(message);
        }
        if (restartAll) {
            masterServiceManager.restartAllInstances();
        } else {
            if (instanceId < 0 || instanceId >= masterServiceManager.getInstances()) {
                throw new IllegalArgumentException();
            }
            masterServiceManager.restartInstances(instanceId);
        }
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IllegalStateException ise) {
        String message = String.format("Failed to restart instance for %s because the service may not be ready yet", serviceName);
        LOG.debug(message, ise);
        isSuccess = false;
        throw new ServiceUnavailableException(message);
    } catch (IllegalArgumentException iex) {
        String message = String.format("Failed to restart instance %d for service: %s because invalid instance id", instanceId, serviceName);
        LOG.debug(message, iex);
        isSuccess = false;
        throw new BadRequestException(message);
    } catch (Exception ex) {
        LOG.warn(String.format("Exception when trying to restart instances for service %s", serviceName), ex);
        isSuccess = false;
        throw new Exception(String.format("Error restarting instance %d for service: %s", instanceId, serviceName));
    } finally {
        long endTimeMs = System.currentTimeMillis();
        if (restartAll) {
            serviceStore.setRestartAllInstancesRequest(serviceName, startTimeMs, endTimeMs, isSuccess);
        } else {
            serviceStore.setRestartInstanceRequest(serviceName, startTimeMs, endTimeMs, isSuccess, instanceId);
        }
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) 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) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) JsonSyntaxException(com.google.gson.JsonSyntaxException) ForbiddenException(co.cask.cdap.common.ForbiddenException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException)

Example 25 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class SecureStoreHandler method create.

@Path("/{key-name}")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void create(HttpRequest httpRequest, HttpResponder httpResponder, @PathParam("namespace-id") String namespace, @PathParam("key-name") String name) throws Exception {
    SecureKeyId secureKeyId = new SecureKeyId(namespace, name);
    SecureKeyCreateRequest secureKeyCreateRequest = parseBody(httpRequest, SecureKeyCreateRequest.class);
    if (secureKeyCreateRequest == null) {
        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));
    }
    secureStoreManager.putSecureData(namespace, name, secureKeyCreateRequest.getData(), secureKeyCreateRequest.getDescription(), secureKeyCreateRequest.getProperties());
    httpResponder.sendStatus(HttpResponseStatus.OK);
}
Also used : SecureKeyCreateRequest(co.cask.cdap.proto.security.SecureKeyCreateRequest) SecureKeyId(co.cask.cdap.proto.id.SecureKeyId) BadRequestException(co.cask.cdap.common.BadRequestException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

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