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.browse;
18  
19  import java.util.List;
20  import org.apache.juddi.api_v3.AccessPointType;
21  import org.apache.juddi.v3.client.UDDIConstants;
22  import org.apache.juddi.v3.client.config.UDDIClient;
23  import org.apache.juddi.v3.client.transport.Transport;
24  import org.uddi.api_v3.AuthToken;
25  import org.uddi.api_v3.BindingTemplates;
26  import org.uddi.api_v3.BusinessDetail;
27  import org.uddi.api_v3.BusinessInfos;
28  import org.uddi.api_v3.BusinessList;
29  import org.uddi.api_v3.BusinessService;
30  import org.uddi.api_v3.CategoryBag;
31  import org.uddi.api_v3.Contacts;
32  import org.uddi.api_v3.Description;
33  import org.uddi.api_v3.DiscardAuthToken;
34  import org.uddi.api_v3.FindBusiness;
35  import org.uddi.api_v3.GetAuthToken;
36  import org.uddi.api_v3.GetBusinessDetail;
37  import org.uddi.api_v3.GetServiceDetail;
38  import org.uddi.api_v3.KeyedReference;
39  import org.uddi.api_v3.Name;
40  import org.uddi.api_v3.ServiceDetail;
41  import org.uddi.api_v3.ServiceInfos;
42  import org.uddi.v3_service.UDDIInquiryPortType;
43  import org.uddi.v3_service.UDDISecurityPortType;
44  
45  /**
46   * A Simple UDDI Browser that dumps basic information to console
47   *
48   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
49   */
50  public class SimpleBrowse {
51  
52          private static UDDISecurityPortType security = null;
53          private static UDDIInquiryPortType inquiry = null;
54  
55          /**
56           * This sets up the ws proxies using uddi.xml in META-INF
57           */
58          public SimpleBrowse() {
59                  try {
60          	// create a manager and read the config in the archive; 
61                          // you can use your config file name
62                          UDDIClient client = new UDDIClient("META-INF/simple-browse-uddi.xml");
63          	// a UDDIClient can be a client to multiple UDDI nodes, so 
64                          // supply the nodeName (defined in your uddi.xml.
65                          // The transport can be WS, inVM etc which is defined in the uddi.xml
66                          Transport transport = client.getTransport("default");
67                          // Now you create a reference to the UDDI API
68                          security = transport.getUDDISecurityService();
69                          inquiry = transport.getUDDIInquiryService();
70                  } catch (Exception e) {
71                          e.printStackTrace();
72                  }
73          }
74  
75          /**
76           * Main entry point
77           *
78           * @param args
79           */
80          public static void main(String args[]) {
81  
82                  SimpleBrowse sp = new SimpleBrowse();
83                  sp.Browse(args);
84          }
85  
86          public void Browse(String[] args) {
87                  try {
88  
89                          String token = GetAuthKey("uddi", "uddi");
90                          BusinessList findBusiness = GetBusinessList(token);
91                          PrintBusinessInfo(findBusiness.getBusinessInfos());
92                          PrintBusinessDetails(findBusiness.getBusinessInfos(), token);
93                          PrintServiceDetailsByBusiness(findBusiness.getBusinessInfos(), token);
94  
95                          security.discardAuthToken(new DiscardAuthToken(token));
96  
97                  } catch (Exception e) {
98                          e.printStackTrace();
99                  }
100         }
101 
102         /**
103          * Find all of the registered businesses. This list may be filtered
104          * based on access control rules
105          *
106          * @param token
107          * @return
108          * @throws Exception
109          */
110         private BusinessList GetBusinessList(String token) throws Exception {
111                 FindBusiness fb = new FindBusiness();
112                 fb.setAuthInfo(token);
113                 org.uddi.api_v3.FindQualifiers fq = new org.uddi.api_v3.FindQualifiers();
114                 fq.getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
115 
116                 fb.setFindQualifiers(fq);
117                 Name searchname = new Name();
118                 searchname.setValue(UDDIConstants.WILDCARD);
119                 fb.getName().add(searchname);
120                 BusinessList findBusiness = inquiry.findBusiness(fb);
121                 return findBusiness;
122 
123         }
124 
125         /**
126          * Converts category bags of tmodels to a readable string
127          *
128          * @param categoryBag
129          * @return
130          */
131         private String CatBagToString(CategoryBag categoryBag) {
132                 StringBuilder sb = new StringBuilder();
133                 if (categoryBag == null) {
134                         return "no data";
135                 }
136                 for (int i = 0; i < categoryBag.getKeyedReference().size(); i++) {
137                         sb.append(KeyedReferenceToString(categoryBag.getKeyedReference().get(i)));
138                 }
139                 for (int i = 0; i < categoryBag.getKeyedReferenceGroup().size(); i++) {
140                         sb.append("Key Ref Grp: TModelKey=");
141                         for (int k = 0; k < categoryBag.getKeyedReferenceGroup().get(i).getKeyedReference().size(); k++) {
142                                 sb.append(KeyedReferenceToString(categoryBag.getKeyedReferenceGroup().get(i).getKeyedReference().get(k)));
143                         }
144                 }
145                 return sb.toString();
146         }
147 
148         private String KeyedReferenceToString(KeyedReference item) {
149                 StringBuilder sb = new StringBuilder();
150                 sb.append("Key Ref: Name=").
151                         append(item.getKeyName()).
152                         append(" Value=").
153                         append(item.getKeyValue()).
154                         append(" tModel=").
155                         append(item.getTModelKey()).
156                         append(System.getProperty("line.separator"));
157                 return sb.toString();
158         }
159 
160         private void PrintContacts(Contacts contacts) {
161                 if (contacts == null) {
162                         return;
163                 }
164                 for (int i = 0; i < contacts.getContact().size(); i++) {
165                         System.out.println("Contact " + i + " type:" + contacts.getContact().get(i).getUseType());
166                         for (int k = 0; k < contacts.getContact().get(i).getPersonName().size(); k++) {
167                                 System.out.println("Name: " + contacts.getContact().get(i).getPersonName().get(k).getValue());
168                         }
169                         for (int k = 0; k < contacts.getContact().get(i).getEmail().size(); k++) {
170                                 System.out.println("Email: " + contacts.getContact().get(i).getEmail().get(k).getValue());
171                         }
172                         for (int k = 0; k < contacts.getContact().get(i).getAddress().size(); k++) {
173                                 System.out.println("Address sort code " + contacts.getContact().get(i).getAddress().get(k).getSortCode());
174                                 System.out.println("Address use type " + contacts.getContact().get(i).getAddress().get(k).getUseType());
175                                 System.out.println("Address tmodel key " + contacts.getContact().get(i).getAddress().get(k).getTModelKey());
176                                 for (int x = 0; x < contacts.getContact().get(i).getAddress().get(k).getAddressLine().size(); x++) {
177                                         System.out.println("Address line value " + contacts.getContact().get(i).getAddress().get(k).getAddressLine().get(x).getValue());
178                                         System.out.println("Address line key name " + contacts.getContact().get(i).getAddress().get(k).getAddressLine().get(x).getKeyName());
179                                         System.out.println("Address line key value " + contacts.getContact().get(i).getAddress().get(k).getAddressLine().get(x).getKeyValue());
180                                 }
181                         }
182                         for (int k = 0; k < contacts.getContact().get(i).getDescription().size(); k++) {
183                                 System.out.println("Desc: " + contacts.getContact().get(i).getDescription().get(k).getValue());
184                         }
185                         for (int k = 0; k < contacts.getContact().get(i).getPhone().size(); k++) {
186                                 System.out.println("Phone: " + contacts.getContact().get(i).getPhone().get(k).getValue());
187                         }
188                 }
189 
190         }
191 
192         private void PrintServiceDetail(BusinessService get) {
193                 if (get == null) {
194                         return;
195                 }
196                 System.out.println("Name " + ListToString(get.getName()));
197                 System.out.println("Desc " + ListToDescString(get.getDescription()));
198                 System.out.println("Key " + (get.getServiceKey()));
199                 System.out.println("Cat bag " + CatBagToString(get.getCategoryBag()));
200                 if (!get.getSignature().isEmpty()) {
201                         System.out.println("Item is digitally signed");
202                 } else {
203                         System.out.println("Item is not digitally signed");
204                 }
205                 PrintBindingTemplates(get.getBindingTemplates());
206         }
207 
208         /**
209          * This function is useful for translating UDDI's somewhat complex data
210          * format to something that is more useful.
211          *
212          * @param bindingTemplates
213          */
214         private void PrintBindingTemplates(BindingTemplates bindingTemplates) {
215                 if (bindingTemplates == null) {
216                         return;
217                 }
218                 for (int i = 0; i < bindingTemplates.getBindingTemplate().size(); i++) {
219                         System.out.println("Binding Key: " + bindingTemplates.getBindingTemplate().get(i).getBindingKey());
220             //TODO The UDDI spec is kind of strange at this point.
221                         //An access point could be a URL, a reference to another UDDI binding key, a hosting redirector (which is 
222                         //esscentially a pointer to another UDDI registry) or a WSDL Deployment
223                         //From an end client's perspective, all you really want is the endpoint.
224                         //http://uddi.org/pubs/uddi_v3.htm#_Ref8977716
225                         //So if you have a wsdlDeployment useType, fetch the wsdl and parse for the invocation URL
226                         //If its hosting director, you'll have to fetch that data from uddi recursively until the leaf node is found
227                         //Consult the UDDI specification for more information
228 
229                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint() != null) {
230                                 System.out.println("Access Point: " + bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getValue() + " type " + bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType());
231                                 if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType() != null) {
232                                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType().equalsIgnoreCase(AccessPointType.END_POINT.toString())) {
233                                                 System.out.println("Use this access point value as an invocation endpoint.");
234                                         }
235                                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType().equalsIgnoreCase(AccessPointType.BINDING_TEMPLATE.toString())) {
236                                                 System.out.println("Use this access point value as a reference to another binding template.");
237                                         }
238                                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType().equalsIgnoreCase(AccessPointType.WSDL_DEPLOYMENT.toString())) {
239                                                 System.out.println("Use this access point value as a URL to a WSDL document, which presumably will have a real access point defined.");
240                                         }
241                                         if (bindingTemplates.getBindingTemplate().get(i).getAccessPoint().getUseType().equalsIgnoreCase(AccessPointType.HOSTING_REDIRECTOR.toString())) {
242                                                 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).");
243                                         }
244                                 }
245                         }
246 
247                 }
248         }
249 
250         private enum AuthStyle {
251 
252                 HTTP_BASIC,
253                 HTTP_DIGEST,
254                 HTTP_NTLM,
255                 UDDI_AUTH,
256                 HTTP_CLIENT_CERT
257         }
258 
259         /**
260          * Gets a UDDI style auth token, otherwise, appends credentials to the
261          * ws proxies (not yet implemented)
262          *
263          * @param username
264          * @param password
265          * @param style
266          * @return
267          */
268         private String GetAuthKey(String username, String password) {
269                 try {
270 
271                         GetAuthToken getAuthTokenRoot = new GetAuthToken();
272                         getAuthTokenRoot.setUserID(username);
273                         getAuthTokenRoot.setCred(password);
274 
275                         // Making API call that retrieves the authentication token for the user.
276                         AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
277                         System.out.println(username + " AUTHTOKEN = (don't log auth tokens!");
278                         return rootAuthToken.getAuthInfo();
279                 } catch (Exception ex) {
280                         System.out.println("Could not authenticate with the provided credentials " + ex.getMessage());
281                 }
282                 return null;
283         }
284 
285         
286 
287         private void PrintBusinessInfo(BusinessInfos businessInfos) {
288                 if (businessInfos == null) {
289                         System.out.println("No data returned");
290                 } else {
291                         for (int i = 0; i < businessInfos.getBusinessInfo().size(); i++) {
292                                 System.out.println("===============================================");
293                                 System.out.println("Business Key: " + businessInfos.getBusinessInfo().get(i).getBusinessKey());
294                                 System.out.println("Name: " + ListToString(businessInfos.getBusinessInfo().get(i).getName()));
295 
296                                 System.out.println("Description: " + ListToDescString(businessInfos.getBusinessInfo().get(i).getDescription()));
297                                 System.out.println("Services:");
298                                 PrintServiceInfo(businessInfos.getBusinessInfo().get(i).getServiceInfos());
299                         }
300                 }
301         }
302 
303         private String ListToString(List<Name> name) {
304                 StringBuilder sb = new StringBuilder();
305                 for (int i = 0; i < name.size(); i++) {
306                         sb.append(name.get(i).getValue()).append(" ");
307                 }
308                 return sb.toString();
309         }
310 
311         private String ListToDescString(List<Description> name) {
312                 StringBuilder sb = new StringBuilder();
313                 for (int i = 0; i < name.size(); i++) {
314                         sb.append(name.get(i).getValue()).append(" ");
315                 }
316                 return sb.toString();
317         }
318 
319         private void PrintServiceInfo(ServiceInfos serviceInfos) {
320                 for (int i = 0; i < serviceInfos.getServiceInfo().size(); i++) {
321                         System.out.println("-------------------------------------------");
322                         System.out.println("Service Key: " + serviceInfos.getServiceInfo().get(i).getServiceKey());
323                         System.out.println("Owning Business Key: " + serviceInfos.getServiceInfo().get(i).getBusinessKey());
324                         System.out.println("Name: " + ListToString(serviceInfos.getServiceInfo().get(i).getName()));
325                 }
326         }
327 
328         private void PrintBusinessDetails(BusinessInfos businessInfos, String token) throws Exception {
329                 GetBusinessDetail gbd = new GetBusinessDetail();
330                 gbd.setAuthInfo(token);
331                 for (int i = 0; i < businessInfos.getBusinessInfo().size(); i++) {
332                         gbd.getBusinessKey().add(businessInfos.getBusinessInfo().get(i).getBusinessKey());
333                 }
334                 BusinessDetail businessDetail = inquiry.getBusinessDetail(gbd);
335                 for (int i = 0; i < businessDetail.getBusinessEntity().size(); i++) {
336                         System.out.println("Business Detail - key: " + businessDetail.getBusinessEntity().get(i).getBusinessKey());
337                         System.out.println("Name: " + ListToString(businessDetail.getBusinessEntity().get(i).getName()));
338                         System.out.println("CategoryBag: " + CatBagToString(businessDetail.getBusinessEntity().get(i).getCategoryBag()));
339                         PrintContacts(businessDetail.getBusinessEntity().get(i).getContacts());
340                 }
341         }
342 
343         private void PrintServiceDetailsByBusiness(BusinessInfos businessInfos, String token) throws Exception {
344                 for (int i = 0; i < businessInfos.getBusinessInfo().size(); i++) {
345                         GetServiceDetail gsd = new GetServiceDetail();
346                         for (int k = 0; k < businessInfos.getBusinessInfo().get(i).getServiceInfos().getServiceInfo().size(); k++) {
347                                 gsd.getServiceKey().add(businessInfos.getBusinessInfo().get(i).getServiceInfos().getServiceInfo().get(k).getServiceKey());
348                         }
349                         gsd.setAuthInfo(token);
350                         System.out.println("Fetching data for business " + businessInfos.getBusinessInfo().get(i).getBusinessKey());
351                         ServiceDetail serviceDetail = inquiry.getServiceDetail(gsd);
352                         for (int k = 0; k < serviceDetail.getBusinessService().size(); k++) {
353                                 PrintServiceDetail(serviceDetail.getBusinessService().get(k));
354                         }
355                         System.out.println("................");
356 
357                 }
358         }
359 }