Java Source Code: org.crosswire.common.util.WebResource


   1: /**
   2:  * Distribution License:
   3:  * BibleDesktop is free software; you can redistribute it and/or modify it under
   4:  * the terms of the GNU General Public License, version 2 as published by
   5:  * the Free Software Foundation. This program is distributed in the hope
   6:  * that it will be useful, but WITHOUT ANY WARRANTY; without even the
   7:  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   8:  * See the GNU General Public License for more details.
   9:  *
  10:  * The License is available on the internet at:
  11:  *       http://www.gnu.org/copyleft/gpl.html
  12:  * or by writing to:
  13:  *      Free Software Foundation, Inc.
  14:  *      59 Temple Place - Suite 330
  15:  *      Boston, MA 02111-1307, USA
  16:  *
  17:  * Copyright: 2005
  18:  *     The copyright to this program is held by it's authors.
  19:  *
  20:  * ID: $Id: WebResource.java 1167 2006-10-19 21:47:05Z dmsmith $
  21:  */
  22: package org.crosswire.common.util;
  23: 
  24: import java.io.IOException;
  25: import java.io.InputStream;
  26: import java.io.OutputStream;
  27: import java.net.URL;
  28: import java.util.Date;
  29: 
  30: import org.apache.commons.httpclient.HostConfiguration;
  31: import org.apache.commons.httpclient.HttpClient;
  32: import org.apache.commons.httpclient.HttpHost;
  33: import org.apache.commons.httpclient.HttpMethod;
  34: import org.apache.commons.httpclient.HttpStatus;
  35: import org.apache.commons.httpclient.ProxyHost;
  36: import org.apache.commons.httpclient.methods.GetMethod;
  37: import org.apache.commons.httpclient.methods.HeadMethod;
  38: import org.apache.commons.httpclient.util.HttpURLConnection;
  39: 
  40: /**
  41:  * A WebResource is backed by an URL and potentially the proxy through which it
  42:  * need go. It can get basic information about the resource and it can get the
  43:  * resource.
  44:  * 
  45:  * @see gnu.lgpl.License for license details.<br> The copyright to this program is
  46:  *      held by it's authors.
  47:  * @author DM Smith [dmsmith555 at yahoo dot com]
  48:  */
  49: public class WebResource
  50:	  {
  51:    public WebResource(URL theURL)
  52:	      {
  53:        this(theURL, null);
  54:    }
  55:
  56:    public WebResource(URL theURL, String theProxyHost)
  57:	      {
  58:        this(theURL, theProxyHost, null);
  59:    }
  60:
  61:    public WebResource(URL theURL, String theProxyHost, Integer theProxyPort)
  62:	      {
  63:        url = theURL;
  64:        client = new HttpClient();
  65:        HostConfiguration config = client.getHostConfiguration();
  66:        config.setHost(new HttpHost(theURL.getHost(), theURL.getPort()));
  67:        if (theProxyHost != null && theProxyHost.length() > 0)
  68:	          {
  69:            config.setProxyHost(new ProxyHost(theProxyHost, theProxyPort == null ? -1 : theProxyPort.intValue()));
  70:        }
  71:    }
  72:
  73:    /**
  74:     * Determine the size of this WebResource.
  75:     * <p>Note that the http client may read the entire file to determine this.</p>
  76:     * 
  77:     * @return the size of the file
  78:     */
  79:    public int getSize()
  80:	      {
  81:        HttpMethod method = new HeadMethod(url.getPath());
  82:
  83:        try
  84:	          {
  85:            // Execute the method.
  86:            int status = client.executeMethod(method);
  87:            if (status == HttpStatus.SC_OK)
  88:	              {
  89:                return new HttpURLConnection(method, url).getContentLength();
  90:            }
  91:            String reason = HttpStatus.getStatusText(status);
  92:            Reporter.informUser(this, Msg.MISSING_FILE, new Object[] { reason + ':' + url.getFile() });
  93:        }
  94:        catch (IOException e)
  95:	          {
  96:            return 0;
  97:        }
  98:        finally
  99:	          {
 100:            // Release the connection.
 101:            method.releaseConnection();
 102:        }
 103:        return 0;
 104:    }
 105:
 106:    /**
 107:     * Determine the last modified date of this WebResource.
 108:     * <p>Note that the http client may read the entire file.</p>
 109:     * 
 110:     * @return the last mod date of the file
 111:     */
 112:    public long getLastModified()
 113:	      {
 114:        HttpMethod method = new HeadMethod(url.getPath());
 115:
 116:        try
 117:	          {
 118:            // Execute the method.
 119:            if (client.executeMethod(method) == HttpStatus.SC_OK)
 120:	              {
 121:                return new HttpURLConnection(method, url).getLastModified();
 122:            }
 123:        }
 124:        catch (IOException e)
 125:	          {
 126:            return new Date().getTime();
 127:        }
 128:        finally
 129:	          {
 130:            // Release the connection.
 131:            method.releaseConnection();
 132:        }
 133:        return new Date().getTime();
 134:    }
 135:
 136:    /**
 137:     * Copy this WebResource to the destination.
 138:     * 
 139:     * @param dest
 140:     * @throws LucidException
 141:     */
 142:    public void copy(URL dest) throws LucidException
 143:	      {
 144:        InputStream in = null;
 145:        OutputStream out = null;
 146:
 147:        HttpMethod method = new GetMethod(url.getPath());
 148:
 149:        try
 150:	          {
 151:            // Execute the method.
 152:            if (client.executeMethod(method) == HttpStatus.SC_OK)
 153:	              {
 154:                in = method.getResponseBodyAsStream();
 155:
 156:                // Download the index file
 157:                out = NetUtil.getOutputStream(dest);
 158:
 159:                byte[] buf = new byte[4096];
 160:                int count = in.read(buf);
 161:                while (-1 != count)
 162:	                  {
 163:                    out.write(buf, 0, count);
 164:                    count = in.read(buf);
 165:                }
 166:            }
 167:        }
 168:        catch (IOException e)
 169:	          {
 170:            throw new LucidException(Msg.MISSING_FILE, e);
 171:        }
 172:        finally
 173:	          {
 174:            // Release the connection.
 175:            method.releaseConnection();
 176:            // Close the streams
 177:            IOUtil.close(in);
 178:            IOUtil.close(out);
 179:        }
 180:    }
 181:
 182:    private URL url;
 183:    private HttpClient client;
 184:}