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;
17  
18  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
19  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidatorAssertions.*;
20  
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Objects;
24  import java.util.Set;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectContainer;
28  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryObject;
29  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlot;
30  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlotList;
31  
32  import static org.openehealth.ipf.commons.ihe.xds.core.XdsJaxbDataBinding.isExtraMetadataSlotName;
33  
34  /**
35   * Validates lengths of ebXML slot values and uniqueness of slot names.
36   *
37   * @author Jens Riemschneider
38   * @author Dmytro Rud
39   */
40  public class SlotLengthAndNameUniquenessValidator {
41  
42      /**
43       * Validates slot lengths and name uniqueness
44       * for the given ebXML object container.
45       *
46       * @param container the container of ebXML objects.
47       * @throws XDSMetaDataException if a slot length validation failed.
48       */
49      public void validateContainer(EbXMLObjectContainer container) throws XDSMetaDataException {
50          validateRegistryObjects(container.getAssociations());
51          validateRegistryObjects(container.getExtrinsicObjects());
52          validateRegistryObjects(container.getRegistryPackages());
53          validateSlotLists(container.getClassifications());
54      }
55  
56      private void validateRegistryObjects(List<? extends EbXMLRegistryObject> regObjects) throws XDSMetaDataException {
57          validateSlotLists(regObjects);
58          for (EbXMLRegistryObject regObj : regObjects) {
59              validateSlotLists(regObj.getClassifications());
60          }
61      }
62  
63      private void validateSlotLists(List<? extends EbXMLSlotList> slotListContainers) throws XDSMetaDataException {
64          for (EbXMLSlotList slotList : slotListContainers) {
65              doValidateSlots(slotList.getSlots(), false, null);
66          }
67      }
68  
69      public void validateQuerySlots(
70              List<? extends EbXMLSlot> slots,
71              Set<String> allowedSlotNamesMultiple) throws XDSMetaDataException {
72          doValidateSlots(slots, true, allowedSlotNamesMultiple);
73      }
74  
75      /**
76       * Validates uniqueness of slot names and maximal lengths of slot values in the given collection.
77       *
78       * @param slots                    ebXML slot collection.
79       * @param queryMode                <code>true</code> iff the given slots represent parameters of a stored query.
80       * @param allowedSlotNamesMultiple names of slots which are allowed to be present more than once (only for queries).
81       * @throws XDSMetaDataException when the validation fails.
82       */
83      private void doValidateSlots(
84              List<? extends EbXMLSlot> slots,
85              boolean queryMode,
86              Set<String> allowedSlotNamesMultiple) throws XDSMetaDataException {
87          HashSet<String> names = new HashSet<>();
88          for (EbXMLSlot slot : slots) {
89              // validate format and uniqueness of slot names
90              String name = slot.getName();
91              metaDataAssert(StringUtils.isNotEmpty(name), MISSING_SLOT_NAME);
92  
93              if (queryMode) {
94                  metaDataAssert((name.length() > 1) && (name.charAt(0) == '$'), WRONG_QUERY_SLOT_NAME, name);
95                  metaDataAssert(names.add(name)
96                                  || allowedSlotNamesMultiple.contains(name)
97                                  || isExtraMetadataSlotName(name.substring(1)),
98                          DUPLICATE_SLOT_NAME, name);
99              } else {
100                 metaDataAssert(names.add(name), DUPLICATE_SLOT_NAME, name);
101             }
102 
103             // validate lengths of slot values
104             slot.getValueList().stream()
105                     .filter(Objects::nonNull)
106                     .forEach(value -> metaDataAssert(value.length() <= slot.getValueLengthLimit(), SLOT_VALUE_TOO_LONG, name));
107         }
108     }
109 
110 }