DocumentTemplateBinaryResource { @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) public 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) throws EntityNotFoundException, UserNotActiveException, AccessRightException, NotAllowedException, PreconditionFailedException, RequestedRangeNotSatisfiableException, WorkspaceNotEnabledException { String fullName = workspaceId + "/document-templates/" + templateId + "/" + fileName; BinaryResource binaryResource = documentService.getTemplateBinaryResource(fullName); BinaryResourceDownloadMeta binaryResourceDownloadMeta = new BinaryResourceDownloadMeta(binaryResource); Response.ResponseBuilder rb = request.evaluatePreconditions(binaryResourceDownloadMeta.getLastModified(), binaryResourceDownloadMeta.getETag()); if (rb != null) { return rb.build(); } InputStream binaryContentInputStream = null; boolean isToBeCached = false; try { if (output != null && !output.isEmpty()) { binaryContentInputStream = getConvertedBinaryResource(binaryResource, output, userLocale); if(range == null || range.isEmpty()){ binaryResourceDownloadMeta.setLength(0); } } else { binaryContentInputStream = storageManager.getBinaryResourceInputStream(binaryResource); } return BinaryResourceDownloadResponseBuilder.prepareResponse(binaryContentInputStream, binaryResourceDownloadMeta, range, isToBeCached); } catch (StorageException | FileConversionException e) { Streams.close(binaryContentInputStream); return BinaryResourceDownloadResponseBuilder.downloadError(e, fullName); } } 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 downloadDocumentTemplateFile() throws Exception { Request request = Mockito.mock(Request.class); binaryResource = Mockito.spy(new BinaryResource(ResourceUtil.FILENAME1, ResourceUtil.DOCUMENT_SIZE, new Date())); String fullName = ResourceUtil.WORKSPACE_ID + "/document-templates/" + ResourceUtil.DOC_TEMPLATE_ID + "/" + ResourceUtil.FILENAME1; Mockito.when(documentService.getTemplateBinaryResource(fullName)).thenReturn(binaryResource); URL resource = getClass().getClassLoader().getResource(ResourceUtil.SOURCE_FILE_STORAGE + ResourceUtil.FILENAME1); Assert.assertNotNull(resource); File input = new File(resource.getFile()); FileInputStream fileInputStream = new FileInputStream(input); Mockito.when(storageManager.getBinaryResourceInputStream(binaryResource)).thenReturn(fileInputStream); Response response = documentTemplateBinaryResource.downloadDocumentTemplateFile(request, ResourceUtil.RANGE, ResourceUtil.WORKSPACE_ID, ResourceUtil.DOC_TEMPLATE_ID, ResourceUtil.FILENAME1, ResourceUtil.FILE_TYPE, null); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), 206); Assert.assertEquals(response.getStatusInfo(), Response.Status.PARTIAL_CONTENT); Assert.assertNotNull(response.getEntity()); }