From d8bda5c721e1bedde28f95168d47f1b5d3b61fc2 Mon Sep 17 00:00:00 2001 From: Peter Nijs Date: Fri, 24 Apr 2020 17:41:54 +0200 Subject: [PATCH 1/5] 70471: Add configuration properties endpoint --- .../test/data/dspaceFolder/config/local.cfg | 9 ++++ .../app/rest/ConfigurationRestController.java | 46 +++++++++++++++++++ .../repository/ConfigurationRepository.java | 45 ++++++++++++++++++ .../config/spring/rest/configuration.xml | 15 ++++++ .../app/rest/ConfigurationControllerIT.java | 43 +++++++++++++++++ dspace/config/spring/rest/configuration.xml | 13 ++++++ 6 files changed, 171 insertions(+) create mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/ConfigurationRestController.java create mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRepository.java create mode 100644 dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/configuration.xml create mode 100644 dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationControllerIT.java create mode 100644 dspace/config/spring/rest/configuration.xml diff --git a/dspace-api/src/test/data/dspaceFolder/config/local.cfg b/dspace-api/src/test/data/dspaceFolder/config/local.cfg index 3c4b4a839dd1..4550b3d62628 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/local.cfg +++ b/dspace-api/src/test/data/dspaceFolder/config/local.cfg @@ -108,3 +108,12 @@ plugin.sequence.java.util.Collection = \ java.util.LinkedList, \ java.util.Stack, \ java.util.TreeSet + +########################################### +# PROPERTIES USED TO TEST CONFIGURATION # +# PROPERTY EXPOSURE VIA REST # +########################################### + +configuration.not.exposed = secret_value +configuration.exposed.single.value = public_value +configuration.exposed.array.value = public_value_1, public_value_2 diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/ConfigurationRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ConfigurationRestController.java new file mode 100644 index 000000000000..3e0c1434e6b9 --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ConfigurationRestController.java @@ -0,0 +1,46 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest; + +import org.dspace.app.rest.model.RestModel; +import org.dspace.app.rest.repository.ConfigurationRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * This will be the entry point for the api/config/properties endpoint + */ +@RestController +@RequestMapping("/api/" + RestModel.CONFIGURATION + "/properties") +public class ConfigurationRestController { + @Autowired + private ConfigurationRepository configurationRepository; + + /** + * This method gets a configuration property + * + * Example: + *
+     * {@code
+     * curl http:///api/config/properties/google.analytics.key
+     *  -XGET \
+     *  -H 'Authorization: Bearer eyJhbGciOiJI...'
+     * }
+     * 
+ * + * @param property The key of a configuration property + * @return The value of that property + */ + @RequestMapping(method = RequestMethod.GET, path = "/{property}") + public String[] getProperty(@PathVariable String property) { + return configurationRepository.getValue(property); + } +} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRepository.java new file mode 100644 index 000000000000..954cff3b4066 --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRepository.java @@ -0,0 +1,45 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest.repository; + +import java.util.ArrayList; +import javax.annotation.Resource; + +import org.dspace.app.rest.model.RestModel; +import org.dspace.services.ConfigurationService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.rest.webmvc.ResourceNotFoundException; +import org.springframework.stereotype.Component; + +/** + * This is the repository responsible of exposing configuration properties + */ +@Component(RestModel.CONFIGURATION + ".properties") +public class ConfigurationRepository { + @Autowired + private ConfigurationService configurationService; + + @Resource(name = "exposedConfigurationProperties") + private ArrayList exposedProperties; + + /** + * Gets the value of a configuration property if it is exposed via REST + * + * @param property + * @return + */ + public String[] getValue(String property) { + String[] propertyValues = configurationService.getArrayProperty(property); + + if (!exposedProperties.contains(property) || propertyValues.length == 0) { + throw new ResourceNotFoundException("No such configuration property" + property); + } + + return propertyValues; + } +} diff --git a/dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/configuration.xml b/dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/configuration.xml new file mode 100644 index 000000000000..78b1230af532 --- /dev/null +++ b/dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/configuration.xml @@ -0,0 +1,15 @@ + + + + + + + configuration.exposed.single.value + configuration.exposed.array.value + configuration.not.existing + + + + \ No newline at end of file diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationControllerIT.java new file mode 100644 index 000000000000..c364dbf701b5 --- /dev/null +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationControllerIT.java @@ -0,0 +1,43 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest; + +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.dspace.app.rest.test.AbstractControllerIntegrationTest; +import org.junit.Test; + +public class ConfigurationControllerIT extends AbstractControllerIntegrationTest { + @Test + public void getSingleValue() throws Exception { + getClient().perform(get("/api/config/properties/configuration.exposed.single.value")) + .andExpect(jsonPath("$[0]", is("public_value"))); + } + + @Test + public void getArrayValue() throws Exception { + getClient().perform(get("/api/config/properties/configuration.exposed.array.value")) + .andExpect(jsonPath("$[0]", is("public_value_1"))) + .andExpect(jsonPath("$[1]", is("public_value_2"))); + } + + @Test + public void getNonExistingValue() throws Exception { + getClient().perform(get("/api/config/properties/configuration.not.existing")) + .andExpect(status().isNotFound()); + } + + @Test + public void getNonExposedValue() throws Exception { + getClient().perform(get("/api/config/properties/configuration.not.exposed")) + .andExpect(status().isNotFound()); + } +} diff --git a/dspace/config/spring/rest/configuration.xml b/dspace/config/spring/rest/configuration.xml new file mode 100644 index 000000000000..460449109965 --- /dev/null +++ b/dspace/config/spring/rest/configuration.xml @@ -0,0 +1,13 @@ + + + + + + + google.analytics.key + + + + \ No newline at end of file From f85a5b3c4d8127ff8f9f622e51948171fef76d33 Mon Sep 17 00:00:00 2001 From: Peter Nijs Date: Mon, 4 May 2020 17:56:14 +0200 Subject: [PATCH 2/5] 70603: Make the configuration properties endpoint conform to the contract --- .../app/rest/ConfigurationRestController.java | 46 ----------- .../link/PropertyResourceHalLinkFactory.java | 34 ++++++++ .../dspace/app/rest/model/PropertyRest.java | 51 ++++++++++++ .../rest/model/hateoas/PropertyResource.java | 18 +++++ .../repository/ConfigurationRepository.java | 45 ----------- .../ConfigurationRestRepository.java | 78 +++++++++++++++++++ .../java/org/dspace/app/rest/utils/Utils.java | 4 + ...l => exposed-properties-configuration.xml} | 0 ...ava => ConfigurationRestRepositoryIT.java} | 20 ++++- ...l => exposed-properties-configuration.xml} | 0 10 files changed, 201 insertions(+), 95 deletions(-) delete mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/ConfigurationRestController.java create mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java create mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java create mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/PropertyResource.java delete mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRepository.java create mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java rename dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/{configuration.xml => exposed-properties-configuration.xml} (100%) rename dspace-server-webapp/src/test/java/org/dspace/app/rest/{ConfigurationControllerIT.java => ConfigurationRestRepositoryIT.java} (64%) rename dspace/config/spring/rest/{configuration.xml => exposed-properties-configuration.xml} (100%) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/ConfigurationRestController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/ConfigurationRestController.java deleted file mode 100644 index 3e0c1434e6b9..000000000000 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/ConfigurationRestController.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * The contents of this file are subject to the license and copyright - * detailed in the LICENSE and NOTICE files at the root of the source - * tree and available online at - * - * http://www.dspace.org/license/ - */ -package org.dspace.app.rest; - -import org.dspace.app.rest.model.RestModel; -import org.dspace.app.rest.repository.ConfigurationRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -/** - * This will be the entry point for the api/config/properties endpoint - */ -@RestController -@RequestMapping("/api/" + RestModel.CONFIGURATION + "/properties") -public class ConfigurationRestController { - @Autowired - private ConfigurationRepository configurationRepository; - - /** - * This method gets a configuration property - * - * Example: - *
-     * {@code
-     * curl http:///api/config/properties/google.analytics.key
-     *  -XGET \
-     *  -H 'Authorization: Bearer eyJhbGciOiJI...'
-     * }
-     * 
- * - * @param property The key of a configuration property - * @return The value of that property - */ - @RequestMapping(method = RequestMethod.GET, path = "/{property}") - public String[] getProperty(@PathVariable String property) { - return configurationRepository.getValue(property); - } -} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java new file mode 100644 index 000000000000..39af3eefcf87 --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java @@ -0,0 +1,34 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest.link; + +import java.util.LinkedList; + +import org.dspace.app.rest.RestResourceController; +import org.dspace.app.rest.model.hateoas.PropertyResource; +import org.springframework.data.domain.Pageable; +import org.springframework.hateoas.Link; +import org.springframework.stereotype.Component; + +@Component +public class PropertyResourceHalLinkFactory extends HalLinkFactory { + @Override + protected void addLinks(PropertyResource halResource, Pageable pageable, LinkedList list) throws Exception { + halResource.removeLinks(); + } + + @Override + protected Class getControllerClass() { + return RestResourceController.class; + } + + @Override + protected Class getResourceClass() { + return PropertyResource.class; + } +} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java new file mode 100644 index 000000000000..6cc2e02eaadb --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java @@ -0,0 +1,51 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest.model; + +import java.util.List; + +import org.dspace.app.rest.RestResourceController; + +public class PropertyRest extends RestAddressableModel { + public static final String NAME = "property"; + public static final String CATEGORY = RestAddressableModel.CONFIGURATION; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getValues() { + return values; + } + + public void setValues(List values) { + this.values = values; + } + + public String name; + public List values; + + @Override + public String getCategory() { + return CATEGORY; + } + + @Override + public Class getController() { + return RestResourceController.class; + } + + @Override + public String getType() { + return NAME; + } +} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/PropertyResource.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/PropertyResource.java new file mode 100644 index 000000000000..1aa1007c52ec --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/PropertyResource.java @@ -0,0 +1,18 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest.model.hateoas; + +import org.dspace.app.rest.model.PropertyRest; +import org.dspace.app.rest.utils.Utils; + +public class PropertyResource extends DSpaceResource { + + public PropertyResource(PropertyRest data, Utils utils) { + super(data, utils); + } +} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRepository.java deleted file mode 100644 index 954cff3b4066..000000000000 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRepository.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * The contents of this file are subject to the license and copyright - * detailed in the LICENSE and NOTICE files at the root of the source - * tree and available online at - * - * http://www.dspace.org/license/ - */ -package org.dspace.app.rest.repository; - -import java.util.ArrayList; -import javax.annotation.Resource; - -import org.dspace.app.rest.model.RestModel; -import org.dspace.services.ConfigurationService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.rest.webmvc.ResourceNotFoundException; -import org.springframework.stereotype.Component; - -/** - * This is the repository responsible of exposing configuration properties - */ -@Component(RestModel.CONFIGURATION + ".properties") -public class ConfigurationRepository { - @Autowired - private ConfigurationService configurationService; - - @Resource(name = "exposedConfigurationProperties") - private ArrayList exposedProperties; - - /** - * Gets the value of a configuration property if it is exposed via REST - * - * @param property - * @return - */ - public String[] getValue(String property) { - String[] propertyValues = configurationService.getArrayProperty(property); - - if (!exposedProperties.contains(property) || propertyValues.length == 0) { - throw new ResourceNotFoundException("No such configuration property" + property); - } - - return propertyValues; - } -} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java new file mode 100644 index 000000000000..4fc1d48c6fe7 --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java @@ -0,0 +1,78 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest.repository; + +import java.util.ArrayList; +import java.util.Arrays; +import javax.annotation.Resource; + +import org.dspace.app.rest.exception.RepositoryMethodNotImplementedException; +import org.dspace.app.rest.model.PropertyRest; +import org.dspace.core.Context; +import org.dspace.services.ConfigurationService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.rest.webmvc.ResourceNotFoundException; +import org.springframework.stereotype.Component; + +/** + * This is the repository responsible of exposing configuration properties + */ +@Component(PropertyRest.CATEGORY + "." + PropertyRest.NAME) +public class ConfigurationRestRepository extends DSpaceRestRepository { + @Autowired + private ConfigurationService configurationService; + + @Resource(name = "exposedConfigurationProperties") + private ArrayList exposedProperties; + + /** + * Gets the value of a configuration property if it is exposed via REST + * + * Example: + *
+     * {@code
+     * curl http:///api/config/properties/google.analytics.key
+     *  -XGET \
+     *  -H 'Authorization: Bearer eyJhbGciOiJI...'
+     * }
+     * 
+ * + * @param property + * @return + */ + @Override + public PropertyRest findOne(Context context, String property) { + if (!exposedProperties.contains(property)) { + throw new ResourceNotFoundException("No such configuration property" + property); + } + + String[] propertyValues = configurationService.getArrayProperty(property); + + if (propertyValues.length == 0) { + throw new ResourceNotFoundException("No such configuration property" + property); + } + + PropertyRest propertyRest = new PropertyRest(); + propertyRest.setName(property); + propertyRest.setValues(Arrays.asList(propertyValues)); + + return propertyRest; + } + + @Override + public Page findAll(Context context, Pageable pageable) { + throw new RepositoryMethodNotImplementedException("No implementation found; Method not allowed!", ""); + } + + @Override + public Class getDomainClass() { + return PropertyRest.class; + } +} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/Utils.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/Utils.java index f241ba3b30e8..034f9e2f55eb 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/Utils.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/utils/Utils.java @@ -53,6 +53,7 @@ import org.dspace.app.rest.model.LinkRest; import org.dspace.app.rest.model.LinksRest; import org.dspace.app.rest.model.ProcessRest; +import org.dspace.app.rest.model.PropertyRest; import org.dspace.app.rest.model.ResourcePolicyRest; import org.dspace.app.rest.model.RestAddressableModel; import org.dspace.app.rest.model.RestModel; @@ -232,6 +233,9 @@ public static String makeSingular(String modelPlural) { if (StringUtils.equals(modelPlural, "versionhistories")) { return VersionHistoryRest.NAME; } + if (StringUtils.equals(modelPlural, "properties")) { + return PropertyRest.NAME; + } return modelPlural.replaceAll("s$", ""); } diff --git a/dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/configuration.xml b/dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/exposed-properties-configuration.xml similarity index 100% rename from dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/configuration.xml rename to dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/exposed-properties-configuration.xml diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationRestRepositoryIT.java similarity index 64% rename from dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationControllerIT.java rename to dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationRestRepositoryIT.java index c364dbf701b5..f1ecc474b489 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationRestRepositoryIT.java @@ -15,18 +15,24 @@ import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.junit.Test; -public class ConfigurationControllerIT extends AbstractControllerIntegrationTest { +/** + * Integration Tests against the /api/config/properties/[property] endpoint + */ +public class ConfigurationRestRepositoryIT extends AbstractControllerIntegrationTest { @Test public void getSingleValue() throws Exception { getClient().perform(get("/api/config/properties/configuration.exposed.single.value")) - .andExpect(jsonPath("$[0]", is("public_value"))); + .andExpect(jsonPath("$.values[0]", is("public_value"))) + .andExpect(jsonPath("$.type", is("property"))) + .andExpect(jsonPath("$.name", is("configuration.exposed.single.value"))) + .andExpect(jsonPath("$._links").doesNotExist()); } @Test public void getArrayValue() throws Exception { getClient().perform(get("/api/config/properties/configuration.exposed.array.value")) - .andExpect(jsonPath("$[0]", is("public_value_1"))) - .andExpect(jsonPath("$[1]", is("public_value_2"))); + .andExpect(jsonPath("$.values[0]", is("public_value_1"))) + .andExpect(jsonPath("$.values[1]", is("public_value_2"))); } @Test @@ -40,4 +46,10 @@ public void getNonExposedValue() throws Exception { getClient().perform(get("/api/config/properties/configuration.not.exposed")) .andExpect(status().isNotFound()); } + + @Test + public void getAll() throws Exception { + getClient().perform(get("/api/config/properties/")) + .andExpect(status().isMethodNotAllowed()); + } } diff --git a/dspace/config/spring/rest/configuration.xml b/dspace/config/spring/rest/exposed-properties-configuration.xml similarity index 100% rename from dspace/config/spring/rest/configuration.xml rename to dspace/config/spring/rest/exposed-properties-configuration.xml From 7fcb782230bb5b71a799322e23998d427aac5419 Mon Sep 17 00:00:00 2001 From: Peter Nijs Date: Mon, 11 May 2020 12:56:27 +0200 Subject: [PATCH 3/5] 70824: Configuration properties endpoint feedback --- .../link/PropertyResourceHalLinkFactory.java | 3 +++ .../dspace/app/rest/model/PropertyRest.java | 4 ++++ .../rest/model/hateoas/PropertyResource.java | 3 +++ .../ConfigurationRestRepository.java | 10 +++------- .../rest/exposed-properties-configuration.xml | 19 +++++++++---------- .../rest/exposed-properties-configuration.xml | 15 +++++++-------- 6 files changed, 29 insertions(+), 25 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java index 39af3eefcf87..98d06179d24b 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java @@ -15,6 +15,9 @@ import org.springframework.hateoas.Link; import org.springframework.stereotype.Component; +/** + * This class' purpose is to remove all links from the PropertyResource. + */ @Component public class PropertyResourceHalLinkFactory extends HalLinkFactory { @Override diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java index 6cc2e02eaadb..a6b6fa73afaf 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java @@ -11,6 +11,10 @@ import org.dspace.app.rest.RestResourceController; +/** + * This class acts as the REST representation of a DSpace configuration property. + * This class acts as a data holder for the PropertyResource + */ public class PropertyRest extends RestAddressableModel { public static final String NAME = "property"; public static final String CATEGORY = RestAddressableModel.CONFIGURATION; diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/PropertyResource.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/PropertyResource.java index 1aa1007c52ec..96df54ce9a41 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/PropertyResource.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/hateoas/PropertyResource.java @@ -10,6 +10,9 @@ import org.dspace.app.rest.model.PropertyRest; import org.dspace.app.rest.utils.Utils; +/** + * The purpose of this class is to wrap the information of the PropertyRest into a HAL resource + */ public class PropertyResource extends DSpaceResource { public PropertyResource(PropertyRest data, Utils utils) { diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java index 4fc1d48c6fe7..1b35d07794a9 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java @@ -49,16 +49,12 @@ public class ConfigurationRestRepository extends DSpaceRestRepository findAll(Context context, Pageable pageable) { - throw new RepositoryMethodNotImplementedException("No implementation found; Method not allowed!", ""); + throw new RepositoryMethodNotImplementedException("No implementation found; Method not allowed", ""); } @Override diff --git a/dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/exposed-properties-configuration.xml b/dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/exposed-properties-configuration.xml index 78b1230af532..38e1f241f720 100644 --- a/dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/exposed-properties-configuration.xml +++ b/dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/exposed-properties-configuration.xml @@ -1,15 +1,14 @@ - - - - configuration.exposed.single.value - configuration.exposed.array.value - configuration.not.existing - - - + + + configuration.exposed.single.value + configuration.exposed.array.value + configuration.not.existing + \ No newline at end of file diff --git a/dspace/config/spring/rest/exposed-properties-configuration.xml b/dspace/config/spring/rest/exposed-properties-configuration.xml index 460449109965..159d2ed8a37e 100644 --- a/dspace/config/spring/rest/exposed-properties-configuration.xml +++ b/dspace/config/spring/rest/exposed-properties-configuration.xml @@ -1,13 +1,12 @@ - - - - google.analytics.key - - - + + + google.analytics.key + \ No newline at end of file From 4e61be497ee9981dbd7027283939061fc9ce895c Mon Sep 17 00:00:00 2001 From: Peter Nijs Date: Thu, 4 Jun 2020 14:04:41 +0200 Subject: [PATCH 4/5] 71220: Configuration properties endpoint - PR feedback --- .../test/data/dspaceFolder/config/local.cfg | 3 ++ .../link/PropertyResourceHalLinkFactory.java | 37 ------------------- .../dspace/app/rest/model/PropertyRest.java | 10 ++++- .../ConfigurationRestRepository.java | 15 +++++--- .../rest/exposed-properties-configuration.xml | 14 ------- .../rest/ConfigurationRestRepositoryIT.java | 2 +- dspace/config/modules/rest.cfg | 6 +++ .../rest/exposed-properties-configuration.xml | 12 ------ 8 files changed, 28 insertions(+), 71 deletions(-) delete mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java delete mode 100644 dspace-server-webapp/src/test/data/dspaceFolder/config/spring/rest/exposed-properties-configuration.xml delete mode 100644 dspace/config/spring/rest/exposed-properties-configuration.xml diff --git a/dspace-api/src/test/data/dspaceFolder/config/local.cfg b/dspace-api/src/test/data/dspaceFolder/config/local.cfg index 4550b3d62628..51ce1a016521 100644 --- a/dspace-api/src/test/data/dspaceFolder/config/local.cfg +++ b/dspace-api/src/test/data/dspaceFolder/config/local.cfg @@ -113,6 +113,9 @@ plugin.sequence.java.util.Collection = \ # PROPERTIES USED TO TEST CONFIGURATION # # PROPERTY EXPOSURE VIA REST # ########################################### +rest.properties.exposed = configuration.exposed.single.value +rest.properties.exposed = configuration.exposed.array.value +rest.properties.exposed = configuration.not.existing configuration.not.exposed = secret_value configuration.exposed.single.value = public_value diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java deleted file mode 100644 index 98d06179d24b..000000000000 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/link/PropertyResourceHalLinkFactory.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * The contents of this file are subject to the license and copyright - * detailed in the LICENSE and NOTICE files at the root of the source - * tree and available online at - * - * http://www.dspace.org/license/ - */ -package org.dspace.app.rest.link; - -import java.util.LinkedList; - -import org.dspace.app.rest.RestResourceController; -import org.dspace.app.rest.model.hateoas.PropertyResource; -import org.springframework.data.domain.Pageable; -import org.springframework.hateoas.Link; -import org.springframework.stereotype.Component; - -/** - * This class' purpose is to remove all links from the PropertyResource. - */ -@Component -public class PropertyResourceHalLinkFactory extends HalLinkFactory { - @Override - protected void addLinks(PropertyResource halResource, Pageable pageable, LinkedList list) throws Exception { - halResource.removeLinks(); - } - - @Override - protected Class getControllerClass() { - return RestResourceController.class; - } - - @Override - protected Class getResourceClass() { - return PropertyResource.class; - } -} diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java index a6b6fa73afaf..ada5f5db2fe6 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java @@ -9,13 +9,15 @@ import java.util.List; +import com.fasterxml.jackson.annotation.JsonIgnore; import org.dspace.app.rest.RestResourceController; +import org.springframework.hateoas.Identifiable; /** * This class acts as the REST representation of a DSpace configuration property. * This class acts as a data holder for the PropertyResource */ -public class PropertyRest extends RestAddressableModel { +public class PropertyRest extends RestAddressableModel implements Identifiable { public static final String NAME = "property"; public static final String CATEGORY = RestAddressableModel.CONFIGURATION; @@ -38,6 +40,12 @@ public void setValues(List values) { public String name; public List values; + @Override + @JsonIgnore + public String getId() { + return this.name; + } + @Override public String getCategory() { return CATEGORY; diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java index 1b35d07794a9..4a696bb664a4 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java @@ -7,9 +7,8 @@ */ package org.dspace.app.rest.repository; -import java.util.ArrayList; import java.util.Arrays; -import javax.annotation.Resource; +import java.util.List; import org.dspace.app.rest.exception.RepositoryMethodNotImplementedException; import org.dspace.app.rest.model.PropertyRest; @@ -26,11 +25,15 @@ */ @Component(PropertyRest.CATEGORY + "." + PropertyRest.NAME) public class ConfigurationRestRepository extends DSpaceRestRepository { - @Autowired + private ConfigurationService configurationService; + private List exposedProperties; - @Resource(name = "exposedConfigurationProperties") - private ArrayList exposedProperties; + @Autowired + public ConfigurationRestRepository(ConfigurationService configurationService) { + this.configurationService = configurationService; + this.exposedProperties = Arrays.asList(configurationService.getArrayProperty("rest.properties.exposed")); + } /** * Gets the value of a configuration property if it is exposed via REST @@ -49,7 +52,7 @@ public class ConfigurationRestRepository extends DSpaceRestRepository - - - - - configuration.exposed.single.value - configuration.exposed.array.value - configuration.not.existing - - \ No newline at end of file diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationRestRepositoryIT.java index f1ecc474b489..1eab1ef68eb7 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ConfigurationRestRepositoryIT.java @@ -25,7 +25,7 @@ public void getSingleValue() throws Exception { .andExpect(jsonPath("$.values[0]", is("public_value"))) .andExpect(jsonPath("$.type", is("property"))) .andExpect(jsonPath("$.name", is("configuration.exposed.single.value"))) - .andExpect(jsonPath("$._links").doesNotExist()); + .andExpect(jsonPath("$._links.self.href", is("http://localhost/api/config/properties/configuration.exposed.single.value"))); } @Test diff --git a/dspace/config/modules/rest.cfg b/dspace/config/modules/rest.cfg index 2e13d73f9730..29e5c1d77e64 100644 --- a/dspace/config/modules/rest.cfg +++ b/dspace/config/modules/rest.cfg @@ -152,3 +152,9 @@ rest.report-regex-non-ascii = ^.*[^\\p{ASCII}].*$ # The maximum number of results to return for 1 request rest.search.max.results = 100 + +# Define which configuration properties are exposed through the http:///api/config/properties/ +# rest endpoint. If a rest request is made for a property which exists, but isn't listed here, the server will +# respond that the property wasn't found. This property can be defined multiple times to allow access to multiple +# configuration properties. +rest.properties.exposed = google.analytics.key diff --git a/dspace/config/spring/rest/exposed-properties-configuration.xml b/dspace/config/spring/rest/exposed-properties-configuration.xml deleted file mode 100644 index 159d2ed8a37e..000000000000 --- a/dspace/config/spring/rest/exposed-properties-configuration.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - google.analytics.key - - \ No newline at end of file From a7aeb102d4d714c26d9d4a138088cc40f1df1bef Mon Sep 17 00:00:00 2001 From: Peter Nijs Date: Fri, 5 Jun 2020 17:28:24 +0200 Subject: [PATCH 5/5] 71220: Configuration properties endpoint - PR feedback - fixes after merging with latest master --- .../src/main/java/org/dspace/app/rest/model/PropertyRest.java | 3 +-- .../app/rest/repository/ConfigurationRestRepository.java | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java index ada5f5db2fe6..365a6790192b 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/PropertyRest.java @@ -11,13 +11,12 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import org.dspace.app.rest.RestResourceController; -import org.springframework.hateoas.Identifiable; /** * This class acts as the REST representation of a DSpace configuration property. * This class acts as a data holder for the PropertyResource */ -public class PropertyRest extends RestAddressableModel implements Identifiable { +public class PropertyRest extends BaseObjectRest { public static final String NAME = "property"; public static final String CATEGORY = RestAddressableModel.CONFIGURATION; diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java index 4a696bb664a4..caadb9f6f34e 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ConfigurationRestRepository.java @@ -18,6 +18,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.rest.webmvc.ResourceNotFoundException; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Component; /** @@ -51,6 +52,7 @@ public ConfigurationRestRepository(ConfigurationService configurationService) { * @return */ @Override + @PreAuthorize("permitAll()") public PropertyRest findOne(Context context, String property) { if (!exposedProperties.contains(property) || !configurationService.hasProperty(property)) { throw new ResourceNotFoundException("No such configuration property: " + property);