View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.client;
30  
31  import java.net.URL;
32  import java.net.MalformedURLException;
33  import java.io.File;
34  
35  import uk.ac.rdg.resc.jstyx.client.StyxConnection;
36  import uk.ac.rdg.resc.jstyx.client.CStyxFile;
37  import uk.ac.rdg.resc.jstyx.client.CStyxFileChangeAdapter;
38  import uk.ac.rdg.resc.jstyx.StyxUtils;
39  import uk.ac.rdg.resc.jstyx.StyxException;
40  
41  /***
42   * A simple application that downloads a file from a remote server and times
43   * the download, providing an indication of progress as it goes.
44   *
45   * @author Jon Blower
46   * $Revision: 86 $
47   * $Date $
48   * $Log$
49   * Revision 1.1  2005/03/07 08:27:52  jonblower
50   * Initial import
51   *
52   */
53  public class StyxGet extends CStyxFileChangeAdapter
54  {
55      private URL styxURL;
56      private File targetDir;
57      
58      public StyxGet(String styxURL, String targetDir) throws Exception
59      {
60          try
61          {
62              this.styxURL = new URL(styxURL);
63          }
64          catch(MalformedURLException mue)
65          {
66              throw new Exception("Not a valid URL (have you set the" +
67                  " java.protocol.handler.pkgs property correctly?)");
68          }
69          if (!this.styxURL.getProtocol().equals("styx"))
70          {
71              throw new Exception("Can only download from styx:// URLs");
72          }
73          String path = this.styxURL.getPath();
74          if (path.endsWith("/"))
75          {
76              throw new Exception("Cannot download a directory");
77          }
78          this.targetDir = new File(targetDir);
79          if (!this.targetDir.exists() || !this.targetDir.isDirectory())
80          {
81              throw new Exception(targetDir + " is not a directory");
82          }
83          
84          // Get the name of the remote file (i.e. the last part of the path)
85          int lastSlash = path.lastIndexOf("/");
86          // We've already checked that the URL doesn't end with a slash
87          String filename = path.substring(lastSlash + 1);
88          
89          // Check that the file we're going to create doesn't exist
90          File targetFile = new File(targetDir, filename);
91          if (targetFile.exists())
92          {
93              throw new Exception(targetFile + " already exists");
94          }
95      }
96      
97      public void get() throws StyxException
98      {
99          StyxConnection conn = new
100             StyxConnection(this.styxURL.getHost(), this.styxURL.getPort());
101         conn.connect();
102         CStyxFile file = conn.openFile(this.styxURL.getPath(), StyxUtils.OREAD);
103         long length = file.getLength();
104         
105     }
106     
107     public static void main (String[] args) throws Exception
108     {
109         // TODO: throw up a GUI if no arguments or if -gui switch used?
110         if (args.length != 2)
111         {
112             System.err.println("Usage: StyxGet <styx:// URL> <target directory>");
113             return;
114         }
115         
116     }
117     
118 }