Search in sources :

Example 1 with ArtifactSummaryProperties

use of io.cdap.cdap.proto.artifact.ArtifactSummaryProperties 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));
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) CapabilityReader(io.cdap.cdap.internal.capability.CapabilityReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) JsonSyntaxException(com.google.gson.JsonSyntaxException) ArtifactPropertiesRequest(io.cdap.cdap.proto.artifact.ArtifactPropertiesRequest) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ArtifactSummaryProperties(io.cdap.cdap.proto.artifact.ArtifactSummaryProperties) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)1 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 ArtifactDetail (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)1 CapabilityReader (io.cdap.cdap.internal.capability.CapabilityReader)1 ArtifactPropertiesRequest (io.cdap.cdap.proto.artifact.ArtifactPropertiesRequest)1 ArtifactSortOrder (io.cdap.cdap.proto.artifact.ArtifactSortOrder)1 ArtifactSummaryProperties (io.cdap.cdap.proto.artifact.ArtifactSummaryProperties)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1