/*********************************************************************** * * Copyright (C) 2005-2006 Novell, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; version 2.1 * of the License. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * To contact Novell about this file by physical or electronic mail, * you may find current contact information at www.novell.com. * ***********************************************************************/ import com.novell.casa.MiCasa; import com.novell.casa.MiCasaException; import com.novell.casa.NetCredential; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class JavaSample { static String APPID = "JavaSampleApp"; static String USERNAME = "JavaSampl€User"; static String PASSWORD = "JavaSampl€Password"; public static void main(String[] args) { System.out.println("*******************************************************"); System.out.println("******** miCASA Sample Program written in Java ******"); System.out.println("*******************************************************"); ShowMenu(); return; } static private void ShowMenu() { while (true) { System.out.println(""); System.out.println("********** Menu *********"); System.out.println("* 1. Add secret *"); System.out.println("* 2. Display secret *"); System.out.println("* 3. Remove secret *"); System.out.println("* 4. Run automated test *"); System.out.println("* 5. Quit *"); System.out.println("***************************"); System.out.println("Select option and Press enter"); try { byte[] ba = new byte[100]; int bytesRead; bytesRead = System.in.read(ba); if (ba[0] == ('5')) break; if (ba[0] == '1') AddSecret(); else if (ba[0] == ('3')) RemoveSecret(); else if (ba[0] == ('2')) DisplaySecret(); else if (ba[0] == ('4')) RunTest(); } catch (Exception e) { } } } static String GetInfo(String prompt) { System.out.print("Enter " + prompt); BufferedReader keyb = new BufferedReader(new InputStreamReader(System.in)); try { String line = keyb.readLine(); return line; } catch (IOException e) { e.printStackTrace(); return null; } } static private void AddSecret() { while (true) { String sID = GetInfo("SecretID: "); String sUsername = GetInfo("Username: "); String sPassword = GetInfo("Password: "); if (sID != null && sUsername != null && sPassword != null) { try { MiCasa.setCredential(0, sID, null, MiCasa.USERNAME_TYPE_CN_F, sUsername, sPassword); } catch (MiCasaException e) { System.out.println(e.getMessage()); } catch (Exception e) { e.printStackTrace(); } return; } else { System.out.println("Invalid input, try again"); } } } static private void RemoveSecret() { String sID = GetInfo("SecretID to remove: "); if (sID != null) { try { MiCasa.removeCredential(0, sID, null); } catch (MiCasaException e) { System.out.println(e.getMessage()); } catch (Exception e) { e.printStackTrace(); } } } static private void DisplaySecret() { String sID = GetInfo("SecretID to display: "); if (sID != null) { try { NetCredential nc = MiCasa.getCredential(0, sID, null, MiCasa.USERNAME_TYPE_CN_F); if (nc != null) { System.out.println("Username:" + nc.getUsername()); System.out.println("Password:" + nc.getPassword()); } else System.out.println(sID + " not found"); } catch (MiCasaException e) { System.out.println(e.getMessage()); } catch (Exception e) { e.printStackTrace(); } } } static private void RunTest() { int count = 1; String sCount = GetInfo("times to run: "); try { int icount = Integer.parseInt(sCount); count = icount; } catch (Exception e) { } for (int i = 0; i < count; i++) { System.out.println("Setting Credential ....."); try { MiCasa.setCredential(0, APPID, null, 0, USERNAME, PASSWORD); System.out.println("Succeeded"); } catch (Exception e) { System.out.println("Failed"); System.out.println(e.toString()); return; } System.out.println(""); // did we get it the credential back System.out.println("Getting Credential ....."); try { NetCredential bc = MiCasa.getCredential(0, APPID, null, 0); if (bc != null) { if (bc.getUsername().equals(USERNAME)) System.out.println(" Username matched : " + bc.getUsername()); if (bc.getPassword().equals(PASSWORD)) System.out.println(" Password matched : " + bc.getPassword()); } else { System.out.println("Failed"); } } catch (Exception e) { System.out.println(e.toString()); } try { System.out.println("\r\nRemoving Credential"); MiCasa.removeCredential(0, APPID, null); } catch (Exception e) { System.out.println(e.toString()); } try { NetCredential bc = MiCasa.getCredential(0, APPID, null, 0); if (bc != null) { System.out.println("\r\nCredential exists and should not - FAILED!"); } else System.out.println("\r\nCredential not found as expected - SUCCESS!"); } catch (Exception e) { System.out.println("\r\n Credential not found as expected - SUCCESS!"); } System.out.println("Test completed "); } } }