Search in sources :

Example 1 with PluginEndpoint

use of co.cask.cdap.internal.app.runtime.plugin.PluginEndpoint 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 2 with PluginEndpoint

use of co.cask.cdap.internal.app.runtime.plugin.PluginEndpoint 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(FullHttpRequest 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.content().toString(StandardCharsets.UTF_8);
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    ArtifactId 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, Id.Artifact.fromEntityId(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 : ArtifactId(co.cask.cdap.proto.id.ArtifactId) 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) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST) Beta(co.cask.cdap.api.annotation.Beta)

Aggregations

Beta (co.cask.cdap.api.annotation.Beta)2 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)2 ArtifactRangeNotFoundException (co.cask.cdap.common.ArtifactRangeNotFoundException)2 BadRequestException (co.cask.cdap.common.BadRequestException)2 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)2 NotFoundException (co.cask.cdap.common.NotFoundException)2 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)2 PluginEndpoint (co.cask.cdap.internal.app.runtime.plugin.PluginEndpoint)2 ArtifactId (co.cask.cdap.proto.id.ArtifactId)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Id (co.cask.cdap.proto.Id)1