Search in sources :

Example 11 with Id

use of co.cask.cdap.proto.Id in project cdap by caskdata.

the class ArtifactHttpHandler method callArtifactPluginMethod.

@Beta
@POST
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/" + "versions/{artifact-version}/plugintypes/{plugin-type}/plugins/{plugin-name}/methods/{plugin-method}")
@AuditPolicy({ AuditDetail.REQUEST_BODY, AuditDetail.RESPONSE_BODY })
public void callArtifactPluginMethod(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-name") String pluginName, @PathParam("plugin-type") String pluginType, @PathParam("plugin-method") String methodName, @QueryParam("scope") @DefaultValue("user") String scope) throws Exception {
    String requestBody = request.getContent().toString(Charsets.UTF_8);
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    Id.Artifact artifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    if (requestBody.isEmpty()) {
        throw new BadRequestException("Request body is used as plugin method parameter, " + "Received empty request body.");
    }
    try {
        PluginEndpoint pluginEndpoint = pluginService.getPluginEndpoint(namespace, artifactId, pluginType, pluginName, methodName);
        Object response = pluginEndpoint.invoke(GSON.fromJson(requestBody, pluginEndpoint.getMethodParameterType()));
        responder.sendString(HttpResponseStatus.OK, GSON.toJson(response));
    } catch (JsonSyntaxException e) {
        LOG.error("Exception while invoking plugin method.", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "Unable to deserialize request body to method parameter type");
    } catch (InvocationTargetException e) {
        LOG.error("Exception while invoking plugin method.", e);
        if (e.getCause() instanceof javax.ws.rs.NotFoundException) {
            throw new NotFoundException(e.getCause());
        } else if (e.getCause() instanceof javax.ws.rs.BadRequestException) {
            throw new BadRequestException(e.getCause());
        } else if (e.getCause() instanceof IllegalArgumentException && e.getCause() != null) {
            responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getCause().getMessage());
        } else {
            Throwable rootCause = Throwables.getRootCause(e);
            String message = String.format("Error while invoking plugin method %s.", methodName);
            if (rootCause != null && rootCause.getMessage() != null) {
                message = String.format("%s %s", message, rootCause.getMessage());
            }
            responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, message);
        }
    }
}
Also used : NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) ArtifactRangeNotFoundException(co.cask.cdap.common.ArtifactRangeNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(co.cask.cdap.common.BadRequestException) PluginEndpoint(co.cask.cdap.internal.app.runtime.plugin.PluginEndpoint) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST) Beta(co.cask.cdap.api.annotation.Beta)

Example 12 with Id

use of co.cask.cdap.proto.Id in project cdap by caskdata.

the class ArtifactHttpHandler method getArtifactPluginTypes.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/extensions")
public void getArtifactPluginTypes(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @QueryParam("scope") @DefaultValue("user") String scope) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException {
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    Id.Artifact artifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    try {
        SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = artifactRepository.getPlugins(namespace, artifactId);
        Set<String> pluginTypes = Sets.newHashSet();
        for (Set<PluginClass> pluginClasses : plugins.values()) {
            for (PluginClass pluginClass : pluginClasses) {
                pluginTypes.add(pluginClass.getType());
            }
        }
        responder.sendJson(HttpResponseStatus.OK, pluginTypes);
    } catch (IOException e) {
        LOG.error("Exception looking up plugins for artifact {}", artifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactDescriptor(co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) IOException(java.io.IOException) PluginClass(co.cask.cdap.api.plugin.PluginClass) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 13 with Id

use of co.cask.cdap.proto.Id in project cdap by caskdata.

the class ArtifactHttpHandler method getArtifactPlugins.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/extensions/{plugin-type}")
public void getArtifactPlugins(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-type") String pluginType, @QueryParam("scope") @DefaultValue("user") String scope) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException {
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    Id.Artifact artifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    try {
        SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = artifactRepository.getPlugins(namespace, artifactId, pluginType);
        List<PluginSummary> pluginSummaries = Lists.newArrayList();
        // flatten the map
        for (Map.Entry<ArtifactDescriptor, Set<PluginClass>> pluginsEntry : plugins.entrySet()) {
            ArtifactDescriptor pluginArtifact = pluginsEntry.getKey();
            ArtifactSummary pluginArtifactSummary = ArtifactSummary.from(pluginArtifact.getArtifactId());
            for (PluginClass pluginClass : pluginsEntry.getValue()) {
                pluginSummaries.add(new PluginSummary(pluginClass.getName(), pluginClass.getType(), pluginClass.getDescription(), pluginClass.getClassName(), pluginArtifactSummary));
            }
        }
        responder.sendJson(HttpResponseStatus.OK, pluginSummaries);
    } catch (IOException e) {
        LOG.error("Exception looking up plugins for artifact {}", artifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) IOException(java.io.IOException) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactDescriptor(co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginSummary(co.cask.cdap.proto.artifact.PluginSummary) PluginClass(co.cask.cdap.api.plugin.PluginClass) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 14 with Id

use of co.cask.cdap.proto.Id in project cdap by caskdata.

the class ArtifactHttpHandler method getArtifactInfo.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}")
public void getArtifactInfo(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @QueryParam("scope") @DefaultValue("user") String scope) throws Exception {
    NamespaceId namespace = validateAndGetScopedNamespace(Ids.namespace(namespaceId), scope);
    Id.Artifact artifactId = validateAndGetArtifactId(namespace, artifactName, artifactVersion);
    try {
        ArtifactDetail detail = artifactRepository.getArtifact(artifactId);
        ArtifactDescriptor descriptor = detail.getDescriptor();
        // info hides some fields that are available in detail, such as the location of the artifact
        ArtifactInfo info = new ArtifactInfo(descriptor.getArtifactId(), detail.getMeta().getClasses(), detail.getMeta().getProperties(), detail.getMeta().getUsableBy());
        responder.sendJson(HttpResponseStatus.OK, info, ArtifactInfo.class, GSON);
    } 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.");
    }
}
Also used : ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ArtifactDescriptor(co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) IOException(java.io.IOException) ArtifactDetail(co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 15 with Id

use of co.cask.cdap.proto.Id in project cdap by caskdata.

the class ArtifactHttpHandler method writeProperty.

@PUT
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/properties/{property}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void writeProperty(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("property") String key) throws Exception {
    NamespaceId namespace = NamespaceId.SYSTEM.getNamespace().equalsIgnoreCase(namespaceId) ? NamespaceId.SYSTEM : validateAndGetNamespace(namespaceId);
    Id.Artifact artifactId = validateAndGetArtifactId(namespace, artifactName, artifactVersion);
    String value = request.getContent().toString(Charsets.UTF_8);
    if (value == null) {
        responder.sendStatus(HttpResponseStatus.OK);
        return;
    }
    try {
        artifactRepository.writeArtifactProperty(artifactId, key, value);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IOException e) {
        LOG.error("Exception writing properties for artifact {}.", artifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error writing property to artifact.");
    }
}
Also used : NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) IOException(java.io.IOException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Aggregations

Id (co.cask.cdap.proto.Id)24 NamespaceId (co.cask.cdap.proto.id.NamespaceId)19 IOException (java.io.IOException)15 Path (javax.ws.rs.Path)14 ArtifactId (co.cask.cdap.proto.id.ArtifactId)13 GET (javax.ws.rs.GET)6 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)5 ProgramId (co.cask.cdap.proto.id.ProgramId)5 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)4 PluginClass (co.cask.cdap.api.plugin.PluginClass)4 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 BadRequestException (co.cask.cdap.common.BadRequestException)4 ArtifactDescriptor (co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor)4 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)3 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)3 ArtifactAlreadyExistsException (co.cask.cdap.common.ArtifactAlreadyExistsException)3 ArtifactRangeNotFoundException (co.cask.cdap.common.ArtifactRangeNotFoundException)3 NotFoundException (co.cask.cdap.common.NotFoundException)3 ArtifactDetail (co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail)3 PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)3