forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'w2p-114022_expose-feature-versions-in-the-REST-API' int…
…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
Showing
4 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
dspace-server-webapp/src/main/java/org/dspace/app/rest/AtmireVersionsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
|
||
} |
3 changes: 3 additions & 0 deletions
3
dspace-server-webapp/src/test/data/dspaceFolder/atmire-versions.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"this" : "is for testing" | ||
} |
53 changes: 53 additions & 0 deletions
53
dspace-server-webapp/src/test/java/org/dspace/app/rest/AtmireVersionsControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters