FileService extends BasicService implements OwnerAPISupport<File> { @POST @Path("/files") public Response uploadFile() { String fileName = null; long fileSize = 0; FileItem document = null; byte[] content = null; if (!ServletFileUpload.isMultipartContent(request)) { fileSize = request.getContentLength(); fileName = request.getHeader("filename"); if (fileName == null) { throwBadRequest("Header 'filename' is missing in request."); } try { content = IOUtils.readFully(request.getInputStream(), request.getContentLength()); } catch (IOException e) { logger.error("File could not be read.", e); return RESPONSE_BAD; } } else { try { document = UploadHandler.getUploadedDocument(request); } catch (FileUploadException e) { logger.error("File could not be uploaded.", e); return RESPONSE_BAD; } fileName = document.getName(); fileSize = document.getSize(); content = document.get(); } fileName = org.apache.commons.io.FilenameUtils.getName(fileName); String key = Math.abs(new Random().nextInt()) + "_" + fileName; if (!fileName.contains(".")) { if (document != null) { document.delete(); } throwBadRequest("File type could not be determined."); } String fileType = FilenameUtils.getExtension(fileName).toLowerCase(); if (!mimeTypes.containsKey(fileType)) { if (document != null) { document.delete(); } throwBadRequest("Unsupported file type."); } try { getECM().uploadDocument(key, content, mimeTypes.get(fileType)); fileName = FilenameUtils.getBaseName(fileName); File file = createFile(fileName, fileType, key, fileSize); addContentMetadata(file, new ByteArrayInputStream(content)); if (document != null) { document.delete(); } } catch (CmisNameConstraintViolationException e) { logger.error("Filename exception.", e); return RESPONSE_BAD; } deleteThumbnails(key); return RESPONSE_OK; }  @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 testUploadFile() throws Exception { assertEquals(0, getFiles().size()); UploadHelper.simulateFileUpload(requestMock, "600x400.png", "image/png"); fileService.uploadFile(); assertEquals(1, getFiles().size()); File file = getFiles().get(0); assertEquals(simpleUser.getUserId(), file.getOwners().get(0).getUser().getUserId()); assertEquals("png", file.getFileType()); assertEquals(600, file.getWidth()); assertEquals(400, file.getHeight()); assertTrue(file.getName().endsWith("600x400")); assertTrue(file.getSize() > 0); }
@Test(expected = WebApplicationException.class) public void testUploadUnsupportedFile() throws Exception { UploadHelper.simulateFileUpload(requestMock, "textfile.txt", "text/plain"); fileService.uploadFile(); }