Search in sources :

Example 1 with ArtifactSummaryProperties

use of co.cask.cdap.proto.artifact.ArtifactSummaryProperties in project cdap by caskdata.

the class ArtifactHttpHandlerTest method testBatchProperties.

@Test
public void testBatchProperties() throws Exception {
    ArtifactId wordcountId1 = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.code(), addAppArtifact(Id.Artifact.fromEntityId(wordcountId1), WordCountApp.class).getStatusLine().getStatusCode());
    addArtifactProperties(Id.Artifact.fromEntityId(wordcountId1), ImmutableMap.of("k1", "v1", "k2", "v2"));
    ArtifactId wordcountId2 = NamespaceId.DEFAULT.artifact("wc", "2.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.code(), addAppArtifact(Id.Artifact.fromEntityId(wordcountId2), WordCountApp.class).getStatusLine().getStatusCode());
    addArtifactProperties(Id.Artifact.fromEntityId(wordcountId2), ImmutableMap.of("k2", "v20", "k3", "v30"));
    List<String> props = ImmutableList.of("k1", "k2", "k3");
    List<ArtifactPropertiesRequest> requestList = ImmutableList.of(new ArtifactPropertiesRequest(wordcountId1.getArtifact(), wordcountId1.getVersion(), ArtifactScope.USER, props), new ArtifactPropertiesRequest(wordcountId2.getArtifact(), wordcountId2.getVersion(), ArtifactScope.USER, props));
    URL url = getEndPoint(String.format("%s/namespaces/%s/artifactproperties", Constants.Gateway.API_VERSION_3, NamespaceId.DEFAULT.getNamespace())).toURL();
    HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(requestList)).build();
    co.cask.common.http.HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(HttpResponseStatus.OK.code(), response.getResponseCode());
    List<ArtifactSummaryProperties> actual = GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<List<ArtifactSummaryProperties>>() {
    }.getType());
    List<ArtifactSummaryProperties> expected = ImmutableList.of(new ArtifactSummaryProperties(wordcountId1.getArtifact(), wordcountId1.getVersion(), ArtifactScope.USER, ImmutableMap.of("k1", "v1", "k2", "v2")), new ArtifactSummaryProperties(wordcountId2.getArtifact(), wordcountId2.getVersion(), ArtifactScope.USER, ImmutableMap.of("k2", "v20", "k3", "v30")));
    Assert.assertEquals(expected, actual);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) ArtifactId(co.cask.cdap.proto.id.ArtifactId) URL(java.net.URL) ArtifactPropertiesRequest(co.cask.cdap.proto.artifact.ArtifactPropertiesRequest) TypeToken(com.google.gson.reflect.TypeToken) WordCountApp(co.cask.cdap.WordCountApp) ArtifactSummaryProperties(co.cask.cdap.proto.artifact.ArtifactSummaryProperties) Test(org.junit.Test)

Example 2 with ArtifactSummaryProperties

use of co.cask.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) throws Exception {
    NamespaceId namespace = validateAndGetNamespace(namespaceId);
    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;
        ArtifactId artifactId = validateAndGetArtifactId(requestNamespace, propertiesRequest.getName(), propertiesRequest.getVersion());
        ArtifactDetail artifactDetail;
        try {
            artifactDetail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId));
        } catch (ArtifactNotFoundException e) {
            continue;
        }
        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));
            }
        }
        result.add(new ArtifactSummaryProperties(propertiesRequest.getName(), propertiesRequest.getVersion(), propertiesRequest.getScope(), filteredProperties));
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result, BATCH_ARTIFACT_PROPERTIES_RESPONSE));
}
Also used : InputStreamReader(java.io.InputStreamReader) ArtifactId(co.cask.cdap.proto.id.ArtifactId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) JsonSyntaxException(com.google.gson.JsonSyntaxException) ArtifactPropertiesRequest(co.cask.cdap.proto.artifact.ArtifactPropertiesRequest) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ArtifactSummaryProperties(co.cask.cdap.proto.artifact.ArtifactSummaryProperties) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) ArtifactDetail(co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

ArtifactPropertiesRequest (co.cask.cdap.proto.artifact.ArtifactPropertiesRequest)2 ArtifactSummaryProperties (co.cask.cdap.proto.artifact.ArtifactSummaryProperties)2 ArtifactId (co.cask.cdap.proto.id.ArtifactId)2 WordCountApp (co.cask.cdap.WordCountApp)1 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 ArtifactDetail (co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 HttpRequest (co.cask.common.http.HttpRequest)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 TypeToken (com.google.gson.reflect.TypeToken)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Test (org.junit.Test)1