This project has retired. For details please refer to its Attic page.
SimpleBrowse xref
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.v3.client.cli;
18  
19  import java.util.List;
20  import javax.xml.bind.JAXB;
21  import org.apache.juddi.api_v3.AccessPointType;
22  import org.apache.juddi.v3.client.UDDIConstants;
23  import org.apache.juddi.v3.client.config.UDDIClient;
24  import org.apache.juddi.v3.client.transport.Transport;
25  import org.uddi.api_v3.AuthToken;
26  import org.uddi.api_v3.BindingTemplates;
27  import org.uddi.api_v3.BusinessDetail;
28  import org.uddi.api_v3.BusinessInfos;
29  import org.uddi.api_v3.BusinessList;
30  import org.uddi.api_v3.BusinessService;
31  import org.uddi.api_v3.CategoryBag;
32  import org.uddi.api_v3.Contacts;
33  import org.uddi.api_v3.Description;
34  import org.uddi.api_v3.DiscardAuthToken;
35  import org.uddi.api_v3.FindBusiness;
36  import org.uddi.api_v3.FindQualifiers;
37  import org.uddi.api_v3.FindService;
38  import org.uddi.api_v3.FindTModel;
39  import org.uddi.api_v3.GetAuthToken;
40  import org.uddi.api_v3.GetBusinessDetail;
41  import org.uddi.api_v3.GetServiceDetail;
42  import org.uddi.api_v3.KeyedReference;
43  import org.uddi.api_v3.Name;
44  import org.uddi.api_v3.ServiceDetail;
45  import org.uddi.api_v3.ServiceInfos;
46  import org.uddi.api_v3.ServiceList;
47  import org.uddi.api_v3.TModelInfo;
48  import org.uddi.api_v3.TModelInfos;
49  import org.uddi.api_v3.TModelList;
50  import org.uddi.v3_service.UDDIInquiryPortType;
51  import org.uddi.v3_service.UDDISecurityPortType;
52  
53  /**
54   * A Simple UDDI Browser that dumps basic information to console
55   *
56   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
57   */
58  public class SimpleBrowse {
59  
60          private UDDISecurityPortType security = null;
61          private UDDIInquiryPortType inquiry = null;
62  
63          /**
64           * This sets up the ws proxies using uddi.xml in META-INF
65           */
66          public SimpleBrowse() {
67                  try {
68                          // create a manager and read the config in the archive; 
69                          // you can use your config file name
70                          UDDIClient client = new UDDIClient("META-INF/simple-browse-uddi.xml");
71                          // a UDDIClient can be a client to multiple UDDI nodes, so 
72                          // supply the nodeName (defined in your uddi.xml.
73                          // The transport can be WS, inVM, RMI etc which is defined in the uddi.xml
74                          Transport transport = client.getTransport("default");
75                          // Now you create a reference to the UDDI API
76                          security = transport.getUDDISecurityService();
77                          inquiry = transport.getUDDIInquiryService();
78                  } catch (Exception e) {
79                          e.printStackTrace();
80                  }
81          }
82  
83          /**
84           * Main entry point
85           *
86           * @param args
87           */
88          public static void main(String args[]) {
89  
90                  SimpleBrowse sp = new SimpleBrowse();
91                  sp.browse(args);
92          }
93  
94          public SimpleBrowse(Transport transport) {
95                  try {
96  
97                          // Now you create a reference to the UDDI API
98                          security = transport.getUDDISecurityService();
99                          inquiry = transport.getUDDIInquiryService();
100                 } catch (Exception e) {
101                         e.printStackTrace();
102                 }
103         }
104 
105         public void browse(String[] args) {
106                 try {
107 
108                         String token = getAuthKey("uddi", "uddi");
109                         BusinessList findBusiness = getBusinessList(token, null, 0, 100);
110                         printBusinessInfo(findBusiness.getBusinessInfos());
111                         printBusinessDetails(findBusiness.getBusinessInfos(), token);
112                         printServiceDetailsByBusiness(findBusiness.getBusinessInfos(), token);
113 
114                         security.discardAuthToken(new DiscardAuthToken(token));
115 
116                 } catch (Exception e) {
117                         e.printStackTrace();
118                 }
119         }
120 
121         /**
122          * Find all of the registered businesses. This list may be filtered
123          * based on access control rules
124          *
125          * @param token
126          * @return
127          * @throws Exception
128          */
129         private BusinessList getBusinessList(String token, String query, int offset, int maxrecords) throws Exception {
130                 FindBusiness fb = new FindBusiness();
131                 fb.setAuthInfo(token);
132                 org.uddi.api_v3.FindQualifiers fq = new org.uddi.api_v3.FindQualifiers();
133                 fq.getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
134 
135                 fb.setFindQualifiers(fq);
136                 Name searchname = new Name();
137                 if (query == null || query.equalsIgnoreCase("")) {
138                         searchname.setValue(UDDIConstants.WILDCARD);
139                 } else {
140                         searchname.setValue(query);
141                 }
142                 fb.getName().add(searchname);
143                 fb.setListHead(offset);
144                 fb.setMaxRows(maxrecords);
145                 BusinessList findBusiness = inquiry.findBusiness(fb);
146                 return findBusiness;
147 
148         }
149 
150         /**
151          * Converts category bags of tmodels to a readable string
152          *
153          * @param categoryBag
154          * @return
155          */
156         private String catBagToString(CategoryBag categoryBag) {
157                 StringBuilder sb = new StringBuilder();
158                 if (categoryBag == null) {
159                         return "no data";
160                 }
161                 for (int i = 0; i < categoryBag.getKeyedReference().size(); i++) {
162                         sb.append(keyedReferenceToString(categoryBag.getKeyedReference().get(i)));
163                 }
164                 for (int i = 0; i < categoryBag.getKeyedReferenceGroup().size(); i++) {
165                         sb.append("Key Ref Grp: TModelKey=");
166                         for (int k = 0; k < categoryBag.getKeyedReferenceGroup().get(i).getKeyedReference().size(); k++) {
167                                 sb.append(keyedReferenceToString(categoryBag.getKeyedReferenceGroup().get(i).getKeyedReference().get(k)));
168                         }
169                 }
170                 return sb.toString();
171         }
172 
173         private String keyedReferenceToString(KeyedReference item) {
174                 StringBuilder sb = new StringBuilder();
175                 sb.append("Key Ref: Name=").
176                         append(item.getKeyName()).
177                         append(" Value=").
178                         append(item.getKeyValue()).
179                         append(" tModel=").
180                         append(item.getTModelKey()).
181                         append(System.getProperty("line.separator"));
182                 return sb.toString();
183         }
184 
185         private void printContacts(Contacts contacts) {
186                 if (contacts == null) {
187                         return;
188                 }
189                 for (int i = 0; i < contacts.getContact().size(); i++) {
190                         System.out.println("Contact " + i + " type:" + contacts.getContact().get(i).getUseType());
191                         for (int k = 0; k < contacts.getContact().get(i).getPersonName().size(); k++) {
192                                 System.out.println("Name: " + contacts.getContact().get(i).getPersonName().get(k).getValue());
193                         }
194                         for (int k = 0; k < contacts.getContact().get(i).getEmail().size(); k++) {
195                                 System.out.println("Email: " + contacts.getContact().get(i).getEmail().get(k).getValue());
196                         }
197                         for (int k = 0; k < contacts.getContact().get(i).getAddress().size(); k++) {
198                                 System.out.println("Address sort code " + contacts.getContact().get(i).getAddress().get(k).getSortCode());
199                                 System.out.println("Address use type " + contacts.getContact().get(i).getAddress().get(k).getUseType());
200                                 System.out.println("Address tmodel key " + contacts.getContact().get(i).getAddress().get(k).getTModelKey());
201                                 for (int x = 0; x < contacts.getContact().get(i).getAddress().get(k).getAddressLine().size(); x++) {
202                                         System.out.println("Address line value " + contacts.getContact().get(i).getAddress().get(k).getAddressLine().get(x).getValue());
203                                         System.out.println("Address line key name " + contacts.getContact().get(i).getAddress().get(k).getAddressLine().get(x).getKeyName());
204                                         System.out.println("Address line key value " + contacts.getContact().get(i).getAddress().get(k).getAddressLine().get(x).getKeyValue());
205                                 }
206                         }
207                         for (int k = 0; k < contacts.getContact().get(i).getDescription().size(); k++) {
208                                 System.out.println("Desc: " + contacts.getContact().get(i).getDescription().get(k).getValue());
209                         }
210                         for (int k = 0; k < contacts.getContact().get(i).getPhone().size(); k++) {
211                                 System.out.println("Phone: " + contacts.getContact().get(i).getPhone().get(k).getValue());
212                         }
213                 }
214 
215         }
216 
217         private void printServiceDetail(BusinessService get) {
218                 if (get == null) {
219                         return;
220                 }
221                 System.out.println("Name " + listToString(get.getName()));
222                 System.out.println("Desc " + listToDescString(get.getDescription()));
223                 System.out.println("Key " + (get.getServiceKey()));
224                 System.out.println("Cat bag " + catBagToString(get.getCategoryBag()));
225                 if (!get.getSignature().isEmpty()) {
226                         System.out.println("Item is digitally signed");
227                 } else {
228                         System.out.println("Item is not digitally signed");
229                 }
230                 printBindingTemplates(get.getBindingTemplates());
231         }
232 
233         /**
234          * This function is useful for translating UDDI's somewhat complex data
235          * format to something that is more useful.
236          *
237          * @param bindingTemplates
238          */
239         private void printBindingTemplates(BindingTemplates bindingTemplates) {
240                 if (bindingTemplates == null) {
241                         return;
242                 }
243                 for (int i = 0; i < bindingTemplates.getBindingTemplate().size(); i++) {
244                         System.out.println("Binding Key: " + bindingTemplates.getBindingTemplate().get(i).getBindingKey());
245                         //TODO The UDDI spec is kind of strange at this point.
246                         //An access point could be a URL, a reference to another UDDI binding key, a hosting redirector (which is 
247                         //esscentially a pointer to another UDDI registry) or a WSDL Deployment
248                         //From an end client's perspective, all you really want is the endpoint.
249                         //http://uddi.org/pubs/uddi_v3.htm#_Ref8977716
250                         //So if you have a wsdlDeployment useType, fetch the wsdl and parse for the invocation URL
251                         //If its hosting director, you'll have to fetch that data from uddi recursively until the leaf node is found
252                         //Consult the UDDI specification for more information
253 
254                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint() != null) {
255                                 System.out.println("Access Point: " + bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getValue() + " type " + bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType());
256                                 if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType() != null) {
257                                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType().equalsIgnoreCase(AccessPointType.END_POINT.toString())) {
258                                                 System.out.println("Use this access point value as an invocation endpoint.");
259                                         }
260                                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType().equalsIgnoreCase(AccessPointType.BINDING_TEMPLATE.toString())) {
261                                                 System.out.println("Use this access point value as a reference to another binding template.");
262                                         }
263                                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType().equalsIgnoreCase(AccessPointType.WSDL_DEPLOYMENT.toString())) {
264                                                 System.out.println("Use this access point value as a URL to a WSDL document, which presumably will have a real access point defined.");
265                                         }
266                                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType().equalsIgnoreCase(AccessPointType.HOSTING_REDIRECTOR.toString())) {
267                                                 System.out.println("Use this access point value as an Inquiry URL of another UDDI registry, look up the same binding template there (usage varies).");
268                                         }
269                                 }
270                         }
271 
272                 }
273         }
274 
275         public void printBusinessList(String authtoken, String searchString, boolean rawXml) throws Exception {
276 
277                 int offset = 0;
278                 int maxrecords = 100;
279                 BusinessList findBusiness = getBusinessList(authtoken, searchString, offset, maxrecords);
280                 while (findBusiness != null && findBusiness.getBusinessInfos() != null
281                         && !findBusiness.getBusinessInfos().getBusinessInfo().isEmpty()) {
282                         if (rawXml) {
283                                 JAXB.marshal(findBusiness, System.out);
284                         } else {
285                                 printBusinessInfo(findBusiness.getBusinessInfos());
286                                 printBusinessDetails(findBusiness.getBusinessInfos(), authtoken);
287                                 printServiceDetailsByBusiness(findBusiness.getBusinessInfos(), authtoken);
288                         }
289                         offset = offset + maxrecords;
290 
291                         findBusiness = getBusinessList(authtoken, searchString, offset, maxrecords);
292                 }
293         }
294 
295         public void printServiceList(String authtoken, String searchString, boolean rawXml) throws Exception {
296 
297                 int offset = 0;
298                 int maxrecords = 100;
299                 ServiceList findBusiness = getServiceList(authtoken, searchString, offset, maxrecords);
300                 while (findBusiness != null && findBusiness.getServiceInfos() != null
301                         && !findBusiness.getServiceInfos().getServiceInfo().isEmpty()) {
302                         if (rawXml) {
303                                 JAXB.marshal(findBusiness, System.out);
304                         } else {
305                                 printServiceInfo(findBusiness.getServiceInfos());
306                                 //PrintBusinessDetails(findBusiness.getBusinessInfos(), authtoken);
307                                 //PrintServiceDetailsByBusiness(findBusiness.getBusinessInfos(), authtoken);
308                         }
309                         offset = offset + maxrecords;
310 
311                         findBusiness = getServiceList(authtoken, searchString, offset, maxrecords);
312                 }
313         }
314 
315         private ServiceList getServiceList(String authtoken, String searchString, int offset, int maxrecords) throws Exception {
316                 FindService fs = new FindService();
317                 fs.setAuthInfo(authtoken);
318                 fs.setListHead(offset);
319                 fs.setMaxRows(maxrecords);
320                 if (searchString != null) {
321                         fs.getName().add(new Name("%" + searchString + " %", null));
322 
323                 } else {
324                         fs.getName().add(new Name(UDDIConstants.WILDCARD, null));
325                 }
326                 fs.setFindQualifiers(new FindQualifiers());
327                 fs.getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
328                 return inquiry.findService(fs);
329         }
330 
331         void printTModelList(String authtoken, String searchString, boolean rawXml) throws Exception {
332                 int offset = 0;
333                 int maxrecords = 100;
334                 TModelList findBusiness = getTmodelList(authtoken, searchString, offset, maxrecords);
335                 while (findBusiness != null && findBusiness.getTModelInfos() != null
336                         && !findBusiness.getTModelInfos().getTModelInfo().isEmpty()) {
337                         if (rawXml) {
338                                 JAXB.marshal(findBusiness, System.out);
339                         } else {
340                                 printTModelInfo(findBusiness.getTModelInfos());
341                                 //PrintBusinessDetails(findBusiness.getBusinessInfos(), authtoken);
342                                 //PrintServiceDetailsByBusiness(findBusiness.getBusinessInfos(), authtoken);
343                         }
344                         offset = offset + maxrecords;
345 
346                         findBusiness = getTmodelList(authtoken, searchString, offset, maxrecords);
347                 }
348         }
349 
350         private void printTModelInfo(TModelInfos tModelInfos) {
351                 if (tModelInfos == null) {
352                         System.out.println("No data returned");
353                         return;
354                 }
355                 for (TModelInfo tModelInfo : tModelInfos.getTModelInfo()) {
356                         System.out.println("tModel key: " + tModelInfo.getTModelKey());
357                         System.out.println("tModel name: " + tModelInfo.getName().getLang() + " " + tModelInfo.getName().getValue());
358                         // PrintServiceInfo(null);
359                         for (int k = 0; k < tModelInfo.getDescription().size(); k++) {
360                                 System.out.println("Desc: " + tModelInfo.getDescription().get(k).getValue());
361                         }
362                 }
363         }
364 
365         private TModelList getTmodelList(String authtoken, String searchString, int offset, int maxrecords) throws Exception {
366                 FindTModel fs = new FindTModel();
367                 fs.setAuthInfo(authtoken);
368                 fs.setListHead(offset);
369                 fs.setMaxRows(maxrecords);
370                 if (searchString != null) {
371                         fs.setName(new Name("%" + searchString + " %", null));
372 
373                 } else {
374                         fs.setName(new Name(UDDIConstants.WILDCARD, null));
375                 }
376                 fs.setFindQualifiers(new FindQualifiers());
377                 fs.getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
378                 return inquiry.findTModel(fs);
379         }
380 
381         private enum AuthStyle {
382 
383                 HTTP_BASIC,
384                 HTTP_DIGEST,
385                 HTTP_NTLM,
386                 UDDI_AUTH,
387                 HTTP_CLIENT_CERT
388         }
389 
390         /**
391          * Gets a UDDI style auth token, otherwise, appends credentials to the
392          * ws proxies (not yet implemented)
393          *
394          * @param username
395          * @param password
396          * @param style
397          * @return
398          */
399         private String getAuthKey(String username, String password) {
400                 try {
401 
402                         GetAuthToken getAuthTokenRoot = new GetAuthToken();
403                         getAuthTokenRoot.setUserID(username);
404                         getAuthTokenRoot.setCred(password);
405 
406                         // Making API call that retrieves the authentication token for the user.
407                         AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
408                         System.out.println(username + " AUTHTOKEN = (don't log auth tokens!");
409                         return rootAuthToken.getAuthInfo();
410                 } catch (Exception ex) {
411                         System.out.println("Could not authenticate with the provided credentials " + ex.getMessage());
412                 }
413                 return null;
414         }
415 
416         private void printBusinessInfo(BusinessInfos businessInfos) {
417                 if (businessInfos == null) {
418                         System.out.println("No data returned");
419                 } else {
420                         for (int i = 0; i < businessInfos.getBusinessInfo().size(); i++) {
421                                 System.out.println("===============================================");
422                                 System.out.println("Business Key: " + businessInfos.getBusinessInfo().get(i).getBusinessKey());
423                                 System.out.println("Name: " + listToString(businessInfos.getBusinessInfo().get(i).getName()));
424 
425                                 System.out.println("Description: " + listToDescString(businessInfos.getBusinessInfo().get(i).getDescription()));
426                                 System.out.println("Services:");
427                                 printServiceInfo(businessInfos.getBusinessInfo().get(i).getServiceInfos());
428                         }
429                 }
430         }
431 
432         private String listToString(List<Name> name) {
433                 StringBuilder sb = new StringBuilder();
434                 for (int i = 0; i < name.size(); i++) {
435                         sb.append(name.get(i).getValue()).append(" ");
436                 }
437                 return sb.toString();
438         }
439 
440         private String listToDescString(List<Description> name) {
441                 StringBuilder sb = new StringBuilder();
442                 for (int i = 0; i < name.size(); i++) {
443                         sb.append(name.get(i).getValue()).append(" ");
444                 }
445                 return sb.toString();
446         }
447 
448         private void printServiceInfo(ServiceInfos serviceInfos) {
449                 for (int i = 0; i < serviceInfos.getServiceInfo().size(); i++) {
450                         System.out.println("-------------------------------------------");
451                         System.out.println("Service Key: " + serviceInfos.getServiceInfo().get(i).getServiceKey());
452                         System.out.println("Owning Business Key: " + serviceInfos.getServiceInfo().get(i).getBusinessKey());
453                         System.out.println("Name: " + listToString(serviceInfos.getServiceInfo().get(i).getName()));
454                 }
455         }
456 
457         private void printBusinessDetails(BusinessInfos businessInfos, String token) throws Exception {
458                 GetBusinessDetail gbd = new GetBusinessDetail();
459                 gbd.setAuthInfo(token);
460                 for (int i = 0; i < businessInfos.getBusinessInfo().size(); i++) {
461                         gbd.getBusinessKey().add(businessInfos.getBusinessInfo().get(i).getBusinessKey());
462                 }
463                 BusinessDetail businessDetail = inquiry.getBusinessDetail(gbd);
464                 for (int i = 0; i < businessDetail.getBusinessEntity().size(); i++) {
465                         System.out.println("Business Detail - key: " + businessDetail.getBusinessEntity().get(i).getBusinessKey());
466                         System.out.println("Name: " + listToString(businessDetail.getBusinessEntity().get(i).getName()));
467                         System.out.println("CategoryBag: " + catBagToString(businessDetail.getBusinessEntity().get(i).getCategoryBag()));
468                         printContacts(businessDetail.getBusinessEntity().get(i).getContacts());
469                 }
470         }
471 
472         private void printServiceDetailsByBusiness(BusinessInfos businessInfos, String token) throws Exception {
473                 for (int i = 0; i < businessInfos.getBusinessInfo().size(); i++) {
474                         GetServiceDetail gsd = new GetServiceDetail();
475                         for (int k = 0; k < businessInfos.getBusinessInfo().get(i).getServiceInfos().getServiceInfo().size(); k++) {
476                                 gsd.getServiceKey().add(businessInfos.getBusinessInfo().get(i).getServiceInfos().getServiceInfo().get(k).getServiceKey());
477                         }
478                         gsd.setAuthInfo(token);
479                         System.out.println("Fetching data for business " + businessInfos.getBusinessInfo().get(i).getBusinessKey());
480                         ServiceDetail serviceDetail = inquiry.getServiceDetail(gsd);
481                         for (int k = 0; k < serviceDetail.getBusinessService().size(); k++) {
482                                 printServiceDetail(serviceDetail.getBusinessService().get(k));
483                         }
484                         System.out.println("................");
485 
486                 }
487         }
488 }