import pk.edu.niit.clarens.client.ClarensClient;
import pk.edu.niit.clarens.services.file.FileInfo;
import java.util.Vector;
import java.io.IOException;
import org.apache.xmlrpc.XmlRpcException;

public class TestClient {
    public static void main(String[] argv) {
        if (argv.length != 1) {
            System.err.println("Usage:  java TestClient serverUrl");
            System.err.println("Example:  java TestClient http://baldrick.hep.caltech.edu:8080/jclarens/xmlrpc");
            System.exit(1);
        }

        String serverUrl = argv[0];
        String clientCertFile = "usercert.pem";
        String clientCertKeyFile = "unencryptedkey.pem";
        ClarensClient client = null;

        System.out.println("Creating the client object");
        // Use PEM certificate/key files for http-based authentication.
        try {
            client = new ClarensClient(serverUrl, clientCertFile, clientCertKeyFile);
        }
        catch (Exception e) {
            System.err.println("Could not create the clarens client.");
            e.printStackTrace();
            System.exit(1);
        }
        System.out.println("Created the client object");

        // Use system properties to set the client certificate / ca certs
        // for https-based authentication.  These should be set on the command
        // line as:
        // java -Dclarens.client.keyStore=/path/to/client/keystore \
        //      -Dclarens.client.keyStorePassword=changit \
        //      -Dclarens.client.keyStoreAlias=youralias \
        //      -Dclarens.client.trustStore=/path/to/cacert.keystore \
        //      -Dclarens.client.trustStorePassword=changeit

        // Authenticate against the remote server.
        client.connect();

        // List all of the files in a directory
        Vector params = new Vector();
        // The first parameter is the directory name, relative to the
        // server's root.
        params.add("/");
        // The second parameter is a glob pattern for matching filenames.
        // An empty string is equivalent to "*".
        params.add("*");

        // Execute the command.  Error handling is left as an exercise for
        // the reader.
        Object result = null;
        try {
            result = client.execute("file.ls", params);
        }
        catch (XmlRpcException xre) {
            System.err.println("Encountered an error executing the xmlrpc query:  " + xre.getMessage());
            xre.printStackTrace();
        }
        catch (IOException ioe) {
            System.err.println("Connection error with the remote server.  " + ioe.getMessage());
            ioe.printStackTrace();
        }

        // Iterate through the results

        Vector resultVec = (Vector) result;
        for (int i=0; i < resultVec.size(); i++) {
            // Convert each result into a more useful format.  See the
            // FileInfo javadoc for more information on the bean properties
            // of FileInfo.
            // http://baldrick.hep.caltech.edu:8080/jclarens/apidoc/index.html
            FileInfo fileInfo = new FileInfo((Vector) resultVec.get(i));
            System.out.println("Name:  " + fileInfo.getFilename());
        }

        // Get the contents of one file.  Make sure that xml parsing is
        // disabled for file.read, as the result is not sent back as
        // an xml document for efficiency.
        params.clear();
        // The first parameter is the name of the file.
        params.add("/testfile.txt");
        // The second parameter is the starting position in the file.
        // This is useful when you need to extract only a part of a larger
        // file.  Usually this is just set to 0.
        params.add(new Integer(0));
        // The third parameter is the number of bytes to read from the file.
        // Set this to -1 to read everything from the starting position to
        // the end of the file.
        params.add(new Integer(-1));

        // Disable xml parsing of the result
        client.enableXmlParsing(false);
        // Make the call.
        byte[] fileContents;
        
        try {
            fileContents = (byte[]) client.execute("file.read", params);
        }
        catch (XmlRpcException xre) {
            System.err.println("Encountered an error executing the xmlrpc query:  " + xre.getMessage());
            xre.printStackTrace();
        }
        catch (IOException ioe) {
            System.err.println("Connection error with the remote server.  " + ioe.getMessage());
            ioe.printStackTrace();
        }
        // Re-enable xml parsing of the result for future xmlrpc calls.
        client.enableXmlParsing(true);

        // Do whatever you want with the file contents.  Be careful with large
        // files because the entire content of the file is stored in memory.
    }
}
