From b0f11368c40f3bc0ebe594fea83c908a0faf2130 Mon Sep 17 00:00:00 2001 From: wout Date: Mon, 15 Apr 2024 13:09:06 +0200 Subject: [PATCH] 114022: atmire-versioning file path definition moved to shared.cfg --- .../app/rest/AtmireVersionsController.java | 37 +++++++++++++------ .../app/rest/AtmireVersionsControllerIT.java | 12 +++--- shared.cfg | 2 + 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/AtmireVersionsController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/AtmireVersionsController.java index 45c82fecf071..5a07507cacb6 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/AtmireVersionsController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/AtmireVersionsController.java @@ -1,8 +1,5 @@ package org.dspace.app.rest; - -import static org.apache.commons.lang.StringUtils.defaultIfEmpty; - import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -11,6 +8,7 @@ import javax.ws.rs.core.MediaType; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.lang3.StringUtils; import org.dspace.app.rest.utils.HttpHeadersInitializer; import org.dspace.services.ConfigurationService; import org.dspace.services.factory.DSpaceServicesFactory; @@ -29,8 +27,7 @@ public class AtmireVersionsController { protected ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService(); - public static final String CONFIG_DIR = "atmire-versions.directory"; - public static final String ATMIRE_VERSIONS_FILE = "atmire-versions.json"; + public static final String ATMIRE_VERSIONS_CONFIG = "atmire-versions.file"; public static final int BUFFER_SIZE = 4096 * 10; private static final String mimetype = MediaType.APPLICATION_JSON; @@ -38,20 +35,17 @@ public class AtmireVersionsController { @RequestMapping(value = "/atmire-versions", method = RequestMethod.GET) public ResponseEntity atmireVersioning(HttpServletRequest request, HttpServletResponse response) { - // get the directory location of the atmire-versioning file out of the config - String directory = defaultIfEmpty(configurationService.getProperty(CONFIG_DIR), "/"); - if (!directory.endsWith("/")) { - directory += '/'; - } - final String file = directory + ATMIRE_VERSIONS_FILE; + // get the file location + String file = getFilePath(); try { // read the file String json = readFileAsJson(file); + // setup headers HttpHeadersInitializer httpHeadersInitializer = new HttpHeadersInitializer() .withBufferSize(BUFFER_SIZE) - .withFileName(ATMIRE_VERSIONS_FILE) + .withFileName(getFileNameFromPath(file)) .withMimetype(mimetype) .with(request) .with(response); @@ -69,6 +63,25 @@ public ResponseEntity atmireVersioning(HttpServletRequest request, HttpS } } + private String getFilePath() { + String file = configurationService.getProperty(ATMIRE_VERSIONS_CONFIG); + if (StringUtils.isBlank(file)) { + throw new IllegalArgumentException(ATMIRE_VERSIONS_CONFIG + " was not correctly defined in the config."); + } + return file; + } + + /** + * This method will give the filename that is specified at the end of a path to a file + * @param path the path to the file + * @return the filename at the end of the path + */ + private static String getFileNameFromPath(String path) { + // get filename + String[] parts = path.split("/"); + return parts[parts.length - 1]; + } + /** * This method reads a json file and returns it's content * @param filePath file to read diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AtmireVersionsControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AtmireVersionsControllerIT.java index a238af353275..de780dd09b00 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AtmireVersionsControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AtmireVersionsControllerIT.java @@ -13,9 +13,10 @@ public class AtmireVersionsControllerIT extends AbstractControllerIntegrationTest { private final ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService(); + @Override public void destroy() throws Exception { - configurationService.setProperty(AtmireVersionsController.CONFIG_DIR, null); + configurationService.setProperty(AtmireVersionsController.ATMIRE_VERSIONS_CONFIG, null); super.destroy(); } @@ -28,7 +29,7 @@ public void test_unauthorisedUserReadsversioning() throws Exception { @Test public void test_adminReadsNotExistingFile() throws Exception { - configurationService.setProperty(AtmireVersionsController.CONFIG_DIR, "/something/wrong/"); + configurationService.setProperty(AtmireVersionsController.ATMIRE_VERSIONS_CONFIG, "/something/wrong/"); String token = getAuthToken(admin.getEmail(), password); getClient(token).perform(get("/api/atmire-versions")) @@ -37,8 +38,9 @@ public void test_adminReadsNotExistingFile() throws Exception { @Test public void test_adminReadsVersioningFile() throws Exception { - - configurationService.setProperty(AtmireVersionsController.CONFIG_DIR, "src/test/data/dspaceFolder/"); + String filename = "atmire-versions.json"; + String path = "src/test/data/dspaceFolder/" + filename; + configurationService.setProperty(AtmireVersionsController.ATMIRE_VERSIONS_CONFIG, path); String token = getAuthToken(admin.getEmail(), password); getClient(token).perform(get("/api/atmire-versions")) @@ -46,6 +48,6 @@ public void test_adminReadsVersioningFile() throws Exception { .andExpect(content().string("{\"this\":\"is for testing\"}")) .andExpect(header().string("Content-Type", "application/json;charset=UTF-8")) .andExpect(header().string("Content-Disposition", - String.format("attachment;filename=\"%s\"",AtmireVersionsController.ATMIRE_VERSIONS_FILE))); + String.format("attachment;filename=\"%s\"", filename))); } } diff --git a/shared.cfg b/shared.cfg index 0c4418dd409b..1ad8f57720c4 100644 --- a/shared.cfg +++ b/shared.cfg @@ -29,3 +29,5 @@ filter.plugins = ImageMagick PDF Thumbnail # Setting the thumbnail width to be a bitter wider than the DSpace default as this matches better with the default theme thumbnail.maxwidth = 300 thumbnail.maxheight = 300 + +atmire-versions.file = ${dspace.dir}/atmire-versions.json \ No newline at end of file