[jsword-svn] common/java/core/org/crosswire/common/util s

jswordcvs at crosswire.org jswordcvs at crosswire.org
Sun Aug 21 13:37:52 MST 2005


Update of /cvs/jsword/common/java/core/org/crosswire/common/util
In directory www.crosswire.org:/tmp/cvs-serv23270/java/core/org/crosswire/common/util

Modified Files:
	NetUtil.java Msg.java Msg.properties 
Added Files:
	WebResource.java 
Log Message:
Willie Thean's laf changes.
Added ability to specify proxy for http download.
Changed default logging to INFO.

--- NEW FILE: WebResource.java ---
/**
 * Distribution License: JSword is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License, version
 * 2.1 as published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details. The License is
 * available on the internet at: http://www.gnu.org/copyleft/lgpl.html or by
 * writing to: Free Software Foundation, Inc. 59 Temple Place - Suite 330
 * Boston, MA 02111-1307, USA Copyright: 2005 The copyright to this program is
 * held by it's authors. ID: $Id: URLFilter.java,v 1.5 2005/07/27 23:26:42
 * dmsmith Exp $
 */
package org.crosswire.common.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Date;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpHost;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.ProxyHost;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.util.HttpURLConnection;

/**
 * A WebResource is backed by an URL and potentially the proxy through which it
 * need go. It can get basic information about the resource and it can get the
 * resource.
 * 
 * @see gnu.lgpl.License for license details. The copyright to this program is
 *      held by it's authors.
 * @author DM Smith [dmsmith555 at yahoo dot com]
 */
public class WebResource
{
    public WebResource(URL theURL)
    {
        this(theURL, null);
    }

    public WebResource(URL theURL, String theProxyHost)
    {
        this(theURL, theProxyHost, null);
    }

    public WebResource(URL theURL, String theProxyHost, Integer theProxyPort)
    {
        url = theURL;
        client = new HttpClient();
        HostConfiguration config = client.getHostConfiguration();
        config.setHost(new HttpHost(theURL.getHost(), theURL.getPort()));
        if (theProxyHost != null)
        {
            config.setProxyHost(new ProxyHost(theProxyHost, theProxyPort == null ? -1 : theProxyPort.intValue()));
        }
    }

    public int getSize()
    {
        HttpMethod method = new GetMethod(url.getQuery());

        try
        {
            // Execute the method.
            if (client.executeMethod(method) == HttpStatus.SC_OK)
            {
                HttpURLConnection connection = new HttpURLConnection(method, url);
                return connection.getContentLength();
            }
        }
        catch (Exception e)
        {
            return 0;
        }
        finally
        {
            // Release the connection.
            method.releaseConnection();
        }
        return 0;
    }

    public long getLastModified()
    {
        HttpMethod method = new GetMethod(url.getQuery());

        try
        {
            // Execute the method.
            if (client.executeMethod(method) == HttpStatus.SC_OK)
            {
                HttpURLConnection connection = new HttpURLConnection(method, url);
                return connection.getLastModified();
            }
        }
        catch (Exception e)
        {
            return new Date().getTime();
        }
        finally
        {
            // Release the connection.
            method.releaseConnection();
        }
        return new Date().getTime();
    }

    /**
     * Copy this WebResource to the destination.
     * 
     * @param dest
     * @throws LucidException
     */
    public void copy(URL dest) throws LucidException
    {
        InputStream in = null;
        OutputStream out = null;

        HttpMethod method = new GetMethod(url.getPath());

        try
        {
            // Execute the method.
            if (client.executeMethod(method) == HttpStatus.SC_OK)
            {
                in = method.getResponseBodyAsStream();

                // Download the index file
                out = NetUtil.getOutputStream(dest);

                byte[] buf = new byte[4096];
                for (int count = 0; -1 != (count = in.read(buf)); )
                {
                    out.write(buf, 0, count);
                }
            }
        }
        catch (Exception e)
        {
            throw new LucidException(Msg.MISSING_FILE, e);
        }
        finally
        {
            // Release the connection.
            method.releaseConnection();
            // Close the streams
            IOUtil.close(in);
            IOUtil.close(out);
        }
    }

    private URL url;
    private HttpClient client;
}

Index: Msg.properties
===================================================================
RCS file: /cvs/jsword/common/java/core/org/crosswire/common/util/Msg.properties,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Msg.properties	27 Jun 2004 22:09:00 -0000	1.2
--- Msg.properties	21 Aug 2005 20:37:49 -0000	1.3
***************
*** 19,20 ****
--- 19,21 ----
  ResourceUtil.NotAssignable=Class {0} does not implement {1}
  ThreadUtil.Unavailable=<Unavailable>
+ WebResource.MissingFile=Unable to find: {0}

Index: NetUtil.java
===================================================================
RCS file: /cvs/jsword/common/java/core/org/crosswire/common/util/NetUtil.java,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** NetUtil.java	18 Aug 2005 20:58:42 -0000	1.24
--- NetUtil.java	21 Aug 2005 20:37:49 -0000	1.25
***************
*** 40,49 ****
  import java.util.jar.JarEntry;
  
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpException;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.util.HttpURLConnection;
- 
  /**
   * The NetUtil class looks after general utility stuff around the
--- 40,43 ----
***************
*** 541,576 ****
      public static int getSize(URL url)
      {
          if (url.getProtocol().equals(PROTOCOL_HTTP))
          {
!             // Create an instance of HttpClient.
!             HttpClient client = new HttpClient();
! 
!             // Create a method instance.
!             GetMethod method = new GetMethod(url.toExternalForm());
!             
!             // Execute the method.
!             try
!             {
!                 int statusCode = client.executeMethod(method);
!                 if (statusCode != HttpStatus.SC_OK)
!                 {
!                     return 0;
!                 }
!             }
!             catch (HttpException e)
!             {
!                 // TODO Auto-generated catch block
!                 e.printStackTrace();
!             }
!             catch (IOException e)
!             {
!                 // TODO Auto-generated catch block
!                 e.printStackTrace();
!             }
! 
!             HttpURLConnection connection = new HttpURLConnection(method, url);
!             
!             return connection.getContentLength();
! 
          }
  
--- 535,550 ----
      public static int getSize(URL url)
      {
+         return getSize(url, null);
+     }
+     public static int getSize(URL url, String proxyHost)
+     {
+         return getSize(url, proxyHost, null);
+     }
+     public static int getSize(URL url, String proxyHost, Integer proxyPort)
+     {
          if (url.getProtocol().equals(PROTOCOL_HTTP))
          {
!             WebResource wr = new WebResource(url, proxyHost, proxyPort);
!             return wr.getSize();
          }
  
***************
*** 593,629 ****
      public static long getLastModified(URL url)
      {
!         if (url.getProtocol().equals(PROTOCOL_HTTP))
!         {
!             // Create an instance of HttpClient.
!             HttpClient client = new HttpClient();
! 
!             // Create a method instance.
!             GetMethod method = new GetMethod(url.toExternalForm());
!             
!             // Execute the method.
!             try
!             {
!                 int statusCode = client.executeMethod(method);
!                 if (statusCode != HttpStatus.SC_OK)
!                 {
!                     return 0;
!                 }
!             }
!             catch (HttpException e)
!             {
!                 // TODO Auto-generated catch block
!                 e.printStackTrace();
!             }
!             catch (IOException e)
!             {
!                 // TODO Auto-generated catch block
!                 e.printStackTrace();
!             }
  
!             HttpURLConnection connection = new HttpURLConnection(method, url);
!             
!             return connection.getLastModified();
  
          }
          try
          {
--- 567,586 ----
      public static long getLastModified(URL url)
      {
!         return getLastModified(url, null);
!     }
  
!     public static long getLastModified(URL url, String proxyHost)
!     {
!         return getLastModified(url, proxyHost, null);
!     }
  
+     public static long getLastModified(URL url, String proxyHost, Integer proxyPort)
+     {
+         if (url.getProtocol().equals(PROTOCOL_HTTP))
+         {
+             WebResource wr = new WebResource(url, proxyHost, proxyPort);
+             return wr.getLastModified();
          }
+ 
          try
          {
***************
*** 658,662 ****
      public static boolean isNewer(URL left, URL right)
      {
!         return NetUtil.getLastModified(left) > NetUtil.getLastModified(right);
      }
  
--- 615,627 ----
      public static boolean isNewer(URL left, URL right)
      {
!         return isNewer(left, right, null);
!     }
!     public static boolean isNewer(URL left, URL right, String proxyHost)
!     {
!         return isNewer(left, right, proxyHost, null);
!     }
!     public static boolean isNewer(URL left, URL right, String proxyHost, Integer proxyPort)
!     {
!         return NetUtil.getLastModified(left, proxyHost, proxyPort) > NetUtil.getLastModified(right, proxyHost, proxyPort);
      }
  

Index: Msg.java
===================================================================
RCS file: /cvs/jsword/common/java/core/org/crosswire/common/util/Msg.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** Msg.java	27 Jul 2005 23:26:42 -0000	1.6
--- Msg.java	21 Aug 2005 20:37:49 -0000	1.7
***************
*** 28,31 ****
--- 28,32 ----
   *      The copyright to this program is held by it's authors.
   * @author Joe Walker [joe at eireneh dot com]
+  * @author DM Smith [dmsmith555 at yahoo dot com]
   */
  final class Msg extends MsgBase
***************
*** 44,47 ****
--- 45,49 ----
      static final Msg NOT_ASSIGNABLE = new Msg("ResourceUtil.NotAssignable"); //$NON-NLS-1$
      static final Msg UNAVILABLE = new Msg("ThreadUtil.Unavailable"); //$NON-NLS-1$
+     static final Msg MISSING_FILE = new Msg("WebResource.MissingFile"); //$NON-NLS-1$
  
      /**



More information about the jsword-svn mailing list