Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Build validators from a list of configuration locations
iay committed May 11, 2018
1 parent e6123a6 commit b33de98
Showing 8 changed files with 260 additions and 4 deletions.
@@ -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;
}

}
@@ -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,17 +44,22 @@ 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;

/**
* 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);
@@ -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);
}

}
@@ -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;
18 changes: 18 additions & 0 deletions 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;
3 changes: 3 additions & 0 deletions 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
15 changes: 15 additions & 0 deletions 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>
15 changes: 15 additions & 0 deletions 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>

0 comments on commit b33de98

Please sign in to comment.