From 44dce44d41e4c39f54100feaa45b5293a6980aa4 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Tue, 3 Oct 2023 15:15:55 +0200 Subject: [PATCH] Fixed checkstyle errors --- .../app/rest/MetadataBitstreamController.java | 22 ++++++--- .../MetadataBitstreamWrapperConverter.java | 3 +- .../model/MetadataBitstreamWrapperRest.java | 5 +- .../MetadataBitstreamWrapperResource.java | 2 +- .../wrapper/MetadataBitstreamWrapper.java | 4 +- .../MetadataBitstreamRestRepository.java | 32 +++++++----- .../rest/MetadataBitstreamControllerIT.java | 37 ++++++-------- .../MetadataBitstreamRestRepositoryIT.java | 49 +++++++++++-------- 8 files changed, 86 insertions(+), 68 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java index 3d97a584f3ad..7334d894bbd5 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java @@ -7,14 +7,14 @@ */ package org.dspace.app.rest; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.sql.SQLException; import java.util.List; import java.util.Objects; import java.util.zip.Deflater; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; @@ -28,8 +28,13 @@ import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.MissingLicenseAgreementException; import org.dspace.authorize.service.AuthorizeService; -import org.dspace.content.*; +import org.dspace.content.Bitstream; +import org.dspace.content.BitstreamFormat; +import org.dspace.content.Bundle; +import org.dspace.content.DSpaceObject; +import org.dspace.content.Item; import org.dspace.content.service.BitstreamService; +import org.dspace.core.Constants; import org.dspace.core.Context; import org.dspace.handle.service.HandleService; import org.dspace.services.ConfigurationService; @@ -39,8 +44,11 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; -import org.dspace.core.Constants; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; /** * This CLARIN Controller download a single file or a ZIP file from the Item's bitstream. @@ -81,7 +89,7 @@ public ResponseEntity downloadSingleFile(@PathVariable("id") String id } DSpaceObject dso = null; - try{ + try { dso = handleService.resolveToObject(context, handleID); } catch (Exception e) { log.error("Cannot resolve handle: " + handleID); @@ -158,7 +166,7 @@ public void downloadFileZip(@RequestParam("handleId") String handleId, DSpaceObject dso = null; String name = ""; - try{ + try { dso = handleService.resolveToObject(context, handleId); } catch (Exception e) { log.error("Cannot resolve handle: " + handleId); diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/MetadataBitstreamWrapperConverter.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/MetadataBitstreamWrapperConverter.java index 593e5fc262f5..49728da0b899 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/MetadataBitstreamWrapperConverter.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/MetadataBitstreamWrapperConverter.java @@ -41,7 +41,8 @@ public MetadataBitstreamWrapperRest convert(MetadataBitstreamWrapper modelObject bitstreamWrapperRest.setId(modelObject.getBitstream().getID().toString()); bitstreamWrapperRest.setDescription(modelObject.getDescription()); bitstreamWrapperRest.setChecksum(modelObject.getBitstream().getChecksum()); - bitstreamWrapperRest.setFileSize(FileTreeViewGenerator.humanReadableFileSize(modelObject.getBitstream().getSizeBytes())); + bitstreamWrapperRest.setFileSize(FileTreeViewGenerator.humanReadableFileSize( + modelObject.getBitstream().getSizeBytes())); bitstreamWrapperRest.setFileInfo(modelObject.getFileInfo()); bitstreamWrapperRest.setHref(modelObject.getHref()); bitstreamWrapperRest.setFormat(modelObject.getFormat()); diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/MetadataBitstreamWrapperRest.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/MetadataBitstreamWrapperRest.java index dcbeea6a1d27..7e3c1c03568e 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/MetadataBitstreamWrapperRest.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/MetadataBitstreamWrapperRest.java @@ -17,7 +17,7 @@ * * @author longtv */ -public class MetadataBitstreamWrapperRest extends BaseObjectRest{ +public class MetadataBitstreamWrapperRest extends BaseObjectRest { public static final String NAME = "metadatabitstream"; public static final String CATEGORY = RestAddressableModel.CORE; @@ -30,7 +30,8 @@ public class MetadataBitstreamWrapperRest extends BaseObjectRest{ private String href; private boolean canPreview; - public MetadataBitstreamWrapperRest(String name, String description, String fileSize, String checksum, List fileInfo, String format, String href, boolean canPreview) { + public MetadataBitstreamWrapperRest(String name, String description, String fileSize, String checksum, + List fileInfo, String format, String href, boolean canPreview) { this.name = name; this.description = description; this.fileSize = fileSize; diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/MetadataBitstreamWrapperResource.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/MetadataBitstreamWrapperResource.java index 735baa6f5ba3..9174ca878e98 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/MetadataBitstreamWrapperResource.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/MetadataBitstreamWrapperResource.java @@ -18,7 +18,7 @@ * @author longtv */ @RelNameDSpaceResource(MetadataBitstreamWrapperRest.NAME) -public class MetadataBitstreamWrapperResource extends DSpaceResource{ +public class MetadataBitstreamWrapperResource extends DSpaceResource { public MetadataBitstreamWrapperResource(MetadataBitstreamWrapperRest data, Utils utils) { super(data, utils); } diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/wrapper/MetadataBitstreamWrapper.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/wrapper/MetadataBitstreamWrapper.java index ae7125c079a7..f2c315ec9278 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/wrapper/MetadataBitstreamWrapper.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/wrapper/MetadataBitstreamWrapper.java @@ -7,11 +7,11 @@ */ package org.dspace.app.rest.model.wrapper; +import java.util.List; + import org.dspace.content.Bitstream; import org.dspace.util.FileInfo; -import java.util.List; - /** * Object which handles data for previewing the bitstream content in the Item View page. * diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index 13ef8021eb2e..2fbd4731fb9f 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -7,8 +7,6 @@ */ package org.dspace.app.rest.repository; -import javax.servlet.http.HttpServletRequest; -import javax.xml.parsers.ParserConfigurationException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -24,6 +22,8 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; +import javax.servlet.http.HttpServletRequest; +import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.ArchiveInputStream; @@ -43,7 +43,11 @@ import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.MissingLicenseAgreementException; import org.dspace.authorize.service.AuthorizeService; -import org.dspace.content.*; +import org.dspace.content.Bitstream; +import org.dspace.content.Bundle; +import org.dspace.content.DSpaceObject; +import org.dspace.content.Item; +import org.dspace.content.Thumbnail; import org.dspace.content.service.BitstreamService; import org.dspace.content.service.ItemService; import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; @@ -64,7 +68,7 @@ * This controller returns content of the bitstream to the `Preview` box in the Item View. */ @Component(MetadataBitstreamWrapperRest.CATEGORY + "." + MetadataBitstreamWrapperRest.NAME) -public class MetadataBitstreamRestRepository extends DSpaceRestRepository{ +public class MetadataBitstreamRestRepository extends DSpaceRestRepository { private static Logger log = org.apache.logging.log4j.LogManager.getLogger(MetadataBitstreamRestRepository.class); private final static int MAX_FILE_PREVIEW_COUNT = 1000; @@ -95,9 +99,10 @@ public class MetadataBitstreamRestRepository extends DSpaceRestRepository findByHandle(@Parameter(value = "handle", required = true) String handle, - @Parameter(value = "fileGrpType", required = false) String fileGrpType, + @Parameter(value = "fileGrpType") String fileGrpType, Pageable pageable) - throws SQLException, ParserConfigurationException, IOException, SAXException, AuthorizeException, ArchiveException { + throws SQLException, ParserConfigurationException, IOException, SAXException, AuthorizeException, + ArchiveException { if (StringUtils.isBlank(handle)) { throw new DSpaceBadRequestException("handle cannot be null!"); } @@ -142,7 +147,9 @@ public Page findByHandle(@Parameter(value = "handl if (canPreview) { fileInfos = getFilePreviewContent(context, bitstream, fileInfos); } - MetadataBitstreamWrapper bts = new MetadataBitstreamWrapper(bitstream, fileInfos, bitstream.getFormat(context).getMIMEType(), bitstream.getFormatDescription(context), url, canPreview); + MetadataBitstreamWrapper bts = new MetadataBitstreamWrapper(bitstream, fileInfos, + bitstream.getFormat(context).getMIMEType(), + bitstream.getFormatDescription(context), url, canPreview); metadataValueWrappers.add(bts); rs.add(metadataBitstreamWrapperConverter.convert(bts, utils.obtainProjection())); } @@ -223,7 +230,8 @@ private List processInputStreamToFilePreview(Context context, Bitstrea data = extractFile(inputStream, "zip"); fileInfos = FileTreeViewGenerator.parse(data); } else if (bitstream.getFormat(context).getExtensions().contains("tar")) { - ArchiveInputStream is = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, inputStream); + ArchiveInputStream is = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, + inputStream); data = extractFile(is, "tar"); fileInfos = FileTreeViewGenerator.parse(data); } @@ -239,14 +247,12 @@ private String composePreviewURL(Context context, Item item, Bitstream bitstream String identifier = null; if (Objects.nonNull(item) && Objects.nonNull(item.getHandle())) { identifier = "handle/" + item.getHandle(); - } - else if (Objects.nonNull(item)) { + } else if (Objects.nonNull(item)) { identifier = "item/" + item.getID(); - } - else { + } else { identifier = "id/" + bitstream.getID(); } - String url = contextPath + "/bitstream/"+identifier; + String url = contextPath + "/bitstream/" + identifier; try { if (bitstream.getName() != null) { url += "/" + Util.encodeBitstreamName(bitstream.getName(), "UTF-8"); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java index 3e6438d99d8e..fbf76442b18f 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java @@ -7,42 +7,32 @@ */ package org.dspace.app.rest; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.util.zip.Deflater; + import org.apache.commons.codec.CharEncoding; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.io.IOUtils; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; -import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.service.AuthorizeService; import org.dspace.builder.BitstreamBuilder; import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CommunityBuilder; import org.dspace.builder.ItemBuilder; import org.dspace.content.Bitstream; -import org.dspace.content.Bundle; import org.dspace.content.Collection; import org.dspace.content.Item; import org.dspace.content.service.BitstreamService; -import org.dspace.core.Constants; -import org.hamcrest.Matcher; -import org.hamcrest.Matchers; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.result.ContentResultMatchers; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.sql.SQLException; -import java.util.List; -import java.util.zip.Deflater; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -public class MetadataBitstreamControllerIT extends AbstractControllerIntegrationTest{ +public class MetadataBitstreamControllerIT extends AbstractControllerIntegrationTest { private static final String METADATABITSTREAM_ENDPOINT = "/bitstream/"; private static final String METADATABITSTREAM_DOWNLOAD_SINGLE_ENDPOINT = METADATABITSTREAM_ENDPOINT + "/handle"; private static final String METADATABITSTREAM_DOWNLOAD_ALL_ENDPOINT = METADATABITSTREAM_ENDPOINT + "/allzip"; @@ -94,7 +84,8 @@ public void downloadSingleFileNullPathVariable() throws Exception { public void downloadSingleFileWithAuthorize() throws Exception { InputStream ip = bitstreamService.retrieve(context, bts); String token = getAuthToken(admin.getEmail(), password); - getClient(token).perform(get(METADATABITSTREAM_DOWNLOAD_SINGLE_ENDPOINT + "/" + publicItem.getHandle() + "/" + bts.getName())) + getClient(token).perform(get(METADATABITSTREAM_DOWNLOAD_SINGLE_ENDPOINT + + "/" + publicItem.getHandle() + "/" + bts.getName())) .andExpect(status().isOk()) .andExpect(content().contentType("application/octet-stream;charset=UTF-8")) .andExpect(content().bytes(IOUtils.toByteArray(ip))); @@ -102,7 +93,8 @@ public void downloadSingleFileWithAuthorize() throws Exception { @Test public void downloadSingleFileWithNoAuthorize() throws Exception { - getClient().perform(get(METADATABITSTREAM_DOWNLOAD_SINGLE_ENDPOINT + "/" + publicItem.getHandle() + "/" + bts.getName())) + getClient().perform(get(METADATABITSTREAM_DOWNLOAD_SINGLE_ENDPOINT + + "/" + publicItem.getHandle() + "/" + bts.getName())) .andExpect(status().isOk()); } @@ -121,7 +113,8 @@ public void downloadAllZip() throws Exception { zip.close(); String token = getAuthToken(admin.getEmail(), password); - getClient(token).perform(get(METADATABITSTREAM_DOWNLOAD_ALL_ENDPOINT ).param("handleId", publicItem.getHandle())) + getClient(token).perform(get(METADATABITSTREAM_DOWNLOAD_ALL_ENDPOINT ).param("handleId", + publicItem.getHandle())) .andExpect(status().isOk()) .andExpect(content().bytes(byteArrayOutputStream.toByteArray())); diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java index 7f8e924f832b..c0de3540fc42 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java @@ -7,16 +7,30 @@ */ package org.dspace.app.rest; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.sql.SQLException; + import org.apache.commons.codec.CharEncoding; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.app.util.Util; import org.dspace.authorize.service.AuthorizeService; -import org.dspace.builder.*; -import org.dspace.content.*; -import org.dspace.content.clarin.ClarinLicense; -import org.dspace.content.clarin.ClarinLicenseResourceMapping; +import org.dspace.builder.BitstreamBuilder; +import org.dspace.builder.CollectionBuilder; +import org.dspace.builder.CommunityBuilder; +import org.dspace.builder.ItemBuilder; +import org.dspace.content.Bitstream; +import org.dspace.content.Collection; +import org.dspace.content.Item; import org.dspace.content.service.clarin.ClarinLicenseResourceMappingService; import org.dspace.core.Constants; import org.dspace.util.FileTreeViewGenerator; @@ -25,20 +39,11 @@ import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.sql.SQLException; -import java.util.List; - -import static org.hamcrest.Matchers.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; - public class MetadataBitstreamRestRepositoryIT extends AbstractControllerIntegrationTest { private static final String METADATABITSTREAM_ENDPOINT = "/api/core/metadatabitstream/"; - private static final String METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT = METADATABITSTREAM_ENDPOINT + "search/byHandle"; + private static final String METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT = + METADATABITSTREAM_ENDPOINT + "search/byHandle"; private static final String FILE_GRP_TYPE = "ORIGINAL"; private static final String AUTHOR = "Test author name"; private Collection col; @@ -104,9 +109,11 @@ public void findByHandle() throws Exception { .andExpect(jsonPath("$._embedded.metadatabitstreams[*].description") .value(Matchers.containsInAnyOrder(Matchers.containsString(bts.getFormatDescription(context))))) .andExpect(jsonPath("$._embedded.metadatabitstreams[*].format") - .value(Matchers.containsInAnyOrder(Matchers.containsString(bts.getFormat(context).getMIMEType())))) + .value(Matchers.containsInAnyOrder(Matchers.containsString( + bts.getFormat(context).getMIMEType())))) .andExpect(jsonPath("$._embedded.metadatabitstreams[*].fileSize") - .value(Matchers.containsInAnyOrder(Matchers.containsString(FileTreeViewGenerator.humanReadableFileSize(bts.getSizeBytes()))))) + .value(Matchers.containsInAnyOrder(Matchers.containsString( + FileTreeViewGenerator.humanReadableFileSize(bts.getSizeBytes()))))) .andExpect(jsonPath("$._embedded.metadatabitstreams[*].canPreview") .value(Matchers.containsInAnyOrder(Matchers.is(canPreview)))) .andExpect(jsonPath("$._embedded.metadatabitstreams[*].fileInfo").exists()) @@ -128,7 +135,9 @@ public void findByHandleEmptyFileGrpType() throws Exception { .andExpect(jsonPath("$.page.totalPages", is(0))) .andExpect(jsonPath("$.page.size", is(20))) .andExpect(jsonPath("$.page.number", is(0))) - .andExpect(jsonPath("$._links.self.href", Matchers.containsString(METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT + "?handle=" + publicItem.getHandle() + "&fileGrpType="))); + .andExpect(jsonPath("$._links.self.href", + Matchers.containsString(METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT + + "?handle=" + publicItem.getHandle() + "&fileGrpType="))); } @Test @@ -152,7 +161,7 @@ private void composeURL() { } else { identifier = "id/" + bts.getID(); } - url = "/bitstream/"+identifier+"/"; + url = "/bitstream/" + identifier + "/"; try { if (bts.getName() != null) { url += Util.encodeBitstreamName(bts.getName(), "UTF-8"); @@ -165,7 +174,7 @@ private void composeURL() { if (authorizeService.authorizeActionBoolean(context, bts, Constants.READ)) { isAllowed = "y"; } - } catch (SQLException e) {/* Do nothing */} + } catch (SQLException e) { /* Do nothing */ } url += "&isAllowed=" + isAllowed; }