View Javadoc
1   /*
2    * Copyright 2001-2008 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  package org.apache.juddi.samples;
17  
18  import java.util.Properties;
19  import java.util.logging.Level;
20  import java.util.logging.Logger;
21  import org.apache.juddi.jaxb.PrintUDDI;
22  import org.apache.juddi.v3.client.UDDIConstants;
23  import org.apache.juddi.v3.client.config.UDDIClerk;
24  import org.apache.juddi.v3.client.config.UDDIClient;
25  import org.apache.juddi.v3.client.transport.Transport;
26  import org.apache.juddi.v3_service.JUDDIApiPortType;
27  import org.uddi.api_v3.BusinessDetail;
28  import org.uddi.api_v3.BusinessEntity;
29  import org.uddi.api_v3.BusinessInfos;
30  import org.uddi.api_v3.BusinessList;
31  import org.uddi.api_v3.Contact;
32  import org.uddi.api_v3.Contacts;
33  import org.uddi.api_v3.DeleteBusiness;
34  import org.uddi.api_v3.Description;
35  import org.uddi.api_v3.DiscoveryURL;
36  import org.uddi.api_v3.DiscoveryURLs;
37  import org.uddi.api_v3.FindBusiness;
38  import org.uddi.api_v3.GetAuthToken;
39  import org.uddi.api_v3.GetBusinessDetail;
40  import org.uddi.api_v3.Name;
41  import org.uddi.api_v3.PersonName;
42  import org.uddi.api_v3.SaveBusiness;
43  import org.uddi.api_v3.SaveTModel;
44  import org.uddi.api_v3.TModel;
45  import org.uddi.v3_service.UDDIInquiryPortType;
46  import org.uddi.v3_service.UDDIPublicationPortType;
47  import org.uddi.v3_service.UDDISecurityPortType;
48  
49  /**
50   * This class was made to try and hunt down a bug reported by an end user.
51   * @author Alex O'Ree
52   */
53  public class FindBusinessBugHunt {
54  
55          static PrintUDDI<TModel> pTModel = new PrintUDDI<TModel>();
56          static Properties properties = new Properties();
57          static String wsdlURL = null;
58          private static UDDISecurityPortType security = null;
59          private static JUDDIApiPortType juddiApi = null;
60          private static UDDIPublicationPortType publish = null;
61          private static UDDIInquiryPortType inquiry;
62  
63          public static void main(String[] args) throws Exception {
64  
65                  // create a manager and read the config in the archive; 
66                  // you can use your config file name
67                  UDDIClient clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
68                  
69                  // a ClerkManager can be a client to multiple UDDI nodes, so 
70                  // supply the nodeName (defined in your uddi.xml.
71                  // The transport can be WS, inVM etc which is defined in the uddi.xml
72                  Transport transport = clerkManager.getTransport();
73                  // Now you create a reference to the UDDI API
74                  security = transport.getUDDISecurityService();
75                  publish = transport.getUDDIPublishService();
76                  inquiry = transport.getUDDIInquiryService();
77                  //step one, get a token
78                  GetAuthToken getAuthTokenRoot = new GetAuthToken();
79                  getAuthTokenRoot.setUserID("uddi");
80                  getAuthTokenRoot.setCred("uddi");
81  
82                  // Making API call that retrieves the authentication token for the 'root' user.
83                  String uddi = security.getAuthToken(getAuthTokenRoot).getAuthInfo();
84  
85                  System.out.println("killing mary's business if it exists");
86                  //first check is Mary's business exists and delete
87                  DeleteIfExists("uddi:uddi.marypublisher.com:marybusinessone", uddi);
88  
89                  System.out.println("making mary's tmodel key gen");
90                  //make the key gen since our test case uses some custom keys
91                  TModel createKeyGenator = UDDIClerk.createKeyGenator("uddi.marypublisher.com", "mary key gen", "en");
92                  //clerk.register(createKeyGenator);
93                  System.out.println("saving...");
94                  SaveTM(createKeyGenator, uddi);
95  
96                  System.out.println("fetching business list");
97                  BusinessList before = getBusinessList(uddi);
98                  if (before.getBusinessInfos() == null) {
99                          System.out.println("before no businesses returned!");
100                         before.setBusinessInfos(new BusinessInfos());
101                 } else {
102                         System.out.println(before.getBusinessInfos().getBusinessInfo().size() + " businesses returned before");
103                 }
104 
105                 System.out.println("saving mary");
106                 SaveMary(uddi);
107 
108                 BusinessList after = getBusinessList(uddi);
109                 if (after.getBusinessInfos() == null) {
110                         System.out.println("after no businesses returned!");
111                         after.setBusinessInfos(new BusinessInfos());
112                 } else {
113                         System.out.println(after.getBusinessInfos().getBusinessInfo().size() + " businesses returned after");
114                 }
115                 PrintUDDI<BusinessList> p = new PrintUDDI<BusinessList>();
116                 if (before.getBusinessInfos().getBusinessInfo().size()
117                         < after.getBusinessInfos().getBusinessInfo().size()) {
118                         System.out.println("hey it worked as advertised, double checking");
119                         if (CheckFor(after, "uddi:uddi.marypublisher.com:marybusinessone")) {
120                                 System.out.println("ok!");
121                         } else {
122                                 System.out.println("no good!");
123                         }
124                 } else {
125 
126                         System.out.println("something's not right, here's the before service listing");
127                         System.out.println(p.print(before));
128                         System.out.println(p.print(after));
129                 }
130 
131         }
132 
133         private static void DeleteIfExists(String key, String authInfo) {
134                 GetBusinessDetail gbd = new GetBusinessDetail();
135                 gbd.setAuthInfo(authInfo);
136                 gbd.getBusinessKey().add(key);
137                 boolean found = false;
138                 try {
139                         BusinessDetail businessDetail = inquiry.getBusinessDetail(gbd);
140                         if (businessDetail != null
141                                 && !businessDetail.getBusinessEntity().isEmpty()
142                                 && businessDetail.getBusinessEntity().get(0).getBusinessKey().equals(key)) {
143                                 found = true;
144                         }
145                 } catch (Exception ex) {
146                 }
147                 if (found) {
148                         DeleteBusiness db = new DeleteBusiness();
149                         db.setAuthInfo(authInfo);
150                         db.getBusinessKey().add(key);
151                         try {
152                                 publish.deleteBusiness(db);
153                         } catch (Exception ex) {
154                                 Logger.getLogger(FindBusinessBugHunt.class.getName()).log(Level.SEVERE, null, ex);
155                         }
156                 }
157         }
158 
159         private static BusinessList getBusinessList(String token) throws Exception {
160                 FindBusiness fb = new FindBusiness();
161                 fb.setAuthInfo(token);
162                 org.uddi.api_v3.FindQualifiers fq = new org.uddi.api_v3.FindQualifiers();
163                 fq.getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
164                 fb.setFindQualifiers(fq);
165                 fb.getName().add((new Name(UDDIConstants.WILDCARD, null)));
166                 return inquiry.findBusiness(fb);
167         }
168 
169         private static void SaveMary(String rootAuthToken) throws Exception {
170                 BusinessEntity be = new BusinessEntity();
171                 be.setBusinessKey("uddi:uddi.marypublisher.com:marybusinessone");
172                 be.setDiscoveryURLs(new DiscoveryURLs());
173                 be.getDiscoveryURLs().getDiscoveryURL().add(new DiscoveryURL("home", "http://www.marybusinessone.com"));
174                 be.getDiscoveryURLs().getDiscoveryURL().add(new DiscoveryURL("serviceList", "http://www.marybusinessone.com/services"));
175                 be.getName().add(new Name("Mary Doe Enterprises", "en"));
176                 be.getName().add(new Name("Maria Negocio Uno", "es"));
177                 be.getDescription().add(new Description("This is the description for Mary Business One.", "en"));
178                 be.setContacts(new Contacts());
179                 Contact c = new Contact();
180                 c.setUseType("administrator");
181                 c.getPersonName().add(new PersonName("Mary Doe", "en"));
182                 c.getPersonName().add(new PersonName("Juan Doe", "es"));
183                 c.getDescription().add(new Description("This is the administrator of the service offerings.", "en"));
184                 be.getContacts().getContact().add(c);
185                 SaveBusiness sb = new SaveBusiness();
186                 sb.setAuthInfo(rootAuthToken);
187                 sb.getBusinessEntity().add(be);
188                 publish.saveBusiness(sb);
189         }
190 
191         private static boolean CheckFor(BusinessList list, String key) {
192                 for (int i = 0; i < list.getBusinessInfos().getBusinessInfo().size(); i++) {
193                         if (list.getBusinessInfos().getBusinessInfo().get(i).getBusinessKey().equalsIgnoreCase(key)) {
194                                 return true;
195                         }
196                 }
197                 return false;
198         }
199 
200         private static void SaveTM(TModel createKeyGenator, String uddi) throws Exception {
201                 SaveTModel stm = new SaveTModel();
202                 stm.setAuthInfo(uddi);
203                 stm.getTModel().add(createKeyGenator);
204                 publish.saveTModel(stm);
205         }
206 }