WorkingFileRepositoryRestEndpoint extends WorkingFileRepositoryImpl { @GET @Path(WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX + "{mediaPackageID}/{mediaPackageElementID}") @RestQuery(name = "get", description = "Gets the file from the working repository under /mediaPackageID/mediaPackageElementID", returnDescription = "The file", pathParameters = { @RestParameter(name = "mediaPackageID", description = "the mediapackage identifier", isRequired = true, type = STRING), @RestParameter(name = "mediaPackageElementID", description = "the mediapackage element identifier", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "File returned"), @RestResponse(responseCode = SC_NOT_MODIFIED, description = "If file not modified"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Not found") }) public Response restGet(@PathParam("mediaPackageID") final String mediaPackageID, @PathParam("mediaPackageElementID") final String mediaPackageElementID, @HeaderParam("If-None-Match") String ifNoneMatch) throws NotFoundException { String md5 = null; try { md5 = getMediaPackageElementDigest(mediaPackageID, mediaPackageElementID); if (StringUtils.isNotBlank(ifNoneMatch) && md5.equals(ifNoneMatch)) { return Response.notModified(md5).build(); } } catch (IOException e) { logger.warn("Error reading digest of {}/{}", mediaPackageElementID, mediaPackageElementID); } try { String contentType; File file = getFile(mediaPackageID, mediaPackageElementID); try { contentType = MimeTypes.fromString(file.getPath()).toString(); } catch (UnknownFileTypeException e) { contentType = "application/octet-stream"; } try { return ok(get(mediaPackageID, mediaPackageElementID), contentType, some(file.length()), none("")); } catch (IOException e) { throw new NotFoundException(); } } catch (IllegalStateException e) { logger.error("Unable to provide element '{}' from mediapackage '{}': {}", mediaPackageElementID, mediaPackageID, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } }  @Override @Activate void activate(ComponentContext cc); @Override @Deactivate void deactivate(); @Override @Reference(name = "remoteServiceManager") void setRemoteServiceManager(ServiceRegistry remoteServiceManager); @Override @Reference(name = "securityService") void setSecurityService(SecurityService securityService); @POST @Produces(MediaType.TEXT_HTML) @Path(WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX + "{mediaPackageID}/{mediaPackageElementID}") @RestQuery(name = "put", description = "Store a file in working repository under ./mediaPackageID/mediaPackageElementID", returnDescription = "The URL to access the stored file", pathParameters = { @RestParameter(name = "mediaPackageID", description = "the mediapackage identifier", isRequired = true, type = STRING), @RestParameter(name = "mediaPackageElementID", description = "the mediapackage element identifier", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "OK, file stored") }, restParameters = { @RestParameter(name = "file", description = "the filename", isRequired = true, type = FILE) }) Response restPut(@PathParam("mediaPackageID") String mediaPackageID,
          @PathParam("mediaPackageElementID") String mediaPackageElementID, @Context HttpServletRequest request); @POST @Produces(MediaType.TEXT_HTML) @Path(WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX + "{mediaPackageID}/{mediaPackageElementID}/{filename}") @RestQuery(name = "putWithFilename", description = "Store a file in working repository under ./mediaPackageID/mediaPackageElementID/filename", returnDescription = "The URL to access the stored file", pathParameters = { @RestParameter(name = "mediaPackageID", description = "the mediapackage identifier", isRequired = true, type = STRING), @RestParameter(name = "mediaPackageElementID", description = "the mediapackage element identifier", isRequired = true, type = STRING), @RestParameter(name = "filename", description = "the filename", isRequired = true, type = FILE) }, responses = { @RestResponse(responseCode = SC_OK, description = "OK, file stored") }) Response restPutURLEncoded(@Context HttpServletRequest request,
          @PathParam("mediaPackageID") String mediaPackageID,
          @PathParam("mediaPackageElementID") String mediaPackageElementID, @PathParam("filename") String filename,
          @FormParam("content") String content); @POST @Produces(MediaType.TEXT_HTML) @Path(WorkingFileRepository.COLLECTION_PATH_PREFIX + "{collectionId}") @RestQuery(name = "putInCollection", description = "Store a file in working repository under ./collectionId/filename", returnDescription = "The URL to access the stored file", pathParameters = { @RestParameter(name = "collectionId", description = "the colection identifier", isRequired = true, type = STRING) }, restParameters = { @RestParameter(name = "file", description = "the filename", isRequired = true, type = FILE) }, responses = { @RestResponse(responseCode = SC_OK, description = "OK, file stored") }) Response restPutInCollection(@PathParam("collectionId") String collectionId,
          @Context HttpServletRequest request); @DELETE @Path(WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX + "{mediaPackageID}/{mediaPackageElementID}") @RestQuery(name = "delete", description = "Remove the file from the working repository under /mediaPackageID/mediaPackageElementID", returnDescription = "No content", pathParameters = { @RestParameter(name = "mediaPackageID", description = "the mediapackage identifier", isRequired = true, type = STRING), @RestParameter(name = "mediaPackageElementID", description = "the mediapackage element identifier", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "File deleted"), @RestResponse(responseCode = SC_NOT_FOUND, description = "File did not exist") }) Response restDelete(@PathParam("mediaPackageID") String mediaPackageID,
          @PathParam("mediaPackageElementID") String mediaPackageElementID); @DELETE @Path(WorkingFileRepository.COLLECTION_PATH_PREFIX + "{collectionId}/{fileName}") @RestQuery(name = "deleteFromCollection", description = "Remove the file from the working repository under /collectionId/filename", returnDescription = "No content", pathParameters = { @RestParameter(name = "collectionId", description = "the collection identifier", isRequired = true, type = STRING), @RestParameter(name = "fileName", description = "the file name", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_NO_CONTENT, description = "File deleted"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Collection or file not found") }) Response restDeleteFromCollection(@PathParam("collectionId") String collectionId,
          @PathParam("fileName") String fileName); @DELETE @Path(WorkingFileRepository.COLLECTION_PATH_PREFIX + "cleanup/{collectionId}/{numberOfDays}") @RestQuery(name = "cleanupOldFilesFromCollection", description = "Remove the files from the working repository under /collectionId that are older than N days", returnDescription = "No content", pathParameters = { @RestParameter(name = "collectionId", description = "the collection identifier", isRequired = true, type = STRING), @RestParameter(name = "numberOfDays", description = "files older than this number of days will be deleted", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_NO_CONTENT, description = "Files deleted"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Collection not found") }) Response restCleanupOldFilesFromCollection(@PathParam("collectionId") String collectionId,
          @PathParam("numberOfDays") long days); @GET @Path(WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX + "{mediaPackageID}/{mediaPackageElementID}") @RestQuery(name = "get", description = "Gets the file from the working repository under /mediaPackageID/mediaPackageElementID", returnDescription = "The file", pathParameters = { @RestParameter(name = "mediaPackageID", description = "the mediapackage identifier", isRequired = true, type = STRING), @RestParameter(name = "mediaPackageElementID", description = "the mediapackage element identifier", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "File returned"), @RestResponse(responseCode = SC_NOT_MODIFIED, description = "If file not modified"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Not found") }) Response restGet(@PathParam("mediaPackageID") final String mediaPackageID,
          @PathParam("mediaPackageElementID") final String mediaPackageElementID,
          @HeaderParam("If-None-Match") String ifNoneMatch); @GET @Path(WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX + "{mediaPackageID}/{mediaPackageElementID}/{fileName}") @RestQuery(name = "getWithFilename", description = "Gets the file from the working repository under /mediaPackageID/mediaPackageElementID/filename", returnDescription = "The file", pathParameters = { @RestParameter(name = "mediaPackageID", description = "the mediapackage identifier", isRequired = true, type = STRING), @RestParameter(name = "mediaPackageElementID", description = "the mediapackage element identifier", isRequired = true, type = STRING), @RestParameter(name = "fileName", description = "the file name", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "File returned"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Not found") }) Response restGet(@PathParam("mediaPackageID") String mediaPackageID,
          @PathParam("mediaPackageElementID") String mediaPackageElementID, @PathParam("fileName") String fileName,
          @HeaderParam("If-None-Match") String ifNoneMatch, @HeaderParam("Range") String range); @GET @Path(WorkingFileRepository.COLLECTION_PATH_PREFIX + "{collectionId}/{fileName}") @RestQuery(name = "getFromCollection", description = "Gets the file from the working repository under /collectionId/filename", returnDescription = "The file", pathParameters = { @RestParameter(name = "collectionId", description = "the collection identifier", isRequired = true, type = STRING), @RestParameter(name = "fileName", description = "the file name", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "File returned"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Not found") }) Response restGetFromCollection(@PathParam("collectionId") String collectionId,
          @PathParam("fileName") String fileName); @GET @Path("/collectionuri/{collectionID}/{fileName}") @RestQuery(name = "getUriFromCollection", description = "Gets the URL for a file to be stored in the working repository under /collectionId/filename", returnDescription = "The url to this file", pathParameters = { @RestParameter(name = "collectionID", description = "the collection identifier", isRequired = true, type = STRING), @RestParameter(name = "fileName", description = "the file name", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "URL returned") }) Response restGetCollectionUri(@PathParam("collectionID") String collectionId,
          @PathParam("fileName") String fileName); @GET @Path("/uri/{mediaPackageID}/{mediaPackageElementID}") @RestQuery(name = "getUri", description = "Gets the URL for a file to be stored in the working repository under /mediaPackageID", returnDescription = "The url to this file", pathParameters = { @RestParameter(name = "mediaPackageID", description = "the mediaPackage identifier", isRequired = true, type = STRING), @RestParameter(name = "mediaPackageElementID", description = "the mediaPackage element identifier", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "URL returned") }) Response restGetUri(@PathParam("mediaPackageID") String mediaPackageID,
          @PathParam("mediaPackageElementID") String mediaPackageElementID); @GET @Path("/uri/{mediaPackageID}/{mediaPackageElementID}/{fileName}") @RestQuery(name = "getUriWithFilename", description = "Gets the URL for a file to be stored in the working repository under /mediaPackageID", returnDescription = "The url to this file", pathParameters = { @RestParameter(name = "mediaPackageID", description = "the mediaPackage identifier", isRequired = true, type = STRING), @RestParameter(name = "mediaPackageElementID", description = "the mediaPackage element identifier", isRequired = true, type = STRING), @RestParameter(name = "fileName", description = "the filename", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "URL returned") }) Response restGetUri(@PathParam("mediaPackageID") String mediaPackageID,
          @PathParam("mediaPackageElementID") String mediaPackageElementID, @PathParam("fileName") String fileName); @SuppressWarnings("unchecked") @GET @Produces(MediaType.APPLICATION_JSON) @Path("/list/{collectionId}.json") @RestQuery(name = "filesInCollection", description = "Lists files in a collection", returnDescription = "Links to the URLs in a collection", pathParameters = { @RestParameter(name = "collectionId", description = "the collection identifier", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "URLs returned"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Collection not found") }) Response restGetCollectionContents(@PathParam("collectionId") String collectionId); @POST @Path("/copy/{fromCollection}/{fromFileName}/{toMediaPackage}/{toMediaPackageElement}/{toFileName}") @RestQuery(name = "copy", description = "Copies a file from a collection to a mediapackage", returnDescription = "A URL to the copied file", pathParameters = { @RestParameter(name = "fromCollection", description = "the collection identifier hosting the source", isRequired = true, type = STRING), @RestParameter(name = "fromFileName", description = "the source file name", isRequired = true, type = STRING), @RestParameter(name = "toMediaPackage", description = "the destination mediapackage identifier", isRequired = true, type = STRING), @RestParameter(name = "toMediaPackageElement", description = "the destination mediapackage element identifier", isRequired = true, type = STRING), @RestParameter(name = "toFileName", description = "the destination file name", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "URL returned"), @RestResponse(responseCode = SC_NOT_FOUND, description = "File to copy not found") }) Response restCopyTo(@PathParam("fromCollection") String fromCollection,
          @PathParam("fromFileName") String fromFileName, @PathParam("toMediaPackage") String toMediaPackage,
          @PathParam("toMediaPackageElement") String toMediaPackageElement, @PathParam("toFileName") String toFileName); @POST @Path("/move/{fromCollection}/{fromFileName}/{toMediaPackage}/{toMediaPackageElement}/{toFileName}") @RestQuery(name = "move", description = "Moves a file from a collection to a mediapackage", returnDescription = "A URL to the moved file", pathParameters = { @RestParameter(name = "fromCollection", description = "the collection identifier hosting the source", isRequired = true, type = STRING), @RestParameter(name = "fromFileName", description = "the source file name", isRequired = true, type = STRING), @RestParameter(name = "toMediaPackage", description = "the destination mediapackage identifier", isRequired = true, type = STRING), @RestParameter(name = "toMediaPackageElement", description = "the destination mediapackage element identifier", isRequired = true, type = STRING), @RestParameter(name = "toFileName", description = "the destination file name", isRequired = true, type = STRING) }, responses = { @RestResponse(responseCode = SC_OK, description = "URL returned"), @RestResponse(responseCode = SC_NOT_FOUND, description = "File to move not found") }) Response restMoveTo(@PathParam("fromCollection") String fromCollection,
          @PathParam("fromFileName") String fromFileName, @PathParam("toMediaPackage") String toMediaPackage,
          @PathParam("toMediaPackageElement") String toMediaPackageElement, @PathParam("toFileName") String toFileName); @SuppressWarnings("unchecked") @GET @Produces(MediaType.APPLICATION_JSON) @Path("storage") @RestQuery(name = "storage", description = "Returns a report on the disk usage and availability", returnDescription = "Plain text containing the report", responses = { @RestResponse(responseCode = SC_OK, description = "Report returned") }) Response restGetTotalStorage(); @GET @Produces(MediaType.TEXT_PLAIN) @Path("/baseUri") @RestQuery(name = "baseuri", description = "Returns a base URI for this repository", returnDescription = "Plain text containing the base URI", responses = { @RestResponse(responseCode = SC_OK, description = "Base URI returned") }) Response restGetBaseUri();  }
@Test public void testExtractImageContentType() throws Exception { String mediaPackageId = "mp"; String image = "element1"; InputStream in = null; InputStream responseIn = null; try { in = getClass().getResourceAsStream("/opencast_header.gif"); endpoint.put(mediaPackageId, image, "opencast_header.gif", in); } finally { IOUtils.closeQuietly(in); } Response response = endpoint.restGet(mediaPackageId, image, null); Assert.assertEquals("Gif content type", "image/gif", response.getMetadata().getFirst("Content-Type")); try { in = getClass().getResourceAsStream("/opencast_header.gif"); byte[] bytesFromClasspath = IOUtils.toByteArray(in); responseIn = (InputStream) response.getEntity(); byte[] bytesFromRepo = IOUtils.toByteArray(responseIn); Assert.assertTrue(Arrays.equals(bytesFromClasspath, bytesFromRepo)); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(responseIn); } }
@Test public void testExtractXmlContentType() throws Exception { String mediaPackageId = "mp"; String dc = "element1"; try (InputStream in = getClass().getResourceAsStream("/dublincore.xml")) { endpoint.put(mediaPackageId, dc, "dublincore.xml", in); } Response response = endpoint.restGet(mediaPackageId, dc, null); Assert.assertEquals("DC content type", "text/xml", response.getMetadata().getFirst("Content-Type")); try (InputStream in = getClass().getResourceAsStream("/dublincore.xml")) { byte[] imageBytesFromClasspath = IOUtils.toByteArray(in); try (InputStream responseIn = (InputStream) response.getEntity()) { byte[] imageBytesFromRepo = IOUtils.toByteArray(responseIn); Assert.assertTrue(Arrays.equals(imageBytesFromClasspath, imageBytesFromRepo)); } } }