Skip to content

Commit

Permalink
Fixed checkstyle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmajchrak committed Oct 3, 2023
1 parent 83b6b7d commit 44dce44
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -81,7 +89,7 @@ public ResponseEntity<Resource> downloadSingleFile(@PathVariable("id") String id
}

DSpaceObject dso = null;
try{
try {
dso = handleService.resolveToObject(context, handleID);
} catch (Exception e) {
log.error("Cannot resolve handle: " + handleID);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @author longtv
*/
public class MetadataBitstreamWrapperRest extends BaseObjectRest<String>{
public class MetadataBitstreamWrapperRest extends BaseObjectRest<String> {
public static final String NAME = "metadatabitstream";
public static final String CATEGORY = RestAddressableModel.CORE;

Expand All @@ -30,7 +30,8 @@ public class MetadataBitstreamWrapperRest extends BaseObjectRest<String>{
private String href;
private boolean canPreview;

public MetadataBitstreamWrapperRest(String name, String description, String fileSize, String checksum, List<FileInfo> fileInfo, String format, String href, boolean canPreview) {
public MetadataBitstreamWrapperRest(String name, String description, String fileSize, String checksum,
List<FileInfo> fileInfo, String format, String href, boolean canPreview) {
this.name = name;
this.description = description;
this.fileSize = fileSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author longtv
*/
@RelNameDSpaceResource(MetadataBitstreamWrapperRest.NAME)
public class MetadataBitstreamWrapperResource extends DSpaceResource<MetadataBitstreamWrapperRest>{
public class MetadataBitstreamWrapperResource extends DSpaceResource<MetadataBitstreamWrapperRest> {
public MetadataBitstreamWrapperResource(MetadataBitstreamWrapperRest data, Utils utils) {
super(data, utils);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<MetadataBitstreamWrapperRest, Integer>{
public class MetadataBitstreamRestRepository extends DSpaceRestRepository<MetadataBitstreamWrapperRest, Integer> {
private static Logger log = org.apache.logging.log4j.LogManager.getLogger(MetadataBitstreamRestRepository.class);
private final static int MAX_FILE_PREVIEW_COUNT = 1000;

Expand Down Expand Up @@ -95,9 +99,10 @@ public class MetadataBitstreamRestRepository extends DSpaceRestRepository<Metada

@SearchRestMethod(name = "byHandle")
public Page<MetadataBitstreamWrapperRest> 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!");
}
Expand Down Expand Up @@ -142,7 +147,9 @@ public Page<MetadataBitstreamWrapperRest> 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()));
}
Expand Down Expand Up @@ -223,7 +230,8 @@ private List<FileInfo> 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);
}
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -94,15 +84,17 @@ 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)));
}

@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());
}

Expand All @@ -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()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand All @@ -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");
Expand All @@ -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;
}
Expand Down

0 comments on commit 44dce44

Please sign in to comment.