FileService extends BasicService implements OwnerAPISupport<File> { @GET @Path("/files/{fileId}/references") @Produces(MediaType.APPLICATION_JSON) public FileReferences getReferences(@PathParam("fileId") long fileId) { File existingFile = getExistingFile(fileId, true); FileReferences refs = new FileReferences(); List<Page> pages = PageDAO.getPagesByFileOrWithTemplate(existingFile); for (Page page : pages) { if (page.getPageType() == PageType.TEMPLATE) { if (page.getFile() != null && new TemplateHelper(page.getFile().getParameters()) .containsFile(existingFile, page.getTemplateValues())) { if (this.isAdmin() || inAdminOwners(page.getOwners())) { refs.addUserPage(page); } else { refs.setForeignPages(refs.getForeignPages() + 1); } } } else { if (this.isAdmin() || inAdminOwners(page.getOwners())) { refs.addUserPage(page); } else { refs.setForeignPages(refs.getForeignPages() + 1); } } } return refs; }  @GET @Path("/files") @Produces(MediaType.APPLICATION_JSON) List<File> getFiles(@QueryParam("userId") String userId); @GET @Path("/files/{fileId}") @Produces(MediaType.APPLICATION_JSON) File getFile(@PathParam("fileId") long id); @PUT @Path("/files/{fileId}") @Produces(MediaType.APPLICATION_JSON) File updateFile(@PathParam("fileId") long fileId, File fileData); @POST @Path("/files") Response uploadFile(); @POST @Path("/files/{fileId}/rotate") Response rotateImage(@PathParam("fileId") long fileId); @POST @Path("/files/{fileId}/content") Response updateFileContent(@PathParam("fileId") long fileId); @DELETE @Path("/files/{fileId}") Response deleteFile(@PathParam("fileId") long fileId); @GET @Path("/files/{fileId}/references") @Produces(MediaType.APPLICATION_JSON) FileReferences getReferences(@PathParam("fileId") long fileId); @GET @Path("/files/{fileId}/content") @Produces({ "image/jpeg,image/png,image/gif,image/bmp,application/pdf,image/svg+xml,image/x-icon,video/mp4" }) Response getFileContent(@PathParam("fileId") long fileId,
			@DefaultValue("false") @QueryParam("thumbnail") boolean thumbnail,
			@DefaultValue("0") @QueryParam("page") int page,
			@DefaultValue("false") @QueryParam("download") boolean download); @GET @Path("/files/{fileId}/content/{path}") @Produces({ "text/html,image/jpeg,image/png,image/gif,image/bmp,application/pdf,image/svg+xml,image/x-icon,video/mp4" }) Response getFileContent(@PathParam("fileId") long fileId, @PathParam("path") String path,
			@DefaultValue("0") @QueryParam("page") long pageId); @Override @POST @Path("/files/{fileId}/owners") @Consumes(MediaType.APPLICATION_JSON) File addOwners(@PathParam("fileId") long fileId, Owner[] ownerData); @Override @DELETE @Path("/files/{fileId}/owners/{ownerId}") @Produces(MediaType.APPLICATION_JSON) File deleteOwner(@PathParam("fileId") long fileId, @PathParam("ownerId") long ownerId); @GET @Path("/screenkey/{key}") @Produces({ "image/jpeg,image/png,image/gif,image/bmp,application/pdf,image/svg+xml,image/x-icon,video/mp4" }) Response getScreenshotContent(@PathParam("key") String key); @POST @Path("/screenkey/{key}") Response uploadScreenshot(@PathParam("key") String key); @GET @Path("/ecm") @Produces(MediaType.APPLICATION_JSON) List<File> getRawECM(); @DELETE @Path("/ecm/{key}") @Produces(MediaType.APPLICATION_JSON) Response deleteECMFile(@PathParam("key") String ecmKey); @POST @Path("/deletethumbnails") Response deleteThumbnails();  }
@Test public void testGetReferences() throws Exception { UploadHelper.simulateFileUpload(requestMock, "testtemplate.zip", "application/zip"); fileService.uploadFile(); File template = fileService.getFiles(null).get(0); File file1 = fileService.createFile("f1", "jpg", "f1", 99); Page page1 = new Page("p1"); page1.setFile(file1); page1 = pageService.createPage(page1); Page page2 = new Page("p2"); page2.setFile(file1); page2 = pageService.createPage(page2); page2.getOwners().clear(); page2 = PageDAO.save(page2); FileReferences refs = fileService.getReferences(file1.getId()); assertEquals(1, refs.getUserPages().size()); assertEquals(1, refs.getForeignPages()); Page page3 = new Page("p3"); page3.setPageType(PageType.TEMPLATE); page3.setFile(template); page3.setTemplateValues("{\"image\":\"" + file1.getId() + "\"}"); page3 = pageService.createPage(page3); refs = fileService.getReferences(file1.getId()); assertEquals(2, refs.getUserPages().size()); assertEquals(1, refs.getForeignPages()); }
@Test(expected = WebApplicationException.class) public void testGetReferencesForNonExistingPage() throws Exception { fileService.getReferences(-1); }
@Test(expected = WebApplicationException.class) public void testGetReferencesAsUnauthorized() throws Exception { File file1 = fileService.createFile("f1", "jpg", "f1", 99); Page page1 = new Page("p1"); page1.setFile(file1); page1 = pageService.createPage(page1); requestMock = new HttpServletRequestMock(simpleUser2); pageService.request = requestMock; pageService.getReferences(file1.getId()); }