Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / Download.java @ 38431

History | View | Annotate | Download (7.5 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.installer.lib.impl.utils;
23

    
24
import java.io.BufferedInputStream;
25
import java.io.BufferedOutputStream;
26
import java.io.File;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import java.net.URL;
30
import java.net.URLConnection;
31
import java.util.Date;
32
import java.util.StringTokenizer;
33

    
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.task.AbstractMonitorableTask;
39
import org.gvsig.tools.task.SimpleTaskStatus;
40
import org.gvsig.tools.task.TaskStatusManager;
41

    
42
/**
43
 * @author gvSIG Team
44
 * @version $Id$
45
 * 
46
 */
47
public class Download extends AbstractMonitorableTask {
48

    
49
        private static final Logger LOG = LoggerFactory.getLogger(Download.class);
50
        
51
        /**
52
         * If file size provided by URLConnection is smaller than this,
53
         * then the downloaded file size will always be accepted (unit: bytes)
54
         */
55
    private static final int FILE_SIZE_ESTIMATED_MINIMUM = 10000;
56
    
57
    /**
58
     * Downloaded file size will be accepted if it is bigger than this ratio
59
     * multiplied by the estimated file size provided by URLConnection
60
     * (unit: bytes)
61
     */
62
    private static final double FILE_SIZE_ESTIMATED_RATIO = 0.85;
63
    
64
        private boolean selfCreatedTaskStatus = true;
65

    
66
        /**
67
         * @param taskName
68
         */
69
        public Download(SimpleTaskStatus taskStatus) {
70
                this();
71
                
72
                /*
73
                 * constructor without params is adding
74
                 * itself to manager, so we remove it
75
                 */
76
        TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
77
        manager.remove(getTaskStatus());
78
        
79
        /*
80
         * The received taskstatus is being managed by somebody else,
81
         * we don not add it to the manager
82
         */
83
                this.taskStatus = taskStatus;
84
                selfCreatedTaskStatus = false;
85
        }
86

    
87
        public Download() {
88
            /**
89
             * this call is adding the task status to the manager
90
             */
91
                super("Downloading...");
92
        }
93

    
94
        public File downloadFile(URL url, String defaultFileName)
95
                        throws IOException {
96

    
97
                URL downloadURL = url;
98

    
99
                // check if the URL ends with '/' and append the file name
100
                if (defaultFileName != null) {
101
                        String urlStr = url.toString();
102
                        if (urlStr.endsWith("/")) {
103
                                urlStr = urlStr.concat(defaultFileName);
104
                                downloadURL = new URL(urlStr);
105
                        }
106
                }
107

    
108
                URLConnection connection = downloadURL.openConnection();
109
                String fileName = getFileName(connection);
110

    
111
                if (LOG.isDebugEnabled()) {
112
                        Date date = new Date(connection.getLastModified());
113
                        LOG
114
                                        .debug(
115
                                                        "Downloading file {} from URL {}, with last modified date: {}",
116
                                                        new Object[] { fileName, downloadURL, date });
117
                }
118

    
119
                String fileNamePrefix = fileName;
120
                String fileNameSuffix = "zip";
121
                int dotPosition = fileName.lastIndexOf('.');
122
                if ((dotPosition > -1) && (dotPosition < fileName.length() - 1)) {
123
                        fileNamePrefix = fileName.substring(0, dotPosition);
124
                        fileNameSuffix = fileName.substring(dotPosition);
125
                }
126

    
127
                BufferedInputStream bis = new BufferedInputStream(downloadURL
128
                                .openStream());
129

    
130
                File localFile = File.createTempFile(fileNamePrefix, fileNameSuffix);
131

    
132
                BufferedOutputStream bos = new BufferedOutputStream(
133
                                new FileOutputStream(localFile));
134

    
135
                int expected_size = connection.getContentLength();
136
                this.taskStatus.setRangeOfValues(0, expected_size);
137
                try {
138
                        byte[] data = new byte[1024];
139
                        int count = 0;
140
                        long totalCount = 0; // total bytes read
141
                        while ((count = bis.read(data, 0, 1024)) >= 0) {
142
                            
143
                                bos.write(data, 0, count);
144
                                totalCount += count;
145
                                
146
                                this.taskStatus.setCurValue(totalCount);
147

    
148
                                if (this.taskStatus.isCancellationRequested()) {
149
                                        break;
150
                                }
151
                        }
152
                        
153
                        if (selfCreatedTaskStatus) {
154
                                this.taskStatus.terminate();
155
                                this.taskStatus.remove();
156
                        }
157
                        
158
                        // out from the read loop
159
                        // perhaps it was cancelled:
160
            if (this.taskStatus.isCancellationRequested()) {
161
                return null;
162
            }
163
            // check real size = expected size:
164
            if (!acceptableFileSize(localFile, expected_size)) {
165
                throw new IOException("Bad download file size ("
166
                    + localFile.length()
167
                    + " / "
168
                    + expected_size
169
                    + ")");
170
            }
171
                        
172
            // everything seems to be OK
173
                        return localFile;
174
                } finally {
175
                        bis.close();
176
                        bos.flush();
177
                        bos.close();
178
                }
179

    
180
        }
181

    
182
        /**
183
         * Returns the file name associated to an url connection.<br />
184
         * The result is not a path but just a file name.
185
         * 
186
         * @param urlConnection
187
         *            - the url connection
188
         * @return the file name
189
         * 
190
         * @throws IOException
191
         *             Signals that an I/O exception has occurred.
192
         */
193
        private String getFileName(URLConnection urlConnection) throws IOException {
194
                String fileName = null;
195

    
196
                String contentDisposition = urlConnection
197
                                .getHeaderField("content-disposition");
198

    
199
                if (contentDisposition != null) {
200
                        fileName = extractFileNameFromContentDisposition(contentDisposition);
201
                }
202

    
203
                // if the file name cannot be extracted from the content-disposition
204
                // header, using the url.getFilename() method
205
                if (fileName == null) {
206
                        StringTokenizer st = new StringTokenizer(urlConnection.getURL()
207
                                        .getFile(), "/");
208
                        while (st.hasMoreTokens()) {
209
                                fileName = st.nextToken();
210
                        }
211
                }
212

    
213
                return fileName;
214
        }
215

    
216
        /**
217
         * Extract the file name from the content disposition header.
218
         * <p>
219
         * See <a
220
         * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html">http:
221
         * //www.w3.org/Protocols/rfc2616/rfc2616-sec19.html</a> for detailled
222
         * information regarding the headers in HTML.
223
         * 
224
         * @param contentDisposition
225
         *            - the content-disposition header. Cannot be <code>null>/code>.
226
         * @return the file name, or <code>null</code> if the content-disposition
227
         *         header does not contain the filename attribute.
228
         */
229
        private String extractFileNameFromContentDisposition(
230
                        String contentDisposition) {
231
                String[] attributes = contentDisposition.split(";");
232

    
233
                for (String a : attributes) {
234
                        if (a.toLowerCase().contains("filename")) {
235
                                // The attribute is the file name. The filename is between
236
                                // quotes.
237
                                return a.substring(a.indexOf('\"') + 1, a.lastIndexOf('\"'));
238
                        }
239
                }
240

    
241
                // not found
242
                return null;
243

    
244
        }
245
        
246
        /**
247
         * {@link URLConnection}'s method getContentLength() does not give
248
         * exact size.
249
         * 
250
         * @return whether the size is acceptable
251
         */
252
        public static boolean acceptableFileSize(File f, int estimated_size) {
253
            
254
            if (f == null) {
255
                return false;
256
            }
257
            
258
            if (estimated_size == -1) {
259
                // -1 means unknown
260
                return true;
261
            }
262
            
263
            if (estimated_size < FILE_SIZE_ESTIMATED_MINIMUM) {
264
                return true;
265
            }
266
            
267
            return f.length() > (FILE_SIZE_ESTIMATED_RATIO * estimated_size);
268
        }
269
}