Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2057 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / Download.java @ 39186

History | View | Annotate | Download (8.67 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
                connection.setUseCaches(false);
110
                connection.setConnectTimeout(30000);
111
                connection.setReadTimeout(20000);
112
                
113
                String fileName = getFileName(connection);
114

    
115
                // if (LOG.isDebugEnabled()) {
116
                        Date date = new Date(connection.getLastModified());
117
                        LOG
118
                                        .info(
119
                                                        "Downloading file {} from URL {}, with last modified date: {}",
120
                                                        new Object[] { fileName, downloadURL, date });
121
                // }
122

    
123
                String fileNamePrefix = fileName;
124
                String fileNameSuffix = "zip";
125
                int dotPosition = fileName.lastIndexOf('.');
126
                if ((dotPosition > -1) && (dotPosition < fileName.length() - 1)) {
127
                        fileNamePrefix = fileName.substring(0, dotPosition);
128
                        fileNameSuffix = fileName.substring(dotPosition);
129
                }
130

    
131
                BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
132

    
133
                File localFile = File.createTempFile(fileNamePrefix, fileNameSuffix);
134

    
135
                BufferedOutputStream bos = new BufferedOutputStream(
136
                                new FileOutputStream(localFile));
137

    
138
                int expected_size = connection.getContentLength();
139
                this.taskStatus.setRangeOfValues(0, expected_size);
140
                
141

    
142
                        byte[] data = new byte[1024];
143
                        int count = 0;
144
                        long totalCount = 0; // total bytes read
145
                        while ((count = bis.read(data, 0, 1024)) >= 0) {
146
                            
147
                                bos.write(data, 0, count);
148
                                totalCount += count;
149
                                
150
                                this.taskStatus.setCurValue(totalCount);
151

    
152
                                if (this.taskStatus.isCancellationRequested()) {
153
                                        break;
154
                                }
155
                        }
156
                        
157
                        try {
158
                         bis.close();
159
                         bos.flush();
160
                         bos.close();
161
                        } catch (Exception ex) {
162
                            LOG.info("Error while closing download streams: " + ex.getMessage());
163
                        }
164
                        
165
                        if (selfCreatedTaskStatus) {
166
                                this.taskStatus.terminate();
167
                                this.taskStatus.remove();
168
                        }
169
                        
170
                        // out from the read loop
171
                        // perhaps it was cancelled:
172
            if (this.taskStatus.isCancellationRequested()) {
173
                return null;
174
            }
175
            
176
            String md5_ = MD5BinaryFileUtils.getMD5InRemoteFile(downloadURL);
177
            
178
            if (md5_ != null) {
179
                // ==========================================
180
                // check md5
181
                String local_md5 = null;
182
                try {
183
                    local_md5 = MD5BinaryFileUtils.getMD5Checksum(localFile);
184
                } catch (Exception e) {
185
                    throw new IOException("Unable to get MD5 for file: " + downloadURL.toString());
186
                }
187
                if (local_md5.compareTo(md5_) != 0) {
188
                    throw new IOException("MD5 does not match for file: " + downloadURL.toString());
189
                }
190
                // ==========================================
191
            } else {
192
                // ==========================================
193
                // check real size and expected size:
194
                if (!acceptableFileSize(localFile, expected_size)) {
195
                    throw new IOException("Bad download file size ("
196
                        + localFile.length()
197
                        + " / "
198
                        + expected_size
199
                        + ")");
200
                }
201
                // ==========================================
202
            }
203
                        
204
            // everything seems to be OK
205
                        return localFile;
206

    
207
        }
208

    
209
        /**
210
         * Returns the file name associated to an url connection.<br />
211
         * The result is not a path but just a file name.
212
         * 
213
         * @param urlConnection
214
         *            - the url connection
215
         * @return the file name
216
         * 
217
         * @throws IOException
218
         *             Signals that an I/O exception has occurred.
219
         */
220
        private String getFileName(URLConnection urlConnection) throws IOException {
221
                String fileName = null;
222

    
223
                String contentDisposition = urlConnection
224
                                .getHeaderField("content-disposition");
225

    
226
                if (contentDisposition != null) {
227
                        fileName = extractFileNameFromContentDisposition(contentDisposition);
228
                }
229

    
230
                // if the file name cannot be extracted from the content-disposition
231
                // header, using the url.getFilename() method
232
                if (fileName == null) {
233
                        StringTokenizer st = new StringTokenizer(urlConnection.getURL()
234
                                        .getFile(), "/");
235
                        while (st.hasMoreTokens()) {
236
                                fileName = st.nextToken();
237
                        }
238
                }
239

    
240
                return fileName;
241
        }
242

    
243
        /**
244
         * Extract the file name from the content disposition header.
245
         * <p>
246
         * See <a
247
         * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html">http:
248
         * //www.w3.org/Protocols/rfc2616/rfc2616-sec19.html</a> for detailled
249
         * information regarding the headers in HTML.
250
         * 
251
         * @param contentDisposition
252
         *            - the content-disposition header. Cannot be <code>null>/code>.
253
         * @return the file name, or <code>null</code> if the content-disposition
254
         *         header does not contain the filename attribute.
255
         */
256
        private String extractFileNameFromContentDisposition(
257
                        String contentDisposition) {
258
                String[] attributes = contentDisposition.split(";");
259

    
260
                for (String a : attributes) {
261
                        if (a.toLowerCase().contains("filename")) {
262
                                // The attribute is the file name. The filename is between
263
                                // quotes.
264
                                return a.substring(a.indexOf('\"') + 1, a.lastIndexOf('\"'));
265
                        }
266
                }
267

    
268
                // not found
269
                return null;
270

    
271
        }
272
        
273
        /**
274
         * {@link URLConnection}'s method getContentLength() does not give
275
         * exact size.
276
         * 
277
         * @return whether the size is acceptable
278
         */
279
        public static boolean acceptableFileSize(File f, int estimated_size) {
280
            
281
            if (f == null) {
282
                return false;
283
            }
284
            
285
            if (estimated_size == -1) {
286
                // -1 means unknown
287
                return true;
288
            }
289
            
290
            if (estimated_size < FILE_SIZE_ESTIMATED_MINIMUM) {
291
                return true;
292
            }
293
            
294
            return f.length() > (FILE_SIZE_ESTIMATED_RATIO * estimated_size);
295
        }
296
}