DocumentBinaryResource { @POST @ApiOperation(value = "Upload document file", response = Response.class, authorizations = {@Authorization(value = "authorization")}) @ApiResponses(value = { @ApiResponse(code = 204, message = "Upload success"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @ApiImplicitParams({ @ApiImplicitParam(name = "upload", paramType = "formData", dataType = "file", required = true) }) @Path("/{iteration}") @Consumes(MediaType.MULTIPART_FORM_DATA) @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) public Response uploadDocumentFiles( @Context HttpServletRequest request, @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") final String workspaceId, @ApiParam(required = true, value = "Document master id") @PathParam("documentId") final String documentId, @ApiParam(required = true, value = "Workspace version") @PathParam("version") final String version, @ApiParam(required = true, value = "Document iteration") @PathParam("iteration") final int iteration) throws EntityNotFoundException, EntityAlreadyExistsException, UserNotActiveException, AccessRightException, NotAllowedException, CreationException, WorkspaceNotEnabledException { try { String fileName = null; DocumentIterationKey docPK = new DocumentIterationKey(workspaceId, documentId, version, iteration); Collection<Part> formParts = request.getParts(); for (Part formPart : formParts) { fileName = uploadAFile(formPart, docPK); } if (fileName == null) { return Response.status(Response.Status.BAD_REQUEST).build(); } if (formParts.size() == 1) { return BinaryResourceUpload.tryToRespondCreated(request.getRequestURI() + URLEncoder.encode(fileName, "UTF-8")); } return Response.noContent().build(); } catch (IOException | ServletException | StorageException e) { return BinaryResourceUpload.uploadError(e); } } DocumentBinaryResource(); @POST @ApiOperation(value = "Upload document file", response = Response.class, authorizations = {@Authorization(value = "authorization")}) @ApiResponses(value = { @ApiResponse(code = 204, message = "Upload success"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @ApiImplicitParams({ @ApiImplicitParam(name = "upload", paramType = "formData", dataType = "file", required = true) }) @Path("/{iteration}") @Consumes(MediaType.MULTIPART_FORM_DATA) @RolesAllowed({UserGroupMapping.REGULAR_USER_ROLE_ID}) Response uploadDocumentFiles(
            @Context HttpServletRequest request,
            @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") final String workspaceId,
            @ApiParam(required = true, value = "Document master id") @PathParam("documentId") final String documentId,
            @ApiParam(required = true, value = "Workspace version") @PathParam("version") final String version,
            @ApiParam(required = true, value = "Document iteration") @PathParam("iteration") final int iteration); @GET @ApiOperation(value = "Download document file", response = File.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "Download success"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Path("/{iteration}/{fileName}") @Compress @Produces(MediaType.APPLICATION_OCTET_STREAM) Response downloadDocumentFile(
            @Context Request request,
            @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") final String workspaceId,
            @ApiParam(required = true, value = "Document master id") @PathParam("documentId") final String documentId,
            @ApiParam(required = true, value = "Workspace version") @PathParam("version") final String version,
            @ApiParam(required = true, value = "Document iteration") @PathParam("iteration") final int iteration,
            @ApiParam(required = true, value = "File name") @PathParam("fileName") final String fileName,
            @ApiParam(required = false, value = "Type") @QueryParam("type") String type,
            @ApiParam(required = false, value = "Output") @QueryParam("output") String output,
            @ApiParam(required = false, value = "Range") @HeaderParam("Range") String range,
            @ApiParam(required = false, value = "Shared entity uuid") @QueryParam("uuid") final String uuid,
            @ApiParam(required = false, value = "Password for private resource") @HeaderParam("password") String password,
            @ApiParam(required = false, value = "Shared entity token") @QueryParam("token") String accessToken);  }
@Test public void uploadDocumentFiles() throws Exception { HttpServletRequestWrapper request = Mockito.mock(HttpServletRequestWrapper.class); Collection<Part> filesParts = new ArrayList<>(); filesParts.add(new PartImpl(new File(ResourceUtil.getFilePath(ResourceUtil.SOURCE_FILE_STORAGE + ResourceUtil.FILENAME1)))); BinaryResource binaryResource = new BinaryResource(ResourceUtil.FILENAME1, ResourceUtil.DOCUMENT_SIZE, new Date()); File uploadedFile1 = File.createTempFile(ResourceUtil.TARGET_FILE_STORAGE + "new_" + ResourceUtil.FILENAME1, ResourceUtil.TEMP_SUFFIX); OutputStream outputStream1 = new FileOutputStream(uploadedFile1); Mockito.when(request.getParts()).thenReturn(filesParts); Mockito.when(documentService.saveFileInDocument(Matchers.any(DocumentIterationKey.class), Matchers.anyString(), Matchers.anyInt())).thenReturn(binaryResource); Mockito.when(storageManager.getBinaryResourceOutputStream(Matchers.any(BinaryResource.class))).thenReturn(outputStream1); Mockito.when(request.getRequestURI()).thenReturn(ResourceUtil.WORKSPACE_ID + "/documents/" + ResourceUtil.DOCUMENT_ID + "/" + ResourceUtil.FILENAME1); Response response = documentBinaryResource.uploadDocumentFiles(request, ResourceUtil.WORKSPACE_ID, ResourceUtil.DOCUMENT_ID, ResourceUtil.VERSION, ResourceUtil.ITERATION); assertNotNull(response); assertEquals(response.getStatus(), 201); assertEquals(response.getStatusInfo(), Response.Status.CREATED); uploadedFile1.deleteOnExit(); }
@Test public void uploadFileWithSpecialCharactersToDocumentTemplates() throws Exception { HttpServletRequestWrapper request = Mockito.mock(HttpServletRequestWrapper.class); Collection<Part> filesParts = new ArrayList<>(); filesParts.add(new PartImpl(new File(ResourceUtil.getFilePath(ResourceUtil.SOURCE_FILE_STORAGE) + ResourceUtil.FILENAME2))); BinaryResource binaryResource = new BinaryResource(Tools.unAccent(ResourceUtil.FILENAME2), ResourceUtil.DOCUMENT_SIZE, new Date()); File uploadedFile1 = File.createTempFile(ResourceUtil.TARGET_FILE_STORAGE + "new_" + ResourceUtil.FILENAME2, ResourceUtil.TEMP_SUFFIX); OutputStream outputStream1 = new FileOutputStream(uploadedFile1); Mockito.when(request.getParts()).thenReturn(filesParts); Mockito.when(documentService.saveFileInDocument(Matchers.any(DocumentIterationKey.class), Matchers.anyString(), Matchers.anyInt())).thenReturn(binaryResource); Mockito.when(storageManager.getBinaryResourceOutputStream(Matchers.any(BinaryResource.class))).thenReturn(outputStream1); Mockito.when(request.getRequestURI()).thenReturn(ResourceUtil.WORKSPACE_ID + "/documents/" + ResourceUtil.DOCUMENT_ID + "/" + ResourceUtil.FILENAME2); Response response = documentBinaryResource.uploadDocumentFiles(request, ResourceUtil.WORKSPACE_ID, ResourceUtil.DOCUMENT_ID, ResourceUtil.VERSION, ResourceUtil.ITERATION); assertNotNull(response); assertEquals(201, response.getStatus()); assertEquals(Response.Status.CREATED, response.getStatusInfo()); uploadedFile1.deleteOnExit(); }
@Test public void uploadSeveralFilesToDocumentsTemplates() throws Exception { HttpServletRequestWrapper request = Mockito.mock(HttpServletRequestWrapper.class); Collection<Part> filesParts = new ArrayList<>(); filesParts.add(new PartImpl(new File(ResourceUtil.getFilePath(ResourceUtil.SOURCE_FILE_STORAGE + ResourceUtil.FILENAME1)))); filesParts.add(new PartImpl(new File(ResourceUtil.getFilePath(ResourceUtil.SOURCE_FILE_STORAGE) + ResourceUtil.FILENAME2))); filesParts.add(new PartImpl(new File(ResourceUtil.getFilePath(ResourceUtil.SOURCE_FILE_STORAGE + ResourceUtil.FILENAME3)))); BinaryResource binaryResource1 = new BinaryResource(ResourceUtil.FILENAME1, ResourceUtil.DOCUMENT_SIZE, new Date()); BinaryResource binaryResource2 = new BinaryResource(ResourceUtil.FILENAME2, ResourceUtil.DOCUMENT_SIZE, new Date()); BinaryResource binaryResource3 = new BinaryResource(ResourceUtil.FILENAME3, ResourceUtil.DOCUMENT_SIZE, new Date()); File uploadedFile1 = File.createTempFile(ResourceUtil.TARGET_FILE_STORAGE + "new_" + ResourceUtil.FILENAME1, ResourceUtil.TEMP_SUFFIX); File uploadedFile2 = File.createTempFile(ResourceUtil.TARGET_FILE_STORAGE + "new_" + ResourceUtil.FILENAME2, ResourceUtil.TEMP_SUFFIX); File uploadedFile3 = File.createTempFile(ResourceUtil.TARGET_FILE_STORAGE + "new_" + ResourceUtil.FILENAME3, ResourceUtil.TEMP_SUFFIX); OutputStream outputStream1 = new FileOutputStream(uploadedFile1); OutputStream outputStream2 = new FileOutputStream(uploadedFile2); OutputStream outputStream3 = new FileOutputStream(uploadedFile3); Mockito.when(request.getParts()).thenReturn(filesParts); Mockito.when(documentService.saveFileInDocument(Matchers.any(DocumentIterationKey.class), Matchers.anyString(), Matchers.anyInt())).thenReturn(binaryResource1, binaryResource1, binaryResource2, binaryResource2, binaryResource3, binaryResource3); Mockito.when(storageManager.getBinaryResourceOutputStream(Matchers.any(BinaryResource.class))).thenReturn(outputStream1, outputStream2, outputStream3); Response response = documentBinaryResource.uploadDocumentFiles(request, ResourceUtil.WORKSPACE_ID, ResourceUtil.DOCUMENT_ID, ResourceUtil.VERSION, ResourceUtil.ITERATION); assertNotNull(response); assertEquals(Response.Status.NO_CONTENT, response.getStatusInfo()); uploadedFile1.deleteOnExit(); uploadedFile2.deleteOnExit(); uploadedFile3.deleteOnExit(); }