use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class PreviewHttpHandler method start.
@POST
@Path("/previews")
public void start(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
NamespaceId namespace = new NamespaceId(namespaceId);
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
AppRequest<?> appRequest = GSON.fromJson(reader, AppRequest.class);
responder.sendString(HttpResponseStatus.OK, GSON.toJson(previewManager.start(namespace, appRequest)));
} catch (JsonSyntaxException e) {
throw new BadRequestException("Request body is invalid json: " + e.getMessage());
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage());
}
}
use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class PreviewHttpHandler method getTracersData.
@POST
@Path("/previews/{preview-id}/tracers")
public void getTracersData(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("preview-id") String previewId) throws Exception {
ApplicationId application = validateAndGetAppId(namespaceId, previewId);
Map<String, List<String>> previewRequest;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
previewRequest = GSON.fromJson(reader, STRING_LIST_MAP_TYPE);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Request body is invalid json: " + e.getMessage());
}
if (previewRequest == null) {
throw new BadRequestException("The body is not provided.");
}
List<String> tracerNames = previewRequest.get("tracers");
if (tracerNames == null || tracerNames.isEmpty()) {
throw new BadRequestException("Tracer names cannot be empty.");
}
Map<String, Map<String, List<JsonElement>>> result = new HashMap<>();
for (String tracerName : tracerNames) {
result.put(tracerName, previewManager.getData(application, tracerName));
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result));
}
use of io.cdap.cdap.common.BadRequestException 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(FullHttpRequest 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));
}
SystemServiceId systemServiceId = new SystemServiceId(serviceName);
contextAccessEnforcer.enforce(systemServiceId, StandardPermission.UPDATE);
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 io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class DefaultNamespaceAdminTest method testConfigUpdate.
@Test
public void testConfigUpdate() throws Exception {
String namespace = "custompaceNamespace";
NamespaceId namespaceId = new NamespaceId(namespace);
// check that root directory for a namespace cannot be updated
// create the custom directory since the namespace is being created with custom root directory it needs to exist
String customRoot = "/some/custom/dir";
Location customlocation = baseLocationFactory.create(customRoot);
Assert.assertTrue(customlocation.mkdirs());
NamespaceMeta nsMeta = new NamespaceMeta.Builder().setName(namespaceId).setRootDirectory(customRoot).build();
namespaceAdmin.create(nsMeta);
Assert.assertTrue(namespaceAdmin.exists(namespaceId));
// Updating the root directory for a namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setRootDirectory("/newloc").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// Updating the HBase namespace for a namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setHBaseNamespace("custns").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// Updating the hive database for a namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setHiveDatabase("newDB").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// removing the root directory mapping for a namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setRootDirectory("").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// updating the principal for an existing namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setPrincipal("newPrincipal").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// updating the keytabURI for an existing namespace with no existing principal should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setKeytabURI("/new/keytab/uri").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// updating the groupname for an existing namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setGroupName("anotherGroup").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// Although disabling explore impersonation should be allowed
Assert.assertTrue(namespaceAdmin.get(nsMeta.getNamespaceId()).getConfig().isExploreAsPrincipal());
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setExploreAsPrincipal(false).build());
Assert.assertFalse(namespaceAdmin.get(nsMeta.getNamespaceId()).getConfig().isExploreAsPrincipal());
// clean up
namespaceAdmin.delete(namespaceId);
Locations.deleteQuietly(customlocation);
}
use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.
the class DefaultNamespaceAdminTest method testKerberos.
@Test
public void testKerberos() throws Exception {
// test that the namespace create handler doesn't allow configuring only one of the following two:
// principal, keytabURI
NamespaceMeta namespaceMeta = new NamespaceMeta.Builder().setName("test_ns").setPrincipal("somePrincipal").build();
try {
namespaceAdmin.create(namespaceMeta);
Assert.fail();
} catch (BadRequestException bre) {
// expected
}
// now check with just key tab uri
namespaceMeta = new NamespaceMeta.Builder().setName("test_ns").setKeytabURI("/some/path").build();
try {
namespaceAdmin.create(namespaceMeta);
Assert.fail();
} catch (BadRequestException bre) {
// expected
}
// if set explore as principal is set to false it should be present with both keytab uri and principal
namespaceMeta = new NamespaceMeta.Builder().setName("test_ns").setKeytabURI("/some/path").setExploreAsPrincipal(false).build();
try {
namespaceAdmin.create(namespaceMeta);
Assert.fail();
} catch (BadRequestException bre) {
// expected
}
// if set explore as principal is set to false it shoule be present with both keytab uri and principal
namespaceMeta = new NamespaceMeta.Builder().setName("test_ns").setPrincipal("somePrincipal").setExploreAsPrincipal(false).build();
try {
namespaceAdmin.create(namespaceMeta);
Assert.fail();
} catch (BadRequestException bre) {
// expected
}
}
Aggregations