use of io.cdap.cdap.common.NamespaceNotFoundException in project cdap by caskdata.
the class ArtifactHttpHandlerInternal method getArtifactDetail.
@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}")
public void getArtifactDetail(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @QueryParam("scope") @DefaultValue("user") String scope) throws Exception {
NamespaceId namespaceId = new NamespaceId(namespace);
if (!namespaceId.equals(NamespaceId.SYSTEM)) {
if (!namespaceQueryAdmin.exists(namespaceId)) {
throw new NamespaceNotFoundException(namespaceId);
}
}
ArtifactId artifactId = new ArtifactId(namespace, artifactName, artifactVersion);
ArtifactDetail artifactDetail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId));
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(artifactDetail));
}
use of io.cdap.cdap.common.NamespaceNotFoundException in project cdap by caskdata.
the class FileSecureStoreService method delete.
/**
* Deletes the element with the given name. Flushes the keystore after deleting the key from the in memory keystore.
* If the flush fails, we attempt to insert to key back to the in memory store and notify the user that delete failed.
* If the insertion in the key store fails after a flush failure then there would be a discrepancy between the
* in memory store and the file on the disk. This will be remedied the next time a flush happens.
* If another flush does not happen and the system is restarted, the only time that file is read,
* then we will have an extra key in the keystore.
* @param namespace The namespace this key belongs to.
* @param name Name of the element to be deleted.
* @throws NamespaceNotFoundException If the specified namespace does not exist.
* @throws NotFoundException If the key to be deleted is not found.
* @throws IOException If their was a problem during deleting the key from the in memory store
* or if there was a problem persisting the keystore after deleting the element.
*/
@Override
public void delete(String namespace, String name) throws Exception {
checkNamespaceExists(namespace);
String keyName = getKeyName(namespace, name);
Key key = null;
writeLock.lock();
try {
if (!keyStore.containsAlias(keyName)) {
throw new NotFoundException(new SecureKeyId(namespace, name));
}
key = deleteFromStore(keyName, password);
flush();
LOG.debug(String.format("Successfully deleted key %s from namespace %s", name, namespace));
} catch (UnrecoverableKeyException | NoSuchAlgorithmException | KeyStoreException e) {
throw new IOException("Failed to delete the key. ", e);
} catch (IOException ioe) {
try {
keyStore.setKeyEntry(keyName, key, password, null);
} catch (KeyStoreException e) {
ioe.addSuppressed(e);
}
throw ioe;
} finally {
writeLock.unlock();
}
}
use of io.cdap.cdap.common.NamespaceNotFoundException in project cdap by caskdata.
the class DefaultNamespaceAdminTest method testNamespaces.
@Test
public void testNamespaces() throws Exception {
String namespace = "namespace";
NamespaceId namespaceId = new NamespaceId(namespace);
NamespaceMeta.Builder builder = new NamespaceMeta.Builder();
int initialCount = namespaceAdmin.list().size();
// TEST_NAMESPACE_META1 is already created in AppFabricTestBase#beforeClass
Assert.assertTrue(namespaceAdmin.exists(new NamespaceId(TEST_NAMESPACE1)));
// It should be present in cache too
Assert.assertNotNull(getFromCache(new NamespaceId(TEST_NAMESPACE1)));
try {
namespaceAdmin.create(TEST_NAMESPACE_META1);
Assert.fail("Should not create duplicate namespace.");
} catch (NamespaceAlreadyExistsException e) {
Assert.assertEquals(TEST_NAMESPACE_META1.getNamespaceId(), e.getId());
}
// "random" namespace should not exist
try {
namespaceAdmin.get(new NamespaceId("random"));
Assert.fail("Namespace 'random' should not exist.");
} catch (NamespaceNotFoundException e) {
Assert.assertEquals(new NamespaceId("random"), e.getId());
}
try {
namespaceAdmin.create(null);
Assert.fail("Namespace with null metadata should fail.");
} catch (IllegalArgumentException e) {
Assert.assertEquals("Namespace metadata should not be null.", e.getMessage());
}
Assert.assertEquals(initialCount, namespaceAdmin.list().size());
Assert.assertFalse(namespaceAdmin.exists(new NamespaceId(namespace)));
try {
namespaceAdmin.create(builder.build());
Assert.fail("Namespace with no name should fail");
} catch (IllegalArgumentException e) {
Assert.assertEquals("Namespace id cannot be null.", e.getMessage());
}
Assert.assertEquals(initialCount, namespaceAdmin.list().size());
Assert.assertFalse(namespaceAdmin.exists(namespaceId));
// namespace with default fields
namespaceAdmin.create(builder.setName(namespace).build());
Assert.assertEquals(initialCount + 1, namespaceAdmin.list().size());
Assert.assertTrue(namespaceAdmin.exists(namespaceId));
// it should be loaded in cache too since exists calls get
Assert.assertNotNull(getFromCache(namespaceId));
try {
NamespaceMeta namespaceMeta = namespaceAdmin.get(namespaceId);
Assert.assertEquals(namespaceId.getNamespace(), namespaceMeta.getName());
Assert.assertEquals("", namespaceMeta.getDescription());
namespaceAdmin.delete(namespaceId);
// it should be deleted from the cache too
Assert.assertNull(getFromCache(namespaceId));
} catch (NotFoundException e) {
Assert.fail(String.format("Namespace '%s' should be found since it was just created.", namespaceId.getNamespace()));
}
namespaceAdmin.create(builder.setDescription("describes " + namespace).build());
Assert.assertEquals(initialCount + 1, namespaceAdmin.list().size());
Assert.assertTrue(namespaceAdmin.exists(namespaceId));
try {
NamespaceMeta namespaceMeta = namespaceAdmin.get(namespaceId);
// it should be loaded in cache too
Assert.assertNotNull(getFromCache(namespaceId));
Assert.assertEquals(namespaceId.getNamespace(), namespaceMeta.getName());
Assert.assertEquals("describes " + namespaceId.getNamespace(), namespaceMeta.getDescription());
namespaceAdmin.delete(namespaceId);
// it should be deleted from the cache
Assert.assertNull(getFromCache(namespaceId));
} catch (NotFoundException e) {
Assert.fail(String.format("Namespace '%s' should be found since it was just created.", namespaceId.getNamespace()));
}
// Verify NotFoundException's contents as well, instead of just checking namespaceService.exists = false
verifyNotFound(namespaceId);
}
use of io.cdap.cdap.common.NamespaceNotFoundException in project cdap by caskdata.
the class FileSecureStoreService method get.
/**
* Returns the data stored in the secure store.
* @param namespace The namespace this key belongs to.
* @param name Name of the data element.
* @return An object representing the securely stored data associated with the name.
* @throws NamespaceNotFoundException If the specified namespace does not exist.
* @throws NotFoundException If the key is not found in the store.
* @throws IOException If there was a problem reading from the store.
*/
@Override
public SecureStoreData get(String namespace, String name) throws Exception {
checkNamespaceExists(namespace);
String keyName = getKeyName(namespace, name);
readLock.lock();
try {
if (!keyStore.containsAlias(keyName)) {
throw new NotFoundException(name + " not found in the secure store.");
}
Key key = keyStore.getKey(keyName, password);
return deserialize(key.getEncoded());
} catch (NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException e) {
throw new IOException("Unable to retrieve the key " + name, e);
} finally {
readLock.unlock();
}
}
use of io.cdap.cdap.common.NamespaceNotFoundException in project cdap by caskdata.
the class AbstractNamespaceClient method deleteDatasets.
@Override
public void deleteDatasets(NamespaceId namespaceId) throws Exception {
URL url = resolve(String.format("unrecoverable/namespaces/%s/datasets", namespaceId.getNamespace()));
HttpResponse response = execute(HttpRequest.delete(url).build());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NamespaceNotFoundException(namespaceId);
} else if (HttpURLConnection.HTTP_FORBIDDEN == response.getResponseCode()) {
String msg = String.format("Datasets in the namespace '%s' cannot be deleted. Reason: '%s'.", namespaceId, response.getResponseBodyAsString());
throw new NamespaceCannotBeDeletedException(namespaceId, msg);
} else if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
return;
}
throw new IOException(String.format("Cannot delete datasets in namespace %s. Reason: %s", namespaceId, response.getResponseBodyAsString()));
}
Aggregations