use of io.cdap.cdap.proto.artifact.ArtifactSortOrder in project cdap by caskdata.
the class ArtifactHttpHandler method getArtifactProperties.
@POST
@Path("/namespaces/{namespace-id}/artifactproperties")
public void getArtifactProperties(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("order") @DefaultValue("DESC") String order) throws Exception {
NamespaceId namespace = validateAndGetNamespace(namespaceId);
ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
List<ArtifactPropertiesRequest> propertyRequests;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
propertyRequests = GSON.fromJson(reader, BATCH_ARTIFACT_PROPERTIES_REQUEST);
} catch (JsonSyntaxException e) {
throw new BadRequestException("Unable to parse request: " + e.getMessage(), e);
}
List<ArtifactSummaryProperties> result = new ArrayList<>(propertyRequests.size());
for (ArtifactPropertiesRequest propertiesRequest : propertyRequests) {
NamespaceId requestNamespace = propertiesRequest.getScope() == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : namespace;
ArtifactRange range = new ArtifactRange(requestNamespace.getNamespace(), propertiesRequest.getName(), ArtifactVersionRange.parse(propertiesRequest.getVersion()));
List<ArtifactDetail> artifactDetails = artifactRepository.getArtifactDetails(range, 1, sortOrder);
for (ArtifactDetail artifactDetail : artifactDetails) {
Map<String, String> properties = artifactDetail.getMeta().getProperties();
Map<String, String> filteredProperties = new HashMap<>(propertiesRequest.getProperties().size());
for (String propertyKey : propertiesRequest.getProperties()) {
if (properties.containsKey(propertyKey)) {
filteredProperties.put(propertyKey, properties.get(propertyKey));
}
}
String artifactVersion = artifactDetail.getDescriptor().getArtifactId().getVersion().getVersion();
result.add(new ArtifactSummaryProperties(propertiesRequest.getName(), artifactVersion, propertiesRequest.getScope(), filteredProperties));
}
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result, BATCH_ARTIFACT_PROPERTIES_RESPONSE));
}
use of io.cdap.cdap.proto.artifact.ArtifactSortOrder in project cdap by caskdata.
the class ArtifactHttpHandlerInternal method getArtifactDetailForVersions.
@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions")
public void getArtifactDetailForVersions(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("artifact-name") String artifactName, @QueryParam("lower") String lower, @QueryParam("upper") String upper, @QueryParam("limit") @DefaultValue("1") int limit, @QueryParam("order") String order, @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);
}
}
ArtifactRange range = new ArtifactRange(namespaceId.getNamespace(), artifactName, new ArtifactVersionRange(new ArtifactVersion(lower), true, new ArtifactVersion(upper), true));
ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
List<ArtifactDetail> artifactDetailList = artifactRepository.getArtifactDetails(range, limit, sortOrder);
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(artifactDetailList, ARTIFACT_DETAIL_LIST_TYPE));
}
use of io.cdap.cdap.proto.artifact.ArtifactSortOrder in project cdap by caskdata.
the class ArtifactHttpHandler method getArtifactPlugin.
@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/" + "versions/{artifact-version}/extensions/{plugin-type}/plugins/{plugin-name}")
public void getArtifactPlugin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-type") String pluginType, @PathParam("plugin-name") String pluginName, @QueryParam("scope") @DefaultValue("user") final String scope, @QueryParam("artifactName") final String pluginArtifactName, @QueryParam("artifactVersion") String pluginVersion, @QueryParam("artifactScope") final String pluginScope, @QueryParam("limit") @DefaultValue("2147483647") String limit, @QueryParam("order") @DefaultValue("UNORDERED") String order) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException, InvalidArtifactRangeException {
NamespaceId namespace = Ids.namespace(namespaceId);
NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
final NamespaceId pluginArtifactNamespace = validateAndGetScopedNamespace(namespace, pluginScope);
ArtifactId parentArtifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
final ArtifactVersionRange pluginRange = pluginVersion == null ? null : ArtifactVersionRange.parse(pluginVersion);
int limitNumber = Integer.parseInt(limit);
limitNumber = limitNumber <= 0 ? Integer.MAX_VALUE : limitNumber;
ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
Predicate<ArtifactId> predicate = new Predicate<ArtifactId>() {
@Override
public boolean apply(ArtifactId input) {
// by default, the scoped namespace is for USER scope
return (((pluginScope == null && NamespaceId.SYSTEM.equals(input.getParent())) || pluginArtifactNamespace.equals(input.getParent())) && (pluginArtifactName == null || pluginArtifactName.equals(input.getArtifact())) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
}
};
try {
SortedMap<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(namespace, Id.Artifact.fromEntityId(parentArtifactId), pluginType, pluginName, predicate, limitNumber, sortOrder);
List<PluginInfo> pluginInfos = Lists.newArrayList();
// flatten the map
for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
ArtifactDescriptor pluginArtifact = pluginsEntry.getKey();
ArtifactSummary pluginArtifactSummary = ArtifactSummary.from(pluginArtifact.getArtifactId());
PluginClass pluginClass = pluginsEntry.getValue();
try {
capabilityReader.checkAllEnabled(pluginClass.getRequirements().getCapabilities());
} catch (CapabilityNotAvailableException e) {
LOG.debug("Skipping plugin {} because of disabled capability", pluginClass, e);
continue;
}
pluginInfos.add(new PluginInfo(pluginClass, pluginArtifactSummary));
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(pluginInfos));
} catch (PluginNotExistsException e) {
responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
} catch (IOException e) {
LOG.error("Exception looking up plugins for artifact {}", parentArtifactId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
}
}
use of io.cdap.cdap.proto.artifact.ArtifactSortOrder in project cdap by caskdata.
the class ArtifactHttpHandler method getArtifactVersions.
@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}")
public void getArtifactVersions(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @QueryParam("scope") @DefaultValue("user") String scope, @QueryParam("artifactVersion") String versionRange, @QueryParam("limit") @DefaultValue("2147483647") String limit, @QueryParam("order") @DefaultValue("UNORDERED") String order) throws Exception {
NamespaceId namespace = validateAndGetScopedNamespace(Ids.namespace(namespaceId), scope);
ArtifactRange range = versionRange == null ? null : new ArtifactRange(namespaceId, artifactName, ArtifactVersionRange.parse(versionRange));
int limitNumber = Integer.parseInt(limit);
limitNumber = limitNumber <= 0 ? Integer.MAX_VALUE : limitNumber;
ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
try {
if (range == null) {
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(artifactRepository.getArtifactSummaries(namespace, artifactName, limitNumber, sortOrder)));
} else {
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(artifactRepository.getArtifactSummaries(range, limitNumber, sortOrder)));
}
} catch (IOException e) {
LOG.error("Exception reading artifacts named {} for namespace {} from the store.", artifactName, namespaceId, e);
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading artifact metadata from the store.");
}
}
use of io.cdap.cdap.proto.artifact.ArtifactSortOrder in project cdap by caskdata.
the class DefaultApplicationUpdateContext method getScopedPluginArtifacts.
private List<ArtifactId> getScopedPluginArtifacts(String pluginType, String pluginName, ArtifactScope pluginScope, @Nullable ArtifactVersionRange pluginRange, int limit) throws Exception {
List<ArtifactId> pluginArtifacts = new ArrayList<>();
NamespaceId pluginArtifactNamespace = ArtifactScope.SYSTEM.equals(pluginScope) ? NamespaceId.SYSTEM : namespaceId;
Predicate<io.cdap.cdap.proto.id.ArtifactId> predicate = input -> {
// Check if it is from the scoped namespace and should check if plugin is in given range if provided.
return (pluginArtifactNamespace.equals(input.getParent()) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
};
try {
// TODO: Pass ArtifactSortOrder as argument for better flexibility.
Map<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(pluginArtifactNamespace, Artifact.from(Namespace.fromEntityId(namespaceId), applicationArtifactId), pluginType, pluginName, predicate, limit, ArtifactSortOrder.ASC);
for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
ArtifactId plugin = pluginsEntry.getKey().getArtifactId();
// Consider if it is a non-snapshot version artifact or it is a snapshot version than allowSnapshot is true.
if ((plugin.getVersion().isSnapshot() && allowSnapshot) || !plugin.getVersion().isSnapshot()) {
pluginArtifacts.add(plugin);
}
}
} catch (PluginNotExistsException e) {
LOG.trace("No plugin found for plugin {} of type {} in scope {} for app {}", pluginName, pluginType, pluginScope, applicationId, e);
return Collections.emptyList();
} catch (Exception e) {
throw e;
}
return pluginArtifacts;
}
Aggregations