/* * Copyright 2005-2007 The Regents of the University of Michigan * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.tranche.example; import java.io.File; import org.tranche.add.AddFileTool; import org.tranche.add.CommandLineAddFileToolListener; import org.tranche.hash.BigHash; import org.tranche.users.UserZipFile; /** *
A simple example of uploading data. Note that you must actually have a server set up and a user account on that server to use this code. If you want to test the code for development, look at the UploadProjectDev.java source-code. It sets up a new server and makes a custom user then uploads data. * * @author Bryan E. Smith * @author Jayson Falkner - jfalkner@umich.edu */ public class UploadProject { public static void main(String[] args) throws Exception { // specify the three needed things: file(s), user file/passphrase, and server(s) String fileOrDirectory = "/files/myfile.txt"; String userFile = "/files/john.smith.zip.encrypted"; String userPassphrase = "secret password..."; String[] servers = new String[]{ "tranche://127.0.0.1:443" }; // load the user file UserZipFile uzf = new UserZipFile(new File(userFile)); uzf.setPassphrase(userPassphrase); // Use the AddFileTool to upload to network AddFileTool addTool = new AddFileTool(uzf.getCertificate(), uzf.getPrivateKey()); // We'll set up the add file tool to use our server. Note. You don't need to specify servers if the user is registered on a server known to the ServerUtil class. for (String server : servers) { addTool.addServerURL(server); } // Any title will do addTool.setTitle("The title."); // Any description will do addTool.setDescription("The description."); // add a listener that'll report info on the command-line CommandLineAddFileToolListener claftl = new CommandLineAddFileToolListener(System.out); addTool.addListener(claftl); // The addFile method will execute our request, returning the hash, which // can be used to download the file. BigHash hash = addTool.addFile(new File(fileOrDirectory)); // print the hash for luck System.out.println("The file's hash: " + hash); } }