Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HttpSwordInstaller |
|
| 2.5714285714285716;2.571 |
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 or later | |
5 | * as published by the Free Software Foundation. This program is distributed | |
6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
7 | * the 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 | * © CrossWire Bible Society, 2005 - 2016 | |
18 | * | |
19 | */ | |
20 | package org.crosswire.jsword.book.install.sword; | |
21 | ||
22 | import java.net.URI; | |
23 | import java.net.URISyntaxException; | |
24 | ||
25 | import org.crosswire.common.progress.Progress; | |
26 | import org.crosswire.common.util.LucidException; | |
27 | import org.crosswire.common.util.NetUtil; | |
28 | import org.crosswire.common.util.WebResource; | |
29 | import org.crosswire.jsword.JSMsg; | |
30 | import org.crosswire.jsword.book.Book; | |
31 | import org.crosswire.jsword.book.install.InstallException; | |
32 | ||
33 | /** | |
34 | * An implementation of Installer for reading data from Sword Web sites. | |
35 | * | |
36 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
37 | * @author Mark Goodwin | |
38 | * @author Joe Walker | |
39 | * @author DM Smith | |
40 | */ | |
41 | 0 | public class HttpSwordInstaller extends AbstractSwordInstaller { |
42 | ||
43 | /* (non-Javadoc) | |
44 | * @see org.crosswire.jsword.book.install.Installer#getType() | |
45 | */ | |
46 | public String getType() { | |
47 | 0 | return "sword-http"; |
48 | } | |
49 | ||
50 | /* (non-Javadoc) | |
51 | * @see org.crosswire.jsword.book.install.Installer#getSize(org.crosswire.jsword.book.Book) | |
52 | */ | |
53 | public int getSize(final Book book) { | |
54 | 0 | return NetUtil.getSize(toRemoteURI(book), proxyHost, proxyPort); |
55 | } | |
56 | ||
57 | /* (non-Javadoc) | |
58 | * @see org.crosswire.jsword.book.install.Installer#toRemoteURI(org.crosswire.jsword.book.Book) | |
59 | */ | |
60 | public URI toRemoteURI(final Book book) { | |
61 | try { | |
62 | 0 | return new URI(NetUtil.PROTOCOL_HTTP, host, packageDirectory + '/' + book.getInitials() + ZIP_SUFFIX, null); |
63 | 0 | } catch (URISyntaxException e) { |
64 | 0 | return null; |
65 | } | |
66 | } | |
67 | ||
68 | /* (non-Javadoc) | |
69 | * @see org.crosswire.jsword.book.install.sword.AbstractSwordInstaller#download(org.crosswire.common.progress.Progress, java.lang.String, java.lang.String, java.net.URI) | |
70 | */ | |
71 | @Override | |
72 | protected void download(Progress job, String dir, String file, URI dest) throws InstallException { | |
73 | URI uri; | |
74 | try { | |
75 | 0 | uri = new URI(NetUtil.PROTOCOL_HTTP, host, dir + '/' + file, null); |
76 | 0 | } catch (URISyntaxException ex) { |
77 | // TRANSLATOR: Common error condition: {0} is a placeholder for the URL of what could not be found. | |
78 | 0 | throw new InstallException(JSMsg.gettext("Unable to find: {0}", dir + '/' + file), ex); |
79 | 0 | } |
80 | ||
81 | try { | |
82 | 0 | copy(job, uri, dest); |
83 | 0 | } catch (LucidException ex) { |
84 | // TRANSLATOR: Common error condition: {0} is a placeholder for the URL of what could not be found. | |
85 | 0 | throw new InstallException(JSMsg.gettext("Unable to find: {0}", uri.toString()), ex); |
86 | 0 | } |
87 | 0 | } |
88 | ||
89 | /** | |
90 | * @param job | |
91 | * @param uri | |
92 | * @param dest | |
93 | * @throws LucidException | |
94 | */ | |
95 | private void copy(Progress job, URI uri, URI dest) throws LucidException { | |
96 | 0 | if (job != null) { |
97 | // TRANSLATOR: Progress label for downloading one or more files. | |
98 | 0 | job.setSectionName(JSMsg.gettext("Downloading files")); |
99 | } | |
100 | ||
101 | 0 | WebResource wr = new WebResource(uri, proxyHost, proxyPort); |
102 | 0 | wr.copy(dest, job); |
103 | 0 | wr.shutdown(); |
104 | 0 | } |
105 | ||
106 | /* (non-Javadoc) | |
107 | * @see org.crosswire.jsword.book.install.sword.AbstractSwordInstaller#equals(java.lang.Object) | |
108 | */ | |
109 | @Override | |
110 | public boolean equals(Object object) { | |
111 | 0 | if (!(object instanceof HttpSwordInstaller)) { |
112 | 0 | return false; |
113 | } | |
114 | 0 | HttpSwordInstaller that = (HttpSwordInstaller) object; |
115 | ||
116 | 0 | if (!super.equals(that)) { |
117 | 0 | return false; |
118 | } | |
119 | ||
120 | 0 | return true; |
121 | } | |
122 | ||
123 | /* (non-Javadoc) | |
124 | * @see org.crosswire.jsword.book.install.sword.AbstractSwordInstaller#hashCode() | |
125 | */ | |
126 | @Override | |
127 | public int hashCode() { | |
128 | 0 | return super.hashCode(); |
129 | } | |
130 | } |