View Javadoc
1   /*
2    * Copyright 2001-2013 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.ArrayList;
20  import java.util.GregorianCalendar;
21  import java.util.List;
22  import javax.xml.datatype.DatatypeFactory;
23  import javax.xml.datatype.XMLGregorianCalendar;
24  import javax.xml.ws.Holder;
25  import org.apache.juddi.v3.client.config.UDDIClient;
26  import org.apache.juddi.v3.client.transport.Transport;
27  import org.apache.juddi.v3_service.JUDDIApiPortType;
28  import org.uddi.api_v3.*;
29  import org.uddi.v3_service.UDDIPublicationPortType;
30  import org.uddi.v3_service.UDDISecurityPortType;
31  
32  /**
33   * This will create two businesses under different users, then setup a
34   * relationship between the two
35   *
36   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
37   */
38  public class UddiRelatedBusinesses {
39  
40          private UDDISecurityPortType security = null;
41          private UDDIPublicationPortType publish = null;
42  
43          public UddiRelatedBusinesses() {
44                  try {
45                          // create a manager and read the config in the archive; 
46                          // you can use your config file name
47                          UDDIClient clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
48                          Transport transport = clerkManager.getTransport();
49                          // Now you create a reference to the UDDI API
50                          security = transport.getUDDISecurityService();
51                          publish = transport.getUDDIPublishService();
52                  } catch (Exception e) {
53                          e.printStackTrace();
54                  }
55          }
56  
57          public void fire(String businessKey, String authInfo, String businessKey1, String authInfo1, String relationship) throws Exception {
58                  try {
59  
60                          GregorianCalendar gcal = new GregorianCalendar();
61                          gcal.setTimeInMillis(System.currentTimeMillis());
62                          //ROOT creates half of the relationship
63                          //create a business relationship (publisher assertion)
64                          Holder<List<PublisherAssertion>> x = new Holder<List<PublisherAssertion>>();
65                          PublisherAssertion pa = new PublisherAssertion();
66                          pa.setFromKey(businessKey);
67                          pa.setToKey(businessKey1);
68                          pa.setKeyedReference(new KeyedReference());
69                          pa.getKeyedReference().setKeyName("Subsidiary");
70                          pa.getKeyedReference().setKeyValue(relationship);
71  
72                          pa.getKeyedReference().setTModelKey("uddi:uddi.org:relationships");
73                          x.value = new ArrayList<PublisherAssertion>();
74                          x.value.add(pa);
75                          publish.setPublisherAssertions(authInfo, x);
76  
77                          //now "UDDI" the user, creates the other half. It has to be mirrored exactly
78                          x = new Holder<List<PublisherAssertion>>();
79                          pa = new PublisherAssertion();
80                          pa.setFromKey(businessKey);
81                          pa.setToKey(businessKey1);
82                          pa.setKeyedReference(new KeyedReference());
83                          pa.getKeyedReference().setKeyName("Subsidiary");
84                          pa.getKeyedReference().setKeyValue(relationship);
85  
86                          pa.getKeyedReference().setTModelKey("uddi:uddi.org:relationships");
87                          x.value = new ArrayList<PublisherAssertion>();
88                          x.value.add(pa);
89                          publish.setPublisherAssertions(authInfo1, x);
90  
91                          System.out.println("Success!");
92                          /*
93                           * Here's some notes:
94                           * You can use
95                           * List<AssertionStatusItem> assertionStatusReport = publish.getAssertionStatusReport(UDDIAuthToken.getAuthInfo(), CompletionStatus.STATUS_FROM_KEY_INCOMPLETE);
96                           * to determine if there's any assertions/relationships requests that are pending
97                           * this should have one item it in, the relationship that's incomplete
98                           * 
99                           * There's also publish.deletePublisherAssertions();
100                          */
101                 } catch (Exception e) {
102                         e.printStackTrace();
103                 }
104         }
105 
106         public static void main(String args[]) throws Exception {
107                 UddiRelatedBusinesses sp = new UddiRelatedBusinesses();
108 
109                 GetAuthToken getAuthTokenRoot = new GetAuthToken();
110                 getAuthTokenRoot.setUserID("root");
111                 getAuthTokenRoot.setCred("root");
112 
113                 // Making API call that retrieves the authentication token for the 'root' user.
114                 AuthToken rootAuthToken = sp.security.getAuthToken(getAuthTokenRoot);
115                 System.out.println("root AUTHTOKEN = " + "don't log auth tokens!");
116                 BusinessEntity rootbiz = sp.createBusiness("root");
117 
118                 getAuthTokenRoot = new GetAuthToken();
119                 getAuthTokenRoot.setUserID("uddi");
120                 getAuthTokenRoot.setCred("uddi");
121 
122                 // Making API call that retrieves the authentication token for the 'root' user.
123                 AuthToken uddiAuthToken = sp.security.getAuthToken(getAuthTokenRoot);
124                 System.out.println("uddi AUTHTOKEN = " + "don't log auth tokens!");
125                 BusinessEntity uddibiz = sp.createBusiness("uddi");
126 
127                 //save user uddi's business
128                 SaveBusiness sb = new SaveBusiness();
129                 sb.setAuthInfo(uddiAuthToken.getAuthInfo());
130                 sb.getBusinessEntity().add(uddibiz);
131                 BusinessDetail uddibize = sp.publish.saveBusiness(sb);
132 
133                 sb = new SaveBusiness();
134                 sb.setAuthInfo(rootAuthToken.getAuthInfo());
135                 sb.getBusinessEntity().add(rootbiz);
136                 BusinessDetail rootbize = sp.publish.saveBusiness(sb);
137 
138                 sp.fire(rootbize.getBusinessEntity().get(0).getBusinessKey(), rootAuthToken.getAuthInfo(),
139                         uddibize.getBusinessEntity().get(0).getBusinessKey(), uddiAuthToken.getAuthInfo(),
140                         "parent-child");
141         }
142 
143         private BusinessEntity createBusiness(String user) {
144                 BusinessEntity be = new BusinessEntity();
145                 be.getName().add(new Name(user + "'s business", null));
146                 return be;
147         }
148 
149 }