1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 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 Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.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: HttpSwordInstaller.java 2099 2011-03-07 17:13:00Z dmsmith $
21   */
22  package org.crosswire.jsword.book.install.sword;
23  
24  import java.net.URI;
25  import java.net.URISyntaxException;
26  
27  import org.crosswire.common.progress.Progress;
28  import org.crosswire.common.util.LucidException;
29  import org.crosswire.common.util.NetUtil;
30  import org.crosswire.common.util.WebResource;
31  import org.crosswire.jsword.JSMsg;
32  import org.crosswire.jsword.book.Book;
33  import org.crosswire.jsword.book.install.InstallException;
34  
35  /**
36   * An implementation of Installer for reading data from Sword Web sites.
37   * 
38   * @see gnu.lgpl.License for license details.<br>
39   *      The copyright to this program is held by it's authors.
40   * @author Mark Goodwin [goodwinster at gmail dot com]
41   * @author Joe Walker [joe at eireneh dot com]
42   * @author DM Smith [dmsmith555 at yahoo dot com]
43   */
44  /**
45   *
46   *
47   * @see gnu.lgpl.License for license details.<br>
48   *      The copyright to this program is held by it's authors.
49   * @author DM Smith [dmsmith555 at yahoo dot com]
50   */
51  public class HttpSwordInstaller extends AbstractSwordInstaller {
52  
53      /* (non-Javadoc)
54       * @see org.crosswire.jsword.book.install.Installer#getType()
55       */
56      public String getType() {
57          return "sword-http";
58      }
59  
60      /* (non-Javadoc)
61       * @see org.crosswire.jsword.book.install.Installer#getSize(org.crosswire.jsword.book.Book)
62       */
63      public int getSize(Book book) {
64          return NetUtil.getSize(toRemoteURI(book), proxyHost, proxyPort);
65      }
66  
67      /* (non-Javadoc)
68       * @see org.crosswire.jsword.book.install.Installer#toRemoteURI(org.crosswire.jsword.book.Book)
69       */
70      public URI toRemoteURI(Book book) {
71          try {
72              return new URI(NetUtil.PROTOCOL_HTTP, host, packageDirectory + '/' + book.getInitials() + ZIP_SUFFIX, null);
73          } catch (URISyntaxException e) {
74              return null;
75          }
76      }
77  
78      /* (non-Javadoc)
79       * @see org.crosswire.jsword.book.install.sword.AbstractSwordInstaller#download(org.crosswire.common.progress.Progress, java.lang.String, java.lang.String, java.net.URI)
80       */
81      @Override
82      protected void download(Progress job, String dir, String file, URI dest) throws InstallException {
83          URI uri;
84          try {
85              uri = new URI(NetUtil.PROTOCOL_HTTP, host, dir + '/' + file, null);
86          } catch (URISyntaxException e1) {
87              // TRANSLATOR: Common error condition: {0} is a placeholder for the URL of what could not be found.
88              throw new InstallException(JSMsg.gettext("Unable to find: {0}", dir + '/' + file), e1);
89          }
90  
91          try {
92              copy(job, uri, dest);
93          } catch (LucidException ex) {
94              // TRANSLATOR: Common error condition: {0} is a placeholder for the URL of what could not be found.
95              throw new InstallException(JSMsg.gettext("Unable to find: {0}", uri.toString()), ex);
96          }
97      }
98  
99      /**
100      * @param job
101      * @param uri
102      * @param dest
103      * @throws LucidException
104      */
105     private void copy(Progress job, URI uri, URI dest) throws LucidException {
106         if (job != null) {
107             // TRANSLATOR: Progress label for downloading one or more files.
108             job.setSectionName(JSMsg.gettext("Downloading files"));
109         }
110 
111         WebResource wr = new WebResource(uri, proxyHost, proxyPort);
112         wr.copy(dest, job);
113         wr.shutdown();
114     }
115 
116     /* (non-Javadoc)
117      * @see org.crosswire.jsword.book.install.sword.AbstractSwordInstaller#equals(java.lang.Object)
118      */
119     @Override
120     public boolean equals(Object object) {
121         if (!(object instanceof HttpSwordInstaller)) {
122             return false;
123         }
124         HttpSwordInstaller that = (HttpSwordInstaller) object;
125 
126         if (!super.equals(that)) {
127             return false;
128         }
129 
130         return true;
131     }
132 
133     /* (non-Javadoc)
134      * @see org.crosswire.jsword.book.install.sword.AbstractSwordInstaller#hashCode()
135      */
136     @Override
137     public int hashCode() {
138         return super.hashCode();
139     }
140 }
141