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.net.URL;
20import java.util.Map;
21import java.util.Properties;
22import java.util.Set;
2324import javax.wsdl.Definition;
25import javax.wsdl.PortType;
26import javax.xml.namespace.QName;
2728import org.apache.juddi.jaxb.PrintUDDI;
29import org.apache.juddi.v3.client.config.UDDIClerk;
30import org.apache.juddi.v3.client.config.UDDIClient;
31import org.apache.juddi.v3.client.mapping.URLLocalizerDefaultImpl;
32import org.apache.juddi.v3.client.mapping.wsdl.ReadWSDL;
33import org.apache.juddi.v3.client.mapping.wsdl.WSDL2UDDI;
34import org.apache.juddi.v3.client.transport.Transport;
35import org.apache.juddi.v3_service.JUDDIApiPortType;
36import org.uddi.api_v3.AuthToken;
37import org.uddi.api_v3.BusinessDetail;
38import org.uddi.api_v3.BusinessEntity;
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 */55publicclassWsdlImport {
5657private Properties properties = new Properties();
58private String wsdlURL = null;
59privateUDDISecurityPortType security = null;
60privateUDDIPublicationPortType publish = null;
6162publicstaticvoid main(String[] args) throws Exception {
63newWsdlImport().fire("http://svn.apache.org/repos/asf/juddi/trunk/uddi-ws/src/main/resources/juddi_api_v1.wsdl", null, null, null);
64 }
6566publicvoid fire(String pathOrURL, String businessKey, String token, Transport transport) throws Exception {
6768if (transport == null) {
69// create a manager and read the config in the archive; 70// you can use your config file name71UDDIClient clerkManager = newUDDIClient("META-INF/simple-publish-uddi.xml");
72 transport = clerkManager.getTransport();
73 }
74// Now you create a reference to the UDDI API75 security = transport.getUDDISecurityService();
76 publish = transport.getUDDIPublishService();
7778if (token == null) {
79//step one, get a token80GetAuthToken getAuthTokenRoot = newGetAuthToken();
81 getAuthTokenRoot.setUserID("uddi");
82 getAuthTokenRoot.setCred("uddi");
8384// Making API call that retrieves the authentication token for the 'root' user.85AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
86 token = rootAuthToken.getAuthInfo();
87 }
8889//step two, identify the key used for all your stuff90//you must have a key generator created already91//here, we are assuming that you don't have one92//NOTE: these are some of the publicly available WSDLs that were used to test WSDL2UDDI93//URL url = new URL("http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL");94//http://www.bccs.uni.no/~pve002/wsdls/ebi-mafft.wsdl");95//http://www.webservicex.net/GenericNAICS.asmx?WSDL");96//http://www.webservicex.net/stockquote.asmx?WSDL");97//http://www.webservicex.com/globalweather.asmx?WSDL");98//http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl");99 String domain = "localhost";
100int port = 80;
101if (pathOrURL.startsWith("http")) {
102 URL url = new URL(pathOrURL);
103 domain = url.getHost();
104 port = url.getPort();
105if (port == -1) {
106if (pathOrURL.startsWith("https://")) {
107 port = 443;
108 }
109if (pathOrURL.startsWith("http://")) {
110 port = 80;
111 }
112113 }
114 }
115116TModel keygen = UDDIClerk.createKeyGenator("uddi:" + domain + ":keygenerator", domain, "en");
117//save the keygen118SaveTModel stm = newSaveTModel();
119 stm.setAuthInfo(token);
120 stm.getTModel().add(keygen);
121 System.out.println("Saving key gen " + keygen.getTModelKey());
122 publish.saveTModel(stm);
123 System.out.println("Saved!");
124125//step three, we have two options126//1) import the wsdl's services into a brand new business127//2) import the wsdl's services into an existing business128//in either case, we're going to have to parse the WSDL129ReadWSDL rw = newReadWSDL();
130 Definition wsdlDefinition = null;
131if (pathOrURL.startsWith("http")) {
132 wsdlDefinition = rw.readWSDL(new URL(pathOrURL));
133 } else {
134 wsdlDefinition = rw.readWSDL(pathOrURL);
135 }
136137if (wsdlDefinition == null) {
138 System.out.println("There was an error parsing the WSDL!");
139return;
140 }
141 properties.put("keyDomain", domain);
142 properties.put("businessName", domain);
143 properties.put("serverName", domain);
144 properties.put("serverPort", port);
145 wsdlURL = wsdlDefinition.getDocumentBaseURI();
146WSDL2UDDI wsdl2UDDI = newWSDL2UDDI(null, newURLLocalizerDefaultImpl(), properties);
147BusinessServices businessServices = wsdl2UDDI.createBusinessServices(wsdlDefinition);
148 @SuppressWarnings("unchecked")
149 Map<QName, PortType> portTypes = (Map<QName, PortType>) wsdlDefinition.getAllPortTypes();
150 Set<TModel> portTypeTModels = wsdl2UDDI.createWSDLPortTypeTModels(wsdlURL, portTypes);
151 Map allBindings = wsdlDefinition.getAllBindings();
152 Set<TModel> createWSDLBindingTModels = wsdl2UDDI.createWSDLBindingTModels(wsdlURL, allBindings);
153//When parsing a WSDL, there's really two things going on154//1) convert a bunch of stuff (the portTypes) to tModels155//2) convert the service definition to a BusinessService156157//Since the service depends on the tModel, we have to save the tModels first158 stm = newSaveTModel();
159 stm.setAuthInfo(token);
160161TModel[] tmodels = portTypeTModels.toArray(newTModel[0]);
162for (int i = 0; i < tmodels.length; i++) {
163 stm.getTModel().add(tmodels[i]);
164 }
165166 tmodels = createWSDLBindingTModels.toArray(newTModel[0]);
167for (int i = 0; i < tmodels.length; i++) {
168 stm.getTModel().add(tmodels[i]);
169 }
170171//important, you'll need to save your new tModels first, or else saving the business/service may fail172 System.out.println(new PrintUDDI<SaveTModel>().print(stm));
173 System.out.println("Saving " + stm.getTModel().size() + " tModels");
174 publish.saveTModel(stm);
175 System.out.println("Saved!");
176177if (businessKey == null || businessKey.length() == 0) {
178SaveBusiness sb = newSaveBusiness();
179 sb.setAuthInfo(token);
180BusinessEntity be = newBusinessEntity();
181 be.setBusinessKey(businessServices.getBusinessService().get(0).getBusinessKey());
182 be.getName().add(new Name());
183 be.getName().get(0).setValue(domain);
184 be.getName().get(0).setLang("en");
185 sb.getBusinessEntity().add(be);
186BusinessDetail saveBusiness = publish.saveBusiness(sb);
187 businessKey = saveBusiness.getBusinessEntity().get(0).getBusinessKey();
188 System.out.println("new business created key= " + businessKey);
189 }
190191//finaly, we're ready to save all of the services defined in the WSDL192//again, we're creating a new business, if you have one already, look it up using the Inquiry getBusinessDetails193SaveService ss = newSaveService();
194 ss.setAuthInfo(token);
195for (int i = 0; i < businessServices.getBusinessService().size(); i++) {
196 businessServices.getBusinessService().get(i).setBusinessKey(businessKey);
197 ss.getBusinessService().add(businessServices.getBusinessService().get(i));
198199 }
200201 System.out.println("Here's our new service(s): " + new PrintUDDI<SaveService>().print(ss));
202203 publish.saveService(ss);
204 System.out.println("Saved!");
205206//and we're done207//Be sure to report any problems to the jUDDI JIRA bug tracker at 208//https://issues.apache.org/jira/browse/JUDDI209 }
210 }