Search in sources :

Example 1 with File

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

the class RestIntegrationTest method testFileResources.

@Test
public void testFileResources() throws Exception {
    Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
    String response = null;
    String url = rootUrl + "files";
    // POST
    File file = new File();
    file.setName("milkyway-jar");
    file.setVersion(System.currentTimeMillis());
    MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
    String initialJarContent = "milkyway-jar-contents";
    final InputStream fileStream = new ByteArrayInputStream(initialJarContent.getBytes());
    multiPart.bodyPart(new StreamDataBodyPart("file", fileStream, "file"));
    multiPart.bodyPart(new FormDataBodyPart("fileInfo", file, MediaType.APPLICATION_JSON_TYPE));
    File postedFile = client.target(url).request(MediaType.MULTIPART_FORM_DATA_TYPE, MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE).post(Entity.entity(multiPart, multiPart.getMediaType()), File.class);
    // DOWNLOAD
    InputStream downloadInputStream = client.target(url + "/download/" + postedFile.getId()).request().get(InputStream.class);
    ByteArrayOutputStream downloadedJarOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(downloadInputStream, downloadedJarOutputStream);
    ByteArrayOutputStream uploadedOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(new ByteArrayInputStream(initialJarContent.getBytes()), uploadedOutputStream);
    Assert.assertArrayEquals(uploadedOutputStream.toByteArray(), downloadedJarOutputStream.toByteArray());
    // GET all
    response = client.target(url).request(MediaType.APPLICATION_JSON_TYPE).get(String.class);
    List<File> files = getEntities(response, File.class);
    Assert.assertEquals(files.size(), 1);
    Assert.assertEquals(files.iterator().next().getName(), file.getName());
    // GET /files/1
    File receivedFile = client.target(url + "/" + postedFile.getId()).request(MediaType.APPLICATION_JSON_TYPE).get(File.class);
    Assert.assertEquals(receivedFile.getName(), postedFile.getName());
    Assert.assertEquals(receivedFile.getId(), postedFile.getId());
    // PUT
    postedFile.setName("andromeda-jar");
    postedFile.setVersion(System.currentTimeMillis());
    multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
    InputStream updatedFileStream = new ByteArrayInputStream("andromeda-jar-contents".getBytes());
    multiPart.bodyPart(new StreamDataBodyPart("file", updatedFileStream, "file"));
    multiPart.bodyPart(new FormDataBodyPart("fileInfo", postedFile, MediaType.APPLICATION_JSON_TYPE));
    File updatedFile = client.target(url).request(MediaType.MULTIPART_FORM_DATA_TYPE, MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(multiPart, multiPart.getMediaType()), File.class);
    Assert.assertEquals(updatedFile.getId(), postedFile.getId());
    Assert.assertEquals(updatedFile.getName(), postedFile.getName());
    // DELETE
    final File deletedFile = client.target(url + "/" + updatedFile.getId()).request().delete(File.class);
    Assert.assertEquals(deletedFile.getId(), updatedFile.getId());
    // GET
    response = client.target(url).request(MediaType.APPLICATION_JSON_TYPE).get(String.class);
    files = getEntities(response, File.class);
    Assert.assertTrue(files.isEmpty());
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MultiPartFeature(org.glassfish.jersey.media.multipart.MultiPartFeature) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) Client(javax.ws.rs.client.Client) File(com.hortonworks.streamline.streams.catalog.File) IntegrationTest(com.hortonworks.streamline.common.test.IntegrationTest) Test(org.junit.Test)

Example 2 with File

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

the class FileCatalogResource method addFile.

/**
 * Adds given resource to the configured file-storage and adds an entry in entity storage.
 *
 * Below example describes how a file can be added along with metadata
 * <blockquote><pre>
 * curl -X POST -i -F file=@user-lib.jar -F "fileInfo={\"name\":\"jar-1\",\"version\":1};type=application/json"  http://localhost:8080/api/v1/catalog/files
 *
 * HTTP/1.1 100 Continue
 *
 * HTTP/1.1 201 Created
 * Date: Fri, 15 Apr 2016 10:36:33 GMT
 * Content-Type: application/json
 * Content-Length: 239
 *
 * {"responseCode":1000,"responseMessage":"Success","entity":{"id":1234,"name":"jar-1","className":null,"storedFileName":"/tmp/test-hdfs/jar-1-ea41fe3a-12f9-45d4-ae24-818d570b8963.jar","version":1,"timestamp":1460716593157,"auxiliaryInfo":null}}
 * </pre></blockquote>
 *
 * @param inputStream actual file content as {@link InputStream}.
 * @param contentDispositionHeader {@link FormDataContentDisposition} instance of the received file
 * @param file configuration of the file resource {@link File}
 * @return
 */
@Timed
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/files")
public Response addFile(@FormDataParam("file") final InputStream inputStream, @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader, @FormDataParam("fileInfo") final File file, @Context SecurityContext securityContext) throws IOException {
    SecurityUtil.checkRole(authorizer, securityContext, Roles.ROLE_ADMIN);
    log.info("Received fileInfo: [{}]", file);
    File updatedFile = addFile(inputStream, file);
    SecurityUtil.addAcl(authorizer, securityContext, File.NAMESPACE, updatedFile.getId(), EnumSet.allOf(Permission.class));
    return WSUtils.respondEntity(updatedFile, CREATED);
}
Also used : Permission(com.hortonworks.streamline.streams.security.Permission) File(com.hortonworks.streamline.streams.catalog.File) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed)

Example 3 with File

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

the class CatalogService method removeFile.

public File removeFile(Long fileId) {
    File file = new File();
    file.setId(fileId);
    return dao.remove(new StorableKey(FILE_NAMESPACE, file.getPrimaryKey()));
}
Also used : StorableKey(com.hortonworks.registries.storage.StorableKey) File(com.hortonworks.streamline.streams.catalog.File)

Example 4 with File

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

the class CatalogService method getFile.

public File getFile(Long jarId) {
    File file = new File();
    file.setId(jarId);
    return dao.get(new StorableKey(FILE_NAMESPACE, file.getPrimaryKey()));
}
Also used : StorableKey(com.hortonworks.registries.storage.StorableKey) File(com.hortonworks.streamline.streams.catalog.File)

Example 5 with File

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

the class FileCatalogResource method removeFile.

/**
 * Deletes the file of given {@code fileId}
 *
 * @param fileId
 */
@DELETE
@Path("/files/{id}")
@Timed
public Response removeFile(@PathParam("id") Long fileId, @Context SecurityContext securityContext) throws IOException {
    SecurityUtil.checkPermissions(authorizer, securityContext, File.NAMESPACE, fileId, DELETE);
    try {
        File removedFile = catalogService.removeFile(fileId);
        SecurityUtil.removeAcl(authorizer, securityContext, File.NAMESPACE, fileId);
        log.info("Removed File entry is [{}]", removedFile);
        if (removedFile != null) {
            boolean removed = catalogService.deleteFileFromStorage(removedFile.getStoredFileName());
            logDeletionMessage(removedFile.getStoredFileName(), removed);
            return WSUtils.respondEntity(removedFile, OK);
        }
    } catch (IOException ex) {
        log.error("Encountered error in removing file with id [{}]", fileId, ex);
        throw ex;
    }
    log.info("File entry with id [{}] is not found", fileId);
    throw EntityNotFoundException.byId(fileId.toString());
}
Also used : IOException(java.io.IOException) File(com.hortonworks.streamline.streams.catalog.File) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) DELETE(com.hortonworks.streamline.streams.security.Permission.DELETE) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

File (com.hortonworks.streamline.streams.catalog.File)8 Timed (com.codahale.metrics.annotation.Timed)5 Path (javax.ws.rs.Path)5 StorableKey (com.hortonworks.registries.storage.StorableKey)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 IntegrationTest (com.hortonworks.streamline.common.test.IntegrationTest)1 Permission (com.hortonworks.streamline.streams.security.Permission)1 DELETE (com.hortonworks.streamline.streams.security.Permission.DELETE)1 IOException (java.io.IOException)1 DELETE (javax.ws.rs.DELETE)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 Produces (javax.ws.rs.Produces)1 Client (javax.ws.rs.client.Client)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)1 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)1 MultiPartFeature (org.glassfish.jersey.media.multipart.MultiPartFeature)1 StreamDataBodyPart (org.glassfish.jersey.media.multipart.file.StreamDataBodyPart)1