Search in sources :

Example 71 with NotFoundException

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

the class ArtifactHttpHandler method callArtifactPluginMethod.

@Beta
@POST
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/" + "versions/{artifact-version}/plugintypes/{plugin-type}/plugins/{plugin-name}/methods/{plugin-method}")
@AuditPolicy({ AuditDetail.REQUEST_BODY, AuditDetail.RESPONSE_BODY })
public void callArtifactPluginMethod(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-name") String pluginName, @PathParam("plugin-type") String pluginType, @PathParam("plugin-method") String methodName, @QueryParam("scope") @DefaultValue("user") String scope) throws Exception {
    String requestBody = request.getContent().toString(Charsets.UTF_8);
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    Id.Artifact artifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    if (requestBody.isEmpty()) {
        throw new BadRequestException("Request body is used as plugin method parameter, " + "Received empty request body.");
    }
    try {
        PluginEndpoint pluginEndpoint = pluginService.getPluginEndpoint(namespace, artifactId, pluginType, pluginName, methodName);
        Object response = pluginEndpoint.invoke(GSON.fromJson(requestBody, pluginEndpoint.getMethodParameterType()));
        responder.sendString(HttpResponseStatus.OK, GSON.toJson(response));
    } catch (JsonSyntaxException e) {
        LOG.error("Exception while invoking plugin method.", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "Unable to deserialize request body to method parameter type");
    } catch (InvocationTargetException e) {
        LOG.error("Exception while invoking plugin method.", e);
        if (e.getCause() instanceof javax.ws.rs.NotFoundException) {
            throw new NotFoundException(e.getCause());
        } else if (e.getCause() instanceof javax.ws.rs.BadRequestException) {
            throw new BadRequestException(e.getCause());
        } else if (e.getCause() instanceof IllegalArgumentException && e.getCause() != null) {
            responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getCause().getMessage());
        } else {
            Throwable rootCause = Throwables.getRootCause(e);
            String message = String.format("Error while invoking plugin method %s.", methodName);
            if (rootCause != null && rootCause.getMessage() != null) {
                message = String.format("%s %s", message, rootCause.getMessage());
            }
            responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, message);
        }
    }
}
Also used : NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) ArtifactRangeNotFoundException(co.cask.cdap.common.ArtifactRangeNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(co.cask.cdap.common.BadRequestException) PluginEndpoint(co.cask.cdap.internal.app.runtime.plugin.PluginEndpoint) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST) Beta(co.cask.cdap.api.annotation.Beta)

Example 72 with NotFoundException

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

the class MonitorHandler method setServiceInstance.

/**
   * Sets the number of instances of CDAP Services
   */
@Path("/system/services/{service-name}/instances")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void setServiceInstance(HttpRequest request, HttpResponder responder, @PathParam("service-name") final String serviceName) throws Exception {
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    MasterServiceManager serviceManager = serviceManagementMap.get(serviceName);
    int instances = getInstances(request);
    if (!serviceManager.isServiceEnabled()) {
        throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
    }
    int currentInstances = getSystemServiceInstanceCount(serviceName);
    if (instances < serviceManager.getMinInstances() || instances > serviceManager.getMaxInstances()) {
        String response = String.format("Instance count should be between [%s,%s]", serviceManager.getMinInstances(), serviceManager.getMaxInstances());
        throw new BadRequestException(response);
    } else if (instances == currentInstances) {
        responder.sendStatus(HttpResponseStatus.OK);
        return;
    }
    serviceStore.setServiceInstance(serviceName, instances);
    if (serviceManager.setInstances(instances)) {
        responder.sendStatus(HttpResponseStatus.OK);
    } else {
        throw new BadRequestException("Operation did not succeed");
    }
}
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) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 73 with NotFoundException

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

the class ProgramScheduleStoreDataset method findSchedules.

/**
   * Find all schedules that have a trigger with a given trigger key.
   *
   * @param triggerKey the trigger key to look up
   * @return a list of all schedules that are triggered by this key; never null
   */
public Collection<ProgramScheduleRecord> findSchedules(String triggerKey) {
    Map<ScheduleId, ProgramScheduleRecord> schedulesFound = new HashMap<>();
    try (Scanner scanner = store.readByIndex(TRIGGER_KEY_COLUMN_BYTES, Bytes.toBytes(triggerKey))) {
        Row triggerRow;
        while ((triggerRow = scanner.next()) != null) {
            String triggerRowKey = Bytes.toString(triggerRow.getRow());
            try {
                ScheduleId scheduleId = extractScheduleIdFromTriggerKey(triggerRowKey);
                if (schedulesFound.containsKey(scheduleId)) {
                    continue;
                }
                Row row = store.get(new Get(rowKeyForSchedule(scheduleId)));
                byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES);
                if (serialized == null) {
                    throw new NotFoundException(scheduleId);
                }
                ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class);
                ProgramScheduleMeta meta = extractMetaFromRow(scheduleId, row);
                ProgramScheduleRecord record = new ProgramScheduleRecord(schedule, meta);
                schedulesFound.put(scheduleId, record);
            } catch (IllegalArgumentException | NotFoundException e) {
                // the only exceptions we know to be thrown here are IllegalArgumentException (ill-formed key) or
                // NotFoundException (if the schedule does not exist). Both should never happen, so we warn and ignore.
                // we will let any other exception propagate up, because it would be a DataSetException or similarly serious.
                LOG.warn("Problem with trigger id '{}' found for trigger key '{}': {}. Skipping entry.", triggerRowKey, triggerKey, e.getMessage());
            }
        }
    }
    return schedulesFound.values();
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) ProgramScheduleMeta(co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleMeta) HashMap(java.util.HashMap) NotFoundException(co.cask.cdap.common.NotFoundException) ScheduleId(co.cask.cdap.proto.id.ScheduleId) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) Get(co.cask.cdap.api.dataset.table.Get) ProgramScheduleRecord(co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) Row(co.cask.cdap.api.dataset.table.Row)

Example 74 with NotFoundException

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

the class ProgramScheduleStoreDataset method getSchedule.

/**
   * Read a schedule from the store.
   *
   * @param scheduleId the id of the schedule to read
   * @return the schedule from the store
   * @throws NotFoundException if the schedule does not exist in the store
   */
public ProgramSchedule getSchedule(ScheduleId scheduleId) throws NotFoundException {
    Row row = store.get(new Get(rowKeyForSchedule(scheduleId)));
    byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES);
    if (serialized == null) {
        throw new NotFoundException(scheduleId);
    }
    return GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class);
}
Also used : Get(co.cask.cdap.api.dataset.table.Get) NotFoundException(co.cask.cdap.common.NotFoundException) Row(co.cask.cdap.api.dataset.table.Row)

Example 75 with NotFoundException

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

the class AppFabricClient method scheduleStatus.

public String scheduleStatus(String namespaceId, String appId, String schedId, int expectedResponseCode) throws Exception {
    MockResponder responder = new MockResponder();
    String uri = String.format("%s/apps/%s/schedules/%s/status", getNamespacePath(namespaceId), appId, schedId);
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri);
    try {
        programLifecycleHttpHandler.getStatus(request, responder, namespaceId, appId, "schedules", schedId);
    } catch (NotFoundException e) {
        return "NOT_FOUND";
    }
    verifyResponse(HttpResponseStatus.valueOf(expectedResponseCode), responder.getStatus(), "Get schedules status failed");
    Map<String, String> json = responder.decodeResponseContent(MAP_TYPE);
    return json.get("status");
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) NotFoundException(co.cask.cdap.common.NotFoundException)

Aggregations

NotFoundException (co.cask.cdap.common.NotFoundException)122 URL (java.net.URL)42 HttpResponse (co.cask.common.http.HttpResponse)41 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)28 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)26 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)25 Path (javax.ws.rs.Path)25 BadRequestException (co.cask.cdap.common.BadRequestException)22 ProgramId (co.cask.cdap.proto.id.ProgramId)19 ApplicationId (co.cask.cdap.proto.id.ApplicationId)17 NamespaceId (co.cask.cdap.proto.id.NamespaceId)14 Test (org.junit.Test)14 POST (javax.ws.rs.POST)12 HttpRequest (co.cask.common.http.HttpRequest)10 IOException (java.io.IOException)10 GET (javax.ws.rs.GET)10 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)9 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)8 DatasetTypeNotFoundException (co.cask.cdap.common.DatasetTypeNotFoundException)8 TypeToken (com.google.common.reflect.TypeToken)8