/*************************************************************************** *cr *cr (C) Copyright 2000-2007 The Board of Trustees of the *cr University of Illinois *cr All Rights Reserved *cr ***************************************************************************/ import java.io.*; import javax.swing.*; import java.awt.*; import java.util.Date; import java.util.Vector; import java.util.StringTokenizer; import edu.uiuc.ks.biocore.BioCoRE; import edu.uiuc.ks.biocore.BiofsFile; import edu.uiuc.ks.biocore.BiofsDirectory; import edu.uiuc.ks.biocore.UserManagement; import edu.uiuc.ks.biocore.ApiException; import util.xmlparser.DOMElement; import util.xmlparser.ParseDOM; // for webdav import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.HttpURL; import org.apache.commons.httpclient.HttpsURL; import org.apache.commons.httpclient.URIException; /** test program to make sure that the API is working properly. * This can be executed by doing: * ant testJavaAPI * which will run through a test sequence. Or, it can be manually * executed by doing: * * java -classpath .:../../../build/externalAPI/java:../../../web/lib/commons-httpclient-2.0.2.jar:../../../web/lib/jakarta-slide-webdavlib-2.0.jar:../../../web/lib/commons-logging.jar TestBioCoRE * from the 'test' subdirectory. (TestBioCoRE needs to be compiled for * that to work, of course.. ant testJavaAPI is a nifty way to get it * compiled) * * @version $Revision: 1.41 $ $Date: 2007/01/08 21:39:36 $ $Author: kvandivo $ */ public class TestBioCoRE { private static Wdr m_wdr = null; private static String m_strBase = null; // ------------------------------------------------------------------ /** main */ static public void main(String[] rgstrArgs) { try { // set ourselves up to handle ssl. This is java 1.2+ only. java.security.Security.addProvider( (java.security.Provider) Class.forName( "com.sun.net.ssl.internal.ssl.Provider") .newInstance()); System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol"); BioCoRE bHandle = null; if (rgstrArgs.length == 0) { bHandle = new BioCoRE("test application"); System.out.println("User validity:" + bHandle.verify()); } else if (rgstrArgs.length == 1) { bHandle = new BioCoRE("test application", rgstrArgs[0]); System.out.println("User validity:" + bHandle.verify()); } else if (rgstrArgs.length == 2) { bHandle = new BioCoRE("test application", rgstrArgs[0], rgstrArgs[1]); } else { System.out.println("If called with zero arguments, will attempt " + "to use the\ndefault session file. If called with one arg " + "will\nattempt to use the filename specified. If called with " + "\n2 command line arguments, first is \n" + "secure URL, such as\n https://localhost:8443/WEBAPP/ \nand " + "second is non secure URL such as\n " + "http://localhost:8080/WEBAPP/ (replace WEBAPP appropriately)\n" ); return; } BufferedReader brIn = new BufferedReader( new InputStreamReader(System.in)); String str; int iProj; boolean bContinue = true; String strListenerId = "null"; // set up a simple menu where the user can choose what they // wish to do. do { System.out.println("--------------\nCurrent Status"); System.out.println(" Listener ID: " + strListenerId); System.out.println("\nChoice (98 for help):"); String strIn = brIn.readLine(); while (strIn.startsWith("#")) { System.out.println(strIn); strIn = brIn.readLine(); } int iChoice = 98; try { iChoice = Integer.parseInt(strIn); }catch (Exception e) { System.out.println("Unknown option: " + strIn); } switch (iChoice) { case 1: testLogin(bHandle, brIn); break; case 2: strListenerId = testRegisterListener(bHandle, brIn); break; case 3: testGetProjectList(bHandle, false); break; case 4: testGetListenerPrefs(bHandle, brIn, false); break; case 5: testSendChat(bHandle, brIn); break; case 6: testGetLatestListenerMessages(bHandle, strListenerId, false); break; case 7: testDeregisterListener(bHandle, strListenerId); strListenerId = "null"; break; case 8: testGetProjectList(bHandle, true); break; case 9: testGetListenerPrefs(bHandle, brIn, true); break; case 10: testGetLatestListenerMessages(bHandle, strListenerId, true); break; case 11: System.out.println("Count to:"); int iCount = Integer.parseInt(brIn.readLine()); for (int i = 0; i , " ); } System.out.println(""); } catch (HttpException he) { System.out.println("Requesting: " + m_wdr.getPath() + " Code: " + he.getReasonCode() + " Reason: " + he.getReason()); he.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } // end of testWebDAVList // --------------------------------------------------------------------- static void disconnectWebDAV(BioCoRE bHandle) throws IOException { if (m_wdr == null) { System.out.println("web dav already disconnected"); return; } try { m_wdr.close(); m_wdr = null; System.out.println("Web Dav disconnected"); } catch (HttpException he) { System.out.println("Requesting: " + m_wdr.getPath() + " Code: " + he.getReasonCode() + " Reason: " + he.getReason()); he.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } // end of disconnectWebDAV() // --------------------------------------------------------------------- static void setUpWebDAV(BioCoRE bHandle) throws IOException { String strSess = bHandle.getSession(); if (strSess == null) { System.out.println("Null Session. Can't init WebDAV"); return; } if (m_wdr != null) { System.out.println("web dav already initialized"); return; } try { // System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); // System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); // System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); // System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug"); HttpURL httpu = uriToHttpURL(bHandle.getBaseURLWithPort() + "biofs/"); System.out.println("sess is " + strSess + " httpurl is " + httpu); Cookie cook = new Cookie(httpu.getHost(), "JSESSIONID", strSess); m_strBase = new String(httpu.getRawPath()); cook.setPath(m_strBase); cook.setSecure(httpu.getScheme().equals("https")); System.out.println("cookie is " + cook); System.out.println("cookie is " + cook.toExternalForm()); System.out.println("cookie:comment: " + cook.getComment()); System.out.println("cookie:domain: " + cook.getDomain()); System.out.println("cookie:date: " + cook.getExpiryDate()); System.out.println("cookie:isExp: " + cook.isExpired()); System.out.println("cookie:isPersistent: " + cook.isPersistent()); System.out.println("cookie:path: " + cook.getPath()); System.out.println("cookie:secure: " + cook.getSecure()); System.out.println("cookie:version: " + cook.getVersion()); System.out.println("raw path is " + new String(httpu.getRawPath())); m_wdr = new Wdr(cook, httpu); System.out.println("Web Dav set up"); } catch (HttpException he) { System.out.println("Requesting: " + m_wdr.getPath() + " Code: " + he.getReasonCode() + " Reason: " + he.getReason()); he.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } // end of setUpWebDAV() // --------------------------------------------------------------------- private static HttpURL uriToHttpURL(String uri) throws URIException { return uri.startsWith("https") ? new HttpsURL(uri.toCharArray()) : new HttpURL(uri.toCharArray()); } // --------------------------------------------------------------------- // --------------------------------------------------------------------- // --------------------------------------------------------------------- // --------------------------------------------------------------------- // CODE BELOW THIS POINT MIGHT BE STALE AND IS USED AT YOUR OWN RISK // CODE BELOW THIS POINT MIGHT BE STALE AND IS USED AT YOUR OWN RISK // CODE BELOW THIS POINT MIGHT BE STALE AND IS USED AT YOUR OWN RISK // --------------------------------------------------------------------- // --------------------------------------------------------------------- static void testGetBiofsDirectory(BioCoRE bHandle, BufferedReader brIn) throws IOException { BiofsDirectory bfd; int iId; System.out.println("Testing getDirectory"); System.out.println("Directory id?"); iId = Integer.parseInt(brIn.readLine().trim()); try { bfd = bHandle.getBiofsDirectory(iId, false); System.out.println("Directory is:\n" + bfd); } catch (ApiException e) { System.out.println(e.toString() + " Exception: " + e.getMessage()); } } // end of testGetBiofsDirectory // --------------------------------------------------------------------- static void testPutBiofsFile(BioCoRE bHandle, BufferedReader brIn) throws IOException { BiofsFile bff = new BiofsFile(); System.out.println("Testing upload Biofs File"); System.out.println("Directory id to put file in?"); bff.setParent(Integer.parseInt(brIn.readLine().trim())); System.out.println("Local filename to send?"); bff.setLocalName(brIn.readLine().trim()); // NOTE: // You don't have to have a file on the local system. We could, // instead of specifying a local filename, say the following: // byte [] bt = // contents of file you wish to create in BioFS // bff.setIsLocalFile(false); // bff.setContents(bt); System.out.println("Name file will be given in BioFS?"); bff.setRemoteName(brIn.readLine().trim()); try { bHandle.putBiofsFile(bff); // if we make it here, no exceptions were thrown System.out.println("File Upload Successful."); } catch (ApiException e) { System.out.println(e.toString() + " Exception: " + e.getMessage()); } } // end of testPutBiofsFile // --------------------------------------------------------------------- static void testSetUserConfig(BioCoRE bHandle, BufferedReader brIn) throws Exception{ System.out.println("Testing setting of User Config"); System.out.println("Project id for the change?"); int projectId = Integer.parseInt(brIn.readLine().trim()); System.out.println("Project id is: " + projectId); System.out.println("appName to change?"); String appName = brIn.readLine().trim(); System.out.println("AppName is: " + appName); System.out.println("Value to change it to?"); String value = brIn.readLine().trim(); System.out.println("Value is " + value); System.out.println("Apply to all projects? (true/false)"); boolean allProjects = (new Boolean(brIn.readLine().trim())).booleanValue(); System.out.println("Apply to all project is: " + allProjects); try { bHandle.setUserConfigString(projectId, appName, value, allProjects); } catch(ApiException e) { System.out.println(e.toString() + " Exception: " + e.getMessage()); } } // end of testSetUserConfig } // end of class TestBioCoRE