Search in sources :

Example 51 with BadRequestException

use of co.cask.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 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(co.cask.cdap.proto.NamespaceMeta) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 52 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ArtifactClient method add.

/**
   * Add an artifact.
   *
   * @param namespace the namespace to add the artifact to
   * @param artifactName the name of the artifact to add
   * @param artifactContents an input supplier for the contents of the artifact
   * @param artifactVersion the version of the artifact to add. If null, the version will be derived from the
   *                        manifest of the artifact
   * @param parentArtifacts the set of artifacts this artifact extends
   * @param additionalPlugins the set of plugins contained in the artifact that cannot be determined
   *                          through jar inspection. This set should include any classes that are plugins but could
   *                          not be annotated as such. For example, 3rd party classes like jdbc drivers fall into
   *                          this category.
   * @throws ArtifactAlreadyExistsException if the artifact already exists
   * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
   * @throws ArtifactRangeNotFoundException if the parent artifacts do not exist
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public void add(NamespaceId namespace, String artifactName, InputSupplier<? extends InputStream> artifactContents, @Nullable String artifactVersion, @Nullable Set<ArtifactRange> parentArtifacts, @Nullable Set<PluginClass> additionalPlugins) throws ArtifactAlreadyExistsException, BadRequestException, IOException, UnauthenticatedException, ArtifactRangeNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespace, String.format("artifacts/%s", artifactName));
    HttpRequest.Builder requestBuilder = HttpRequest.post(url);
    if (artifactVersion != null) {
        requestBuilder.addHeader("Artifact-Version", artifactVersion);
    }
    if (parentArtifacts != null && !parentArtifacts.isEmpty()) {
        requestBuilder.addHeader("Artifact-Extends", Joiner.on('/').join(parentArtifacts));
    }
    if (additionalPlugins != null && !additionalPlugins.isEmpty()) {
        requestBuilder.addHeader("Artifact-Plugins", GSON.toJson(additionalPlugins));
    }
    HttpRequest request = requestBuilder.withBody(artifactContents).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_CONFLICT, HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_CONFLICT) {
        throw new ArtifactAlreadyExistsException(response.getResponseBodyAsString());
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactRangeNotFoundException(parentArtifacts);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) ArtifactRangeNotFoundException(co.cask.cdap.common.ArtifactRangeNotFoundException) ArtifactAlreadyExistsException(co.cask.cdap.common.ArtifactAlreadyExistsException) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Example 53 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ArtifactClient method deleteProperty.

/**
   * Delete a property for an artifact. If the property does not exist, this will be a no-op.
   *
   * @param artifactId the artifact to delete a property from
   * @param key the property to delete
   * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   * @throws ArtifactNotFoundException if the artifact does not exist
   * @throws IOException if a network error occurred
   */
public void deleteProperty(ArtifactId artifactId, String key) throws IOException, UnauthenticatedException, ArtifactNotFoundException, BadRequestException, UnauthorizedException {
    String path = String.format("artifacts/%s/versions/%s/properties/%s", artifactId.getArtifact(), artifactId.getVersion(), key);
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
    HttpRequest.Builder requestBuilder = HttpRequest.delete(url);
    HttpRequest request = requestBuilder.build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Example 54 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ArtifactClient method delete.

/**
   * Delete an artifact.
   *
   * @param artifactId the artifact to delete
   *
   * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public void delete(ArtifactId artifactId) throws IOException, UnauthenticatedException, BadRequestException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), String.format("artifacts/%s/versions/%s", artifactId.getArtifact(), artifactId.getVersion()));
    HttpRequest request = HttpRequest.delete(url).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Example 55 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class ApplicationClient method update.

/**
   * Update an existing app to use a different artifact version or config.
   *
   * @param appId the id of the application to update
   * @param updateRequest the request to update the application with
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   * @throws NotFoundException if the app or requested artifact could not be found
   * @throws BadRequestException if the request is invalid
   */
public void update(ApplicationId appId, AppRequest<?> updateRequest) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(appId.getParent(), String.format("apps/%s/update", appId.getApplication()));
    HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(updateRequest)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException("app or app artifact");
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(String.format("Bad Request. Reason: %s", response.getResponseBodyAsString()));
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Aggregations

BadRequestException (co.cask.cdap.common.BadRequestException)82 Path (javax.ws.rs.Path)29 NotFoundException (co.cask.cdap.common.NotFoundException)25 HttpResponse (co.cask.common.http.HttpResponse)17 JsonSyntaxException (com.google.gson.JsonSyntaxException)17 URL (java.net.URL)17 POST (javax.ws.rs.POST)17 NamespaceId (co.cask.cdap.proto.id.NamespaceId)16 IOException (java.io.IOException)15 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)13 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)12 HttpRequest (co.cask.common.http.HttpRequest)12 InputStreamReader (java.io.InputStreamReader)11 Reader (java.io.Reader)11 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)9 ApplicationId (co.cask.cdap.proto.id.ApplicationId)8 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)7 ProgramType (co.cask.cdap.proto.ProgramType)7 ProgramId (co.cask.cdap.proto.id.ProgramId)6 Test (org.junit.Test)6