View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.xds.core.validate.query;
17  
18  import static org.apache.commons.lang3.Validate.notNull;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAdhocQueryRequest;
20  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter;
21  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.query.QuerySlotHelper;
22  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
23  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.metaDataAssert;
24  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValueValidator;
25  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
26  
27  import java.util.List;
28  import java.util.regex.Pattern;
29  
30  /**
31   * Query parameter validation for parameters that are StringList-based. 
32   * @author Jens Riemschneider
33   */
34  public class StringListValidation implements QueryParameterValidation {
35      private static final Pattern PATTERN =
36              Pattern.compile("\\(\\s*'.*'(\\s*,\\s*'.*')*\\s*\\)");
37  
38      private final QueryParameter param;
39      private final ValueValidator validator;
40  
41      /**
42       * Constructs a validation object.
43       * @param param
44       *          parameter to validate.
45       * @param validator
46       *          validator used on each of the string in the string list. 
47       */
48      public StringListValidation(QueryParameter param, ValueValidator validator) {
49          notNull(param, "param cannot be null");
50          notNull(validator, "validator cannot be null");
51          this.param = param;
52          this.validator = validator;
53      }
54  
55      @Override
56      public void validate(EbXMLAdhocQueryRequest request) throws XDSMetaDataException {
57          List<String> slotValues = request.getSlotValues(param.getSlotName());
58          for (String slotValue : slotValues) {
59              metaDataAssert(slotValue != null, MISSING_REQUIRED_QUERY_PARAMETER, param);
60              metaDataAssert(PATTERN.matcher(slotValue).matches(),
61                      PARAMETER_VALUE_NOT_STRING_LIST, param);
62          }
63  
64          QuerySlotHelper slots = new QuerySlotHelper(request);
65          List<String> list = slots.toStringList(param);
66          
67          if (list != null) {
68              for (String value : list) {
69                  metaDataAssert(value != null, INVALID_QUERY_PARAMETER_VALUE, param);
70                  validator.validate(value);
71              }
72          }
73      }
74  }