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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 *16 */17package org.apache.juddi.v3.client.cli;
1819import java.io.File;
20import java.net.URL;
21import java.util.List;
22import java.util.Properties;
23import java.util.Set;
2425import javax.xml.namespace.QName;
2627import org.apache.juddi.jaxb.PrintUDDI;
28import org.apache.juddi.v3.client.config.UDDIClerk;
29import org.apache.juddi.v3.client.config.UDDIClient;
30import org.apache.juddi.v3.client.mapping.URLLocalizerDefaultImpl;
31import org.apache.juddi.v3.client.mapping.wadl.Application;
32import org.apache.juddi.v3.client.mapping.wadl.WADL2UDDI;
33import org.apache.juddi.v3.client.transport.Transport;
34import org.apache.juddi.v3_service.JUDDIApiPortType;
35import org.uddi.api_v3.AuthToken;
36import org.uddi.api_v3.BusinessDetail;
37import org.uddi.api_v3.BusinessEntity;
38import org.uddi.api_v3.BusinessService;
39import org.uddi.api_v3.BusinessServices;
40import org.uddi.api_v3.GetAuthToken;
41import org.uddi.api_v3.Name;
42import org.uddi.api_v3.SaveBusiness;
43import org.uddi.api_v3.SaveService;
44import org.uddi.api_v3.SaveTModel;
45import org.uddi.api_v3.TModel;
46import org.uddi.v3_service.UDDIPublicationPortType;
47import org.uddi.v3_service.UDDISecurityPortType;
4849/**50 * This class shows how to perform a WSDL2UDDI import manually. More51 * specifically, this is WSDL2UDDI without using annotations.52 *53 * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>54 */55publicclassWadlImport {
5657private Properties properties = new Properties();
58privateUDDISecurityPortType security = null;
59privateUDDIPublicationPortType publish = null;
6061publicvoid fire(String pathOrURL, String businessKey, String token, Transport transport) throws Exception {
6263if (transport == null) {
64// create a manager and read the config in the archive; 65// you can use your config file name66UDDIClient clerkManager = newUDDIClient("META-INF/simple-publish-uddi.xml");
67 transport = clerkManager.getTransport();
68 }
69// Now you create a reference to the UDDI API70 security = transport.getUDDISecurityService();
71 publish = transport.getUDDIPublishService();
7273if (token == null) {
74//step one, get a token75GetAuthToken getAuthTokenRoot = newGetAuthToken();
76 getAuthTokenRoot.setUserID("uddi");
77 getAuthTokenRoot.setCred("uddi");
7879// Making API call that retrieves the authentication token for the 'root' user.80AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
81 token = rootAuthToken.getAuthInfo();
82 }
8384//step two, identify the key used for all your stuff85//you must have a key generator created already86//here, we are assuming that you don't have one87//NOTE: these are some of the publicly available WSDLs that were used to test WSDL2UDDI88//publish.saveTModel(stm);89//step three, we have two options90//1) import the wsdl's services into a brand new business91//2) import the wsdl's services into an existing business92//in either case, we're going to have to parse the WSDL93//Application app = WADL2UDDI.parseWadl(new URL("http://server/wsdl.wsdl"), "username", "password", clerkManager.getClientConfig().isX_To_Wsdl_Ignore_SSL_Errors() );94Application app = null;
95if (!pathOrURL.startsWith("http")) {
96 File f = new File("test.wadl");
97if (!f.exists()) {
98 System.out.println(pathOrURL + " doesn't exist!");
99return;
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 }
107108 List<URL> urls = WADL2UDDI.getBaseAddresses(app);
109 URL url = urls.get(0);
110 String domain = url.getHost();
111 PrintUDDI<TModel> tmodelPrinter = new PrintUDDI<TModel>();
112TModel keygen = UDDIClerk.createKeyGenator("uddi:" + domain + ":keygenerator", domain, "en");
113114//save the keygen115SaveTModel stm = newSaveTModel();
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);
121122 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();127WADL2UDDI wadl2UDDI = newWADL2UDDI(null, newURLLocalizerDefaultImpl(), properties);
128129BusinessService businessServices = wadl2UDDI.createBusinessService(new QName(domain, domain), app);
130131 Set<TModel> portTypeTModels = wadl2UDDI.createWADLPortTypeTModels(pathOrURL, app);
132133// Set<TModel> createWSDLBindingTModels = wadl2UDDI.createWSDLBindingTModels(wsdlURL, allBindings);134//When parsing a WSDL, there's really two things going on135//1) convert a bunch of stuff (the portTypes) to tModels136//2) convert the service definition to a BusinessService137//Since the service depends on the tModel, we have to save the tModels first138 stm = newSaveTModel();
139 stm.setAuthInfo(token);
140141TModel[] tmodels = portTypeTModels.toArray(newTModel[0]);
142for (int i = 0; i < tmodels.length; i++) {
143 System.out.println(tmodelPrinter.print(tmodels[i]));
144 stm.getTModel().add(tmodels[i]);
145 }
146147 tmodels = wadl2UDDI.createWADLTModels(pathOrURL, app).toArray(newTModel[0]);
148for (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 fail153 System.out.println("Saving the following tModels " + stm.getTModel().size());
154 publish.saveTModel(stm);
155156//finaly, we're ready to save all of the services defined in the WSDL157//again, we're creating a new business, if you have one already, look it up using the Inquiry getBusinessDetails158 PrintUDDI<BusinessService> servicePrinter = new PrintUDDI<BusinessService>();
159160 System.out.println("here's our new service: " + servicePrinter.print(businessServices));
161162if (businessKey == null || businessKey.length() == 0) {
163BusinessEntity be = newBusinessEntity();
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(newBusinessServices());
169 be.getBusinessServices().getBusinessService().add(businessServices);
170SaveBusiness sb = newSaveBusiness();
171 sb.setAuthInfo(token);
172 sb.getBusinessEntity().add(be);
173BusinessDetail saveBusiness = publish.saveBusiness(sb);
174 System.out.println("new business created, key = " + saveBusiness.getBusinessEntity().get(0).getBusinessKey());
175 }
176SaveService ss = newSaveService();
177 ss.setAuthInfo(token);
178 businessServices.setBusinessKey(businessKey);
179 ss.getBusinessService().add(businessServices);
180 publish.saveService(ss);
181 System.out.println("Saved! " + businessServices.getServiceKey());
182183//and we're done184//Be sure to report any problems to the jUDDI JIRA bug tracker at 185//https://issues.apache.org/jira/browse/JUDDI186 }
187188publicstaticvoid main(String[] args) throws Exception {
189190newWadlImport().fire("http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.wadl", null, null, null);
191192 }
193 }