DocumentTemplateBinaryResource { @POST @ApiOperation(value = "Upload document template file", 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 uploadDocumentTemplateFiles( @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; DocumentMasterTemplateKey templatePK = new DocumentMasterTemplateKey(workspaceId, templateId); Collection<Part> formParts = request.getParts(); for (Part formPart : formParts) { fileName = Normalizer.normalize(formPart.getSubmittedFileName(), Normalizer.Form.NFC); binaryResource = documentService.saveFileInTemplate(templatePK, fileName, 0); OutputStream outputStream = storageManager.getBinaryResourceOutputStream(binaryResource); length = BinaryResourceUpload.uploadBinary(outputStream, formPart); documentService.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); } } DocumentTemplateBinaryResource(); @POST @ApiOperation(value = "Upload document template file", 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 uploadDocumentTemplateFiles(
            @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 document 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 downloadDocumentTemplateFile(
            @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,
            @ApiParam(required = false, value = "Type") @QueryParam("type") String type,
            @ApiParam(required = false, value = "Output") @QueryParam("output") String output);  }
@Test public void downloadDocumentTemplateFiles() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); binaryResource = Mockito.spy(new BinaryResource(ResourceUtil.FILENAME1, ResourceUtil.DOCUMENT_SIZE, new Date())); Collection<Part> filesParts = new ArrayList<>(); URL resource = getClass().getClassLoader().getResource(ResourceUtil.SOURCE_FILE_STORAGE + ResourceUtil.FILENAME1); Assert.assertNotNull(resource); filesParts.add(new PartImpl(new File(resource.getFile()))); File uploadedFile = File.createTempFile(ResourceUtil.TARGET_FILE_STORAGE + "new_" + ResourceUtil.FILENAME1, ResourceUtil.TEMP_SUFFIX); OutputStream outputStream = new FileOutputStream(uploadedFile); Mockito.when(documentService.saveFileInTemplate(Matchers.any(DocumentMasterTemplateKey.class), Matchers.anyString(), Matchers.anyInt())).thenReturn(binaryResource); Mockito.when(storageManager.getBinaryResourceOutputStream(binaryResource)).thenReturn(outputStream); Mockito.when(request.getRequestURI()).thenReturn(ResourceUtil.WORKSPACE_ID + "/documents/" + ResourceUtil.DOCUMENT_ID + "/" + ResourceUtil.FILENAME1); Mockito.when(request.getParts()).thenReturn(filesParts); Response response = documentTemplateBinaryResource.uploadDocumentTemplateFiles(request, ResourceUtil.WORKSPACE_ID, ResourceUtil.DOC_TEMPLATE_ID); Assert.assertNotNull(response); Assert.assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); Assert.assertNotNull(response.getLocation()); uploadedFile.deleteOnExit(); }