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());
}
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);
}
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()));
}
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()));
}
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());
}
Aggregations