1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.BufferedInputStream;
|
---|
7 | import java.io.BufferedOutputStream;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.FileOutputStream;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.InputStream;
|
---|
13 | import java.net.URL;
|
---|
14 | import java.net.URLConnection;
|
---|
15 | import java.util.Arrays;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.Enumeration;
|
---|
18 | import java.util.zip.ZipEntry;
|
---|
19 | import java.util.zip.ZipFile;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Mirrors a file to a local file.
|
---|
25 | * <p>
|
---|
26 | * The file mirrored is only downloaded if it has been more than 7 days since last download
|
---|
27 | */
|
---|
28 | public class MirroredInputStream extends InputStream {
|
---|
29 | InputStream fs = null;
|
---|
30 | File file = null;
|
---|
31 |
|
---|
32 | public MirroredInputStream(String name) throws IOException {
|
---|
33 | this(name, null, -1L);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public MirroredInputStream(String name, long maxTime) throws IOException {
|
---|
37 | this(name, null, maxTime);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public MirroredInputStream(String name, String destDir) throws IOException {
|
---|
41 | this(name, destDir, -1L);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Get an inputstream from a given filename, url or internal resource.
|
---|
46 | * @param name can be
|
---|
47 | * - relative or absolute file name
|
---|
48 | * - file:///SOME/FILE the same as above
|
---|
49 | * - resource://SOME/FILE file from the classpath (usually in the current *.jar)
|
---|
50 | * - http://... a url. It will be cached on disk.
|
---|
51 | * @param destDir the destination directory for the cache file. only applies for urls.
|
---|
52 | * @param maxTime the maximum age of the cache file (in seconds)
|
---|
53 | * @throws IOException when the resource with the given name could not be retrieved
|
---|
54 | */
|
---|
55 | public MirroredInputStream(String name, String destDir, long maxTime) throws IOException {
|
---|
56 | URL url;
|
---|
57 | try {
|
---|
58 | url = new URL(name);
|
---|
59 | if (url.getProtocol().equals("file")) {
|
---|
60 | file = new File(name.substring("file:/".length()));
|
---|
61 | if (!file.exists()) {
|
---|
62 | file = new File(name.substring("file://".length()));
|
---|
63 | }
|
---|
64 | } else {
|
---|
65 | if(Main.applet) {
|
---|
66 | URLConnection conn = url.openConnection();
|
---|
67 | conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
|
---|
68 | conn.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
|
---|
69 | fs = new BufferedInputStream(conn.getInputStream());
|
---|
70 | file = new File(url.getFile());
|
---|
71 | } else {
|
---|
72 | file = checkLocal(url, destDir, maxTime);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | } catch (java.net.MalformedURLException e) {
|
---|
76 | if(name.startsWith("resource://")) {
|
---|
77 | fs = getClass().getResourceAsStream(
|
---|
78 | name.substring("resource:/".length()));
|
---|
79 | if (fs == null)
|
---|
80 | throw new IOException(tr("Failed to open input stream for resource ''{0}''", name));
|
---|
81 | return;
|
---|
82 | }
|
---|
83 | file = new File(name);
|
---|
84 | }
|
---|
85 | if (file == null)
|
---|
86 | throw new IOException();
|
---|
87 | fs = new FileInputStream(file);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Replies an input stream for a file in a ZIP-file. Replies a file in the top
|
---|
92 | * level directory of the ZIP file which has an extension <code>extension</code>. If more
|
---|
93 | * than one files have this extension, the last file whose name includes <code>namepart</code>
|
---|
94 | * is opened.
|
---|
95 | *
|
---|
96 | * @param extension the extension of the file we're looking for
|
---|
97 | * @param namepart the name part
|
---|
98 | * @return an input stream. Null if this mirrored input stream doesn't represent a zip file or if
|
---|
99 | * there was no matching file in the ZIP file
|
---|
100 | */
|
---|
101 | public InputStream getZipEntry(String extension, String namepart) {
|
---|
102 | if (file == null)
|
---|
103 | return null;
|
---|
104 | InputStream res = null;
|
---|
105 | try {
|
---|
106 | ZipFile zipFile = new ZipFile(file);
|
---|
107 | ZipEntry resentry = null;
|
---|
108 | Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
---|
109 | while (entries.hasMoreElements()) {
|
---|
110 | ZipEntry entry = entries.nextElement();
|
---|
111 | if (entry.getName().endsWith("." + extension)) {
|
---|
112 | /* choose any file with correct extension. When more than
|
---|
113 | one file, prefer the one which matches namepart */
|
---|
114 | if (resentry == null || entry.getName().indexOf(namepart) >= 0) {
|
---|
115 | resentry = entry;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | if (resentry != null) {
|
---|
120 | res = zipFile.getInputStream(resentry);
|
---|
121 | } else {
|
---|
122 | zipFile.close();
|
---|
123 | }
|
---|
124 | } catch (Exception e) {
|
---|
125 | if(file.getName().endsWith(".zip")) {
|
---|
126 | System.err.println(tr("Warning: failed to open file with extension ''{2}'' and namepart ''{3}'' in zip file ''{0}''. Exception was: {1}",
|
---|
127 | file.getName(), e.toString(), extension, namepart));
|
---|
128 | }
|
---|
129 | }
|
---|
130 | return res;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public File getFile()
|
---|
134 | {
|
---|
135 | return file;
|
---|
136 | }
|
---|
137 |
|
---|
138 | static public void cleanup(String name)
|
---|
139 | {
|
---|
140 | cleanup(name, null);
|
---|
141 | }
|
---|
142 | static public void cleanup(String name, String destDir)
|
---|
143 | {
|
---|
144 | URL url;
|
---|
145 | try {
|
---|
146 | url = new URL(name);
|
---|
147 | if (!url.getProtocol().equals("file"))
|
---|
148 | {
|
---|
149 | String prefKey = getPrefKey(url, destDir);
|
---|
150 | // FIXME: replace with normal getCollection after july 2011
|
---|
151 | Collection<String> localPath = Main.pref.getCollectionOld(prefKey, ";");
|
---|
152 | if(localPath.size() == 2) {
|
---|
153 | String[] lp = (String[]) localPath.toArray();
|
---|
154 | File lfile = new File(lp[1]);
|
---|
155 | if(lfile.exists()) {
|
---|
156 | lfile.delete();
|
---|
157 | }
|
---|
158 | }
|
---|
159 | Main.pref.put(prefKey, null);
|
---|
160 | }
|
---|
161 | } catch (java.net.MalformedURLException e) {}
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * get preference key to store the location and age of the cached file.
|
---|
166 | * 2 resources that point to the same url, but that are to be stored in different
|
---|
167 | * directories will not share a cache file.
|
---|
168 | */
|
---|
169 | private static String getPrefKey(URL url, String destDir) {
|
---|
170 | StringBuilder prefKey = new StringBuilder("mirror.");
|
---|
171 | if (destDir != null) {
|
---|
172 | String prefDir = Main.pref.getPreferencesDir();
|
---|
173 | if (destDir.startsWith(prefDir)) {
|
---|
174 | destDir = destDir.substring(prefDir.length());
|
---|
175 | }
|
---|
176 | prefKey.append(destDir);
|
---|
177 | prefKey.append(".");
|
---|
178 | }
|
---|
179 | prefKey.append(url.toString());
|
---|
180 | return prefKey.toString().replaceAll("=","_");
|
---|
181 | }
|
---|
182 |
|
---|
183 | private File checkLocal(URL url, String destDir, long maxTime) throws IOException {
|
---|
184 | String prefKey = getPrefKey(url, destDir);
|
---|
185 | long age = 0L;
|
---|
186 | File file = null;
|
---|
187 | // FIXME: replace with normal getCollection after july 2011
|
---|
188 | Collection<String> localPathEntry = Main.pref.getCollectionOld(prefKey, ";");
|
---|
189 | if(localPathEntry.size() == 2) {
|
---|
190 | String[] lp = (String[]) localPathEntry.toArray();
|
---|
191 | file = new File(lp[1]);
|
---|
192 | if(!file.exists())
|
---|
193 | file = null;
|
---|
194 | else {
|
---|
195 | if (maxTime <= 0) {
|
---|
196 | maxTime = Main.pref.getInteger("mirror.maxtime", 7*24*60*60);
|
---|
197 | }
|
---|
198 | age = System.currentTimeMillis() - Long.parseLong(lp[0]);
|
---|
199 | if (age < maxTime*1000) {
|
---|
200 | return file;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 | if(destDir == null) {
|
---|
205 | destDir = Main.pref.getPreferencesDir();
|
---|
206 | }
|
---|
207 |
|
---|
208 | File destDirFile = new File(destDir);
|
---|
209 | if (!destDirFile.exists()) {
|
---|
210 | destDirFile.mkdirs();
|
---|
211 | }
|
---|
212 |
|
---|
213 | String a = url.toString().replaceAll("[^A-Za-z0-9_.-]", "_");
|
---|
214 | String localPath = "mirror_" + a;
|
---|
215 | destDirFile = new File(destDir, localPath + ".tmp");
|
---|
216 | BufferedOutputStream bos = null;
|
---|
217 | BufferedInputStream bis = null;
|
---|
218 | try {
|
---|
219 | URLConnection conn = url.openConnection();
|
---|
220 | conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
|
---|
221 | conn.setReadTimeout(Main.pref.getInteger("socket.timeout.read",30)*1000);
|
---|
222 | bis = new BufferedInputStream(conn.getInputStream());
|
---|
223 | FileOutputStream fos = new FileOutputStream(destDirFile);
|
---|
224 | bos = new BufferedOutputStream(fos);
|
---|
225 | byte[] buffer = new byte[4096];
|
---|
226 | int length;
|
---|
227 | while ((length = bis.read(buffer)) > -1) {
|
---|
228 | bos.write(buffer, 0, length);
|
---|
229 | }
|
---|
230 | bos.close();
|
---|
231 | bos = null;
|
---|
232 | /* close fos as well to be sure! */
|
---|
233 | fos.close();
|
---|
234 | fos = null;
|
---|
235 | file = new File(destDir, localPath);
|
---|
236 | if(Main.platform.rename(destDirFile, file)) {
|
---|
237 | Main.pref.putCollection(prefKey, Arrays.asList(new String[]
|
---|
238 | {Long.toString(System.currentTimeMillis()), file.toString()}));
|
---|
239 | } else {
|
---|
240 | System.out.println(tr("Failed to rename file {0} to {1}.",
|
---|
241 | destDirFile.getPath(), file.getPath()));
|
---|
242 | }
|
---|
243 | } catch (IOException e) {
|
---|
244 | if (age >= maxTime*1000 && age < maxTime*1000*2) {
|
---|
245 | System.out.println(tr("Failed to load {0}, use cached file and retry next time: {1}",
|
---|
246 | url, e));
|
---|
247 | return file;
|
---|
248 | } else {
|
---|
249 | throw e;
|
---|
250 | }
|
---|
251 | } finally {
|
---|
252 | if (bis != null) {
|
---|
253 | try {
|
---|
254 | bis.close();
|
---|
255 | } catch (IOException e) {
|
---|
256 | e.printStackTrace();
|
---|
257 | }
|
---|
258 | }
|
---|
259 | if (bos != null) {
|
---|
260 | try {
|
---|
261 | bos.close();
|
---|
262 | } catch (IOException e) {
|
---|
263 | e.printStackTrace();
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | return file;
|
---|
269 | }
|
---|
270 | @Override
|
---|
271 | public int available() throws IOException
|
---|
272 | { return fs.available(); }
|
---|
273 | @Override
|
---|
274 | public void close() throws IOException
|
---|
275 | { fs.close(); }
|
---|
276 | @Override
|
---|
277 | public int read() throws IOException
|
---|
278 | { return fs.read(); }
|
---|
279 | @Override
|
---|
280 | public int read(byte[] b) throws IOException
|
---|
281 | { return fs.read(b); }
|
---|
282 | @Override
|
---|
283 | public int read(byte[] b, int off, int len) throws IOException
|
---|
284 | { return fs.read(b,off, len); }
|
---|
285 | @Override
|
---|
286 | public long skip(long n) throws IOException
|
---|
287 | { return fs.skip(n); }
|
---|
288 | }
|
---|