use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.
the class ArtifactStore method delete.
/**
* Delete the specified artifact. Programs that use the artifact will no longer be able to start.
*
* @param artifactId the id of the artifact to delete
* @throws IOException if there was an IO error deleting the metadata or the actual artifact
*/
public void delete(final Id.Artifact artifactId) throws ArtifactNotFoundException, IOException {
// delete everything in a transaction
TransactionRunners.run(transactionRunner, context -> {
// first look up details to get plugins and apps in the artifact
StructuredTable artifactDataTable = getTable(context, StoreDefinition.ArtifactStore.ARTIFACT_DATA_TABLE);
ArtifactCell artifactCell = new ArtifactCell(artifactId);
Optional<StructuredRow> optional = artifactDataTable.read(artifactCell.keys);
if (!optional.isPresent()) {
throw new ArtifactNotFoundException(artifactId.toEntityId());
}
deleteMeta(context, artifactId, GSON.fromJson(optional.get().getString(StoreDefinition.ArtifactStore.ARTIFACT_DATA_FIELD), ArtifactData.class));
}, IOException.class, ArtifactNotFoundException.class);
}
use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.
the class ArtifactLocalizerClient method sendRequest.
private File sendRequest(ArtifactId artifactId, boolean unpack) throws IOException, ArtifactNotFoundException {
String urlPath = String.format("/artifact/namespaces/%s/artifacts/%s/versions/%s?unpack=%b", artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), unpack);
URL url;
try {
url = new URI(sidecarBaseURL + urlPath).toURL();
} catch (URISyntaxException e) {
throw new IOException(e);
}
LOG.debug("Sending request to {}", url);
HttpRequest httpRequest = HttpRequest.builder(HttpMethod.GET, url).build();
Multimap<String, String> headers = httpRequest.getHeaders();
internalAuthenticator.applyInternalAuthenticationHeaders(headers::put);
HttpResponse httpResponse = HttpRequests.execute(httpRequest);
if (httpResponse.getResponseCode() != HttpURLConnection.HTTP_OK) {
if (httpResponse.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ArtifactNotFoundException(artifactId);
}
BasicThrowable basicThrowable = GSON.fromJson(httpResponse.getResponseBodyAsString(), BasicThrowable.class);
throw new IOException(RemoteExecutionException.fromBasicThrowable(basicThrowable));
}
String path = httpResponse.getResponseBodyAsString();
LOG.debug("ArtifactLocalizer request returned path {}", path);
return new File(path);
}
use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.
the class RemoteIsolatedPluginFinder method getArtifactLocation.
@Override
protected Location getArtifactLocation(ArtifactId artifactId) throws IOException, ArtifactNotFoundException, UnauthorizedException {
String url = String.format("namespaces/%s/artifacts/%s/versions/%s/download?scope=%s", artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), artifactId.getNamespace().equals(NamespaceId.SYSTEM) ? ArtifactScope.SYSTEM.name().toLowerCase() : ArtifactScope.USER.name().toLowerCase());
HttpURLConnection urlConn = remoteClientInternal.openConnection(HttpMethod.GET, url);
try {
int responseCode = urlConn.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
switch(responseCode) {
// throw retryable error if app fabric is not available, might be due to restarting
case HttpURLConnection.HTTP_BAD_GATEWAY:
case HttpURLConnection.HTTP_UNAVAILABLE:
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
throw new ServiceUnavailableException(Constants.Service.APP_FABRIC_HTTP, Constants.Service.APP_FABRIC_HTTP + " service is not available with status " + responseCode);
case HttpURLConnection.HTTP_NOT_FOUND:
throw new ArtifactNotFoundException(artifactId);
}
throw new IOException(String.format("Exception while downloading artifact for artifact %s with reason: %s", artifactId, urlConn.getResponseMessage()));
}
File artifactLocation = new File(pluginDir, Artifacts.getFileName(artifactId.toApiArtifactId()));
try (InputStream in = urlConn.getInputStream()) {
Files.copy(in, artifactLocation.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
return Locations.toLocation(artifactLocation);
} finally {
urlConn.disconnect();
}
}
use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.
the class RemotePluginFinder method findPlugin.
@Override
public Map.Entry<ArtifactDescriptor, PluginClass> findPlugin(NamespaceId pluginNamespaceId, ArtifactId parentArtifactId, String pluginType, String pluginName, PluginSelector selector) throws PluginNotExistsException {
try {
return Retries.callWithRetries(() -> {
List<PluginInfo> infos = getPlugins(pluginNamespaceId, parentArtifactId, pluginType, pluginName);
if (infos.isEmpty()) {
throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
}
SortedMap<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> plugins = new TreeMap<>();
for (PluginInfo info : infos) {
ArtifactSummary artifactSummary = info.getArtifact();
io.cdap.cdap.api.artifact.ArtifactId pluginArtifactId = new io.cdap.cdap.api.artifact.ArtifactId(artifactSummary.getName(), new ArtifactVersion(artifactSummary.getVersion()), artifactSummary.getScope());
PluginClass pluginClass = PluginClass.builder().setName(info.getName()).setType(info.getType()).setDescription(info.getDescription()).setClassName(info.getClassName()).setProperties(info.getProperties()).setConfigFieldName(info.getConfigFieldName()).build();
plugins.put(pluginArtifactId, pluginClass);
}
Map.Entry<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> selected = selector.select(plugins);
if (selected == null) {
throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
}
Location artifactLocation = getArtifactLocation(Artifacts.toProtoArtifactId(pluginNamespaceId, selected.getKey()));
return Maps.immutableEntry(new ArtifactDescriptor(pluginNamespaceId.getEntityName(), selected.getKey(), artifactLocation), selected.getValue());
}, retryStrategy);
} catch (PluginNotExistsException e) {
throw e;
} catch (ArtifactNotFoundException e) {
throw new PluginNotExistsException(pluginNamespaceId, pluginType, pluginName);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.
the class RemotePluginFinder method getArtifactLocation.
/**
* Retrieves the {@link Location} of a given artifact.
*/
protected Location getArtifactLocation(ArtifactId artifactId) throws IOException, ArtifactNotFoundException, UnauthorizedException {
HttpRequest.Builder requestBuilder = remoteClientInternal.requestBuilder(HttpMethod.GET, String.format("namespaces/%s/artifacts/%s/versions/%s/location", artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion()));
HttpResponse response = remoteClientInternal.execute(requestBuilder.build());
int responseCode = response.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
switch(responseCode) {
// throw retryable error if app fabric is not available, might be due to restarting
case HttpURLConnection.HTTP_BAD_GATEWAY:
case HttpURLConnection.HTTP_UNAVAILABLE:
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
throw new ServiceUnavailableException(Constants.Service.APP_FABRIC_HTTP, Constants.Service.APP_FABRIC_HTTP + " service is not available with status " + responseCode);
case HttpURLConnection.HTTP_NOT_FOUND:
throw new ArtifactNotFoundException(artifactId);
}
throw new IOException("Exception while getting artifacts list: " + response.getResponseCode() + ": " + response.getResponseBodyAsString());
}
String path = response.getResponseBodyAsString();
Location location = Locations.getLocationFromAbsolutePath(locationFactory, path);
if (!location.exists()) {
throw new IOException(String.format("Artifact Location does not exist %s for artifact %s version %s", path, artifactId.getArtifact(), artifactId.getVersion()));
}
return location;
}
Aggregations