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