[Webfunds-commits] java/webfunds/client/util UpgradesManager.java

Ian Grigg iang@cypherpunks.ai
Sun, 15 Apr 2001 01:33:01 -0400 (AST)


iang        01/04/15 01:33:01

  Modified:    webfunds/client/util UpgradesManager.java
  Log:
  1.  fixed URL reading code so can download of package works
  2.  added fancy check latest button and code so can get the
      latest plugin (doesn't handle WebFunds itself yet).

Revision  Changes    Path
1.10      +306 -35   java/webfunds/client/util/UpgradesManager.java

Index: UpgradesManager.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/util/UpgradesManager.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- UpgradesManager.java	2001/04/14 20:37:29	1.9
+++ UpgradesManager.java	2001/04/15 05:33:00	1.10
@@ -1,5 +1,5 @@
 /*
- * $Id: UpgradesManager.java,v 1.9 2001/04/14 20:37:29 iang Exp $
+ * $Id: UpgradesManager.java,v 1.10 2001/04/15 05:33:00 iang Exp $
  *
  * Copyright (c) 2000 Systemics Inc on behalf of
  * the WebFunds Development Team.  All Rights Reserved.
@@ -18,6 +18,7 @@
 
 import webfunds.util.Panic;
 import webfunds.util.Log;
+import webfunds.util.Support;
 
 import webfunds.comms.RawHttp;
 import webfunds.comms.RawException;
@@ -45,6 +46,7 @@
     public String getGeneralAction()  { return generalaction; }
 
     public static final String INSTALL_BUTTON = "Install Upgrade",
+                               LATEST_BUTTON  = "Check Latest Version",
                                GO_BUTTON      = "Go!",
                                DELETE_BUTTON  = "Delete",
                                FILE_BUTTON    = "Explorer",
@@ -126,6 +128,7 @@
                         // showContract((Contract)list.getSelectedValue());
                         // urlField.setText("");
                         installButton.setEnabled(true);
+                        checkButton.setEnabled(true);
                     }
                 }
             });
@@ -222,6 +225,15 @@
           logmsg("start from INSTALL ... " + (String)list.getSelectedValue());
           t1.start();
         }
+        else if (action == LATEST_BUTTON)
+        {
+          Thread t1 = new Thread()
+            {
+              public void run() { UpgradesManager.this.checkLatestVersion(); }
+            };
+          logmsg("start from LATEST ... " + (String)list.getSelectedValue());
+          t1.start();
+        }
         else if (action == CLOSE_BUTTON)
         {
             frame.dispose();
@@ -257,6 +269,7 @@
     private void refreshList()
     {
         installButton.setEnabled(false);
+        checkButton.setEnabled(false);
 
         DefaultListModel listmodel = (DefaultListModel)list.getModel();
         listmodel.clear();
@@ -419,6 +432,7 @@
         logmsg("download: " + url);
         list.getSelectionModel().clearSelection();
         installButton.setEnabled(false);
+        checkButton.setEnabled(false);
 
             if (s.endsWith(".html") || s.endsWith(".htm") || s.endsWith("/"))
             {
@@ -481,7 +495,7 @@
         }
 
         try {
-            copy(is, fos);
+            Support.copyStreamToStream(is, fos);
         } catch (IOException ex) {
             ui.errorMessage("File " + f + " failed to download: " + ex);
             return ;
@@ -492,13 +506,13 @@
     }
 
     /**
-     *  Read the url page and display it.
+     *  Download the url into a file.
      *
      *  This needs to be multithreaded!!!!
      *
      *  @return an array of the contents found at url
      */
-    void displayURL(URL url)
+    protected byte[] getURL(URL url)
     {
         byte[] b;
         try {
@@ -506,8 +520,23 @@
             b = http.getPage(10240);
         } catch (RawException ex) {
             ui.errorMessage("URL " + url + " cannot access: " + ex);
-                return ;
+                return null;
         }
+        return b;
+    }
+
+    /**
+     *  Read the url page and display it.
+     *
+     *  This needs to be multithreaded!!!!
+     *
+     *  @return an array of the contents found at url
+     */
+    void displayURL(URL url)
+    {
+        byte[] b = getURL(url);
+        if (b == null)
+            return ;
         showPage(b);
     }
 
@@ -561,25 +590,10 @@
 
     private void installPackage(File f)
     {
-        if (!f.exists())
-        {
-            ui.errorMessage("Package " + f + " does not exist?");
-            return ;
-        }
-       
-        logmsg("Installing ... " + f);
-
-        logmsg("Reading ... " + f);
-        FileInputStream fis;
-        try {
-            fis = new FileInputStream(f);
-        } catch (FileNotFoundException ex) {
-            ui.errorMessage("Package " + f + " does not exist: " + ex);
+        ZipInputStream zis = getZipInputStream(f);
+        if (zis == null)
             return ;
-        }
 
-        logmsg("Unzipping ... " + f);
-        ZipInputStream zis = new ZipInputStream(fis);
         boolean notEOF = true;
         while (notEOF)
         {
@@ -631,7 +645,7 @@
         }
 
         try {
-            copy(is, fos);
+            Support.copyStreamToStream(is, fos);
         } catch (FileNotFoundException ex) {
             throw new IOException("file " + f + " copy failed: " + ex);
         }
@@ -639,15 +653,18 @@
 
     }
 
+    public static final String TAG_HINT = "\nlatest";
+    public static final String TAG_LATEST = " name=\"latest\"";
+    public static final String TAG_HREF = "href=";
 
-    private static void copy(InputStream is, OutputStream os)
+    private static String turnIntoString(InputStream is)
         throws IOException
     {
         int size = 10240;
         byte[] buf = new byte[size];
 
         int offset = 0;
-        while (is.available() >= 0)
+        while (is.available() >= 0 && offset < size)
         {
             int i;
             i = is.read(buf, offset, size - offset);
@@ -657,20 +674,257 @@
                throw new IOException("read returned " + i);
 // logmsg("i == " + i + ";  offset == " + offset + "   (" + size + ")");
             offset += i;
-            if (offset < size)             // partial read?
-                continue ;                 // keep reading until full
+        }
+
+        String s = new String(buf, 0, offset);
+        return s;
+    }
+
+    private static String searchForHint(InputStream is)
+        throws IOException
+    {
+        String s = turnIntoString(is);
+//System.err.println("This is the file:\n\n_____________________" + s +
+//                   "\n------------------------\n");
+        int start = s.indexOf(TAG_HINT);
+        if (start < 0)
+            return null;
+
+//System.err.println("start is " + start);
+
+        int equate = skipWhiteSpace(s, start + TAG_HINT.length());
+        if (equate < 0)
+            return null;
+
+//System.err.println("equate is " + equate);
+        if (s.charAt(equate) != '=')
+            return null;
+
+        int hintLead = skipWhiteSpace(s, equate + 1);
+        if (hintLead < 0)
+            return null;
+//System.err.println("hintLead is " + hintLead);
+        int hintEnd =  findWhiteSpace(s, hintLead);   // should be an eoln
+        if (hintEnd < 0)
+            return null;
+        
+//System.err.println("hintEnd is " + hintEnd);
+        String hint = s.substring(hintLead, hintEnd);
+
+        return hint;
+    }
+
+    private static String searchForLatest(String s)
+        throws IOException
+    {
+//System.err.println("This is the file:\n\n_____________________" + s +
+//                   "\n------------------------\n");
+        int nameTag = s.indexOf(TAG_LATEST);
+        if (nameTag < 0)
+            return null;
+
+//System.err.println("nameTag is " + nameTag);
+
+        int white = nameTag + TAG_LATEST.length();
+        if (!isWhiteSpace(s.charAt(white)))
+            return null;
+
+//System.err.println("white is " + white);
+        int href = skipWhiteSpace(s, white);
+        if (href < 0)
+            return null;
+
+
+//System.err.println("href is " + href);
+        if (!s.startsWith(TAG_HREF, href))
+            return null;
+        int quote = href + TAG_HREF.length();
+
+//System.err.println("quote is " + quote);
+        if (s.charAt(quote) != '"' || quote >= (s.length() - 2))
+            return null;
+
+        int latestLead = quote + 1;
+//System.err.println("latestLead is " + latestLead);
+        int latestEnd =  s.indexOf('"', latestLead);
+        if (latestEnd < 0)
+            return null;
+        
+//System.err.println("latestEnd is " + latestEnd);
+        String latest = s.substring(latestLead, latestEnd);
+
+        return latest;
+    }
+
+    public static boolean isWhiteSpace(char c) { return !(' ' < c && c <= '~');}
+
+    /**
+     *  @return the index of the next non-white char, else -1
+     */
+    public static int skipWhiteSpace(String s, int i)
+        throws IOException
+    {
+        if (s == null || i < 0)
+            return -1;
+
+        int len = s.length();
+
+        while (i < len)
+        {
+            char a = s.charAt(i);
+            if (!isWhiteSpace(a))
+                return i;
+            i++;
+        }
+            
+        return -1;
+    }
+
+    /**
+     *  @return the index of the next white char, else -1
+     */
+    public static int findWhiteSpace(String s, int i)
+        throws IOException
+    {
+        if (s == null || i < 0)
+            return -1;
+
+        int len = s.length();
+
+        while (i < len)
+        {
+            char a = s.charAt(i++);
+            if (isWhiteSpace(a))
+                return i;
+            i++;
+        }
+            
+        return -1;
+    }
+
+
+    private ZipInputStream getZipInputStream(File f)
+    {
+        if (!f.exists())
+        {
+            ui.errorMessage("Package " + f + " does not exist?");
+            return null;
+        }
+       
+        logmsg("Reading ... " + f);
+        FileInputStream fis;
+        try {
+            fis = new FileInputStream(f);
+        } catch (FileNotFoundException ex) {
+            ui.errorMessage("Package " + f + " does not exist: " + ex);
+            return null;
+        }
+
+        logmsg("Unzipping ... " + f);
+        ZipInputStream zis = new ZipInputStream(fis);
+        return zis;
+
+    }
+
+    private String getLatestHintURL(File f)
+    {
+        /*
+         *  Search the existing download for an INI file that gives us a hint.
+         */
+        ZipInputStream zis = getZipInputStream(f);
+        if (zis == null)
+            return null;
+        boolean notEOF = true;
+        String name = null;
+        String hint = null;
+
+        while (notEOF)
+        {
+            try {
+                ZipEntry ze = zis.getNextEntry();
+                if (ze == null)               // is EOF!
+                    break ;
+                name = ze.getName();
+                if (name.endsWith(".ini"))
+                {
+                    logmsg("found ZipEntry: " + ze);
+                    hint = searchForHint(zis);
+                }
+                zis.closeEntry();
+                if (hint != null)
+                    break;
+            } catch (IOException ex) {
+                ui.errorMessage("zip failed: " + ex);
+                return null;
+            }
+        }
+
+        System.err.println(
+              "package " + f.getName() + " file " + name + " suggests\n\n" +
+              "Zip Entry: " + hint +
+              "");
+
+        /*
+         *  Get the hint URL and search it for the latest download version.
+         */
+        URL url;
+        try {
+            url = new URL(hint);
+        } catch (MalformedURLException ex) {
+            ui.errorMessage("hint is bad URL:\n\n" + hint + "\n\n" + ex);
+            return null;
+        }
+        byte[] b = getURL(url);
+
+        String latest;
+        try {
+            latest = searchForLatest(new String(b));
+        } catch (IOException ex) {
+            ui.errorMessage("url is bad IO:\n\n" + url + "\n\n" + ex);
+            return null;
+        }
+        if (latest == null)
+        {
+            ui.errorMessage("No Latest found at Hint!" + "\n\n" + url);
+            return null;
+        }
+
+        /*
+         *  Construct the URL from the latest version, assuming the
+         *  same place as the hint, if necessary.
+         */
+        System.err.println("check " + latest + " for 'http:'");
+        if (!latest.startsWith("http:"))       // fix it as relative to hint
+        {
+            String root = hint.substring(0, hint.lastIndexOf('/'));
+            String composite = root + '/' + latest;
+            latest = composite;
+        }
+        ui.infoMessage("Latest is" + "\n\n" + latest +
+                       "\n\nHit Go! to download");
+        return latest;
+    }
 
-            os.write(buf);
-            offset = 0;
+    private void checkLatestVersion()
+    {
+        String s = (String)list.getSelectedValue();
+        if (s == null)
+        {
+            ui.errorMessage("No Install Package Selected");
+            return ;
         }
+        logmsg("String ... " + s);
+        File f = new File(downloadDir, s);
+        String latest = getLatestHintURL(f);
+        if (latest == null)
+            return;
 
-        if (offset > 0)
-            os.write(buf, 0, offset);
-        os.flush();
+        urlField.setText(latest);
     }
 
 
     JButton installButton;
+    JButton checkButton;
 
     private void addUrlPanel(JPanel panel)
     {
@@ -678,9 +932,17 @@
         JLabel urllabel = new JLabel("URL: ");
         JButton goButton  = new JButton(GO_BUTTON);
         JButton fileButton  = new JButton(FILE_BUTTON);
+
         installButton  = new JButton(INSTALL_BUTTON);
         installButton.setEnabled(false);
+        checkButton  = new JButton(LATEST_BUTTON);
+        checkButton.setEnabled(false);
 
+	JPanel buttonPanel = new JPanel();
+	buttonPanel.setLayout(new FlowLayout());
+	buttonPanel.add(installButton);
+	buttonPanel.add(checkButton);
+
         GridBagConstraints c = new GridBagConstraints();
 
         panel.setLayout(new GridBagLayout());
@@ -715,10 +977,18 @@
         c.gridx = 0;
         c.gridy = 1;
         c.gridwidth = GridBagConstraints.RELATIVE;
-        c.weightx = 0.0;
-        c.anchor = GridBagConstraints.SOUTH;
+        c.weightx = 2.0;
+        c.anchor = GridBagConstraints.CENTER;
         c.fill = GridBagConstraints.NONE;
-        panel.add(installButton, c);
+        panel.add(buttonPanel, c);
+
+//        c.gridx = 0;
+//        c.gridy = 1;
+//        c.gridwidth = GridBagConstraints.REMAINDER;
+//        c.weightx = 1.0;
+//        c.anchor = GridBagConstraints.EAST;
+//        c.fill = GridBagConstraints.NONE;
+//        panel.add(checkButton, c);
 
 //        c.gridx = 1;
 //        c.gridwidth = GridBagConstraints.REMAINDER;
@@ -729,6 +999,7 @@
                
         goButton.addActionListener( this );
         installButton.addActionListener( this );
+        checkButton.addActionListener( this );
 //        fileButton.addActionListener( this );
     }