Search in sources :

Example 36 with BadRequestException

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());
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) LogReader(io.cdap.cdap.logging.read.LogReader) InputStreamReader(java.io.InputStreamReader) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 37 with BadRequestException

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));
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) Reader(java.io.Reader) LogReader(io.cdap.cdap.logging.read.LogReader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BadRequestException(io.cdap.cdap.common.BadRequestException) List(java.util.List) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Map(java.util.Map) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 38 with BadRequestException

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");
    }
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) ForbiddenException(io.cdap.cdap.common.ForbiddenException) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 39 with BadRequestException

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);
}
Also used : NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 40 with BadRequestException

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
    }
}
Also used : NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) BadRequestException(io.cdap.cdap.common.BadRequestException) Test(org.junit.Test)

Aggregations

BadRequestException (io.cdap.cdap.common.BadRequestException)188 Path (javax.ws.rs.Path)68 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)54 IOException (java.io.IOException)46 JsonSyntaxException (com.google.gson.JsonSyntaxException)44 NotFoundException (io.cdap.cdap.common.NotFoundException)42 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)42 POST (javax.ws.rs.POST)42 HttpResponse (io.cdap.common.http.HttpResponse)36 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)34 URL (java.net.URL)34 ProgramType (io.cdap.cdap.proto.ProgramType)30 InputStreamReader (java.io.InputStreamReader)28 Reader (java.io.Reader)28 ArrayList (java.util.ArrayList)28 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)26 ProgramId (io.cdap.cdap.proto.id.ProgramId)26 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)24 GET (javax.ws.rs.GET)24 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)22