This project has retired. For details please refer to its Attic page.
UddiCustodyTransfer xref
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.samples;
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                  DatatypeFactory df = DatatypeFactory.newInstance();
98                  GregorianCalendar gcal = new GregorianCalendar();
99                  gcal.setTimeInMillis(System.currentTimeMillis());
100                 XMLGregorianCalendar xcal = df.newXMLGregorianCalendar(gcal);
101 
102                 //Create a transfer token from fromUser to toUser
103                 KeyBag kb = new KeyBag();
104                 kb.getKey().add(BusinessKey);
105                 Holder<String> nodeidOUT = new Holder<String>();
106                 Holder<XMLGregorianCalendar> expiresOUT = new Holder<XMLGregorianCalendar>();
107                 Holder<byte[]> tokenOUT = new Holder<byte[]>();
108                 custodyTransferPortType.getTransferToken(fromUserAuthToken, kb, nodeidOUT, expiresOUT, tokenOUT);
109 
110                 System.out.println("Transfer token obtained. Give this to user " + toUser);
111                 System.out.println("Expires " + expiresOUT.value.toXMLFormat());
112                 System.out.println("Node " + nodeidOUT.value);
113                 System.out.println("Token " + org.apache.commons.codec.binary.Base64.encodeBase64String(tokenOUT.value));
114 
115                 if (toUser == null || toUser.length() == 0 || toUserAuthToken == null || toUserAuthToken.length() == 0) {
116                         System.out.println("The toUser parameters are either null or empty, I can't complete the transfer here");
117                         return;
118                 }
119 
120                 //The magic part happens here, the user ROOT needs to give the user UDDI the token information out of band
121                 //in practice, all values must match exactly
122                 //UDDI now accepts the transfer
123                 TransferEntities te = new TransferEntities();
124                 te.setAuthInfo(toUserAuthToken);
125                 te.setKeyBag(kb);
126                 TransferToken tt = new TransferToken();
127                 tt.setExpirationTime(expiresOUT.value);
128                 tt.setNodeID(nodeidOUT.value);
129                 tt.setOpaqueToken(tokenOUT.value);
130                 te.setTransferToken(tt);
131                 System.out.println("Excuting transfer...");
132                 custodyTransferPortType.transferEntities(te);
133                 System.out.println("Complete! verifing ownership change...");
134 
135                 //confirm the transfer
136                 GetOperationalInfo go = new GetOperationalInfo();
137                 go.setAuthInfo(fromUserAuthToken);
138                 go.getEntityKey().add(BusinessKey);
139                 OperationalInfos operationalInfo = uddiInquiryService.getOperationalInfo(go);
140                 boolean ok = false;
141                 boolean found = false;
142                 for (int i = 0; i < operationalInfo.getOperationalInfo().size(); i++) {
143                         if (operationalInfo.getOperationalInfo().get(i).getEntityKey().equalsIgnoreCase(BusinessKey)) {
144                                 found = true;
145                                 if (operationalInfo.getOperationalInfo().get(i).getAuthorizedName().equalsIgnoreCase(fromUser)) {
146                                         System.out.println("Transfer unexpected failed");
147                                 }
148                                 if (operationalInfo.getOperationalInfo().get(i).getAuthorizedName().equalsIgnoreCase(toUser)) {
149                                         ok = true;
150                                 }
151 
152                         }
153                 }
154                 if (!found) {
155                         System.out.println("Could get the operational info the transfed business");
156                 }
157                 System.out.println("Transfer " + (ok ? "success" : " failed"));
158         }
159 
160         private BusinessEntity CreateBusiness(String user) {
161                 BusinessEntity be = new BusinessEntity();
162                 be.getName().add(new Name(user + "'s business", null));
163                 return be;
164         }
165 
166 }