This project has retired. For details please refer to its Attic page.
SimpleCreateTmodelPartition xref
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.partition;
18  
19  import javax.xml.ws.BindingProvider;
20  import org.apache.commons.configuration.ConfigurationException;
21  import org.apache.juddi.v3.client.config.UDDIClerk;
22  
23  import org.apache.juddi.v3.client.config.UDDIClient;
24  import org.apache.juddi.v3.client.transport.Transport;
25  import org.uddi.api_v3.AuthToken;
26  import org.uddi.api_v3.CategoryBag;
27  import org.uddi.api_v3.GetAuthToken;
28  import org.uddi.api_v3.KeyedReference;
29  import org.uddi.api_v3.Name;
30  import org.uddi.api_v3.SaveTModel;
31  import org.uddi.api_v3.TModel;
32  import org.uddi.api_v3.TModelDetail;
33  import org.uddi.v3_service.UDDIPublicationPortType;
34  import org.uddi.v3_service.UDDISecurityPortType;
35  
36  /**
37   * An example for creating a key partition, aka key generator, aka 'special'
38   * tModel
39   *
40   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
41   */
42  public class SimpleCreateTmodelPartition {
43  
44          private static UDDISecurityPortType security = null;
45          private static UDDIPublicationPortType publish = null;
46          private static UDDIClient uddiClient = null;
47  
48          /**
49           * This sets up the ws proxies using uddi.xml in META-INF
50           */
51          public SimpleCreateTmodelPartition() {
52                  try {
53                          // create a manager and read the config in the archive; 
54                          // you can use your config file name
55                          uddiClient = new UDDIClient("META-INF/partition-uddi.xml");
56                          uddiClient.start();
57  
58                          // a UddiClient can be a client to multiple UDDI nodes, so 
59                          // supply the nodeName (defined in your uddi.xml.
60                          // The transport can be WS, inVM etc which is defined in the uddi.xml
61                          Transport transport = uddiClient.getTransport("default");
62                          // Now you create a reference to the UDDI API
63  
64                          security = transport.getUDDISecurityService();
65                          publish = transport.getUDDIPublishService();
66                  } catch (Exception e) {
67                          e.printStackTrace();
68                  }
69          }
70  
71          private static void DisplayHelp() {
72                  //TODO
73          }
74  
75          /**
76           * Main entry point
77           *
78           * @param args
79           */
80          public static void main(String args[]) throws ConfigurationException {
81                  if (args.length == 1 && args[0].equalsIgnoreCase("help")) {
82                          DisplayHelp();
83                          return;
84                  }
85                  SimpleCreateTmodelPartition sp = new SimpleCreateTmodelPartition();
86                  sp.TmodelsTheLongAndHardWay(args);
87                  sp.TmodelsTheEasyWay(args);
88                  
89                  uddiClient.stop();
90          }
91  
92          public void TmodelsTheEasyWay(String[] args) {
93                  try {
94                          
95                          //This reads from the config file
96                          UDDIClerk clerk = uddiClient.getClerk("defaultClerk");
97                          //Since the password isn't set in the above config file, we have to provide it manually
98                          //or thrown some fancy dialog box
99                          clerk.setPublisher("uddi");     //username
100                         clerk.setPassword("uddi");     //pass
101                         
102 
103                         TModel keygen = UDDIClerk.createKeyGenator("www.mycoolcompany.com", "My Company's Keymodel generator", "en");
104                         clerk.register(keygen);
105                         System.out.println("Creation of Partition Success!");
106 
107 
108                         //Now lets make a few tModels using the new domain
109                         TModel tm = new TModel();
110                         tm.setName(new Name());
111                         tm.getName().setValue("My Company's Department");
112                         tm.getName().setLang("en");
113                         tm.setTModelKey("uddi:www.mycoolcompany.com:department");
114                         clerk.register(tm);
115                         System.out.println("Creation of tModel Department Success!");
116 
117                         tm = new TModel();
118                         tm.setName(new Name());
119                         tm.getName().setValue("My Company's Authentication Method");
120                         tm.getName().setLang("en");
121                         tm.setTModelKey("uddi:www.mycoolcompany.com:authmode");
122                         clerk.register(tm);
123                         System.out.println("Creation of tModel Auth Mode Success!");
124 
125                         clerk.discardAuthToken();
126                 } catch (Exception e) {
127                         e.printStackTrace();
128                 }
129         }
130 
131         public void TmodelsTheLongAndHardWay(String[] args) {
132                 try {
133                         //TODO this is where you, the developer need to find out if your UDDI server
134                         //supports UDDI's auth tokens or not. jUDDI does, so moving onwards.
135                         AuthStyle style = AuthStyle.UDDI_AUTH;
136                         //Login
137                         String key = GetAuthKey("uddi", "password", style);
138 
139                         //Note: when creating a tModel Key Generator, aka Partition, you MUST follow the below pattern
140                         //for jUDDI, the following is required
141                         //	Name
142                         //	CategoryBag/KR for the below fixed values
143                         //	a tModelKey that starts with uddi:<something>:keygenerator - all lower case
144                         //First, Here's the long way to do it to make a Key Generator tModel.
145                         SaveTModel st = new SaveTModel();
146                         st.setAuthInfo(key);
147                         TModel tm = new TModel();
148                         tm.setName(new Name());
149                         tm.getName().setValue("My Company's Keymodel generator");
150                         tm.getName().setLang("en");
151                         tm.setCategoryBag(new CategoryBag());
152                         KeyedReference kr = new KeyedReference();
153                         kr.setTModelKey("uddi:uddi.org:categorization:types");
154                         kr.setKeyName("uddi-org:keyGenerator");
155                         kr.setKeyValue("keyGenerator");
156                         tm.getCategoryBag().getKeyedReference().add(kr);
157                         tm.setTModelKey("uddi:www.mycoolcompany.com:keygenerator");
158                         st.getTModel().add(tm);
159                         TModelDetail saveTModel = publish.saveTModel(st);
160                         System.out.println("Creation of Partition Success!");
161 
162                         //Here's the easy and fun way!
163                         TModel keygen = UDDIClerk.createKeyGenator("www.mycoolcompany.com", "My Company's Keymodel generator", "en");
164                         //st = new SaveTModel();
165                         //st.setAuthInfo(key);
166                         //st.getTModel().add(keygen);
167                         //saveTModel = publish.saveTModel(st);
168                         
169 
170                         //Now lets make a few tModels using the new domain
171                         tm = new TModel();
172                         tm.setName(new Name());
173                         tm.getName().setValue("My Company's Department");
174                         tm.getName().setLang("en");
175                         tm.setTModelKey("uddi:www.mycoolcompany.com:department");
176                         st.getTModel().add(tm);
177                         saveTModel = publish.saveTModel(st);
178                         System.out.println("Creation of tModel Department Success!");
179 
180                         tm = new TModel();
181                         tm.setName(new Name());
182                         tm.getName().setValue("My Company's Authentication Method");
183                         tm.getName().setLang("en");
184                         tm.setTModelKey("uddi:www.mycoolcompany.com:authmode");
185                         st.getTModel().add(tm);
186                         saveTModel = publish.saveTModel(st);
187                         System.out.println("Creation of tModel Auth Mode Success!");
188 
189                 } catch (Exception e) {
190                         e.printStackTrace();
191                 }
192         }
193 
194         
195         private enum AuthStyle {
196 
197                 HTTP,
198                 UDDI_AUTH
199 
200         }
201 
202         /**
203          * Gets a UDDI style auth token, otherwise, appends credentials to the
204          * ws proxies (not yet implemented)
205          *
206          * @param username
207          * @param password
208          * @param style
209          * @return
210          */
211         private String GetAuthKey(String username, String password, AuthStyle style) {
212                 switch (style) {
213                         case HTTP:
214                                 ((BindingProvider) publish).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
215                                 ((BindingProvider) publish).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
216                                 return null;
217                         case UDDI_AUTH:
218                                 try {
219 
220                                         GetAuthToken getAuthTokenRoot = new GetAuthToken();
221                                         getAuthTokenRoot.setUserID(username);
222                                         getAuthTokenRoot.setCred(password);
223 
224                                         // Making API call that retrieves the authentication token for the 'root' user.
225                                         AuthToken rootAuthToken = security.getAuthToken(getAuthTokenRoot);
226                                         System.out.println(username + " AUTHTOKEN = (don't log auth tokens!)");
227                                         return rootAuthToken.getAuthInfo();
228                                 } catch (Exception ex) {
229                                         System.out.println("Could not authenticate with the provided credentials " + ex.getMessage());
230                                 }
231                 }
232 
233                 return null;
234         }
235 }