PartTemplateBinaryResource { @POST @ApiOperation(value = "Upload part template files", response = Response.class) @ApiImplicitParams({ @ApiImplicitParam(name = "upload", paramType = "formData", dataType = "file", required = true) }) @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") }) @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadPartTemplateFiles( @Context HttpServletRequest request, @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") final String workspaceId, @ApiParam(required = true, value = "Template id") @PathParam("templateId") final String templateId) throws EntityNotFoundException, EntityAlreadyExistsException, UserNotActiveException, AccessRightException, NotAllowedException, CreationException, WorkspaceNotEnabledException { try { BinaryResource binaryResource; String fileName = null; long length; PartMasterTemplateKey templatePK = new PartMasterTemplateKey(workspaceId, templateId); Collection<Part> formParts = request.getParts(); for (Part formPart : formParts) { fileName = Normalizer.normalize(formPart.getSubmittedFileName(), Normalizer.Form.NFC); binaryResource = productService.saveFileInTemplate(templatePK, fileName, 0); OutputStream outputStream = storageManager.getBinaryResourceOutputStream(binaryResource); length = BinaryResourceUpload.uploadBinary(outputStream, formPart); productService.saveFileInTemplate(templatePK, fileName, length); } 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); } } PartTemplateBinaryResource(); @POST @ApiOperation(value = "Upload part template files", response = Response.class) @ApiImplicitParams({ @ApiImplicitParam(name = "upload", paramType = "formData", dataType = "file", required = true) }) @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") }) @Consumes(MediaType.MULTIPART_FORM_DATA) Response uploadPartTemplateFiles(
            @Context HttpServletRequest request,
            @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") final String workspaceId,
            @ApiParam(required = true, value = "Template id") @PathParam("templateId") final String templateId); @GET @ApiOperation(value = "Download part template 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("/{fileName}") @Compress @Produces(MediaType.APPLICATION_OCTET_STREAM) Response downloadPartTemplateFile(
            @Context Request request,
            @ApiParam(required = false, value = "Range") @HeaderParam("Range") String range,
            @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") final String workspaceId,
            @ApiParam(required = true, value = "Template id") @PathParam("templateId") final String templateId,
            @ApiParam(required = true, value = "File name") @PathParam("fileName") final String fileName);  }
@Test public void uploadPartTemplateFiles() throws Exception { final File fileToUpload = new File(getClass().getClassLoader().getResource(ResourceUtil.SOURCE_PART_STORAGE + ResourceUtil.TEST_PART_FILENAME1).getFile()); File uploadedFile = File.createTempFile(ResourceUtil.TARGET_PART_STORAGE + ResourceUtil.FILENAME_TARGET_PART, ResourceUtil.TEMP_SUFFIX); HttpServletRequestWrapper request = Mockito.mock(HttpServletRequestWrapper.class); Collection<Part> parts = new ArrayList<>(); parts.add(new PartImpl(fileToUpload)); Mockito.when(request.getParts()).thenReturn(parts); binaryResource = new BinaryResource(ResourceUtil.FILENAME1, ResourceUtil.DOCUMENT_SIZE, new Date()); OutputStream outputStream = new FileOutputStream(uploadedFile); Mockito.when(productService.saveFileInTemplate(Matchers.any(PartMasterTemplateKey.class), Matchers.anyString(), Matchers.anyInt())).thenReturn(binaryResource); Mockito.when(storageManager.getBinaryResourceOutputStream(binaryResource)).thenReturn(outputStream); Mockito.when(request.getRequestURI()).thenReturn(ResourceUtil.WORKSPACE_ID + "/parts-templates/" + ResourceUtil.PART_TEMPLATE_ID + "/" + ResourceUtil.FILENAME_TARGET_PART); Response response = partTemplateBinaryResource.uploadPartTemplateFiles(request, ResourceUtil.WORKSPACE_ID, ResourceUtil.PART_TEMPLATE_ID); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), 201); Assert.assertEquals(response.getStatusInfo(), Response.Status.CREATED); uploadedFile.deleteOnExit(); }
@Test public void uploadPartTemplateFilesUnderANameContainingSpecialCharacters() throws Exception { final File fileToUpload = new File(getClass().getClassLoader().getResource(ResourceUtil.SOURCE_PART_STORAGE + ResourceUtil.TEST_PART_FILENAME1).getFile()); File uploadedFile = File.createTempFile(ResourceUtil.TARGET_PART_STORAGE + ResourceUtil.FILENAME_TO_UPLOAD_PART_SPECIAL_CHARACTER, ResourceUtil.TEMP_SUFFIX); HttpServletRequestWrapper request = Mockito.mock(HttpServletRequestWrapper.class); Collection<Part> parts = new ArrayList<>(); parts.add(new PartImpl(fileToUpload)); Mockito.when(request.getParts()).thenReturn(parts); binaryResource = new BinaryResource(ResourceUtil.FILENAME1, ResourceUtil.DOCUMENT_SIZE, new Date()); OutputStream outputStream = new FileOutputStream(uploadedFile); Mockito.when(productService.saveFileInTemplate(Matchers.any(PartMasterTemplateKey.class), Matchers.anyString(), Matchers.anyInt())).thenReturn(binaryResource); Mockito.when(storageManager.getBinaryResourceOutputStream(binaryResource)).thenReturn(outputStream); Mockito.when(request.getRequestURI()).thenReturn(ResourceUtil.WORKSPACE_ID + "/parts-templates/" + ResourceUtil.PART_TEMPLATE_ID + "/" + ResourceUtil.FILENAME_TARGET_PART); Response response = partTemplateBinaryResource.uploadPartTemplateFiles(request, ResourceUtil.WORKSPACE_ID, ResourceUtil.PART_TEMPLATE_ID); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), 201); Assert.assertEquals(response.getStatusInfo(), Response.Status.CREATED); uploadedFile.deleteOnExit(); }
@Test public void uploadPartTemplateFilesNameContainingSpecialCharacters() throws Exception { final File fileToUpload = new File(getClass().getClassLoader().getResource(ResourceUtil.SOURCE_PART_STORAGE + ResourceUtil.FILENAME_TO_UPLOAD_PART_SPECIAL_CHARACTER).toURI()); File uploadedFile = File.createTempFile(ResourceUtil.TARGET_PART_STORAGE + "new_" + ResourceUtil.FILENAME_TO_UPLOAD_PART_SPECIAL_CHARACTER, ResourceUtil.TEMP_SUFFIX); HttpServletRequestWrapper request = Mockito.mock(HttpServletRequestWrapper.class); Collection<Part> parts = new ArrayList<>(); parts.add(new PartImpl(fileToUpload)); Mockito.when(request.getParts()).thenReturn(parts); binaryResource = new BinaryResource(ResourceUtil.FILENAME1, ResourceUtil.DOCUMENT_SIZE, new Date()); OutputStream outputStream = new FileOutputStream(uploadedFile); Mockito.when(productService.saveFileInTemplate(Matchers.any(PartMasterTemplateKey.class), Matchers.anyString(), Matchers.anyInt())).thenReturn(binaryResource); Mockito.when(storageManager.getBinaryResourceOutputStream(binaryResource)).thenReturn(outputStream); Mockito.when(request.getRequestURI()).thenReturn(ResourceUtil.WORKSPACE_ID + "/parts-templates/" + ResourceUtil.PART_TEMPLATE_ID + "/" + ResourceUtil.FILENAME_TARGET_PART); Response response = partTemplateBinaryResource.uploadPartTemplateFiles(request, ResourceUtil.WORKSPACE_ID, ResourceUtil.PART_TEMPLATE_ID); Assert.assertNotNull(response); Assert.assertEquals(201, response.getStatus()); Assert.assertEquals(Response.Status.CREATED, response.getStatusInfo()); uploadedFile.deleteOnExit(); }
@Test public void uploadPartTemplateSeveralFiles() throws Exception { final File fileToUpload1 = File.createTempFile(getClass().getClassLoader().getResource(ResourceUtil.SOURCE_PART_STORAGE + ResourceUtil.FILENAME_TO_UPLOAD_PART_SPECIAL_CHARACTER).getFile(), ResourceUtil.TEMP_SUFFIX); final File fileToUpload2 = new File(getClass().getClassLoader().getResource(ResourceUtil.SOURCE_PART_STORAGE + ResourceUtil.TEST_PART_FILENAME1).getFile()); final File fileToUpload3 = new File(getClass().getClassLoader().getResource(ResourceUtil.SOURCE_PART_STORAGE + ResourceUtil.TEST_PART_FILENAME2).getFile()); File uploadedFile1 = File.createTempFile(ResourceUtil.TARGET_PART_STORAGE + "new_" + ResourceUtil.TEST_PART_FILENAME1, ResourceUtil.TEMP_SUFFIX); File uploadedFile2 = File.createTempFile(ResourceUtil.TARGET_PART_STORAGE + "new_" + ResourceUtil.TEST_PART_FILENAME2, ResourceUtil.TEMP_SUFFIX); File uploadedFile3 = File.createTempFile(ResourceUtil.TARGET_PART_STORAGE + "new_" + ResourceUtil.FILENAME_TO_UPLOAD_PART_SPECIAL_CHARACTER, ResourceUtil.TEMP_SUFFIX); HttpServletRequestWrapper request = Mockito.mock(HttpServletRequestWrapper.class); Collection<Part> parts = new ArrayList<>(); parts.add(new PartImpl(fileToUpload1)); parts.add(new PartImpl(fileToUpload2)); parts.add(new PartImpl(fileToUpload3)); Mockito.when(request.getParts()).thenReturn(parts); BinaryResource binaryResource1 = new BinaryResource(ResourceUtil.TEST_PART_FILENAME1, ResourceUtil.DOCUMENT_SIZE, new Date()); BinaryResource binaryResource2 = new BinaryResource(ResourceUtil.TEST_PART_FILENAME2, ResourceUtil.DOCUMENT_SIZE, new Date()); BinaryResource binaryResource3 = new BinaryResource(ResourceUtil.FILENAME_TO_UPLOAD_PART_SPECIAL_CHARACTER, ResourceUtil.DOCUMENT_SIZE, new Date()); OutputStream outputStream1 = new FileOutputStream(uploadedFile1); OutputStream outputStream2 = new FileOutputStream(uploadedFile2); OutputStream outputStream3 = new FileOutputStream(uploadedFile3); Mockito.when(productService.saveFileInTemplate(Matchers.any(PartMasterTemplateKey.class), Matchers.anyString(), Matchers.anyInt())).thenReturn(binaryResource1, binaryResource1, binaryResource2, binaryResource2, binaryResource3, binaryResource3); Mockito.when(storageManager.getBinaryResourceOutputStream(Matchers.any(BinaryResource.class))).thenReturn(outputStream1, outputStream2, outputStream3); Mockito.when(request.getRequestURI()).thenReturn(ResourceUtil.WORKSPACE_ID + "/parts-templates/" + ResourceUtil.PART_TEMPLATE_ID + "/" + ResourceUtil.FILENAME_TARGET_PART); Response response = partTemplateBinaryResource.uploadPartTemplateFiles(request, ResourceUtil.WORKSPACE_ID, ResourceUtil.PART_TEMPLATE_ID); Assert.assertNotNull(response); Assert.assertEquals(204, response.getStatus()); Assert.assertEquals(Response.Status.NO_CONTENT, response.getStatusInfo()); uploadedFile1.deleteOnExit(); uploadedFile2.deleteOnExit(); uploadedFile3.deleteOnExit(); }