This project has retired. For details please refer to its Attic page.
WadlImport xref
View Javadoc
1   /*
2    * Copyright 2001-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   */
17  package org.apache.juddi.samples;
18  
19  import java.io.File;
20  import java.net.URL;
21  import java.util.List;
22  import java.util.Properties;
23  import java.util.Set;
24  
25  import javax.xml.namespace.QName;
26  
27  import org.apache.juddi.jaxb.PrintUDDI;
28  import org.apache.juddi.v3.client.config.UDDIClerk;
29  import org.apache.juddi.v3.client.config.UDDIClient;
30  import org.apache.juddi.v3.client.mapping.URLLocalizerDefaultImpl;
31  import org.apache.juddi.v3.client.mapping.wadl.Application;
32  import org.apache.juddi.v3.client.mapping.wadl.WADL2UDDI;
33  import org.apache.juddi.v3.client.transport.Transport;
34  import org.apache.juddi.v3_service.JUDDIApiPortType;
35  import org.uddi.api_v3.AuthToken;
36  import org.uddi.api_v3.BusinessDetail;
37  import org.uddi.api_v3.BusinessEntity;
38  import org.uddi.api_v3.BusinessService;
39  import org.uddi.api_v3.BusinessServices;
40  import org.uddi.api_v3.GetAuthToken;
41  import org.uddi.api_v3.Name;
42  import org.uddi.api_v3.SaveBusiness;
43  import org.uddi.api_v3.SaveService;
44  import org.uddi.api_v3.SaveTModel;
45  import org.uddi.api_v3.TModel;
46  import org.uddi.v3_service.UDDIPublicationPortType;
47  import org.uddi.v3_service.UDDISecurityPortType;
48  
49  /**
50   * This class shows how to perform a WSDL2UDDI import manually. More
51   * specifically, this is WSDL2UDDI without using annotations.
52   *
53   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
54   */
55  public class WadlImport {
56  
57          
58          private Properties properties = new Properties();
59  
60          private UDDISecurityPortType security = null;
61          private UDDIPublicationPortType publish = null;
62  
63          public void fire(String pathOrURL, String businessKey, String token, Transport transport) throws Exception {
64  
65                  if (transport == null) {
66                          // create a manager and read the config in the archive; 
67                          // you can use your config file name
68                          UDDIClient clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
69                          transport = clerkManager.getTransport();
70                  }
71                  // Now you create a reference to the UDDI API
72                  security = transport.getUDDISecurityService();
73                  publish = transport.getUDDIPublishService();
74  
75                  if (token == null) {
76                          //step one, get a token
77                          GetAuthToken getAuthTokenRoot = new GetAuthToken();
78                          getAuthTokenRoot.setUserID("uddi");
79                          getAuthTokenRoot.setCred("uddi");
80  
81                          // Making API call that retrieves the authentication token for the 'root' user.
82                          AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
83                          token = rootAuthToken.getAuthInfo();
84                  }
85  
86                  //step two, identify the key used for all your stuff
87                  //you must have a key generator created already
88                  //here, we are assuming that you don't have one
89                  //NOTE: these are some of the publicly available WSDLs that were used to test WSDL2UDDI
90                  //publish.saveTModel(stm);
91                  //step three, we have two options
92                  //1) import the wsdl's services into a brand new business
93                  //2) import the wsdl's services into an existing business
94                  //in either case, we're going to have to parse the WSDL
95                  //Application app = WADL2UDDI.parseWadl(new URL("http://server/wsdl.wsdl"), "username", "password", clerkManager.getClientConfig().isX_To_Wsdl_Ignore_SSL_Errors() );
96                  Application app = null;
97                  if (!pathOrURL.startsWith("http")) {
98                          File f = new File("test.wadl");
99                          if (!f.exists()) {
100                                 System.out.println(pathOrURL + " doesn't exist!");
101                                 return;
102                         } else {
103                                 System.out.println("Attempting to parse " + f.getAbsolutePath());
104                                 app = WADL2UDDI.parseWadl(f);
105                         }
106                 } else {
107                         app = WADL2UDDI.parseWadl(new URL(pathOrURL));
108                 }
109 
110                 List<URL> urls = WADL2UDDI.getBaseAddresses(app);
111                 URL url = urls.get(0);
112                 String domain = url.getHost();
113                 PrintUDDI<TModel> tmodelPrinter = new PrintUDDI<TModel>();
114                 TModel keygen = UDDIClerk.createKeyGenator("uddi:" + domain + ":keygenerator", domain, "en");
115 
116                 //save the keygen
117                 SaveTModel stm = new SaveTModel();
118                 stm.setAuthInfo(token);
119                 stm.getTModel().add(keygen);
120                 System.out.println("Saving the following tModel keygen");
121                 System.out.println(tmodelPrinter.print(keygen));
122                 publish.saveTModel(stm);
123 
124                 properties.put("keyDomain", domain);
125                 properties.put("businessName", domain);
126                 properties.put("serverName", url.getHost());
127                 properties.put("serverPort", url.getPort());
128                 //wsdlURL = wsdlDefinition.getDocumentBaseURI();
129                 WADL2UDDI wadl2UDDI = new WADL2UDDI(null, new URLLocalizerDefaultImpl(), properties);
130 
131                 BusinessService businessServices = wadl2UDDI.createBusinessService(new QName(domain, domain), app);
132 
133                 Set<TModel> portTypeTModels = wadl2UDDI.createWADLPortTypeTModels(pathOrURL, app);
134 
135                 // Set<TModel> createWSDLBindingTModels = wadl2UDDI.createWSDLBindingTModels(wsdlURL, allBindings);
136                 //When parsing a WSDL, there's really two things going on
137                 //1) convert a bunch of stuff (the portTypes) to tModels
138                 //2) convert the service definition to a BusinessService
139                 //Since the service depends on the tModel, we have to save the tModels first
140                 stm = new SaveTModel();
141                 stm.setAuthInfo(token);
142 
143                 TModel[] tmodels = portTypeTModels.toArray(new TModel[0]);
144                 for (int i = 0; i < tmodels.length; i++) {
145                         System.out.println(tmodelPrinter.print(tmodels[i]));
146                         stm.getTModel().add(tmodels[i]);
147                 }
148 
149                 tmodels = wadl2UDDI.createWADLTModels(pathOrURL, app).toArray(new TModel[0]);
150                 for (int i = 0; i < tmodels.length; i++) {
151                         System.out.println(tmodelPrinter.print(tmodels[i]));
152                         stm.getTModel().add(tmodels[i]);
153                 }
154                 //important, you'll need to save your new tModels, or else saving the business/service may fail
155                 System.out.println("Saving the following tModels " + stm.getTModel().size());
156                 publish.saveTModel(stm);
157 
158                 //finaly, we're ready to save all of the services defined in the WSDL
159                 //again, we're creating a new business, if you have one already, look it up using the Inquiry getBusinessDetails
160                 PrintUDDI<BusinessService> servicePrinter = new PrintUDDI<BusinessService>();
161 
162                 System.out.println("here's our new service: " + servicePrinter.print(businessServices));
163 
164                 if (businessKey == null || businessKey.length() == 0) {
165                         BusinessEntity be = new BusinessEntity();
166                         be.setBusinessKey(businessServices.getBusinessKey());
167                         be.getName().add(new Name());
168                         be.getName().get(0).setValue(domain);
169                         be.getName().get(0).setLang("en");
170                         be.setBusinessServices(new BusinessServices());
171                         be.getBusinessServices().getBusinessService().add(businessServices);
172                         SaveBusiness sb = new SaveBusiness();
173                         sb.setAuthInfo(token);
174                         sb.getBusinessEntity().add(be);
175                         BusinessDetail saveBusiness = publish.saveBusiness(sb);
176                         System.out.println("new business created, key = " + saveBusiness.getBusinessEntity().get(0).getBusinessKey());
177                 }
178                 SaveService ss = new SaveService();
179                 ss.setAuthInfo(token);
180                 businessServices.setBusinessKey(businessKey);
181                 ss.getBusinessService().add(businessServices);
182                 publish.saveService(ss);
183                 System.out.println("Saved! " + businessServices.getServiceKey());
184 
185                 //and we're done
186                 //Be sure to report any problems to the jUDDI JIRA bug tracker at 
187                 //https://issues.apache.org/jira/browse/JUDDI
188         }
189 
190         public static void main(String[] args) throws Exception {
191 
192                 new WadlImport().fire("http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.wadl", null, null, null);
193 
194         }
195 }