Search in sources :

Example 1 with HandlerException

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);
}
Also used : HandlerException(io.cdap.cdap.common.HandlerException) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) DatasetAdminOpResponse(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse) Principal(io.cdap.cdap.proto.security.Principal)

Example 2 with HandlerException

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);
}
Also used : HandlerException(io.cdap.cdap.common.HandlerException) DatasetId(io.cdap.cdap.proto.id.DatasetId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 3 with HandlerException

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);
}
Also used : HandlerException(io.cdap.cdap.common.HandlerException) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 4 with HandlerException

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);
}
Also used : HandlerException(io.cdap.cdap.common.HandlerException) DatasetAlreadyExistsException(io.cdap.cdap.common.DatasetAlreadyExistsException) DatasetInstanceConfiguration(io.cdap.cdap.proto.DatasetInstanceConfiguration) DatasetTypeNotFoundException(io.cdap.cdap.common.DatasetTypeNotFoundException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 5 with HandlerException

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;
}
Also used : HandlerException(io.cdap.cdap.common.HandlerException) Discoverable(org.apache.twill.discovery.Discoverable) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy)

Aggregations

HandlerException (io.cdap.cdap.common.HandlerException)5 Path (javax.ws.rs.Path)2 DatasetAlreadyExistsException (io.cdap.cdap.common.DatasetAlreadyExistsException)1 DatasetNotFoundException (io.cdap.cdap.common.DatasetNotFoundException)1 DatasetTypeNotFoundException (io.cdap.cdap.common.DatasetTypeNotFoundException)1 EndpointStrategy (io.cdap.cdap.common.discovery.EndpointStrategy)1 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)1 DatasetAdminOpResponse (io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse)1 DatasetInstanceConfiguration (io.cdap.cdap.proto.DatasetInstanceConfiguration)1 DatasetId (io.cdap.cdap.proto.id.DatasetId)1 Principal (io.cdap.cdap.proto.security.Principal)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 Discoverable (org.apache.twill.discovery.Discoverable)1