View Javadoc
1   /*
2    * Copyright 2001-2010 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.example.publish;
18  
19  import org.uddi.api_v3.*;
20  import org.apache.juddi.api_v3.*;
21  import org.apache.juddi.v3.client.config.UDDIClient;
22  import org.apache.juddi.v3.client.transport.Transport;
23  import org.uddi.v3_service.UDDISecurityPortType;
24  import org.uddi.v3_service.UDDIPublicationPortType;
25  
26  /**
27   * This shows you to interact with a UDDI server by publishing a Business,
28   * Service and Binding Template. It uses some fairly generic code that should be
29   * mostly portable to any other UDDI client library and is therefore consider
30   * "portable". URLs are set in uddi.xml
31   *
32   */
33  public class SimplePublishPortable {
34  
35          private static UDDISecurityPortType security = null;
36          private static UDDIPublicationPortType publish = null;
37  
38          public SimplePublishPortable() {
39                  try {
40                          // create a client and read the config in the archive; 
41                          // you can use your config file name
42                          UDDIClient uddiClient = new UDDIClient("META-INF/uddi.xml");
43                          // a UddiClient can be a client to multiple UDDI nodes, so 
44                          // supply the nodeName (defined in your uddi.xml.
45                          // The transport can be WS, inVM etc which is defined in the uddi.xml
46                          Transport transport = uddiClient.getTransport("default");
47                          // Now you create a reference to the UDDI API
48                          security = transport.getUDDISecurityService();
49                          publish = transport.getUDDIPublishService();
50                  } catch (Exception e) {
51                          e.printStackTrace();
52                  }
53          }
54  
55          /**
56           * This function shows you how to publish to UDDI using a fairly generic
57           * mechanism that should be portable (meaning use any UDDI v3 library
58           * with this code)
59           */
60          public void publish() {
61                  try {
62  
63                          // Login aka retrieve its authentication token
64                          GetAuthToken getAuthTokenMyPub = new GetAuthToken();
65                          getAuthTokenMyPub.setUserID("bob");                    //your username
66                          getAuthTokenMyPub.setCred("bob");                          //your password
67                          AuthToken myPubAuthToken = security.getAuthToken(getAuthTokenMyPub);
68                          System.out.println(getAuthTokenMyPub.getUserID() + "'s AUTHTOKEN = " + "******* never log auth tokens!");
69  
70                          // Creating the parent business entity that will contain our service.
71                          BusinessEntity myBusEntity = new BusinessEntity();
72                          Name myBusName = new Name();
73                          myBusName.setValue("My Business");
74                          myBusEntity.getName().add(myBusName);
75  
76                          // Adding the business entity to the "save" structure, using our publisher's authentication info and saving away.
77                          SaveBusiness sb = new SaveBusiness();
78                          sb.getBusinessEntity().add(myBusEntity);
79                          sb.setAuthInfo(myPubAuthToken.getAuthInfo());
80                          BusinessDetail bd = publish.saveBusiness(sb);
81                          String myBusKey = bd.getBusinessEntity().get(0).getBusinessKey();
82                          System.out.println("myBusiness key:  " + myBusKey);
83  
84                          // Creating a service to save.  Only adding the minimum data: the parent business key retrieved from saving the business 
85                          // above and a single name.
86                          BusinessService myService = new BusinessService();
87                          myService.setBusinessKey(myBusKey);
88                          Name myServName = new Name();
89                          myServName.setValue("My Service");
90                          myService.getName().add(myServName);
91  
92                          // Add binding templates, etc...
93                          BindingTemplate myBindingTemplate = new BindingTemplate();
94                          AccessPoint accessPoint = new AccessPoint();
95                          accessPoint.setUseType(AccessPointType.WSDL_DEPLOYMENT.toString());
96                          accessPoint.setValue("http://example.org/services/myservice?wsdl");
97                          myBindingTemplate.setAccessPoint(accessPoint);
98                          BindingTemplates myBindingTemplates = new BindingTemplates();
99                          //optional but recommended step, this annotations our binding with all the standard SOAP tModel instance infos
100                         myBindingTemplate = UDDIClient.addSOAPtModels(myBindingTemplate);
101                         myBindingTemplates.getBindingTemplate().add(myBindingTemplate);
102 
103                         myService.setBindingTemplates(myBindingTemplates);
104 
105                         // Adding the service to the "save" structure, using our publisher's authentication info and saving away.
106                         SaveService ss = new SaveService();
107                         ss.getBusinessService().add(myService);
108                         ss.setAuthInfo(myPubAuthToken.getAuthInfo());
109                         ServiceDetail sd = publish.saveService(ss);
110                         String myServKey = sd.getBusinessService().get(0).getServiceKey();
111                         System.out.println("myService key:  " + myServKey);
112 
113                         security.discardAuthToken(new DiscardAuthToken(myPubAuthToken.getAuthInfo()));
114                         // Now you have published a business and service via 
115                         // the jUDDI API!
116                         System.out.println("Success!");
117 
118                 } catch (Exception e) {
119                         e.printStackTrace();
120                 }
121         }
122 
123         public static void main(String args[]) {
124                 SimplePublishPortable sp = new SimplePublishPortable();
125                 sp.publish();
126         }
127 }