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.samples;
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 {
565758private Properties properties = new Properties();
5960privateUDDISecurityPortType security = null;
61privateUDDIPublicationPortType publish = null;
6263publicvoid fire(String pathOrURL, String businessKey, String token, Transport transport) throws Exception {
6465if (transport == null) {
66// create a manager and read the config in the archive; 67// you can use your config file name68UDDIClient clerkManager = newUDDIClient("META-INF/simple-publish-uddi.xml");
69 transport = clerkManager.getTransport();
70 }
71// Now you create a reference to the UDDI API72 security = transport.getUDDISecurityService();
73 publish = transport.getUDDIPublishService();
7475if (token == null) {
76//step one, get a token77GetAuthToken getAuthTokenRoot = newGetAuthToken();
78 getAuthTokenRoot.setUserID("uddi");
79 getAuthTokenRoot.setCred("uddi");
8081// Making API call that retrieves the authentication token for the 'root' user.82AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
83 token = rootAuthToken.getAuthInfo();
84 }
8586//step two, identify the key used for all your stuff87//you must have a key generator created already88//here, we are assuming that you don't have one89//NOTE: these are some of the publicly available WSDLs that were used to test WSDL2UDDI90//publish.saveTModel(stm);91//step three, we have two options92//1) import the wsdl's services into a brand new business93//2) import the wsdl's services into an existing business94//in either case, we're going to have to parse the WSDL95//Application app = WADL2UDDI.parseWadl(new URL("http://server/wsdl.wsdl"), "username", "password", clerkManager.getClientConfig().isX_To_Wsdl_Ignore_SSL_Errors() );96Application app = null;
97if (!pathOrURL.startsWith("http")) {
98 File f = new File("test.wadl");
99if (!f.exists()) {
100 System.out.println(pathOrURL + " doesn't exist!");
101return;
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 }
109110 List<URL> urls = WADL2UDDI.getBaseAddresses(app);
111 URL url = urls.get(0);
112 String domain = url.getHost();
113 PrintUDDI<TModel> tmodelPrinter = new PrintUDDI<TModel>();
114TModel keygen = UDDIClerk.createKeyGenator("uddi:" + domain + ":keygenerator", domain, "en");
115116//save the keygen117SaveTModel stm = newSaveTModel();
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);
123124 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();129WADL2UDDI wadl2UDDI = newWADL2UDDI(null, newURLLocalizerDefaultImpl(), properties);
130131BusinessService businessServices = wadl2UDDI.createBusinessService(new QName(domain, domain), app);
132133 Set<TModel> portTypeTModels = wadl2UDDI.createWADLPortTypeTModels(pathOrURL, app);
134135// Set<TModel> createWSDLBindingTModels = wadl2UDDI.createWSDLBindingTModels(wsdlURL, allBindings);136//When parsing a WSDL, there's really two things going on137//1) convert a bunch of stuff (the portTypes) to tModels138//2) convert the service definition to a BusinessService139//Since the service depends on the tModel, we have to save the tModels first140 stm = newSaveTModel();
141 stm.setAuthInfo(token);
142143TModel[] tmodels = portTypeTModels.toArray(newTModel[0]);
144for (int i = 0; i < tmodels.length; i++) {
145 System.out.println(tmodelPrinter.print(tmodels[i]));
146 stm.getTModel().add(tmodels[i]);
147 }
148149 tmodels = wadl2UDDI.createWADLTModels(pathOrURL, app).toArray(newTModel[0]);
150for (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 fail155 System.out.println("Saving the following tModels " + stm.getTModel().size());
156 publish.saveTModel(stm);
157158//finaly, we're ready to save all of the services defined in the WSDL159//again, we're creating a new business, if you have one already, look it up using the Inquiry getBusinessDetails160 PrintUDDI<BusinessService> servicePrinter = new PrintUDDI<BusinessService>();
161162 System.out.println("here's our new service: " + servicePrinter.print(businessServices));
163164if (businessKey == null || businessKey.length() == 0) {
165BusinessEntity be = newBusinessEntity();
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(newBusinessServices());
171 be.getBusinessServices().getBusinessService().add(businessServices);
172SaveBusiness sb = newSaveBusiness();
173 sb.setAuthInfo(token);
174 sb.getBusinessEntity().add(be);
175BusinessDetail saveBusiness = publish.saveBusiness(sb);
176 System.out.println("new business created, key = " + saveBusiness.getBusinessEntity().get(0).getBusinessKey());
177 }
178SaveService ss = newSaveService();
179 ss.setAuthInfo(token);
180 businessServices.setBusinessKey(businessKey);
181 ss.getBusinessService().add(businessServices);
182 publish.saveService(ss);
183 System.out.println("Saved! " + businessServices.getServiceKey());
184185//and we're done186//Be sure to report any problems to the jUDDI JIRA bug tracker at 187//https://issues.apache.org/jira/browse/JUDDI188 }
189190publicstaticvoid main(String[] args) throws Exception {
191192newWadlImport().fire("http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.wadl", null, null, null);
193194 }
195 }