This project has retired. For details please refer to its Attic page.
AbstractSimpleValidator xref
View Javadoc
1   /*
2    * Copyright 2014 The Apache Software Foundation.
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.apache.juddi.validation.vsv;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import org.apache.juddi.v3.error.ErrorMessage;
21  import org.apache.juddi.v3.error.InvalidValueException;
22  import org.uddi.api_v3.BindingTemplate;
23  import org.uddi.api_v3.BusinessEntity;
24  import org.uddi.api_v3.BusinessService;
25  import org.uddi.api_v3.KeyedReference;
26  import org.uddi.api_v3.KeyedReferenceGroup;
27  import org.uddi.api_v3.PublisherAssertion;
28  import org.uddi.api_v3.TModel;
29  import org.uddi.api_v3.TModelInstanceInfo;
30  import org.uddi.v3_service.DispositionReportFaultMessage;
31  
32  /**
33   * A simple base class for the validator interface that lets you define a simple
34   * set of allowed values. All other values will be rejected. Valid values apply
35   * to all UDDI elements
36   *
37   * @author Alex O'Ree
38   * @since 3.2.1
39   *
40   */
41  public abstract class AbstractSimpleValidator implements ValueSetValidator {
42  
43          public abstract String getMyKey();
44  
45          @Override
46          public void validateValuesBindingTemplate(List<BindingTemplate> items, String xpath) throws DispositionReportFaultMessage {
47                  if (items == null) {
48                          return;
49                  }
50                  for (int i = 0; i < items.size(); i++) {
51                          if (items.get(i).getCategoryBag() != null) {
52                                  validatedValuesKeyRef(items.get(i).getCategoryBag().getKeyedReference(), xpath + "bindingTemplate(" + i + ").categoryBag.");
53                                  validatedValuesKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), xpath + "bindingTemplate(" + i + ").categoryBag.");
54                          }
55                          if (items.get(i).getTModelInstanceDetails() != null) {
56  
57                                  validateTmodelInstanceDetails(items.get(i).getTModelInstanceDetails().getTModelInstanceInfo(), xpath + "bindingTemplate(" + i + ").tModelInstanceDetails.");
58                          }
59                  }
60          }
61  
62          @Override
63          public void validateValuesBusinessEntity(List<BusinessEntity> items) throws DispositionReportFaultMessage {
64                  if (items == null) {
65                          return;
66                  }
67                  for (int i = 0; i < items.size(); i++) {
68                          if (items.get(i).getCategoryBag() != null) {
69                                  validatedValuesKeyRef(items.get(i).getCategoryBag().getKeyedReference(), "businessEntity(" + i + ").categoryBag.");
70                                  validatedValuesKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), "businessEntity(" + i + ").categoryBagGroup.");
71                          }
72                          if (items.get(i).getIdentifierBag() != null) {
73                                  validatedValuesKeyRef(items.get(i).getIdentifierBag().getKeyedReference(), "businessEntity(" + i + ").identifierBag.");
74                          }
75                          if (items.get(i).getBusinessServices() != null) {
76                                  validateValuesBusinessService(items.get(i).getBusinessServices().getBusinessService(), "businessEntity(" + i + ").");
77                          }
78                  }
79          }
80  
81          @Override
82          public void validateValuesBusinessService(List<BusinessService> items, String xpath) throws DispositionReportFaultMessage {
83                  if (items == null) {
84                          return;
85                  }
86                  for (int i = 0; i < items.size(); i++) {
87                          if (items.get(i).getCategoryBag() != null) {
88                                  validatedValuesKeyRef(items.get(i).getCategoryBag().getKeyedReference(), xpath + "businessService(" + i + ").categoryBag.");
89                                  validatedValuesKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), xpath + "businessService(" + i + ").categoryBag.");
90                          }
91                          if (items.get(i).getBindingTemplates() != null) {
92                                  validateValuesBindingTemplate(items.get(i).getBindingTemplates().getBindingTemplate(), xpath + xpath + "businessService(" + i + ").");
93                          }
94                  }
95          }
96  
97          @Override
98          public void validateValuesPublisherAssertion(List<PublisherAssertion> items) throws DispositionReportFaultMessage {
99                  if (items == null) {
100                         return;
101                 }
102                 for (int i = 0; i < items.size(); i++) {
103                         if (items.get(i).getKeyedReference() != null) {
104                                 List<KeyedReference> temp = new ArrayList<KeyedReference>();
105                                 temp.add(items.get(i).getKeyedReference());
106                                 validatedValuesKeyRef(temp, "publisherAssertion(" + i + ").");
107                         }
108                 }
109         }
110 
111         private void validatedValuesKeyRef(List<KeyedReference> items, String xpath) throws DispositionReportFaultMessage {
112                 if (items == null) {
113                         return;
114                 }
115                 //StringBuilder badvalues=new StringBuilder();
116                 StringBuilder err = new StringBuilder();
117                 for (int i = 0; i < items.size(); i++) {
118                         if (items.get(i).getTModelKey().equalsIgnoreCase(getMyKey())) {
119                                 List<String> validValues = getValidValues();
120                                 if (validValues != null) {
121                                 //ok we have some work to do
122                                         //boolean valid = false;
123                                         boolean localfound = false;
124                                         for (int k = 0; k < validValues.size(); k++) {
125                                                 if (validValues.get(k).equals(items.get(i).getKeyValue())) {
126                                                         //           valid = true;
127                                                         localfound = true;
128                                                 }
129                                         }
130                                         if (!localfound) {
131                                                 //badvalues.append(items.get(i).getKeyValue()).append(" ");
132                                                 err.append(xpath).append("keyedReference(").append(i).append(")=").append(items.get(i).getKeyValue()).append(" ");
133                                         }
134                                 }
135                         }
136                 }
137                 if (err.length() > 0) {
138                         throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", err.toString() + getPrintableValidValues()));
139                 }
140         }
141 
142         private void validatedValuesKeyRefGrp(List<KeyedReferenceGroup> items, String xpath) throws DispositionReportFaultMessage {
143                 if (items == null) {
144                         return;
145                 }
146                 for (int i = 0; i < items.size(); i++) {
147                         validatedValuesKeyRef(items.get(i).getKeyedReference(), xpath + "keyReferenceGroup(" + i + ").");
148                 }
149         }
150 
151         @Override
152         public void validateTmodelInstanceDetails(List<TModelInstanceInfo> tModelInstanceInfo, String xpath) throws DispositionReportFaultMessage {
153 
154         }
155 
156         @Override
157         public abstract List<String> getValidValues();
158 
159         @Override
160         public void validateValuesTModel(List<TModel> items) throws DispositionReportFaultMessage {
161                 if (items == null) {
162                         return;
163                 }
164                 for (int i = 0; i < items.size(); i++) {
165                         if (items.get(i).getCategoryBag() != null) {
166                                 validatedValuesKeyRef(items.get(i).getCategoryBag().getKeyedReference(), "tModel(" + i + ").categoryBag.");
167                                 validatedValuesKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), "tModel(" + i + ").categoryBag.");
168                         }
169                         if (items.get(i).getIdentifierBag() != null) {
170                                 validatedValuesKeyRef(items.get(i).getIdentifierBag().getKeyedReference(), "tModel(" + i + ").identifierBag.");
171                         }
172                 }
173         }
174 
175         public static void validateKeyNotPresentKeyRef(List<KeyedReference> items, String key, String itemtype) throws DispositionReportFaultMessage {
176                 if (items == null) {
177                         return;
178                 }
179                 for (int i = 0; i < items.size(); i++) {
180                         validateKeyNotPresentKeyRef(items.get(i), key, itemtype);
181                 }
182         }
183 
184         public static void validateKeyNotPresentKeyRef(KeyedReference item, String key, String itemtype) throws DispositionReportFaultMessage {
185                 if (item == null) {
186                         return;
187                 }
188                 if (key.equalsIgnoreCase(item.getTModelKey())) {
189                         throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", "key " + key + " not allowed on " + itemtype));
190                 }
191         }
192 
193         public static void validateKeyNotPresentKeyRefGrp(List<KeyedReferenceGroup> items, String key, String itemtype) throws DispositionReportFaultMessage {
194                 if (items == null) {
195                         return;
196                 }
197                 for (int i = 0; i < items.size(); i++) {
198                         if (key.equalsIgnoreCase(items.get(i).getTModelKey())) {
199                                 throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", "key " + key + " not allowed on " + itemtype));
200                         }
201                         validateKeyNotPresentKeyRef(items.get(i).getKeyedReference(), key, itemtype);
202                 }
203         }
204 
205         private String getPrintableValidValues() {
206                 StringBuilder sb = new StringBuilder();
207                 sb.append(" Valid Values:[");
208                 List<String> validValues = getValidValues();
209                 for (int i = 0; i < validValues.size(); i++) {
210                         sb.append(validValues.get(i));
211                         if (i + 1 < validValues.size()) {
212                                 sb.append(",");
213                         }
214                 }
215                 sb.append("]");
216                 return sb.toString();
217         }
218 
219 }