1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.net;
24
25 import java.io.BufferedInputStream;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.URL;
31 import java.net.URLConnection;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
35 import org.archive.util.ProcessUtils;
36 import org.archive.util.ProcessUtils.ProcessResult;
37
38 /***
39 * An URL Connection that pre-downloads URL reference before passing back a
40 * Stream reference. When closed, it removes the local download file.
41 * @author stack
42 * @version $Date: 2006-08-18 16:13:32 +0000 (Fri, 18 Aug 2006) $, $Revision: 4510 $
43 */
44 public abstract class DownloadURLConnection extends URLConnection {
45 private final String CLASSNAME = DownloadURLConnection.class.getName();
46 private final Logger LOGGER = Logger.getLogger(CLASSNAME);
47 private static final File TMPDIR =
48 new File(System.getProperty("java.io.tmpdir", "/tmp"));
49 private File downloadFile = null;
50
51 protected DownloadURLConnection(URL u) {
52 super(u);
53 }
54
55 protected String getScript() {
56 return System.getProperty(this.getClass().getName() + ".path",
57 "UNDEFINED");
58 }
59
60 protected String [] getCommand(final URL thisUrl,
61 final File downloadFile) {
62 return new String[] {getScript(), thisUrl.getPath(),
63 downloadFile.getAbsolutePath()};
64 }
65
66 /***
67 * Do script copy to local file.
68 * File is available via {@link #getFile()}.
69 * @throws IOException
70 */
71 public void connect() throws IOException {
72 if (this.connected) {
73 return;
74 }
75
76 this.downloadFile = File.createTempFile(CLASSNAME, null, TMPDIR);
77 try {
78 String [] cmd = getCommand(this.url, this.downloadFile);
79 if (LOGGER.isLoggable(Level.FINE)) {
80 StringBuffer buffer = new StringBuffer();
81 for (int i = 0; i < cmd.length; i++) {
82 if (i > 0) {
83 buffer.append(" ");
84 }
85 buffer.append(cmd[i]);
86 }
87 LOGGER.fine("Command: " + buffer.toString());
88 }
89 ProcessResult pr = ProcessUtils.exec(cmd);
90 if (pr.getResult() != 0) {
91 LOGGER.info(cmd + " returned non-null " + pr.getResult());
92 }
93
94 this.connected = true;
95 } catch (IOException ioe) {
96
97 this.downloadFile.delete();
98 this.downloadFile = null;
99
100 throw ioe;
101 }
102 }
103
104 public File getFile() {
105 return this.downloadFile;
106 }
107
108 protected void setFile(final File f) {
109 this.downloadFile = f;
110 }
111
112 public InputStream getInputStream() throws IOException {
113 if (!this.connected) {
114 connect();
115 }
116
117
118
119
120 final DownloadURLConnection connection = this;
121 return new BufferedInputStream(new FileInputStream(this.downloadFile)) {
122 private DownloadURLConnection ruc = connection;
123
124 public void close() throws IOException {
125 super.close();
126 if (this.ruc != null && this.ruc.getFile()!= null &&
127 this.ruc.getFile().exists()) {
128 this.ruc.getFile().delete();
129 this.ruc.setFile(null);
130 }
131 }
132 };
133 }
134 }