svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / taskplanning / retrieving / URLRetrieveTask.java @ 40769
History | View | Annotate | Download (9.34 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
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 3
|
||
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 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.remoteclient.taskplanning.retrieving; |
25 | |||
26 | import java.io.BufferedOutputStream; |
||
27 | import java.io.DataOutputStream; |
||
28 | import java.io.File; |
||
29 | import java.io.FileOutputStream; |
||
30 | import java.io.IOException; |
||
31 | import java.io.InputStream; |
||
32 | import java.net.MalformedURLException; |
||
33 | import java.net.URL; |
||
34 | import java.util.Iterator; |
||
35 | import java.util.Vector; |
||
36 | |||
37 | import org.gvsig.remoteclient.taskplanning.IRunnableTask; |
||
38 | |||
39 | /**
|
||
40 | * Clase para bajar ficheros en un thread independiente.
|
||
41 | * La idea (y parte del c?digo) est? tomada de EarthFlicks
|
||
42 | * @author Luis W. Sevilla (sevilla_lui@gva.es)
|
||
43 | * @see http://today.java.net/lpt/a/212
|
||
44 | */
|
||
45 | public class URLRetrieveTask implements IRunnableTask { |
||
46 | |||
47 | private boolean running, cancelled; |
||
48 | private URLRequest request;
|
||
49 | private Vector listeners = new Vector(); |
||
50 | private RetrieveEvent event = new RetrieveEvent(); |
||
51 | private InputStream is; |
||
52 | |||
53 | /**
|
||
54 | *
|
||
55 | */
|
||
56 | public URLRetrieveTask(URLRequest request, RetrieveListener listener) {
|
||
57 | this.request = request;
|
||
58 | addRetrieveListener(listener); |
||
59 | running = cancelled = false;
|
||
60 | } |
||
61 | |||
62 | |||
63 | public void execute() { |
||
64 | event.setType(RetrieveEvent.NOT_STARTED); |
||
65 | fireEvent(); |
||
66 | cancelled = false;
|
||
67 | running= true;
|
||
68 | |||
69 | long t = System.currentTimeMillis(); |
||
70 | File f = new File(request.getFileName()+System.currentTimeMillis()); |
||
71 | while (f.exists()) {
|
||
72 | t++; |
||
73 | f = new File(request.getFileName()+t); |
||
74 | } |
||
75 | URL url;
|
||
76 | try {
|
||
77 | event.setType(RetrieveEvent.CONNECTING); |
||
78 | fireEvent(); |
||
79 | url = request.getUrl(); |
||
80 | request.setFileName(f.getAbsolutePath()); |
||
81 | System.out.println("downloading '"+url+"' to: "+f.getAbsolutePath()); |
||
82 | |||
83 | DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f))); |
||
84 | byte[] buffer = new byte[1024*256]; |
||
85 | fireEvent(); |
||
86 | is = url.openStream(); |
||
87 | event.setType(RetrieveEvent.TRANSFERRING); |
||
88 | fireEvent(); |
||
89 | if (!cancelled) {
|
||
90 | long readed = 0; |
||
91 | for (int i = is.read(buffer); !cancelled && i>0; i = is.read(buffer)){ |
||
92 | dos.write(buffer, 0, i);
|
||
93 | readed += i; |
||
94 | } |
||
95 | |||
96 | dos.close(); |
||
97 | } |
||
98 | |||
99 | if (cancelled) {
|
||
100 | // Bad incomplete file, delete it.
|
||
101 | System.out.println("download cancelled ("+url+")"); |
||
102 | f.delete(); |
||
103 | } else {
|
||
104 | // register job in the cache
|
||
105 | // TODO aix? deuria d'estar al principi per a poder capturar
|
||
106 | // treballs que s'estan fent per? que encara no s'han acabat
|
||
107 | synchronized (this) { |
||
108 | RequestManager.getInstance().addDownloadedURLRequest(request, f.getAbsolutePath()); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | running = false;
|
||
113 | if (cancelled)
|
||
114 | event.setType(RetrieveEvent.REQUEST_CANCELLED); |
||
115 | else
|
||
116 | event.setType(RetrieveEvent.REQUEST_FINISHED); |
||
117 | |||
118 | } catch (MalformedURLException e) { |
||
119 | e.printStackTrace(); |
||
120 | event.setType(RetrieveEvent.REQUEST_FAILED); |
||
121 | } catch (IOException e) { |
||
122 | e.printStackTrace(); |
||
123 | event.setType(RetrieveEvent.REQUEST_FAILED); |
||
124 | } |
||
125 | fireEvent(); |
||
126 | } |
||
127 | |||
128 | private void fireEvent() { |
||
129 | Iterator it = listeners.iterator();
|
||
130 | while (it.hasNext()) {
|
||
131 | RetrieveListener listener = (RetrieveListener) it.next(); |
||
132 | listener.transferEventReceived(event); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | public void addRetrieveListener(RetrieveListener l) { |
||
137 | if (l!=null) |
||
138 | listeners.add(l); |
||
139 | } |
||
140 | |||
141 | public void cancel() { |
||
142 | cancelled = true;
|
||
143 | try {
|
||
144 | if (is != null) { |
||
145 | is.close(); |
||
146 | is = null;
|
||
147 | } |
||
148 | } catch (IOException e) { |
||
149 | e.printStackTrace(); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | public boolean isRunning() { |
||
154 | return running && !cancelled;
|
||
155 | } |
||
156 | |||
157 | |||
158 | public URLRequest getRequest() {
|
||
159 | return request;
|
||
160 | } |
||
161 | |||
162 | |||
163 | public Vector getListeners() { |
||
164 | return listeners;
|
||
165 | } |
||
166 | |||
167 | |||
168 | public long getTaskTimeout() { |
||
169 | return 30*1000; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | |||
174 | //static private String makeSuffix(String contentType) {
|
||
175 | // contentType = contentType.toLowerCase();
|
||
176 | // if (contentType.indexOf("png") >= 0)
|
||
177 | // return ".png";
|
||
178 | //
|
||
179 | // if (contentType.indexOf("xml") >= 0)
|
||
180 | // return ".xml";
|
||
181 | //
|
||
182 | // if (contentType.indexOf("gif") >= 0)
|
||
183 | // return ".gif";
|
||
184 | //
|
||
185 | // if (contentType.indexOf("tif") >= 0)
|
||
186 | // return ".tif";
|
||
187 | //
|
||
188 | // if (contentType.indexOf("jpg") >= 0
|
||
189 | // || contentType.indexOf("jpeg") >= 0)
|
||
190 | // return ".jpg";
|
||
191 | //
|
||
192 | // if (contentType.indexOf("html") >= 0)
|
||
193 | // return ".html";
|
||
194 | //
|
||
195 | // return "";
|
||
196 | //}
|
||
197 | ///**
|
||
198 | // *
|
||
199 | // */
|
||
200 | //private void end() {
|
||
201 | //}
|
||
202 | //
|
||
203 | ///**
|
||
204 | // *
|
||
205 | // */
|
||
206 | //private void inThreadEnd() {
|
||
207 | //}
|
||
208 | //
|
||
209 | ///**
|
||
210 | // *
|
||
211 | // */
|
||
212 | //private void update() {
|
||
213 | //}
|
||
214 | //
|
||
215 | ///**
|
||
216 | // *
|
||
217 | // */
|
||
218 | //private void begin() {
|
||
219 | //}
|
||
220 | |||
221 | //public void start() {
|
||
222 | //// this.reset();
|
||
223 | //this.thread = new Thread(this);
|
||
224 | //this.thread.start();
|
||
225 | //}
|
||
226 | |||
227 | /* (non-Javadoc)
|
||
228 | * @see java.lang.Runnable#run()
|
||
229 | * /
|
||
230 | public void run() {
|
||
231 | try {
|
||
232 | URL url = request.getUrl();
|
||
233 | this.status = Status.CONNECTING;
|
||
234 | this.begin();
|
||
235 | |||
236 | {
|
||
237 | // Retrieve the contents.
|
||
238 | int numBytesRead = 0;
|
||
239 | // System.out.println("File being retrieved. " + this.getUrl().toString());
|
||
240 | java.net.URLConnection connection = url.openConnection();
|
||
241 | connection.setAllowUserInteraction(true);
|
||
242 | java.io.InputStream incoming = connection.getInputStream();
|
||
243 | this.contentLength = connection.getContentLength();
|
||
244 | this.contentType = connection.getContentType();
|
||
245 | |||
246 | java.io.File destFile = java.io.File.createTempFile("Cq_", makeSuffix(this.contentType));
|
||
247 | destFile.deleteOnExit(); // It's copied later if it's to be persisted.
|
||
248 | this.destination = destFile.getPath();
|
||
249 | java.io.FileOutputStream outgoing = new java.io.FileOutputStream(destFile);
|
||
250 | |||
251 | this.status = Status.RETRIEVING;
|
||
252 | |||
253 | byte[] buffer = new byte[4096];
|
||
254 | |||
255 | try {
|
||
256 | while (!Thread.currentThread().isInterrupted() && numBytesRead >= 0) {
|
||
257 | numBytesRead = incoming.read(buffer);
|
||
258 | if (numBytesRead > 0) {
|
||
259 | this.bytesRead += numBytesRead;
|
||
260 | outgoing.write(buffer, 0, numBytesRead);
|
||
261 | this.update();
|
||
262 | }
|
||
263 | }
|
||
264 | } finally {
|
||
265 | if (incoming != null)
|
||
266 | try {incoming.close();} catch (java.io.IOException e) {}; // TODO: log it maybe
|
||
267 | if (outgoing != null)
|
||
268 | try {outgoing.close();} catch (java.io.IOException e) {}; // TODO: log it maybe
|
||
269 | }
|
||
270 | this.status = Status.POSTPROCESSING;
|
||
271 | }
|
||
272 | } catch (Exception e) {
|
||
273 | this.status = Status.ERROR;
|
||
274 | this.error = e;
|
||
275 | e.printStackTrace();
|
||
276 | } finally {
|
||
277 | if (Thread.currentThread().isInterrupted())
|
||
278 | this.status = Status.INTERRUPTED;
|
||
279 | this.update();
|
||
280 | this.inThreadEnd();
|
||
281 | if (this.status == Status.POSTPROCESSING)
|
||
282 | this.status = Status.DONE;
|
||
283 | this.end();
|
||
284 | }
|
||
285 | }*/
|
||
286 | |||
287 | //File tempDirectory = new File(tempDirectoryPath);
|
||
288 | //if (!tempDirectory.exists())
|
||
289 | //tempDirectory.mkdir();
|
||
290 | //
|
||
291 | //f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis());
|
||
292 | //if (cancelled)
|
||
293 | //throw new TaskCancelledException();
|
||
294 | //addDownloadedURL(url, f.getAbsolutePath());
|
||
295 | //try {
|
||
296 | //URL url = request.getUrl();
|
||
297 | //System.out.println(url);
|
||
298 | //this.status = Status.CONNECTING;
|
||
299 | //this.begin();
|
||
300 | //
|
||
301 | //{
|
||
302 | //// Retrieve the contents.
|
||
303 | //int numBytesRead = 0;
|
||
304 | ////System.out.println("File being retrieved. " + this.getUrl().toString());
|
||
305 | //java.net.URLConnection connection = url.openConnection();
|
||
306 | //connection.setAllowUserInteraction(true);
|
||
307 | //java.io.InputStream incoming = connection.getInputStream();
|
||
308 | //this.contentLength = connection.getContentLength();
|
||
309 | //this.contentType = connection.getContentType();
|
||
310 | //
|
||
311 | //java.io.File destFile = java.io.File.createTempFile("Cq_", makeSuffix(this.contentType));
|
||
312 | //System.out.println(destFile.getAbsolutePath());
|
||
313 | ////destFile.deleteOnExit(); // It's copied later if it's to be persisted.
|
||
314 | //this.destination = destFile.getPath();
|
||
315 | //java.io.FileOutputStream outgoing = new java.io.FileOutputStream(destFile);
|
||
316 | //
|
||
317 | //this.status = Status.RETRIEVING;
|
||
318 | //
|
||
319 | //byte[] buffer = new byte[4096];
|
||
320 | //
|
||
321 | //try {
|
||
322 | //while (!Thread.currentThread().isInterrupted() && numBytesRead >= 0) {
|
||
323 | //numBytesRead = incoming.read(buffer);
|
||
324 | //if (numBytesRead > 0) {
|
||
325 | //this.bytesRead += numBytesRead;
|
||
326 | //outgoing.write(buffer, 0, numBytesRead);
|
||
327 | //this.update();
|
||
328 | //}
|
||
329 | //}
|
||
330 | //} finally {
|
||
331 | //if (incoming != null)
|
||
332 | //try {incoming.close();} catch (java.io.IOException e) {}; // TODO: log it maybe
|
||
333 | //if (outgoing != null)
|
||
334 | //try {outgoing.close();} catch (java.io.IOException e) {}; // TODO: log it maybe
|
||
335 | //}
|
||
336 | //this.status = Status.POSTPROCESSING;
|
||
337 | //}
|
||
338 | //} catch (Exception e) {
|
||
339 | //this.status = Status.ERROR;
|
||
340 | //this.error = e;
|
||
341 | //e.printStackTrace();
|
||
342 | //} finally {
|
||
343 | //if (Thread.currentThread().isInterrupted())
|
||
344 | //this.status = Status.INTERRUPTED;
|
||
345 | //this.update();
|
||
346 | //this.inThreadEnd();
|
||
347 | //if (this.status == Status.POSTPROCESSING)
|
||
348 | //this.status = Status.DONE;
|
||
349 | //this.end();
|
||
350 | //}
|