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.net.URL;
20  import java.util.Map;
21  import java.util.Properties;
22  import java.util.Set;
23  
24  import javax.wsdl.Definition;
25  import javax.wsdl.PortType;
26  import javax.xml.namespace.QName;
27  
28  import org.apache.juddi.jaxb.PrintUDDI;
29  import org.apache.juddi.v3.client.config.UDDIClerk;
30  import org.apache.juddi.v3.client.config.UDDIClient;
31  import org.apache.juddi.v3.client.mapping.URLLocalizerDefaultImpl;
32  import org.apache.juddi.v3.client.mapping.wsdl.ReadWSDL;
33  import org.apache.juddi.v3.client.mapping.wsdl.WSDL2UDDI;
34  import org.apache.juddi.v3.client.transport.Transport;
35  import org.apache.juddi.v3_service.JUDDIApiPortType;
36  import org.uddi.api_v3.AuthToken;
37  import org.uddi.api_v3.BusinessDetail;
38  import org.uddi.api_v3.BusinessEntity;
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 WsdlImport {
56  
57          private Properties properties = new Properties();
58          private String wsdlURL = null;
59          private UDDISecurityPortType security = null;
60          private UDDIPublicationPortType publish = null;
61  
62          public static void main(String[] args) throws Exception {
63                  new WsdlImport().fire("http://svn.apache.org/repos/asf/juddi/trunk/uddi-ws/src/main/resources/juddi_api_v1.wsdl", null, null, null);
64          }
65  
66          public void fire(String pathOrURL, String businessKey, String token, Transport transport) throws Exception {
67  
68                  if (transport == null) {
69                  // create a manager and read the config in the archive; 
70                          // you can use your config file name
71                          UDDIClient clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
72                          transport = clerkManager.getTransport();
73                  }
74                  // Now you create a reference to the UDDI API
75                  security = transport.getUDDISecurityService();
76                  publish = transport.getUDDIPublishService();
77  
78                  if (token == null) {
79                          //step one, get a token
80                          GetAuthToken getAuthTokenRoot = new GetAuthToken();
81                          getAuthTokenRoot.setUserID("uddi");
82                          getAuthTokenRoot.setCred("uddi");
83  
84                          // Making API call that retrieves the authentication token for the 'root' user.
85                          AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
86                          token = rootAuthToken.getAuthInfo();
87                  }
88  
89                  //step two, identify the key used for all your stuff
90                  //you must have a key generator created already
91                  //here, we are assuming that you don't have one
92                  //NOTE: these are some of the publicly available WSDLs that were used to test WSDL2UDDI
93                  //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";
100                 int port = 80;
101                 if (pathOrURL.startsWith("http")) {
102                         URL url = new URL(pathOrURL);
103                         domain = url.getHost();
104                         port = url.getPort();
105                         if (port == -1) {
106                                 if (pathOrURL.startsWith("https://")) {
107                                         port = 443;
108                                 }
109                                 if (pathOrURL.startsWith("http://")) {
110                                         port = 80;
111                                 }
112 
113                         }
114                 }
115 
116                 TModel keygen = UDDIClerk.createKeyGenator("uddi:" + domain + ":keygenerator", domain, "en");
117                 //save the keygen
118                 SaveTModel stm = new SaveTModel();
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!");
124 
125                 //step three, we have two options
126                 //1) import the wsdl's services into a brand new business
127                 //2) import the wsdl's services into an existing business
128                 //in either case, we're going to have to parse the WSDL
129                 ReadWSDL rw = new ReadWSDL();
130                 Definition wsdlDefinition = null;
131                 if (pathOrURL.startsWith("http")) {
132                         wsdlDefinition = rw.readWSDL(new URL(pathOrURL));
133                 } else {
134                         wsdlDefinition = rw.readWSDL(pathOrURL);
135                 }
136 
137                 if (wsdlDefinition == null) {
138                         System.out.println("There was an error parsing the WSDL!");
139                         return;
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();
146                 WSDL2UDDI wsdl2UDDI = new WSDL2UDDI(null, new URLLocalizerDefaultImpl(), properties);
147                 BusinessServices 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 on
154                 //1) convert a bunch of stuff (the portTypes) to tModels
155                 //2) convert the service definition to a BusinessService
156 
157                 //Since the service depends on the tModel, we have to save the tModels first
158                 stm = new SaveTModel();
159                 stm.setAuthInfo(token);
160 
161                 TModel[] tmodels = portTypeTModels.toArray(new TModel[0]);
162                 for (int i = 0; i < tmodels.length; i++) {
163                         stm.getTModel().add(tmodels[i]);
164                 }
165 
166                 tmodels = createWSDLBindingTModels.toArray(new TModel[0]);
167                 for (int i = 0; i < tmodels.length; i++) {
168                         stm.getTModel().add(tmodels[i]);
169                 }
170 
171                 //important, you'll need to save your new tModels first, or else saving the business/service may fail
172                 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!");
176 
177                 if (businessKey == null || businessKey.length() == 0) {
178                         SaveBusiness sb = new SaveBusiness();
179                         sb.setAuthInfo(token);
180                         BusinessEntity be = new BusinessEntity();
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);
186                         BusinessDetail saveBusiness = publish.saveBusiness(sb);
187                         businessKey = saveBusiness.getBusinessEntity().get(0).getBusinessKey();
188                         System.out.println("new business created key= " + businessKey);
189                 }
190 
191                 //finaly, we're ready to save all of the services defined in the WSDL
192                 //again, we're creating a new business, if you have one already, look it up using the Inquiry getBusinessDetails
193                 SaveService ss = new SaveService();
194                 ss.setAuthInfo(token);
195                 for (int i = 0; i < businessServices.getBusinessService().size(); i++) {
196                         businessServices.getBusinessService().get(i).setBusinessKey(businessKey);
197                         ss.getBusinessService().add(businessServices.getBusinessService().get(i));
198 
199                 }
200 
201                 System.out.println("Here's our new service(s): " + new PrintUDDI<SaveService>().print(ss));
202 
203                 publish.saveService(ss);
204                 System.out.println("Saved!");
205 
206                 //and we're done
207                 //Be sure to report any problems to the jUDDI JIRA bug tracker at 
208                 //https://issues.apache.org/jira/browse/JUDDI
209         }
210 }