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.GregorianCalendar;
20  import javax.xml.datatype.DatatypeFactory;
21  import javax.xml.datatype.XMLGregorianCalendar;
22  import javax.xml.ws.Holder;
23  import org.apache.juddi.v3.client.config.UDDIClient;
24  import org.apache.juddi.v3.client.transport.Transport;
25  import org.uddi.api_v3.*;
26  import org.uddi.custody_v3.KeyBag;
27  import org.uddi.custody_v3.TransferEntities;
28  import org.uddi.custody_v3.TransferToken;
29  import org.uddi.v3_service.UDDICustodyTransferPortType;
30  import org.uddi.v3_service.UDDIInquiryPortType;
31  import org.uddi.v3_service.UDDIPublicationPortType;
32  import org.uddi.v3_service.UDDISecurityPortType;
33  
34  /**
35   * This provides an example of how to transfer custody of a business from one
36   * user to another on the same UDDI node. All child objects are also transfer
37   *
38   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
39   */
40  public class UddiCustodyTransfer {
41  
42          private UDDISecurityPortType security = null;
43          private UDDIPublicationPortType publish = null;
44          private UDDIInquiryPortType uddiInquiryService = null;
45          private UDDICustodyTransferPortType custodyTransferPortType = null;
46  
47          public UddiCustodyTransfer() {
48                  try {
49                          // create a manager and read the config in the archive; 
50                          // you can use your config file name
51                          UDDIClient clerkManager = new UDDIClient("META-INF/simple-publish-uddi.xml");
52                          Transport transport = clerkManager.getTransport();
53                          // Now you create a reference to the UDDI API
54                          security = transport.getUDDISecurityService();
55  
56                          publish = transport.getUDDIPublishService();
57                          uddiInquiryService = transport.getUDDIInquiryService();
58                          custodyTransferPortType = transport.getUDDICustodyTransferService();
59                  } catch (Exception e) {
60                          e.printStackTrace();
61                  }
62          }
63  
64          public void main(String args[]) throws Exception {
65                  UddiCustodyTransfer sp = new UddiCustodyTransfer();
66  
67                  GetAuthToken getAuthTokenRoot = new GetAuthToken();
68                  getAuthTokenRoot.setUserID("root");
69                  getAuthTokenRoot.setCred("root");
70  
71                  // Making API call that retrieves the authentication token for the 'root' user.
72                  AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
73                  System.out.println("root AUTHTOKEN = " + "don't log auth tokens!");
74  
75                  getAuthTokenRoot = new GetAuthToken();
76                  getAuthTokenRoot.setUserID("uddi");
77                  getAuthTokenRoot.setCred("uddi");
78  
79                  // Making API call that retrieves the authentication token for the 'root' user.
80                  AuthToken uddiAuthToken = security.getAuthToken(getAuthTokenRoot);
81                  System.out.println("uddi AUTHTOKEN = " + "don't log auth tokens!");
82                  BusinessEntity biz = sp.createBusiness("uddi");
83  
84                  //save user uddi's business
85                  SaveBusiness sb = new SaveBusiness();
86                  sb.setAuthInfo(uddiAuthToken.getAuthInfo());
87                  sb.getBusinessEntity().add(biz);
88                  BusinessDetail saveBusiness = publish.saveBusiness(sb);
89  
90                  sp.transferBusiness(uddiAuthToken.getAuthInfo(), "uddi", rootAuthToken.getAuthInfo(), "root", saveBusiness.getBusinessEntity().get(0).getBusinessKey());
91          }
92  
93          public void transferBusiness(String fromUser, String fromUserAuthToken, String toUser, String toUserAuthToken,
94                  String BusinessKey) throws Exception {
95  
96                  System.out.println("Transfering business key " + BusinessKey);
97                  GregorianCalendar gcal = new GregorianCalendar();
98                  gcal.setTimeInMillis(System.currentTimeMillis());
99         
100                 //Create a transfer token from fromUser to toUser
101                 KeyBag kb = new KeyBag();
102                 kb.getKey().add(BusinessKey);
103                 Holder<String> nodeidOUT = new Holder<String>();
104                 Holder<XMLGregorianCalendar> expiresOUT = new Holder<XMLGregorianCalendar>();
105                 Holder<byte[]> tokenOUT = new Holder<byte[]>();
106                 custodyTransferPortType.getTransferToken(fromUserAuthToken, kb, nodeidOUT, expiresOUT, tokenOUT);
107 
108                 System.out.println("Transfer token obtained. Give this to user " + toUser);
109                 System.out.println("Expires " + expiresOUT.value.toXMLFormat());
110                 System.out.println("Node " + nodeidOUT.value);
111                 System.out.println("Token " + org.apache.commons.codec.binary.Base64.encodeBase64String(tokenOUT.value));
112 
113                 if (toUser == null || toUser.length() == 0 || toUserAuthToken == null || toUserAuthToken.length() == 0) {
114                         System.out.println("The toUser parameters are either null or empty, I can't complete the transfer here");
115                         return;
116                 }
117 
118                 //The magic part happens here, the user ROOT needs to give the user UDDI the token information out of band
119                 //in practice, all values must match exactly
120                 //UDDI now accepts the transfer
121                 TransferEntities te = new TransferEntities();
122                 te.setAuthInfo(toUserAuthToken);
123                 te.setKeyBag(kb);
124                 TransferToken tt = new TransferToken();
125                 tt.setExpirationTime(expiresOUT.value);
126                 tt.setNodeID(nodeidOUT.value);
127                 tt.setOpaqueToken(tokenOUT.value);
128                 te.setTransferToken(tt);
129                 System.out.println("Excuting transfer...");
130                 custodyTransferPortType.transferEntities(te);
131                 System.out.println("Complete! verifing ownership change...");
132 
133                 //confirm the transfer
134                 GetOperationalInfo go = new GetOperationalInfo();
135                 go.setAuthInfo(fromUserAuthToken);
136                 go.getEntityKey().add(BusinessKey);
137                 OperationalInfos operationalInfo = uddiInquiryService.getOperationalInfo(go);
138                 boolean ok = false;
139                 boolean found = false;
140                 for (int i = 0; i < operationalInfo.getOperationalInfo().size(); i++) {
141                         if (operationalInfo.getOperationalInfo().get(i).getEntityKey().equalsIgnoreCase(BusinessKey)) {
142                                 found = true;
143                                 if (operationalInfo.getOperationalInfo().get(i).getAuthorizedName().equalsIgnoreCase(fromUser)) {
144                                         System.out.println("Transfer unexpected failed");
145                                 }
146                                 if (operationalInfo.getOperationalInfo().get(i).getAuthorizedName().equalsIgnoreCase(toUser)) {
147                                         ok = true;
148                                 }
149 
150                         }
151                 }
152                 if (!found) {
153                         System.out.println("Could get the operational info the transfed business");
154                 }
155                 System.out.println("Transfer " + (ok ? "success" : " failed"));
156         }
157 
158         private BusinessEntity createBusiness(String user) {
159                 BusinessEntity be = new BusinessEntity();
160                 be.getName().add(new Name(user + "'s business", null));
161                 return be;
162         }
163 
164 }