diff --git a/src/main/java/uk/org/iay/incommon/validator/ValidatorConfiguration.java b/src/main/java/uk/org/iay/incommon/validator/ValidatorConfiguration.java new file mode 100644 index 0000000..dbcc250 --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/validator/ValidatorConfiguration.java @@ -0,0 +1,56 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package uk.org.iay.incommon.validator; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import uk.org.iay.incommon.validator.context.ValidatorCollection; + +/** + * Validator configuration. + */ +@Configuration +public class ValidatorConfiguration { + + /** Class logger. */ + private static final Logger LOG = LoggerFactory.getLogger(ValidatorConfiguration.class); + + /** Property value telling us which validator configurations to load. */ + @Value("${validator.configurations}") + private String configurations; + + /** + * Build the validator configuration collection. + * + * @param webContext the web application context + * @return the {@link ValidatorCollection} bean + */ + @Bean + public ValidatorCollection validatorCollection(final ApplicationContext webContext) { + final ValidatorCollection c = new ValidatorCollection(webContext); + LOG.info("loading validator configurations: '{}'", configurations); + for (final String configLocation : configurations.split("\\s+")) { + c.add(configLocation); + } + return c; + } + +} diff --git a/src/main/java/uk/org/iay/incommon/validator/api/ValidatorsApiController.java b/src/main/java/uk/org/iay/incommon/validator/api/ValidatorsApiController.java index 16c268c..e3f2584 100644 --- a/src/main/java/uk/org/iay/incommon/validator/api/ValidatorsApiController.java +++ b/src/main/java/uk/org/iay/incommon/validator/api/ValidatorsApiController.java @@ -30,6 +30,7 @@ import org.springframework.web.bind.annotation.RequestBody; import io.swagger.annotations.ApiParam; +import uk.org.iay.incommon.validator.context.ValidatorCollection; import uk.org.iay.incommon.validator.models.Status; import uk.org.iay.incommon.validator.models.Status.StatusEnum; import uk.org.iay.incommon.validator.models.Validator; @@ -43,6 +44,9 @@ public class ValidatorsApiController implements ValidatorsApi { /** Class logger. */ private static final Logger LOG = LoggerFactory.getLogger(ValidatorsApiController.class); + /** Collection of all validators known to us. */ + private final ValidatorCollection validatorCollection; + /** Current {@link HttpServletRequest}. */ private final HttpServletRequest request; @@ -50,10 +54,12 @@ public class ValidatorsApiController implements ValidatorsApi { * Constructor. * * @param req current {@link HttpServletRequest}. + * @param valc {@link ValidatorCollection} */ @Autowired - public ValidatorsApiController(final HttpServletRequest req) { + public ValidatorsApiController(final HttpServletRequest req, final ValidatorCollection valc) { request = req; + validatorCollection = valc; } /** @@ -73,11 +79,13 @@ private Validator makeValidator(final String id, final String description) { @Override public ResponseEntity<List<Validator>> getValidators() { final String accept = request.getHeader("Accept"); + LOG.info("accept {}", accept); if (accept != null && accept.contains("application/json")) { final List<Validator> validators = new ArrayList<>(); - validators.add(makeValidator("default", "default validation pipeline")); - validators.add(makeValidator("eduGAIN", "eduGAIN validation pipeline")); - return new ResponseEntity<List<Validator>>(validators, HttpStatus.NOT_IMPLEMENTED); + for (final ValidatorCollection.Entry entry : validatorCollection.getEntries()) { + validators.add(makeValidator(entry.getId(), entry.getDescription())); + } + return new ResponseEntity<List<Validator>>(validators, HttpStatus.OK); } return new ResponseEntity<List<Validator>>(HttpStatus.NOT_IMPLEMENTED); diff --git a/src/main/java/uk/org/iay/incommon/validator/context/ValidatorCollection.java b/src/main/java/uk/org/iay/incommon/validator/context/ValidatorCollection.java new file mode 100644 index 0000000..7dd355b --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/validator/context/ValidatorCollection.java @@ -0,0 +1,123 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.org.iay.incommon.validator.context; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * Represents a collection of validators. + */ +public class ValidatorCollection { + + /** + * An entry representing a single available validator. + */ + public static class Entry { + + /** Identifier for the validator. */ + private final String id; + + /** Description for the validator. */ + private final String description; + + /** + * Constructor. + * + * @param ctx {@link ApplicationContext} containing the validator. + */ + public Entry(final ApplicationContext ctx) { + id = ctx.getBean("id", String.class); + description = ctx.getBean("description", String.class); + } + + /** + * Return the validator's identifier. + * + * @return the validator's identifier. + */ + public String getId() { + return id; + } + + /** + * Return the validator's description. + * + * @return the validator's description + */ + public String getDescription() { + return description; + } + + } + + /** Class logger. */ + private static final Logger LOG = LoggerFactory.getLogger(ValidatorCollection.class); + + /** Parent context to set on all the ones we load. */ + private final ApplicationContext parentContext; + + /** All entries as a list. */ + private final List<Entry> entries = new ArrayList<>(); + + /** All entries indexed by identifier. */ + private final Map<String, Entry> byId = new HashMap<>(); + + /** + * Constructor. + * + * @param ctx parent {@link ApplicationContext} for all validators + */ + public ValidatorCollection(final ApplicationContext ctx) { + parentContext = ctx; + } + + /** + * Return a {@link List} of all entries. + * + * @return {@link List} of all entries + */ + public List<Entry> getEntries() { + return entries; + } + + /** + * Build an {@link ApplicationContext} from the indicated configuration, + * and add it to the collection. + * + * @param configLocation classpath location for the {@link ApplicationContext}'s + * configuration + */ + public void add(final String configLocation) { + LOG.info("loading context {}", configLocation); + final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(); + ctx.setConfigLocation(configLocation); + ctx.setParent(parentContext); + ctx.setDisplayName(configLocation); + ctx.refresh(); + LOG.info("refreshed {} has {} beans", configLocation, ctx.getBeanDefinitionCount()); + final Entry entry = new Entry(ctx); + entries.add(entry); + byId.put(entry.getId(), entry); + } + +} diff --git a/src/main/java/uk/org/iay/incommon/validator/context/package-info.java b/src/main/java/uk/org/iay/incommon/validator/context/package-info.java new file mode 100644 index 0000000..27e6d07 --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/validator/context/package-info.java @@ -0,0 +1,18 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Context creation and manipulation for the metadata validator service. + */ +package uk.org.iay.incommon.validator.context; diff --git a/src/main/java/uk/org/iay/incommon/validator/package-info.java b/src/main/java/uk/org/iay/incommon/validator/package-info.java new file mode 100644 index 0000000..a6e6ac3 --- /dev/null +++ b/src/main/java/uk/org/iay/incommon/validator/package-info.java @@ -0,0 +1,18 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Metadata validator service. + */ +package uk.org.iay.incommon.validator; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 03ec22f..7b6c3fa 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,3 +3,6 @@ server.contextPath= server.port=8080 spring.jackson.date-format=uk.org.iay.incommon.validator.RFC3339DateFormat spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false + +# Space-separated list of the validator configurations to load. +validator.configurations = default-validator.xml second-validator.xml diff --git a/src/main/resources/default-validator.xml b/src/main/resources/default-validator.xml new file mode 100644 index 0000000..5a85ecd --- /dev/null +++ b/src/main/resources/default-validator.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" + default-lazy-init="true" + xmlns:c="http://www.springframework.org/schema/c" + xmlns:p="http://www.springframework.org/schema/p" + xmlns:util="http://www.springframework.org/schema/util" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> + + <bean id="id" class="java.lang.String" c:_="default"/> + <bean id="description" class="java.lang.String" c:_="Default validator."/> + +</beans> diff --git a/src/main/resources/second-validator.xml b/src/main/resources/second-validator.xml new file mode 100644 index 0000000..2efb5e9 --- /dev/null +++ b/src/main/resources/second-validator.xml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" + default-lazy-init="true" + xmlns:c="http://www.springframework.org/schema/c" + xmlns:p="http://www.springframework.org/schema/p" + xmlns:util="http://www.springframework.org/schema/util" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> + + <bean id="id" class="java.lang.String" c:_="second"/> + <bean id="description" class="java.lang.String" c:_="Second validator."/> + +</beans>