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