Search in sources :

Example 1 with UDF

use of com.hortonworks.streamline.streams.catalog.UDF in project streamline by hortonworks.

the class UDFCatalogResource method addOrUpdateUDF.

/**
 * Update a udf.
 * <p>
 *     curl -X PUT 'http://localhost:8080/api/v1/catalog/udfs/34'
 *     -F udfJarFile=@/tmp/streams-functions-0.6.0-SNAPSHOT.jar
 *     -F udfConfig='{"name":"stddev", "description": "stddev",
 *                   "type":"AGGREGATE", "className":"com.hortonworks.streamline.streams.rule.udaf.Stddev"};type=application/json'
 * </p>
 * <pre>
 * {
 *   "responseCode": 1000,
 *   "responseMessage": "Success",
 *   "entity": {
 *     "id": 48,
 *     "name": "SUBSTRING",
 *     "description": "Substring",
 *     "type": "FUNCTION",
 *     "className": "builtin"
 *   }
 * }
 * </pre>
 */
@PUT
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/udfs/{id}")
@Timed
public Response addOrUpdateUDF(@PathParam("id") Long udfId, @FormDataParam("udfJarFile") final InputStream inputStream, @FormDataParam("udfJarFile") final FormDataContentDisposition contentDispositionHeader, @FormDataParam("udfConfig") final FormDataBodyPart udfConfig, @FormDataParam("builtin") final boolean builtin, @Context SecurityContext securityContext) throws Exception {
    SecurityUtil.checkPermissions(authorizer, securityContext, UDF.NAMESPACE, udfId, WRITE);
    MediaType mediaType = udfConfig.getMediaType();
    LOG.debug("Media type {}", mediaType);
    if (!mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
        throw new UnsupportedMediaTypeException(mediaType.toString());
    }
    UDF udf = udfConfig.getValueAs(UDF.class);
    processUdf(inputStream, udf, false, builtin);
    UDF newUdf = catalogService.addOrUpdateUDF(udfId, udf);
    return WSUtils.respondEntity(newUdf, CREATED);
}
Also used : UnsupportedMediaTypeException(com.hortonworks.streamline.common.exception.service.exception.request.UnsupportedMediaTypeException) UDF(com.hortonworks.streamline.streams.catalog.UDF) MediaType(javax.ws.rs.core.MediaType) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed) PUT(javax.ws.rs.PUT)

Example 2 with UDF

use of com.hortonworks.streamline.streams.catalog.UDF in project streamline by hortonworks.

the class UDFCatalogResource method removeUDF.

/**
 * Remove a UDF by ID.
 *
 * <p>
 * DELETE api/v1/catalog/udfs/:ID
 * </p>
 * <pre>
 * {
 *   "responseCode": 1000,
 *   "responseMessage": "Success",
 *   "entity": {
 *     "id": 48,
 *     "name": "SUBSTRING",
 *     "description": "Substring",
 *     "type": "FUNCTION",
 *     "className": "builtin"
 *   }
 * }
 * </pre>
 */
@DELETE
@Path("/udfs/{id}")
@Timed
public Response removeUDF(@PathParam("id") Long id, @Context SecurityContext securityContext) {
    SecurityUtil.checkPermissions(authorizer, securityContext, UDF.NAMESPACE, id, DELETE);
    UDF removedUDF = catalogService.removeUDF(id);
    if (removedUDF != null) {
        SecurityUtil.removeAcl(authorizer, securityContext, UDF.NAMESPACE, id);
        return WSUtils.respondEntity(removedUDF, OK);
    }
    throw EntityNotFoundException.byId(id.toString());
}
Also used : UDF(com.hortonworks.streamline.streams.catalog.UDF) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) DELETE(com.hortonworks.streamline.streams.security.Permission.DELETE) Timed(com.codahale.metrics.annotation.Timed)

Example 3 with UDF

use of com.hortonworks.streamline.streams.catalog.UDF in project streamline by hortonworks.

the class UDFCatalogResource method downloadUdf.

/**
 * Download the jar corresponding to a specific UDF.
 * <p>
 * E.g. curl http://localhost:8080/api/v1/catalog/udfs/download/34 -o /tmp/file.jar
 * </p>
 */
@Timed
@GET
@Produces({ "application/java-archive", "application/json" })
@Path("/udfs/download/{udfId}")
public Response downloadUdf(@PathParam("udfId") Long udfId, @Context SecurityContext securityContext) throws IOException {
    SecurityUtil.checkPermissions(authorizer, securityContext, UDF.NAMESPACE, udfId, READ, EXECUTE);
    UDF udf = catalogService.getUDF(udfId);
    if (udf != null) {
        StreamingOutput streamOutput = WSUtils.wrapWithStreamingOutput(catalogService.downloadFileFromStorage(udf.getJarStoragePath()));
        return Response.ok(streamOutput).build();
    }
    throw EntityNotFoundException.byId(udfId.toString());
}
Also used : UDF(com.hortonworks.streamline.streams.catalog.UDF) StreamingOutput(javax.ws.rs.core.StreamingOutput) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 4 with UDF

use of com.hortonworks.streamline.streams.catalog.UDF in project streamline by hortonworks.

the class UDFCatalogResource method getUDFById.

/**
 * Get a specific UDF by id.
 *
 * <p>
 * GET api/v1/catalog/udfs/:ID
 * </p>
 * <pre>
 * {
 *   "responseCode": 1000,
 *   "responseMessage": "Success",
 *   "entity": {
 *     "id": 48,
 *     "name": "SUBSTRING",
 *     "description": "Substring",
 *     "type": "FUNCTION",
 *     "className": "builtin"
 *   }
 * }
 * </pre>
 */
@GET
@Path("/udfs/{id}")
@Timed
public Response getUDFById(@PathParam("id") Long id, @Context SecurityContext securityContext) {
    boolean udfUser = SecurityUtil.hasRole(authorizer, securityContext, Roles.ROLE_UDF_USER);
    if (udfUser) {
        LOG.debug("Allowing get UDF, since user has role: {}", Roles.ROLE_UDF_USER);
    } else {
        SecurityUtil.checkPermissions(authorizer, securityContext, UDF.NAMESPACE, id, READ);
    }
    UDF result = catalogService.getUDF(id);
    if (result != null) {
        return WSUtils.respondEntity(result, OK);
    }
    throw EntityNotFoundException.byId(id.toString());
}
Also used : UDF(com.hortonworks.streamline.streams.catalog.UDF) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 5 with UDF

use of com.hortonworks.streamline.streams.catalog.UDF in project streamline by hortonworks.

the class UDFCatalogResource method addUDF.

/**
 * Add a new UDF.
 * <p>
 * curl -X POST 'http://localhost:8080/api/v1/catalog/udfs' -F udfJarFile=/tmp/foo-function.jar
 * -F udfConfig='{"name":"Foo", "description": "testing", "type":"FUNCTION", "className":"com.test.Foo"};type=application/json'
 * </p>
 */
@Timed
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/udfs")
public Response addUDF(@FormDataParam("udfJarFile") final InputStream inputStream, @FormDataParam("udfJarFile") final FormDataContentDisposition contentDispositionHeader, @FormDataParam("udfConfig") final FormDataBodyPart udfConfig, @FormDataParam("builtin") final boolean builtin, @Context SecurityContext securityContext) throws Exception {
    SecurityUtil.checkRole(authorizer, securityContext, Roles.ROLE_UDF_ADMIN);
    MediaType mediaType = udfConfig.getMediaType();
    LOG.debug("Media type {}", mediaType);
    if (!mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
        throw new UnsupportedMediaTypeException(mediaType.toString());
    }
    UDF udf = udfConfig.getValueAs(UDF.class);
    processUdf(inputStream, udf, true, builtin);
    UDF createdUdf = catalogService.addUDF(udf);
    SecurityUtil.addAcl(authorizer, securityContext, UDF.NAMESPACE, createdUdf.getId(), EnumSet.allOf(Permission.class));
    return WSUtils.respondEntity(createdUdf, CREATED);
}
Also used : UnsupportedMediaTypeException(com.hortonworks.streamline.common.exception.service.exception.request.UnsupportedMediaTypeException) UDF(com.hortonworks.streamline.streams.catalog.UDF) Permission(com.hortonworks.streamline.streams.security.Permission) MediaType(javax.ws.rs.core.MediaType) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

UDF (com.hortonworks.streamline.streams.catalog.UDF)15 Test (org.junit.Test)6 Timed (com.codahale.metrics.annotation.Timed)5 Path (javax.ws.rs.Path)5 TopologyRule (com.hortonworks.streamline.streams.catalog.TopologyRule)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Expectations (mockit.Expectations)3 StorableKey (com.hortonworks.registries.storage.StorableKey)2 UnsupportedMediaTypeException (com.hortonworks.streamline.common.exception.service.exception.request.UnsupportedMediaTypeException)2 TopologyStream (com.hortonworks.streamline.streams.catalog.TopologyStream)2 Stream (com.hortonworks.streamline.streams.layout.component.Stream)2 Random (java.util.Random)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 MediaType (javax.ws.rs.core.MediaType)2 QueryParam (com.hortonworks.registries.common.QueryParam)1 Rule (com.hortonworks.streamline.streams.layout.component.rule.Rule)1 Udf (com.hortonworks.streamline.streams.layout.component.rule.expression.Udf)1