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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 *16 */17package org.apache.juddi.example.partition;
1819import javax.xml.ws.BindingProvider;
20import org.apache.commons.configuration.ConfigurationException;
21import org.apache.juddi.v3.client.config.UDDIClerk;
2223import org.apache.juddi.v3.client.config.UDDIClient;
24import org.apache.juddi.v3.client.transport.Transport;
25import org.uddi.api_v3.AuthToken;
26import org.uddi.api_v3.CategoryBag;
27import org.uddi.api_v3.GetAuthToken;
28import org.uddi.api_v3.KeyedReference;
29import org.uddi.api_v3.Name;
30import org.uddi.api_v3.SaveTModel;
31import org.uddi.api_v3.TModel;
32import org.uddi.api_v3.TModelDetail;
33import org.uddi.v3_service.UDDIPublicationPortType;
34import org.uddi.v3_service.UDDISecurityPortType;
3536/**37 * An example for creating a key partition, aka key generator, aka 'special'38 * tModel39 *40 * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>41 */42publicclassSimpleCreateTmodelPartition {
4344privatestatic UDDISecurityPortType security = null;
45privatestatic UDDIPublicationPortType publish = null;
46privatestatic UDDIClient uddiClient = null;
4748/**49 * This sets up the ws proxies using uddi.xml in META-INF50 */51publicSimpleCreateTmodelPartition() {
52try {
53// create a manager and read the config in the archive; 54// you can use your config file name55 uddiClient = new UDDIClient("META-INF/partition-uddi.xml");
56 uddiClient.start();
5758// 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.xml61 Transport transport = uddiClient.getTransport("default");
62// Now you create a reference to the UDDI API6364 security = transport.getUDDISecurityService();
65 publish = transport.getUDDIPublishService();
66 } catch (Exception e) {
67 e.printStackTrace();
68 }
69 }
7071privatestaticvoid DisplayHelp() {
72//TODO73 }
7475/**76 * Main entry point77 *78 * @param args79 */80publicstaticvoid main(String args[]) throws ConfigurationException {
81if (args.length == 1 && args[0].equalsIgnoreCase("help")) {
82 DisplayHelp();
83return;
84 }
85SimpleCreateTmodelPartition sp = newSimpleCreateTmodelPartition();
86 sp.TmodelsTheLongAndHardWay(args);
87 sp.TmodelsTheEasyWay(args);
8889 uddiClient.stop();
90 }
9192publicvoid TmodelsTheEasyWay(String[] args) {
93try {
9495//This reads from the config file96 UDDIClerk clerk = uddiClient.getClerk("defaultClerk");
97//Since the password isn't set in the above config file, we have to provide it manually98//or thrown some fancy dialog box99 clerk.setPublisher("uddi"); //username100 clerk.setPassword("uddi"); //pass101102103 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!");
106107108//Now lets make a few tModels using the new domain109 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!");
116117 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!");
124125 clerk.discardAuthToken();
126 } catch (Exception e) {
127 e.printStackTrace();
128 }
129 }
130131publicvoid TmodelsTheLongAndHardWay(String[] args) {
132try {
133//TODO this is where you, the developer need to find out if your UDDI server134//supports UDDI's auth tokens or not. jUDDI does, so moving onwards.135AuthStyle style = AuthStyle.UDDI_AUTH;
136//Login137 String key = GetAuthKey("uddi", "password", style);
138139//Note: when creating a tModel Key Generator, aka Partition, you MUST follow the below pattern140//for jUDDI, the following is required141// Name142// CategoryBag/KR for the below fixed values143// a tModelKey that starts with uddi:<something>:keygenerator - all lower case144//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!");
161162//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);168169170//Now lets make a few tModels using the new domain171 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!");
179180 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!");
188189 } catch (Exception e) {
190 e.printStackTrace();
191 }
192 }
193194195private enum AuthStyle {
196197 HTTP,
198 UDDI_AUTH
199200 }
201202/**203 * Gets a UDDI style auth token, otherwise, appends credentials to the204 * ws proxies (not yet implemented)205 *206 * @param username207 * @param password208 * @param style209 * @return210 */211private String GetAuthKey(String username, String password, AuthStyle style) {
212switch (style) {
213case HTTP:
214 ((BindingProvider) publish).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
215 ((BindingProvider) publish).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
216returnnull;
217case UDDI_AUTH:
218try {
219220 GetAuthToken getAuthTokenRoot = new GetAuthToken();
221 getAuthTokenRoot.setUserID(username);
222 getAuthTokenRoot.setCred(password);
223224// 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!)");
227return rootAuthToken.getAuthInfo();
228 } catch (Exception ex) {
229 System.out.println("Could not authenticate with the provided credentials " + ex.getMessage());
230 }
231 }
232233returnnull;
234 }
235 }