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);
}
}
}
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");
}
}
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();
}
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);
}
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");
}
Aggregations