View Javadoc
1   /*
2    * Copyright 2001-2008 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   */
17  package org.apache.juddi.api.impl;
18  
19  import java.util.ArrayList;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Set;
24  import javax.jws.WebService;
25  import javax.persistence.EntityManager;
26  import javax.persistence.EntityTransaction;
27  import static org.apache.juddi.api.impl.AuthenticatedService.logger;
28  import org.apache.juddi.api.util.QueryStatus;
29  import org.apache.juddi.api.util.ValueSetValidationQuery;
30  import org.apache.juddi.config.PersistenceManager;
31  import org.apache.juddi.model.Tmodel;
32  import org.apache.juddi.v3.client.UDDIConstants;
33  import org.apache.juddi.v3.client.UDDIConstantsV2;
34  import org.apache.juddi.v3.error.ErrorMessage;
35  import org.apache.juddi.v3.error.FatalErrorException;
36  import org.apache.juddi.v3.error.ValueNotAllowedException;
37  import org.apache.juddi.validation.vsv.AbstractSimpleValidator;
38  import org.apache.juddi.validation.vsv.ValueSetValidator;
39  import org.uddi.api_v3.BindingTemplate;
40  import org.uddi.api_v3.BusinessEntity;
41  import org.uddi.api_v3.BusinessService;
42  import org.uddi.api_v3.DispositionReport;
43  import org.uddi.api_v3.KeyedReference;
44  import org.uddi.api_v3.KeyedReferenceGroup;
45  import org.uddi.api_v3.PublisherAssertion;
46  import org.uddi.api_v3.Result;
47  import org.uddi.api_v3.TModel;
48  import org.uddi.api_v3.TModelInstanceInfo;
49  import org.uddi.v3_service.DispositionReportFaultMessage;
50  import org.uddi.v3_service.UDDIValueSetValidationPortType;
51  import org.uddi.vs_v3.ValidateValues;
52  
53  /**
54   * Implementation the UDDI v3 spec for Value Set Validation This is basically
55   * used to validate Keyed Reference value sets and offers validation via jUDDI's
56   * VSV extensibility framework.<Br><BR>
57   * To use this, define a tModel containing the following
58   * <pre>&lt;categoryBag&gt;
59   * &lt;keyedReference keyName=&quot;&quot;
60   * keyValue=&quot;uddi:juddi.apache.org:servicebindings-valueset-cp&quot;
61   * tModelKey=&quot;uddi:uddi.org:identifier:validatedby&quot;/&gt;
62   * &lt;/categoryBag&gt;
63   * </pre>Where uddi:juddi.apache.org:servicebindings-valueset-cp is the binding
64   * key of the service implementing the VSV API (this service).
65   * <Br><BR>
66   * From there, you need to create a class that either implements
67   * {@link ValueSetValidator} or extends {@link AbstractSimpleValidator}. It must
68   * be in the package named org.apache.juddi.validation.vsv and must by named
69   * following the convention outlined in {@link #convertKeyToClass(java.lang.String)
70   * }
71   *
72   * @see ValueSetValidator
73   * @see AbstractSimpleValidator
74   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
75   */
76  @WebService(serviceName="UDDIValueSetCachingService", endpointInterface = "org.uddi.v3_service.UDDIValueSetValidationPortType"
77         , targetNamespace = "urn:uddi-org:api_v3_portType")
78  public class UDDIValueSetValidationImpl extends AuthenticatedService implements
79       UDDIValueSetValidationPortType {
80  
81          private UDDIServiceCounter serviceCounter;
82  
83          public UDDIValueSetValidationImpl() {
84                  super();
85                  serviceCounter = ServiceCounterLifecycleResource.getServiceCounter(this.getClass());
86          }
87  
88          @Override
89          public DispositionReport validateValues(ValidateValues body)
90               throws DispositionReportFaultMessage {
91                  long startTime = System.currentTimeMillis();
92  
93                  if (body == null) {
94                          long procTime = System.currentTimeMillis() - startTime;
95                          serviceCounter.update(ValueSetValidationQuery.VALIDATE_VALUES,
96                               QueryStatus.FAILED, procTime);
97  
98                          throw new ValueNotAllowedException(new ErrorMessage("errors.valuesetvalidation.noinput"));
99                  }
100                 /*
101                  * The UDDI node that is calling validate_values MUST pass one 
102                  * or more businessEntity elements, one or more businessService 
103                  * elements, one or more bindingTemplate elements, one or more 
104                  * tModel elements, or one or more publisherAssertion elements 
105                  * as the sole argument to this Web service. 
106                  * */
107 
108 
109                 /* performs validation on all of the keyedReferences or keyedReferenceGroups */
110 
111                 /*when the entity being saved is a businessEntity, contained 
112                  * businessService and bindingTemplate entities may themselves 
113                  * reference values from the authorized value sets as well. */
114                 //go through all published items
115                 //pull out all keys
116                 //look up keys in database for a validation class
117                 //dedup results
118                 //run validation classes
119                 List<String> classNames = new ArrayList<String>();
120                 classNames.addAll(validateValuesBindingTemplate(body.getBindingTemplate()));
121                 classNames.addAll(validateValuesBusinessEntity(body.getBusinessEntity()));
122                 classNames.addAll(validateValuesBusinessService(body.getBusinessService()));
123                 classNames.addAll(validateValuesPublisherAssertion(body.getPublisherAssertion()));
124                 classNames.addAll(validateValuesTModel(body.getTModel()));
125                 Set<String> set = new HashSet<String>(classNames);
126                 Iterator<String> iterator = set.iterator();
127                 Set<String> validators = new HashSet<String>();
128                 EntityManager em = PersistenceManager.getEntityManager();
129                 EntityTransaction tx = em.getTransaction();
130                 //for each key to process
131                 try {
132                         while (iterator.hasNext()) {
133 
134                                 String key = iterator.next();
135                                 //find out if it needs to be validated
136                                 Tmodel find = em.find(org.apache.juddi.model.Tmodel.class, key);
137                                 if (find != null) {
138                                         //if it is, added it to the list
139                                         if (containsValidatedKey(find, UDDIConstants.IS_VALIDATED_BY)) {
140                                                 validators.add(key);
141                                         }
142                                         if (containsValidatedKey(find, UDDIConstantsV2.IS_VALIDATED_BY)) {
143                                                 validators.add(key);
144                                         }
145                                 }
146                         }
147 
148                 } catch (Exception drfm) {
149                         logger.warn("Unable to process vsv validation", drfm);
150                         throw new FatalErrorException(new ErrorMessage("errors.valuesetvalidation.fatal", drfm.getMessage()));
151                 } finally {
152                         if (tx.isActive()) {
153                                 tx.rollback();
154                         }
155                         em.close();
156                 }
157                 Iterator<String> iterator1 = validators.iterator();
158                 while (iterator1.hasNext()) {
159                         String tmodelkey = iterator1.next();
160                         String clazz = convertKeyToClass(tmodelkey);
161                         ValueSetValidator vsv;
162                         if (clazz == null) {
163                                 logger.info("No validator found for " + tmodelkey);
164                         } else {
165                                 try {
166                                         vsv = (ValueSetValidator) Class.forName(clazz).newInstance();
167                                         logger.info("translated " + tmodelkey + " to class " + clazz);
168                                         vsv.validateValuesBindingTemplate(body.getBindingTemplate(), "");
169                                         vsv.validateValuesBusinessEntity(body.getBusinessEntity());
170                                         vsv.validateValuesBusinessService(body.getBusinessService(), "");
171                                         vsv.validateValuesPublisherAssertion(body.getPublisherAssertion());
172                                         vsv.validateValuesTModel(body.getTModel());
173                                 } catch (ClassNotFoundException ex) {
174                                         logger.warn("Unable to process vsv validation for " + tmodelkey, ex);
175                                         throw new FatalErrorException(new ErrorMessage("errors.valuesetvalidation.fatal", "key=" + tmodelkey + " class=" + clazz + " " + ex.getMessage()));
176                                 } catch (InstantiationException ex) {
177                                         logger.warn("Unable to process vsv validation for " + tmodelkey, ex);
178                                         throw new FatalErrorException(new ErrorMessage("errors.valuesetvalidation.fatal", "key=" + tmodelkey + " class=" + clazz + " " + ex.getMessage()));
179                                 } catch (IllegalAccessException ex) {
180                                         logger.warn("Unable to process vsv validation for " + tmodelkey, ex);
181                                         throw new FatalErrorException(new ErrorMessage("errors.valuesetvalidation.fatal", "key=" + tmodelkey + " class=" + clazz + " " + ex.getMessage()));
182                                 }
183                         }
184                 }
185 
186                 DispositionReport r = new DispositionReport();
187                 r.getResult().add(new Result());
188                 long procTime = System.currentTimeMillis() - startTime;
189                 serviceCounter.update(ValueSetValidationQuery.VALIDATE_VALUES,
190                      QueryStatus.SUCCESS, procTime);
191 
192                 return r;
193         }
194 
195         private List<String> validateValuesBindingTemplate(List<BindingTemplate> items) {
196                 List<String> ret = new ArrayList<String>();
197                 if (items == null) {
198                         return ret;
199                 }
200                 for (int i = 0; i < items.size(); i++) {
201                         if (items.get(i).getCategoryBag() != null) {
202                                 ret.addAll(validateValuesKeyRef(items.get(i).getCategoryBag().getKeyedReference()));
203                                 ret.addAll(validateValuesKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup()));
204                         }
205                         if (items.get(i).getTModelInstanceDetails() != null) {
206 
207                                 //validateTmodelInstanceDetails(items.get(i).getTModelInstanceDetails().getTModelInstanceInfo(), xpath + "bindingTemplate(" + i + ").tModelInstanceDetails.");
208                         }
209                 }
210                 return ret;
211         }
212 
213         private List<String> validateValuesBusinessEntity(List<BusinessEntity> items) {
214                 List<String> ret = new ArrayList<String>();
215                 if (items == null) {
216                         return ret;
217                 }
218                 for (int i = 0; i < items.size(); i++) {
219                         if (items.get(i).getCategoryBag() != null) {
220                                 ret.addAll(validateValuesKeyRef(items.get(i).getCategoryBag().getKeyedReference()));
221                                 ret.addAll(validateValuesKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup()));
222                         }
223                         if (items.get(i).getIdentifierBag() != null) {
224                                 ret.addAll(validateValuesKeyRef(items.get(i).getIdentifierBag().getKeyedReference()));
225                         }
226                         if (items.get(i).getBusinessServices() != null) {
227                                 ret.addAll(validateValuesBusinessService(items.get(i).getBusinessServices().getBusinessService()));
228                         }
229                 }
230                 return ret;
231         }
232 
233         private List<String> validateValuesBusinessService(List<BusinessService> items) {
234                 List<String> ret = new ArrayList<String>();
235                 if (items == null) {
236                         return ret;
237                 }
238                 for (int i = 0; i < items.size(); i++) {
239                         if (items.get(i).getCategoryBag() != null) {
240                                 ret.addAll(validateValuesKeyRef(items.get(i).getCategoryBag().getKeyedReference()));
241                                 ret.addAll(validateValuesKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup()));
242                         }
243                         if (items.get(i).getBindingTemplates() != null) {
244                                 ret.addAll(validateValuesBindingTemplate(items.get(i).getBindingTemplates().getBindingTemplate()));
245                         }
246                 }
247                 return ret;
248         }
249 
250         private List<String> validateValuesPublisherAssertion(List<PublisherAssertion> items) {
251 
252                 List<String> ret = new ArrayList<String>();
253                 if (items == null) {
254                         return ret;
255                 }
256                 for (int i = 0; i < items.size(); i++) {
257                         if (items.get(i).getKeyedReference() != null) {
258                                 List<KeyedReference> temp = new ArrayList<KeyedReference>();
259                                 temp.add(items.get(i).getKeyedReference());
260                                 ret.addAll(validateValuesKeyRef(temp));
261                         }
262                 }
263                 return ret;
264         }
265 
266         private List<String> validateValuesTModel(List<TModel> items) {
267                 List<String> ret = new ArrayList<String>();
268                 if (items == null) {
269                         return ret;
270                 }
271                 for (int i = 0; i < items.size(); i++) {
272                         if (items.get(i).getCategoryBag() != null) {
273                                 ret.addAll(validateValuesKeyRef(items.get(i).getCategoryBag().getKeyedReference()));
274                                 ret.addAll(validateValuesKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup()));
275                         }
276                         if (items.get(i).getIdentifierBag() != null) {
277                                 ret.addAll(validateValuesKeyRef(items.get(i).getIdentifierBag().getKeyedReference()));
278                         }
279                 }
280                 return ret;
281         }
282 
283         private List<String> validateValuesKeyRef(List<KeyedReference> items) {
284                 List<String> ret = new ArrayList<String>();
285                 if (items == null) {
286                         return ret;
287                 }
288 
289                 for (int i = 0; i < items.size(); i++) {
290                         ret.add(items.get(i).getTModelKey());
291                 }
292 
293                 return ret;
294         }
295 
296         private List<String> validateValuesKeyRefGrp(List<KeyedReferenceGroup> items) {
297                 List<String> ret = new ArrayList<String>();
298                 if (items == null) {
299                         return ret;
300                 }
301                 for (int i = 0; i < items.size(); i++) {
302                         ret.addAll(validateValuesKeyRef(items.get(i).getKeyedReference()));
303                 }
304                 return ret;
305         }
306 
307         
308 
309         public static String convertKeyToClass(String tmodelkey) {
310 
311                 if (tmodelkey == null) {
312                         return null;
313                 }
314                 if (tmodelkey.length() < 2) {
315                         return null;
316                 }
317 
318                 String key = new String(new char[]{tmodelkey.charAt(0)}).toUpperCase() + tmodelkey.substring(1).toLowerCase();
319                 key = key.replaceAll("[^a-zA-Z0-9]", "");
320 
321                 String clazz = "org.apache.juddi.validation.vsv." + key;
322 
323                 return clazz;
324 
325         }
326 
327         public static List<String> getValidValues(String modelKey) {
328                 try {
329                         ValueSetValidator vsv = (ValueSetValidator) Class.forName(convertKeyToClass(modelKey)).newInstance();
330                         return vsv.getValidValues();
331                 } catch (ClassNotFoundException ex) {
332                 } catch (InstantiationException ex) {
333                 } catch (IllegalAccessException ex) {
334                 }
335                 return null;
336         }
337 
338         private boolean containsValidatedKey(Tmodel find, String key) {
339                 if (find.getCategoryBag() != null) {
340                         if (find.getCategoryBag().getKeyedReferences() != null) {
341                                 for (int i = 0; i < find.getCategoryBag().getKeyedReferences().size(); i++) {
342                                         if (key.equalsIgnoreCase(find.getCategoryBag().getKeyedReferences().get(i).getTmodelKeyRef())) {
343                                                 return true;
344                                         }
345                                 }
346                         }
347                         if (find.getCategoryBag().getKeyedReferenceGroups() != null) {
348                                 for (int i = 0; i < find.getCategoryBag().getKeyedReferenceGroups().size(); i++) {
349                                         for (int k = 0; k < find.getCategoryBag().getKeyedReferenceGroups().get(i).getKeyedReferences().size(); k++) {
350                                                 if (key.equalsIgnoreCase(find.getCategoryBag().getKeyedReferenceGroups().get(i).getKeyedReferences().get(k).getTmodelKeyRef())) {
351                                                         return true;
352                                                 }
353                                         }
354                                 }
355                         }
356                 }
357                 if (find.getTmodelIdentifiers() != null) {
358 
359                         for (int i = 0; i < find.getTmodelIdentifiers().size(); i++) {
360                                 if (key.equalsIgnoreCase(find.getTmodelIdentifiers().get(i).getTmodelKeyRef())) {
361                                         return true;
362                                 }
363                         }
364                 }
365 
366                 return false;
367         }
368 }