/* * 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 java.io.IOException; import java.net.BindException; import org.tranche.flatfile.FlatFileTrancheServer; import org.tranche.server.Server; import org.tranche.add.AddFileTool; import org.tranche.add.CommandLineAddFileToolListener; import org.tranche.hash.BigHash; import org.tranche.util.IOUtil; import org.tranche.users.MakeUserZipFileTool; import org.tranche.users.UserZipFile; import org.tranche.users.User; import org.tranche.util.SecurityUtil; /******************************************************************************* * Sample upload tool. Can be used with existing user, can be used with * server already running. Can also be used to create user, create server * instance. * @author Bryan ******************************************************************************/ public class UploadProjectDev { public static void main(String[] args) { // If you have a user set up as admin, set to false Boolean makeUser = true; // If you have the server running, set to false Boolean runServer = true; // If you don't have a user file, we will make one. MakeUserZipFileTool maker; UserZipFile zip = null; // Used to set admin priveledges for user SecurityUtil sUtil = null; // If you don't have a server running, we will start one FlatFileTrancheServer ffserver = null; Server server = null; // This is the URL for the server. Use port 1500 since some // underpriveledged users on some OSs may not have access to default // port to 443. You can change these if you run your own server // at a different address or port. String address = "tranche://127.0.0.1"; String port = ":1500"; String url = address + port; try { // Path for file to upload String filename = "upload/green.gif"; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // Create a user zip file at project directory root, and give it admin // permissions. This will create a certificate and your keys. // Note that if you want to upload to the Proteome Commons Tranche // network, you will need to have your certificate signed by an // administrator at Proteome Commons. However, you can use your // own network, which is why we are starting our own server. if (makeUser) { maker = new MakeUserZipFileTool(); // Create a user maker.setName("John Smith"); maker.setPassphrase("supersecret"); maker.setUserFile(new File("John.zip.encrypted")); zip = (UserZipFile) maker.makeCertificate(); // Set user permissions as admin (server needs user registered or it will // throw a SecurityException on attempted file upload) zip.setFlags((new SecurityUtil()).getProteomeCommonsAdmin().getFlags()); } // If you already have a user set as an admin on the server, change the // path to location of your encrypted user file else { String pathToUserFile = "John.zip.encrypted"; // If your user is password protected, place the password below. Else, // use an empty string String password = "supersecret"; zip = new UserZipFile(new File(pathToUserFile)); zip.setPassphrase(password); } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // Start the server if not running. You can always run the server // through the GUI webstart tool // or using the command line tool. However, we'll use the API to do it here. if (runServer) { // Set up our server in subdirectory directory ffserver = new FlatFileTrancheServer(new File("tmp/ffserver/")); // Add user to server ffserver.getConfiguration().getUsers().add(zip); // Wrap the flat file server in a server. I'm choosing to use port // 1500 because it may be more accessible to try { server = new Server(ffserver, 1500); server.start(); } catch (BindException e) { // Do nothing, server already running... continue // Notice I'm outputing to standard out instead of standard error. // This code is robust in the sense that it works even if a server // is already running System.out.println("Port 1500 already bound, continuing assuming a Tranche server is already running..."); } } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // Use the AddFileTool to upload to network AddFileTool addTool = new AddFileTool(zip.getCertificate(), zip.getPrivateKey()); // We'll set up the add file tool to use our server. addTool.addServerURL(url); addTool.setTitle("The title."); addTool.setDescription("The description."); CommandLineAddFileToolListener claftl = new CommandLineAddFileToolListener(System.out); addTool.addListener(claftl); // The addFile method will execute our request, returning the hash, which // is used to download the file. BigHash hash = addTool.addFile(new File(filename)); System.out.println("The file's hash: " + hash); } catch (IOException e) { System.err.println("An unexpected IO error has occurred: " + e.getMessage()); } catch (Exception e) { System.err.println("An error occurred while uploading file: " + e.getMessage()); } finally { // Close server! try {server.setRun(false);} catch (Exception e){} try {ffserver.close();} catch (Exception e){} } } }