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 String-based.
32   * @author Jens Riemschneider
33   */
34  public class StringValidation implements QueryParameterValidation {
35      private static final Pattern PATTERN = Pattern.compile("'.*'");
36  
37      private final QueryParameter param;
38      private final ValueValidator validator;
39      private final boolean optional;
40  
41      /**
42       * Constructs a validation object.
43       * @param param
44       *          parameter to validate.
45       * @param validator
46       *          validator to use on the parameter value.
47       * @param optional
48       *          <code>true</code> if this parameter is optional.
49       */
50      public StringValidation(QueryParameter param, ValueValidator validator, boolean optional) {
51          notNull(param, "param cannot be null");
52          notNull(validator, "validator cannot be null");
53          
54          this.param = param;
55          this.validator = validator;
56          this.optional = optional;
57      }
58  
59      @Override
60      public void validate(EbXMLAdhocQueryRequest request) throws XDSMetaDataException {
61          List<String> slotValues = request.getSlotValues(param.getSlotName());
62          metaDataAssert(optional || slotValues.size() >= 1, MISSING_REQUIRED_QUERY_PARAMETER, param);
63          metaDataAssert(optional || slotValues.size() == 1, TOO_MANY_VALUES_FOR_QUERY_PARAMETER, param);
64          
65          if (slotValues.size() > 0) {
66              String slotValue = slotValues.get(0);
67              metaDataAssert(slotValue != null || optional, MISSING_REQUIRED_QUERY_PARAMETER, param);
68              metaDataAssert(PATTERN.matcher(slotValue).matches(), PARAMETER_VALUE_NOT_STRING, param);
69          }
70          
71          QuerySlotHelper slots = new QuerySlotHelper(request);        
72          String value = slots.toString(param);
73          metaDataAssert(value != null || optional, MISSING_REQUIRED_QUERY_PARAMETER, param);
74          if (value != null) {
75              validator.validate(value);
76          }
77      }
78  }