use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class DatasetAdminService method drop.
public void drop(final DatasetId datasetInstanceId, final DatasetTypeMeta typeMeta, final DatasetSpecification spec) throws Exception {
LOG.info("Dropping dataset with spec: {}, type meta: {}", spec, typeMeta);
try (DatasetClassLoaderProvider classLoaderProvider = new DirectoryClassLoaderProvider(cConf, locationFactory)) {
UserGroupInformation ugi = getUgiForDataset(impersonator, datasetInstanceId);
ImpersonationUtils.doAs(ugi, new Callable<Void>() {
@Override
public Void call() throws Exception {
DatasetType type = dsFramework.getDatasetType(typeMeta, null, classLoaderProvider);
if (type == null) {
throw new BadRequestException(String.format("Cannot instantiate dataset type using provided type meta: %s", typeMeta));
}
DatasetAdmin admin = type.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec);
admin.drop();
return null;
}
});
}
// Remove metadata for the dataset
metadataStore.removeMetadata(datasetInstanceId);
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class DatasetAdminOpHTTPHandler method drop.
@POST
@Path("/data/datasets/{name}/admin/drop")
public void drop(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String instanceName) throws Exception {
propagateUserId(request);
InternalDatasetDropParams params = GSON.fromJson(request.getContent().toString(Charsets.UTF_8), InternalDatasetDropParams.class);
Preconditions.checkArgument(params.getInstanceSpec() != null, "Missing required 'instanceSpec' parameter.");
Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter.");
DatasetSpecification spec = params.getInstanceSpec();
DatasetTypeMeta typeMeta = params.getTypeMeta();
try {
datasetAdminService.drop(new DatasetId(namespaceId, instanceName), typeMeta, spec);
responder.sendJson(HttpResponseStatus.OK, spec);
} catch (BadRequestException e) {
responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
}
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class MetadataHttpHandler method searchMetadata.
@GET
@Path("/namespaces/{namespace-id}/metadata/search")
public void searchMetadata(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("query") String searchQuery, @QueryParam("target") List<String> targets, @QueryParam("sort") @DefaultValue("") String sort, @QueryParam("offset") @DefaultValue("0") int offset, // 2147483647 is Integer.MAX_VALUE
@QueryParam("limit") @DefaultValue("2147483647") int limit, @QueryParam("numCursors") @DefaultValue("0") int numCursors, @QueryParam("cursor") @DefaultValue("") String cursor, @QueryParam("showHidden") @DefaultValue("false") boolean showHidden, @Nullable @QueryParam("entityScope") String entityScope) throws Exception {
if (searchQuery == null || searchQuery.isEmpty()) {
throw new BadRequestException("query is not specified");
}
Set<EntityTypeSimpleName> types = Collections.emptySet();
if (targets != null) {
types = ImmutableSet.copyOf(Iterables.transform(targets, STRING_TO_TARGET_TYPE));
}
SortInfo sortInfo = SortInfo.of(URLDecoder.decode(sort, "UTF-8"));
if (SortInfo.DEFAULT.equals(sortInfo)) {
if (!(cursor.isEmpty()) || 0 != numCursors) {
throw new BadRequestException("Cursors are not supported when sort info is not specified.");
}
}
try {
MetadataSearchResponse response = metadataAdmin.search(namespaceId, URLDecoder.decode(searchQuery, "UTF-8"), types, sortInfo, offset, limit, numCursors, cursor, showHidden, validateEntityScope(entityScope));
responder.sendJson(HttpResponseStatus.OK, response, MetadataSearchResponse.class, GSON);
} catch (Exception e) {
// if MetadataDataset throws an exception, it gets wrapped
if (Throwables.getRootCause(e) instanceof BadRequestException) {
throw new BadRequestException(e.getMessage(), e);
}
throw e;
}
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class MetadataHttpHandler method readMetadata.
private Map<String, String> readMetadata(HttpRequest request) throws BadRequestException {
ChannelBuffer content = request.getContent();
if (!content.readable()) {
throw new BadRequestException("Unable to read metadata properties from the request.");
}
Map<String, String> metadata;
try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(content), Charsets.UTF_8)) {
metadata = GSON.fromJson(reader, MAP_STRING_STRING_TYPE);
} catch (IOException e) {
throw new BadRequestException("Unable to read metadata properties from the request.", e);
}
if (metadata == null) {
throw new BadRequestException("Null metadata was read from the request");
}
return metadata;
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class StreamHandler method getAndValidateConfig.
/**
* Gets stream properties from the request. If there is request is invalid, a BadRequestException will be thrown.
*/
private StreamProperties getAndValidateConfig(HttpRequest request) throws BadRequestException {
Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()));
StreamProperties properties;
try {
properties = GSON.fromJson(reader, StreamProperties.class);
} catch (Exception e) {
throw new BadRequestException("Invalid stream configuration. Please check that the " + "configuration is a valid JSON Object with a valid schema. Cause: " + e.getMessage());
}
// Validate ttl
Long ttl = properties.getTTL();
if (ttl != null && ttl < 0) {
throw new BadRequestException("Invalid TTL " + ttl + ". TTL value should be positive.");
}
// Validate format
FormatSpecification formatSpec = properties.getFormat();
if (formatSpec != null) {
String formatName = formatSpec.getName();
if (formatName == null) {
throw new BadRequestException("A format name must be specified.");
}
try {
// if a format is given, make sure it is a valid format,
// check that we can instantiate the format class
RecordFormat<?, ?> format = RecordFormats.createInitializedFormat(formatSpec);
// the request may contain a null schema, in which case the default schema of the format should be used.
// create a new specification object that is guaranteed to have a non-null schema.
formatSpec = new FormatSpecification(formatSpec.getName(), format.getSchema(), formatSpec.getSettings());
} catch (UnsupportedTypeException e) {
throw new BadRequestException("Format " + formatName + " does not support the requested schema.");
} catch (Exception e) {
throw new BadRequestException("Invalid format, unable to instantiate format " + formatName);
}
}
// Validate notification threshold
Integer threshold = properties.getNotificationThresholdMB();
if (threshold != null && threshold <= 0) {
throw new BadRequestException("Invalid threshold " + threshold + ". Threshold value should be greater than zero.");
}
// validate owner principal if one is provided
if (properties.getOwnerPrincipal() != null) {
SecurityUtil.validateKerberosPrincipal(properties.getOwnerPrincipal());
}
return new StreamProperties(ttl, formatSpec, threshold, properties.getDescription(), properties.getOwnerPrincipal());
}
Aggregations