View Javadoc
1   /*
2    * Copyright 2013 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;
17  
18  import java.util.List;
19  import javax.xml.ws.BindingProvider;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.juddi.api.impl.UDDIValueSetValidationImpl;
23  import org.apache.juddi.config.Property;
24  import org.apache.juddi.v3.client.UDDIConstants;
25  import org.apache.juddi.v3.client.UDDIService;
26  import org.apache.juddi.v3.error.ErrorMessage;
27  import org.apache.juddi.v3.error.ValueNotAllowedException;
28  import org.uddi.api_v3.BindingTemplate;
29  import org.uddi.api_v3.BusinessEntity;
30  import org.uddi.api_v3.BusinessService;
31  import org.uddi.api_v3.PublisherAssertion;
32  import org.uddi.api_v3.TModel;
33  import org.uddi.v3_service.UDDIValueSetValidationPortType;
34  import org.uddi.vs_v3.ValidateValues;
35  
36  /**
37   * This class handles when this UDDI server receives some kind of SaveXXX
38   * Publish API request and the request uses a tModel KeyedReference that is both
39   * "checked" and has a {@link UDDIConstants#IS_VALIDATED_BY}  property. The value of the
40   * IS_VALIDATED_BY reference points to a BindingTemplate which should in turn
41   * point to a Web Service that handles validation requests for the tModel
42   * KeyedReferences.
43   *
44   * Short Story long, this class handles the external callout. It also handles
45   * classpath:/ URLs (which is currently just jUDDI's implementation of VSV.
46   * 
47   * @author Alex O'Ree
48   * @see UDDIConstants
49   * @since 3.3
50   */
51  public class ValidateValuesFromWebService {
52  
53          private static final Log log = LogFactory.getLog(ValidateValuesFromWebService.class);
54  
55          private static UDDIValueSetValidationPortType getPort(String url) throws ValueNotAllowedException {
56                  UDDIService svc = new UDDIService();
57                  UDDIValueSetValidationPortType vsv = svc.getUDDIValueSetValidationPort();
58  
59                  if (url == null || url.trim().length() == 0) {
60                          log.error("VSV Validation Failed: Calling Url is null");
61                          ValueNotAllowedException x = new ValueNotAllowedException(new ErrorMessage("errors.valuesetvalidation.invalidurl"));
62                          throw x;
63                  } else if (url.startsWith(Property.DEFAULT_BASE_URL)
64                          || url.startsWith(Property.DEFAULT_BASE_URL_SECURE)) {
65                          vsv = new UDDIValueSetValidationImpl();
66  
67                  } else if (url.startsWith("classpath:/")) {
68                          try {
69                                  String clz = url.substring(11);
70                                  Class<UDDIValueSetValidationPortType> forName = (Class<UDDIValueSetValidationPortType>) Class.forName(clz);
71                                  vsv = forName.newInstance();
72                          } catch (Exception ex) {
73                                  log.error("VSV Validation Failed: Cannot locate class from url " + url, ex);
74                          }
75                  } else if (url.toLowerCase().startsWith("http")){
76                          //external service, stick with 
77                          log.info("Calling External VSV Service");
78                          ((BindingProvider) vsv).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
79                  }
80                  else
81                  {
82                          log.warn("Unable to figure out how to use the URL " + url  + " as a Value Set Validation Service transport mechanism.");
83                          ValueNotAllowedException x = new ValueNotAllowedException(new ErrorMessage("errors.valuesetvalidation.invalidurl", url));
84                          throw x;
85                  }
86                  
87                  return vsv;
88          }
89  
90         
91  
92          public static void ValidateTModel(String url, List<TModel> obj) throws ValueNotAllowedException {
93                  UDDIValueSetValidationPortType vsv = getPort(url);
94                  ValidateValues req = new ValidateValues();
95                  req.getTModel().addAll(obj);
96  
97                  try {
98                          vsv.validateValues(req);
99                  } catch (Exception ex) {
100                         log.warn(ex.getMessage());
101                         log.debug(ex.getMessage(),ex);
102                         ValueNotAllowedException x = new ValueNotAllowedException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", ex.getMessage()));
103                         throw x;
104                 }
105         }
106         
107           public static void ValidateBinding(String url, List<BindingTemplate> obj) throws ValueNotAllowedException {
108                 UDDIValueSetValidationPortType vsv = getPort(url);
109                 ValidateValues req = new ValidateValues();
110                 req.getBindingTemplate().addAll(obj);
111 
112                 try {
113                         vsv.validateValues(req);
114                 } catch (Exception ex) {
115                         log.warn(ex.getMessage());
116                         log.debug(ex.getMessage(),ex);
117                         ValueNotAllowedException x = new ValueNotAllowedException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", ex.getMessage()));
118                         throw x;
119                 }
120         }
121 
122         public static void ValidateService(String url, List<BusinessService> obj) throws ValueNotAllowedException {
123                 UDDIValueSetValidationPortType vsv = getPort(url);
124                 ValidateValues req = new ValidateValues();
125                 req.getBusinessService().addAll(obj);
126 
127                 try {
128                         vsv.validateValues(req);
129                 } catch (Exception ex) {
130                         log.warn(ex.getMessage());
131                         log.debug(ex.getMessage(),ex);
132                         ValueNotAllowedException x = new ValueNotAllowedException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", ex.getMessage()));
133                         throw x;
134                 }
135         }
136         
137       
138         public static void ValidateBusiness(String url, List<BusinessEntity> obj) throws ValueNotAllowedException {
139                 UDDIValueSetValidationPortType vsv = getPort(url);
140                 ValidateValues req = new ValidateValues();
141                 req.getBusinessEntity().addAll(obj);
142 
143                 try {
144                         vsv.validateValues(req);
145                 } catch (Exception ex) {
146                         log.warn(ex.getMessage());
147                         log.debug(ex.getMessage(),ex);
148                         ValueNotAllowedException x = new ValueNotAllowedException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", ex.getMessage()));
149                         throw x;
150                 }
151         }
152 
153         public static void ValidatePubAss(String url, PublisherAssertion obj) throws ValueNotAllowedException {
154                 UDDIValueSetValidationPortType vsv = getPort(url);
155                 ValidateValues req = new ValidateValues();
156                 req.getPublisherAssertion().add(obj);
157                 try {
158                         vsv.validateValues(req);
159                 } catch (Exception ex) {
160                         log.warn(ex.getMessage());
161                         log.debug(ex.getMessage(),ex);
162                         ValueNotAllowedException x = new ValueNotAllowedException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", ex.getMessage()));
163                         throw x;
164                 }
165         }
166 }