use of io.cdap.cdap.common.HandlerException in project cdap by caskdata.
the class DatasetInstanceService method executeAdmin.
/**
* Executes an admin operation on a dataset.
*
* @param datasetId the datasetId to execute the admin operation on
* @param method the type of admin operation to execute
* @return the {@link DatasetAdminOpResponse} from the HTTP handler
* @throws NamespaceNotFoundException if the requested namespace was not found
* @throws IOException if there was a problem in checking if the namespace exists over HTTP
* @throws UnauthorizedException if perimeter security and authorization are enabled, and the current user does not
* have -
* <ol>
* <li>{@link StandardPermission#DELETE} privileges on the dataset for "truncate" </li>
* <li>{@link StandardPermission#UPDATE} privileges on the dataset for "upgrade" </li>
* <li>read privileges on the dataset for "exists"</li>
* <ol>
*/
DatasetAdminOpResponse executeAdmin(DatasetId datasetId, String method) throws Exception {
ensureNamespaceExists(datasetId.getParent());
Object result = null;
// NOTE: one cannot directly call create and drop, instead this should be called thru
// POST/DELETE @ /data/datasets/{datasetId-id}. Because we must create/drop metadata for these at same time
Principal principal = authenticationContext.getPrincipal();
switch(method) {
case "exists":
// ensure the user has some privilege on the dataset datasetId if it is not system dataset
if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
accessEnforcer.enforce(datasetId, principal, StandardPermission.GET);
}
result = opExecutorClient.exists(datasetId);
break;
case "truncate":
if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
accessEnforcer.enforce(datasetId, principal, StandardPermission.DELETE);
}
if (instanceManager.get(datasetId) == null) {
throw new DatasetNotFoundException(datasetId);
}
opExecutorClient.truncate(datasetId);
publishAudit(datasetId, AuditType.TRUNCATE);
break;
case "upgrade":
if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
accessEnforcer.enforce(datasetId, principal, StandardPermission.UPDATE);
}
if (instanceManager.get(datasetId) == null) {
throw new DatasetNotFoundException(datasetId);
}
opExecutorClient.upgrade(datasetId);
publishAudit(datasetId, AuditType.UPDATE);
break;
default:
throw new HandlerException(HttpResponseStatus.NOT_FOUND, "Invalid admin operation: " + method);
}
return new DatasetAdminOpResponse(result, null);
}
use of io.cdap.cdap.common.HandlerException in project cdap by caskdata.
the class DatasetInstanceHandler method executeAdmin.
/**
* Executes an admin operation on a dataset instance.
*
* @param namespaceId namespace of the dataset instance
* @param name name of the dataset instance
* @param method the admin operation to execute (e.g. "exists", "truncate", "upgrade")
* @throws Exception
*/
@POST
@Path("/data/datasets/{name}/admin/{method}")
public void executeAdmin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name, @PathParam("method") String method) throws Exception {
logCallReceived(request);
DatasetId instance = ConversionHelpers.toDatasetInstanceId(namespaceId, name);
try {
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(instanceService.executeAdmin(instance, method)));
} catch (HandlerException e) {
responder.sendStatus(e.getFailureStatus());
}
logCallResponded(request);
}
use of io.cdap.cdap.common.HandlerException in project cdap by caskdata.
the class HttpRequestRouter method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
HttpResponse response = cause instanceof HandlerException ? ((HandlerException) cause).createFailureResponse() : createErrorResponse(cause);
HttpUtil.setKeepAlive(response, false);
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
use of io.cdap.cdap.common.HandlerException in project cdap by caskdata.
the class DatasetInstanceHandler method create.
/**
* Creates a new dataset instance.
*
* @param namespaceId namespace of the new dataset instance
* @param name name of the new dataset instance
*/
@PUT
@Path("/data/datasets/{name}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void create(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) throws Exception {
logCallReceived(request);
DatasetInstanceConfiguration creationProperties = ConversionHelpers.getInstanceConfiguration(request);
try {
instanceService.create(namespaceId, name, creationProperties);
responder.sendStatus(HttpResponseStatus.OK);
} catch (DatasetAlreadyExistsException e) {
responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage());
} catch (IllegalArgumentException e) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
} catch (DatasetTypeNotFoundException e) {
responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
} catch (HandlerException e) {
responder.sendString(e.getFailureStatus(), e.getMessage());
}
logCallResponded(request);
}
use of io.cdap.cdap.common.HandlerException in project cdap by caskdata.
the class HttpRequestRouter method getDiscoverable.
/**
* Finds the {@link Discoverable} for the given {@link HttpRequest} to route to.
*/
private Discoverable getDiscoverable(HttpRequest httpRequest) {
EndpointStrategy strategy = serviceLookup.getDiscoverable(httpRequest);
if (strategy == null) {
throw new HandlerException(HttpResponseStatus.SERVICE_UNAVAILABLE, "No endpoint strategy found for request " + getRequestLine(httpRequest));
}
// Do a non-blocking pick first. If the service has been discovered before, this should return an endpoint
// immediately.
Discoverable discoverable = strategy.pick();
if (discoverable != null) {
return discoverable;
}
// Do a blocking pick for up to 1 second. It is for the case where a service is being discovered for the first time,
// in which population of the cache might take time.
discoverable = strategy.pick(1, TimeUnit.SECONDS);
if (discoverable == null) {
throw new HandlerException(HttpResponseStatus.SERVICE_UNAVAILABLE, "No discoverable found for request " + getRequestLine(httpRequest));
}
return discoverable;
}
Aggregations