001: /* $Id: DownloadURLConnection.java 4510 2006-08-18 16:13:32Z stack-sf $
002: *
003: * Created August 11th, 2006
004: *
005: * Copyright (C) 2006 Internet Archive.
006: *
007: * This file is part of the Heritrix web crawler (crawler.archive.org).
008: *
009: * Heritrix is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU Lesser Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * any later version.
013: *
014: * Heritrix is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser Public License
020: * along with Heritrix; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.archive.net;
024:
025: import java.io.BufferedInputStream;
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.net.URL;
031: import java.net.URLConnection;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: import org.archive.util.ProcessUtils;
036: import org.archive.util.ProcessUtils.ProcessResult;
037:
038: /**
039: * An URL Connection that pre-downloads URL reference before passing back a
040: * Stream reference. When closed, it removes the local download file.
041: * @author stack
042: * @version $Date: 2006-08-18 16:13:32 +0000 (Fri, 18 Aug 2006) $, $Revision: 4510 $
043: */
044: public abstract class DownloadURLConnection extends URLConnection {
045: private final String CLASSNAME = DownloadURLConnection.class
046: .getName();
047: private final Logger LOGGER = Logger.getLogger(CLASSNAME);
048: private static final File TMPDIR = new File(System.getProperty(
049: "java.io.tmpdir", "/tmp"));
050: private File downloadFile = null;
051:
052: protected DownloadURLConnection(URL u) {
053: super (u);
054: }
055:
056: protected String getScript() {
057: return System.getProperty(this .getClass().getName() + ".path",
058: "UNDEFINED");
059: }
060:
061: protected String[] getCommand(final URL this Url,
062: final File downloadFile) {
063: return new String[] { getScript(), this Url.getPath(),
064: downloadFile.getAbsolutePath() };
065: }
066:
067: /**
068: * Do script copy to local file.
069: * File is available via {@link #getFile()}.
070: * @throws IOException
071: */
072: public void connect() throws IOException {
073: if (this .connected) {
074: return;
075: }
076:
077: this .downloadFile = File
078: .createTempFile(CLASSNAME, null, TMPDIR);
079: try {
080: String[] cmd = getCommand(this .url, this .downloadFile);
081: if (LOGGER.isLoggable(Level.FINE)) {
082: StringBuffer buffer = new StringBuffer();
083: for (int i = 0; i < cmd.length; i++) {
084: if (i > 0) {
085: buffer.append(" ");
086: }
087: buffer.append(cmd[i]);
088: }
089: LOGGER.fine("Command: " + buffer.toString());
090: }
091: ProcessResult pr = ProcessUtils.exec(cmd);
092: if (pr.getResult() != 0) {
093: LOGGER.info(cmd + " returned non-null "
094: + pr.getResult());
095: }
096: // Assume download went smoothly.
097: this .connected = true;
098: } catch (IOException ioe) {
099: // Clean up my tmp file.
100: this .downloadFile.delete();
101: this .downloadFile = null;
102: // Rethrow.
103: throw ioe;
104: }
105: }
106:
107: public File getFile() {
108: return this .downloadFile;
109: }
110:
111: protected void setFile(final File f) {
112: this .downloadFile = f;
113: }
114:
115: public InputStream getInputStream() throws IOException {
116: if (!this .connected) {
117: connect();
118: }
119:
120: // Return BufferedInputStream so 'delegation' is done for me, so
121: // I don't have to implement all IS methods and pass to my
122: // 'delegate' instance.
123: final DownloadURLConnection connection = this ;
124: return new BufferedInputStream(new FileInputStream(
125: this .downloadFile)) {
126: private DownloadURLConnection ruc = connection;
127:
128: public void close() throws IOException {
129: super.close();
130: if (this.ruc != null && this.ruc.getFile() != null
131: && this.ruc.getFile().exists()) {
132: this.ruc.getFile().delete();
133: this.ruc.setFile(null);
134: }
135: }
136: };
137: }
138: }
|