Skip to content

Commit

Permalink
Merge branch 'w2p-114022_expose-feature-versions-in-the-REST-API' int…
Browse files Browse the repository at this point in the history
…o 'atmire-base-7.6'

Expose backend feature version info in the REST API

See merge request templates/dspace-7.x!89
  • Loading branch information
artlowel committed Apr 26, 2024
2 parents daedf82 + 7495e2d commit c9aabc3
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package org.dspace.app.rest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(value = "/api/")
@RestController
public class AtmireVersionsController {

protected ConfigurationService configurationService
= DSpaceServicesFactory.getInstance().getConfigurationService();

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;

@Autowired
private DiscoverableEndpointsService discoverableEndpointsService;


@PostConstruct
public void afterPropertiesSet() {
discoverableEndpointsService.register(this, List.of(Link.of("/api/atmire-versions", "atmire-versions")));
}

@PreAuthorize("hasAuthority('ADMIN')")
@RequestMapping(value = "/atmire-versions", method = RequestMethod.GET)
public ResponseEntity<String> atmireVersioning(HttpServletRequest request, HttpServletResponse response) {

// 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(getFileNameFromPath(file))
.withMimetype(mimetype)
.with(request)
.with(response);
if (httpHeadersInitializer.isValid()) {
HttpHeaders httpHeaders = httpHeadersInitializer.initialiseHeaders();
return ResponseEntity.ok().headers(httpHeaders).body(json);

}
return ResponseEntity.ok().body(json);

} catch (FileNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Could not find the versions-file");
} catch (IOException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Something went wrong reading the versions-file");
}
}

private String getFilePath() {
String file = configurationService.getProperty(ATMIRE_VERSIONS_CONFIG);
if (StringUtils.isBlank(file)) {
throw new IllegalArgumentException("versions-file 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
* @return content of the file
* @throws IOException file not found, something went wrong trying to read the file
*/
public String readFileAsJson(String filePath) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
File file = new File(filePath);
return objectMapper.writeValueAsString(objectMapper.readTree(file));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"this" : "is for testing"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.junit.Test;

public class AtmireVersionsControllerIT extends AbstractControllerIntegrationTest {
private final ConfigurationService configurationService =
DSpaceServicesFactory.getInstance().getConfigurationService();

@Override
public void destroy() throws Exception {
configurationService.setProperty(AtmireVersionsController.ATMIRE_VERSIONS_CONFIG, null);
super.destroy();
}

@Test
public void test_unauthorisedUserReadsversioning() throws Exception {
// no token given to ensure the request is not authorised
getClient().perform(get("/api/atmire-versions"))
.andExpect(status().isUnauthorized());
}

@Test
public void test_adminReadsNotExistingFile() throws Exception {
configurationService.setProperty(AtmireVersionsController.ATMIRE_VERSIONS_CONFIG, "/something/wrong/");

String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(get("/api/atmire-versions"))
.andExpect(status().isNotFound());
}

@Test
public void test_adminReadsVersioningFile() throws Exception {
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\"", 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 c9aabc3

Please sign in to comment.