# RegisterClient.java Sample

This topic inludes the source code for the RegisterClient.java Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/ProxyClient/register/

## Sample Source Code

`001 package register; 002 003 import java.io.BufferedReader; 004 import java.io.InputStreamReader; 005 import java.io.IOException; 006 import java.rmi.RemoteException; 007 import java.util.Date; 008 009 // proxy classes are located in the weblogic.jws.proxies package. 010 import weblogic.jws.proxies.RegisterPerson_Impl; 011 import weblogic.jws.proxies.RegisterPersonSoap; 012 013 /** 014 * Import the Person class. 015 * Don't import the Contact data structure, because we're illustrating 016 * use of both SOAP document/literal and SOAP-RPC encoded parameters, so 017 * we declare them with fully qualified names as we use the classes below. 018 */ 019 import org.openuri.www.Person; 020 021 /** 022 * <p>This is an example of a web service client that uses the web service 023 * proxy generated by WebLogic Server.</p> 024 * 025 * <p>See "RegisterClient.java Sample" in the WebLogic Workshop documentation.</p> 026 * 027 * <p>This client accesses the web service via proxy classes found in 028 * WEB-INF/lib/RegisterPerson.jar. That JAR file is obtained by selecting 029 * the "Java Proxy" link on a web service's Test View Overview Page. If 030 * the web service is hosted locally, the URL of the Overview Page is: 031 * http://localhost:7001/samples/proxy/register/RegisterPerson.jws</p> 032 */ 033 public class RegisterClient 034 { 035 036 /* 037 * <p>Declare the proxy objects. There is a main proxy object with the 038 * same name as the web service but with "_Impl" appended, then 039 * protocol-specific proxy classes that contain the actual method stubs 040 * for the web service's operations.</p> 041 * 042 * <p>This web service supports SOAP over HTTP, so it 043 * has a proxy class with the name <web service>SOAP.</p> 044 */ 045 RegisterPerson_Impl m_proxyImpl = null; 046 RegisterPersonSoap m_proxy = null; 047 048 boolean m_ConversationStarted = false; 049 050 /** 051 * Utility method that prints prompts to System.out and gets input 052 * values from System.in. Also validates input to make sure web service 053 * is in proper conversational state for the requested action. 054 */ 055 private int getInput() 056 { 057 String input = null; 058 int menuItem = 0; 059 boolean validSelectionMade = false; 060 061 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 062 063 while( !validSelectionMade ) 064 { 065 System.out.println("_______________________"); 066 System.out.println(" 1 - set person"); 067 System.out.println(" 2 - get person"); 068 System.out.println(" 3 - set home contact info to A"); 069 System.out.println(" 4 - set home contact info to B"); 070 System.out.println(" 5 - get home contact"); 071 System.out.println(" 6 - set work contact info to A"); 072 System.out.println(" 7 - set work contact info to B"); 073 System.out.println(" 8 - get work contact"); 074 System.out.println(" 9 - exit"); 075 System.out.println(); 076 077 System.out.print("Enter menu item number:"); 078 try 079 { 080 input = stdin.readLine(); 081 menuItem = Integer.parseInt(input); 082 } 083 catch (IOException ex) 084 { 085 ex.printStackTrace(); 086 continue; 087 } 088 catch (NumberFormatException ex) { 089 System.out.println("Please enter a valid menu item number:"); 090 continue; 091 } 092 093 /* 094 * setPerson is a "start conversation" method. Make sure there isn't already 095 * a conversation started. 096 */ 097 if( menuItem == 1 ) 098 { 099 if( !m_ConversationStarted ) 100 { 101 validSelectionMade = true; 102 } 103 else 104 { 105 System.out.println("There is a current conversation already, " + 106 "please choose another menu option."); 107 } 108 } 109 110 /* 111 * All methods except setPerson require that a conversation already exist. 112 */ 113 else if( menuItem > 1 && menuItem < 9 ) 114 { 115 if( m_ConversationStarted ) 116 { 117 validSelectionMade = true; 118 } 119 else 120 { 121 System.out.println("There is no current conversation, " + 122 "please choose setPerson (1) first."); 123 } 124 } 125 else if( menuItem == 9 ) 126 { 127 validSelectionMade = true; 128 } 129 130 } 131 System.out.println(); 132 133 return menuItem; 134 } 135 136 /** 137 * Utility function to print a Contact to stdout. Specifically, prints a Contact 138 * that is SOAP document/literal encoded. 139 */ 140 public void printLiteralContact(org.openuri.www.Contact c) 141 { 142 if( c != null ) 143 { 144 System.out.println(" " + c.getStreet()); 145 System.out.print(" " + c.getCity()); 146 System.out.println(", " + c.getState() + " " + c.getZip()); 147 } 148 else 149 { 150 System.out.println(" none"); 151 } 152 } 153 154 /** 155 * Utility function that prints a Person to stdout. 156 */ 157 public void printPerson(Person p) 158 { 159 if( p != null ) 160 { 161 System.out.println(" " + p.getName()); 162 163 System.out.println(" home:"); 164 printLiteralContact(p.getHome()); 165 166 System.out.println(" work:"); 167 printLiteralContact(p.getWork()); 168 } 169 else 170 { 171 System.out.println(" person is null"); 172 } 173 } 174 175 /** 176 * Main work method. Loops forever (until the "exit" menu item is selected), 177 * calling getInput to collect input and then invoking the requested operation 178 * on the web service. 179 */ 180 private void run() 181 { 182 boolean done = false; 183 int menuItem = 0; 184 185 /* 186 * Utility variables to hold values returned from web service methods 187 */ 188 Person p; 189 org.openuri.www.Contact soapLiteralContact; 190 org.openuri.www.encodedTypes.Contact soapRPCContact; 191 192 /* 193 * Initialized variables used in the setPerson, setHomeContact and setWorkContact 194 * operations. ContactA is only ever used in SOAP document/literal. ContactB is used 195 * in SOAP document/literal form only when passed to setPerson. The "set xxx contact 196 * to B" operations always use the SOAP-RPC versions of the operation and Contact 197 * class for illustration. 198 */ 199 org.openuri.www.Contact soapLiteralContactA = 200 new org.openuri.www.Contact("100 A Street", "Aville", "AA", "11111", "111-111-1111"); 201 org.openuri.www.Contact soapLiteralContactB = 202 new org.openuri.www.Contact("200 B Street", "Bville", "BB", "22222", "222-222-2222"); 203 org.openuri.www.encodedTypes.Contact soapRPCContactB = 204 new org.openuri.www.encodedTypes.Contact("200 B Street", "Bville", "BB", "22222", "222-222-2222"); 205 206 try 207 { 208 /* 209 * Instantiate the main proxy class. The proxy class has the same name as the 210 * web service, with "_Impl" appended. 211 * 212 * Note that there is an alternative constructor in which you can pass the URL of 213 * the target service's WSDL (which is typically the URL of the service with ?WSDL 214 * appended). That technique is used for "dynamic" proxies. The static WSDL is 215 * already built into the generated proxy and will be used if the no-argument 216 * proxy constructor is used (as below). Using the dynamic form causes an extra 217 * hit on the server for every web service request submitted. 218 */ 219 m_proxyImpl = new RegisterPerson_Impl(); 220 } 221 catch (IOException ex) 222 { 223 System.out.println("Error getting proxy"); 224 ex.printStackTrace(); 225 } 226 /* 227 * Get the protocol-specific proxy class. 228 */ 229 m_proxy = m_proxyImpl.getRegisterPersonSoap(); 230 231 while( !done ) 232 { 233 // get the menu item and perform the action 234 menuItem = getInput(); 235 236 switch( menuItem ) 237 { 238 case 1: 239 System.out.println("set person"); 240 241 p = new Person("John Doe", soapLiteralContactA, soapLiteralContactB); 242 243 /* 244 * Invoke the web service's setPerson method by invoking the stub method 245 * of the proxy class. 246 * 247 * All proxy class stub methods declare that they throw RemoteException, 248 * so we have to surround all calls to proxy stub methods in try/catch 249 * blocks. 250 */ 251 try 252 { 253 m_proxy.setPerson(p); 254 m_ConversationStarted = true; 255 } 256 catch(RemoteException ex) 257 { 258 System.out.println("error calling setPerson"); 259 ex.printStackTrace(); 260 } 261 262 break; 263 case 2: 264 System.out.println("get person"); 265 266 p = null; 267 268 /* 269 * Invoke the web service's getPerson method. 270 */ 271 try 272 { 273 p = m_proxy.getPerson(); 274 } 275 catch(RemoteException ex) 276 { 277 System.out.println("error calling getPerson"); 278 ex.printStackTrace(); 279 } 280 281 printPerson(p); 282 283 break; 284 case 3: 285 System.out.println("set home contact info to A"); 286 287 /* 288 * Invoke the web service's setHomeContact method. 289 * 290 * For illustration purposes, the target web service defines 291 * both SOAP document/literal and SOAP-RPC versions of the 292 * setHomeContact and setWorkContact methods. This action uses 293 * the document/literal version. 294 */ 295 try 296 { 297 m_proxy.setHomeContact(soapLiteralContactA); 298 } 299 catch(RemoteException ex) 300 { 301 System.out.println("error calling setHomeContact"); 302 ex.printStackTrace(); 303 } 304 305 break; 306 case 4: 307 System.out.println("set home contact info to B"); 308 309 /* 310 * Invoke the web service's setHomeContactRPC method. 311 * 312 * For illustration purposes, the target web service defines 313 * both SOAP document/literal and SOAP-RPC versions of the 314 * setHomeContact and setWorkContact methods. This action uses 315 * the SOAP-RPC version. 316 */ 317 try 318 { 319 m_proxy.setHomeContactRPC(soapRPCContactB); 320 } 321 catch(RemoteException ex) 322 { 323 System.out.println("error calling setHomeContactRPC"); 324 ex.printStackTrace(); 325 } 326 327 break; 328 case 5: 329 System.out.println("get home contact"); 330 331 /* 332 * Invoke the web service's getHomeContact method. 333 */ 334 try 335 { 336 soapLiteralContact = m_proxy.getHomeContact(); 337 printLiteralContact(soapLiteralContact); 338 } 339 catch(RemoteException ex) 340 { 341 System.out.println("error calling getHomeContact"); 342 ex.printStackTrace(); 343 } 344 345 break; 346 case 6: 347 System.out.println("set work contact info to A"); 348 349 /* 350 * Invoke the web service's setWorkContact method. 351 * 352 * For illustration purposes, the target web service defines 353 * both SOAP document/literal and SOAP-RPC versions of the 354 * setHomeContact and setWorkContact methods. This action uses 355 * the document/literal version. 356 */ 357 try 358 { 359 m_proxy.setWorkContact(soapLiteralContactA); 360 } 361 catch(RemoteException ex) 362 { 363 System.out.println("error calling setWorkContact"); 364 ex.printStackTrace(); 365 } 366 367 break; 368 case 7: 369 System.out.println("set work contact info to B"); 370 371 /* 372 * Invoke the web service's setWorkContactRPC method. 373 * 374 * For illustration purposes, the target web service defines 375 * both SOAP document/literal and SOAP-RPC versions of the 376 * setHomeContact and setWorkContact methods. This action uses 377 * the SOAP-RPC version. 378 */ 379 try 380 { 381 m_proxy.setWorkContactRPC(soapRPCContactB); 382 } 383 catch(RemoteException ex) 384 { 385 System.out.println("error calling setWorkContactRPC"); 386 ex.printStackTrace(); 387 } 388 389 break; 390 case 8: 391 System.out.println("get work contact"); 392 393 /* 394 * Invoke the web service's getWorkContact method. 395 */ 396 try 397 { 398 soapLiteralContact = m_proxy.getWorkContact(); 399 printLiteralContact(soapLiteralContact); 400 } 401 catch(RemoteException ex) 402 { 403 System.out.println("error calling getWorkContact"); 404 ex.printStackTrace(); 405 } 406 407 break; 408 case 9: 409 System.out.println("exit"); 410 411 /* 412 * Invoke the web service's endSession method, which terminates the 413 * conversation. This client program also exits. 414 */ 415 if( m_ConversationStarted ) 416 { 417 try 418 { 419 m_proxy.endSession(); 420 m_ConversationStarted = false; 421 } 422 catch(RemoteException ex) 423 { 424 System.out.println("error calling endSession"); 425 ex.printStackTrace(); 426 } 427 } 428 429 done = true; 430 break; 431 } 432 } 433 } 434 435 public static void main(String [] args) 436 { 437 438 RegisterClient client = new RegisterClient(); 439 client.run(); 440 441 return; 442 } 443 }`