PartTemplateBinaryResource { @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) public 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) throws EntityNotFoundException, UserNotActiveException, AccessRightException, NotAllowedException, PreconditionFailedException, RequestedRangeNotSatisfiableException, WorkspaceNotEnabledException { String fullName = workspaceId + "/part-templates/" + templateId + "/" + fileName; BinaryResource binaryResource = productService.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 { binaryContentInputStream = storageManager.getBinaryResourceInputStream(binaryResource); return BinaryResourceDownloadResponseBuilder.prepareResponse(binaryContentInputStream, binaryResourceDownloadMeta, range, isToBeCached); } catch (StorageException e) { Streams.close(binaryContentInputStream); return BinaryResourceDownloadResponseBuilder.downloadError(e, fullName); } } 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 downloadPartTemplateFileNonNullRange() throws Exception { Request request = Mockito.mock(Request.class); binaryResource = new BinaryResource(ResourceUtil.TEST_PART_FILENAME1, ResourceUtil.PART_SIZE, new Date()); File file = File.createTempFile(getClass().getClassLoader().getResource(ResourceUtil.SOURCE_PART_STORAGE + ResourceUtil.TEST_PART_FILENAME1).getFile(), ResourceUtil.TEMP_SUFFIX); FileInputStream fileInputStream = new FileInputStream(file); Mockito.when(productService.getTemplateBinaryResource(ResourceUtil.WORKSPACE_ID + "/part-templates/" + ResourceUtil.PART_TEMPLATE_ID + "/" + ResourceUtil.TEST_PART_FILENAME1)).thenReturn(binaryResource); Mockito.when(storageManager.getBinaryResourceInputStream(binaryResource)).thenReturn(fileInputStream); Response response = partTemplateBinaryResource.downloadPartTemplateFile(request, ResourceUtil.RANGE, ResourceUtil.WORKSPACE_ID, ResourceUtil.PART_TEMPLATE_ID, ResourceUtil.TEST_PART_FILENAME1); Assert.assertNotNull(response); Assert.assertEquals(response.getStatusInfo(), Response.Status.PARTIAL_CONTENT); Assert.assertEquals(response.getStatus(), 206); Assert.assertTrue(response.hasEntity()); Assert.assertTrue(response.getEntity() instanceof BinaryResourceBinaryStreamingOutput); file.deleteOnExit(); }
@Test public void downloadPartTemplateFileNullRange() throws Exception { Request request = Mockito.mock(Request.class); binaryResource = new BinaryResource(ResourceUtil.TEST_PART_FILENAME1, ResourceUtil.PART_SIZE, new Date()); File file = File.createTempFile(getClass().getClassLoader().getResource(ResourceUtil.SOURCE_PART_STORAGE + ResourceUtil.TEST_PART_FILENAME1).getFile(), ResourceUtil.TEMP_SUFFIX); FileInputStream fileInputStream = new FileInputStream(file); Mockito.when(productService.getTemplateBinaryResource(ResourceUtil.WORKSPACE_ID + "/part-templates/" + ResourceUtil.PART_TEMPLATE_ID + "/" + ResourceUtil.TEST_PART_FILENAME1)).thenReturn(binaryResource); Mockito.when(storageManager.getBinaryResourceInputStream(binaryResource)).thenReturn(fileInputStream); Response response = partTemplateBinaryResource.downloadPartTemplateFile(request, null, ResourceUtil.WORKSPACE_ID, ResourceUtil.PART_TEMPLATE_ID, ResourceUtil.TEST_PART_FILENAME1); Assert.assertNotNull(response); Assert.assertEquals(response.getStatusInfo(), Response.Status.OK); Assert.assertTrue(response.hasEntity()); Assert.assertTrue(response.getEntity() instanceof BinaryResourceBinaryStreamingOutput); Assert.assertNotNull(response.getHeaders().getFirst("Content-Disposition")); Assert.assertNotNull("attachment;filename=\"" + ResourceUtil.TEST_PART_FILENAME1 + "\"".equals(response.getHeaders().getFirst("Content-Disposition"))); file.deleteOnExit(); }