Skip to content

Commit

Permalink
114022: atmire-versioning file path definition moved to shared.cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout-atmire committed Apr 15, 2024
1 parent d44b7df commit b0f1136
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -29,29 +27,25 @@ 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;

@PreAuthorize("hasAuthority('ADMIN')")
@RequestMapping(value = "/atmire-versions", method = RequestMethod.GET)
public ResponseEntity<String> 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);
Expand All @@ -69,6 +63,25 @@ public ResponseEntity<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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"))
Expand All @@ -37,15 +38,16 @@ 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"))
.andExpect(status().isOk())
.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)));
}
}
2 changes: 2 additions & 0 deletions shared.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b0f1136

Please sign in to comment.