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