ATEST-124:

- implement the QueryValidator class
- adjust / fix tests
This commit is contained in:
Zellweger Christof Ralf
2015-07-31 10:17:11 +02:00
parent 1d07c618a9
commit 22c2adf7cc
6 changed files with 81 additions and 45 deletions
@@ -2,6 +2,7 @@ package ch.psi.daq.queryrest.config;
import java.util.EnumMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
@@ -14,8 +15,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.validation.Validator;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonFactory;
@@ -42,7 +46,7 @@ import ch.psi.daq.queryrest.response.ResponseStreamWriter;
@Configuration
@PropertySource(value = {"classpath:queryrest.properties"})
@PropertySource(value = {"file:${user.home}/.config/daq/queryrest.properties"}, ignoreResourceNotFound = true)
public class QueryRestConfig {
public class QueryRestConfig extends WebMvcConfigurerAdapter {
private static final String QUERYREST_DEFAULT_RESPONSE_AGGREGATIONS = "queryrest.default.response.aggregations";
@@ -65,6 +69,21 @@ public class QueryRestConfig {
@Resource
private Environment env;
/**
* {@inheritDoc}
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
/**
* This is necessary so that the message conversion uses the configured object mapper.
* Otherwise, a separate object mapper is instantiated for Springs message conversion.
*/
converter.setObjectMapper(objectMapper());
converters.add(converter);
super.configureMessageConverters(converters);
}
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
@@ -92,7 +111,7 @@ public class QueryRestConfig {
return mapper;
}
@Bean
public JsonFactory jsonFactory() {
return new JsonFactory();
@@ -14,6 +14,9 @@ import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -40,6 +43,9 @@ public class QueryRestController {
public static final String CHANNELS_REGEX = CHANNELS + "/{regex}";
public static final String QUERY = "query";
@Resource
private Validator queryValidator;
@Resource
private ResponseStreamWriter responseStreamWriter;
@@ -55,10 +61,19 @@ public class QueryRestController {
@Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS)
private Set<Aggregation> defaultResponseAggregations;
@InitBinder
protected void initBinder(WebDataBinder binder) {
/*
* This allows to use the @Valid annotation in the methods below.
*/
binder.addValidators(queryValidator);
}
@RequestMapping(
value = CHANNELS,
method = RequestMethod.GET,
produces = {MediaType.APPLICATION_JSON_VALUE})
consumes = {MediaType.APPLICATION_JSON_VALUE},
method = RequestMethod.GET)
public @ResponseBody Collection<String> getChannels() throws Throwable {
try {
return queryProcessor.getChannels();
@@ -71,8 +86,7 @@ public class QueryRestController {
@RequestMapping(
value = CHANNELS_REGEX,
method = RequestMethod.POST,
consumes = {MediaType.APPLICATION_JSON_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE})
consumes = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody Collection<String> getChannels(@RequestBody String regex) throws Throwable {
try {
return queryProcessor.getChannels(regex);
@@ -86,16 +100,13 @@ public class QueryRestController {
value = QUERY,
method = RequestMethod.POST,
consumes = {MediaType.APPLICATION_JSON_VALUE})
// , produces = {MediaType.APPLICATION_JSON_VALUE})
public void executeQuery(@RequestBody @Valid AbstractQuery query, HttpServletResponse res) throws IOException {
try {
LOGGER.debug("Execute query '{}'", query.getClass().getSimpleName());
LOGGER.debug("Executing query '{}'", query.getClass().getSimpleName());
QueryAnalyzer queryAnalizer = queryAnalizerFactory.apply(query);
queryAnalizer.validate();
// extendQuery(query);
// all the magic happens here
Stream<Entry<String, Stream<? extends DataEvent>>> channelToDataEvents = queryProcessor.process(queryAnalizer);
@@ -110,14 +121,6 @@ public class QueryRestController {
}
}
// private void extendQuery(AbstractQuery query) {
// if (query.getFields() == null || query.getFields().isEmpty()) {
// query.setFields(new LinkedHashSet<>(defaultResponseFields));
// }
// if(query.getAggregations() == null || query.getAggregations().isEmpty()){
// query.setAggregations(new LinkedList<>(defaultResponseAggregations));
// }
// }
// ==========================================================================================
// TODO: This is simply for initial / rudimentary testing - remove once further evolved