2008-02-13 15:08:06 -05:00
|
|
|
|
2008-03-23 18:41:25 -04:00
|
|
|
package net.sourceforge.tuned;
|
2008-02-13 15:08:06 -05:00
|
|
|
|
|
|
|
|
2011-11-04 05:11:11 -04:00
|
|
|
import static net.sourceforge.filebot.web.WebRequest.*;
|
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.channels.Channels;
|
2008-11-19 11:22:31 -05:00
|
|
|
import java.nio.channels.ReadableByteChannel;
|
2008-02-13 15:08:06 -05:00
|
|
|
import java.nio.channels.WritableByteChannel;
|
|
|
|
import java.nio.charset.Charset;
|
2008-06-02 15:12:28 -04:00
|
|
|
import java.util.HashMap;
|
2008-11-19 11:22:31 -05:00
|
|
|
import java.util.List;
|
2008-02-13 15:08:06 -05:00
|
|
|
import java.util.Map;
|
2008-07-30 18:37:01 -04:00
|
|
|
import java.util.Map.Entry;
|
2008-02-13 15:08:06 -05:00
|
|
|
|
|
|
|
import javax.swing.SwingWorker;
|
|
|
|
|
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
public class DownloadTask extends SwingWorker<ByteBuffer, Void> {
|
2008-02-13 15:08:06 -05:00
|
|
|
|
|
|
|
public static final String DOWNLOAD_STATE = "download state";
|
2008-05-03 16:43:15 -04:00
|
|
|
public static final String DOWNLOAD_PROGRESS = "download progress";
|
2008-02-13 15:08:06 -05:00
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
public static enum DownloadState {
|
2012-12-02 04:36:59 -05:00
|
|
|
PENDING, CONNECTING, DOWNLOADING, DONE
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private URL url;
|
|
|
|
|
2008-11-22 10:30:33 -05:00
|
|
|
private long contentLength = -1;
|
2008-02-13 15:08:06 -05:00
|
|
|
private DownloadState state = DownloadState.PENDING;
|
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
private Map<String, String> postParameters;
|
|
|
|
private Map<String, String> requestHeaders;
|
|
|
|
private Map<String, List<String>> responseHeaders;
|
2008-06-02 15:12:28 -04:00
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
public DownloadTask(URL url) {
|
|
|
|
this.url = url;
|
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
protected HttpURLConnection createConnection() throws Exception {
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
if (requestHeaders != null) {
|
|
|
|
for (Entry<String, String> entry : requestHeaders.entrySet()) {
|
|
|
|
connection.addRequestProperty(entry.getKey(), entry.getValue());
|
|
|
|
}
|
2008-06-02 15:12:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return connection;
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
@Override
|
|
|
|
protected ByteBuffer doInBackground() throws Exception {
|
|
|
|
setDownloadState(DownloadState.CONNECTING);
|
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
HttpURLConnection connection = createConnection();
|
2008-02-13 15:08:06 -05:00
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
if (postParameters != null) {
|
2012-12-02 04:36:59 -05:00
|
|
|
ByteBuffer postData = Charset.forName("UTF-8").encode(encodeParameters(postParameters, true));
|
2008-11-22 10:30:33 -05:00
|
|
|
|
|
|
|
// add content type and content length headers
|
|
|
|
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
connection.addRequestProperty("Content-Length", String.valueOf(postData.remaining()));
|
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
connection.setDoOutput(true);
|
2008-11-22 10:30:33 -05:00
|
|
|
|
|
|
|
// write post data
|
2008-02-13 15:08:06 -05:00
|
|
|
WritableByteChannel out = Channels.newChannel(connection.getOutputStream());
|
2008-11-22 10:30:33 -05:00
|
|
|
out.write(postData);
|
2008-02-13 15:08:06 -05:00
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
2008-11-22 10:30:33 -05:00
|
|
|
contentLength = connection.getContentLength();
|
2008-02-13 15:08:06 -05:00
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
responseHeaders = connection.getHeaderFields();
|
2008-06-02 15:12:28 -04:00
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
setDownloadState(DownloadState.DOWNLOADING);
|
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
ReadableByteChannel in = Channels.newChannel(connection.getInputStream());
|
2008-11-22 10:30:33 -05:00
|
|
|
ByteBufferOutputStream buffer = new ByteBufferOutputStream((int) (contentLength > 0 ? contentLength : 32 * 1024));
|
2008-02-13 15:08:06 -05:00
|
|
|
|
|
|
|
try {
|
2008-11-22 10:30:33 -05:00
|
|
|
while (!isCancelled() && ((buffer.transferFrom(in)) >= 0)) {
|
2008-06-02 15:12:28 -04:00
|
|
|
|
2008-11-22 10:30:33 -05:00
|
|
|
firePropertyChange(DOWNLOAD_PROGRESS, null, buffer.position());
|
2008-06-02 15:12:28 -04:00
|
|
|
|
2008-11-22 10:30:33 -05:00
|
|
|
if (isContentLengthKnown()) {
|
|
|
|
setProgress((int) ((buffer.position() * 100) / contentLength));
|
2008-06-02 15:12:28 -04:00
|
|
|
}
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2008-11-22 10:30:33 -05:00
|
|
|
// if the content length is not known in advance an IOException (Premature EOF)
|
|
|
|
// is always thrown after all the data has been read
|
|
|
|
if (isContentLengthKnown())
|
2008-02-13 15:08:06 -05:00
|
|
|
throw e;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
in.close();
|
2008-11-19 11:22:31 -05:00
|
|
|
|
|
|
|
// download either finished or an exception is thrown
|
|
|
|
setDownloadState(DownloadState.DONE);
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
return buffer.getByteBuffer();
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
protected void setDownloadState(DownloadState state) {
|
2008-02-13 15:08:06 -05:00
|
|
|
this.state = state;
|
2008-06-02 15:12:28 -04:00
|
|
|
firePropertyChange(DOWNLOAD_STATE, null, state);
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
public DownloadState getDownloadState() {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-06-02 15:12:28 -04:00
|
|
|
public URL getUrl() {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-11-22 10:30:33 -05:00
|
|
|
public boolean isContentLengthKnown() {
|
|
|
|
return contentLength >= 0;
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-11-22 10:30:33 -05:00
|
|
|
public long getContentLength() {
|
|
|
|
return contentLength;
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
public void setRequestHeaders(Map<String, String> requestHeaders) {
|
|
|
|
this.requestHeaders = new HashMap<String, String>(requestHeaders);
|
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
public void setPostParameters(Map<String, String> postParameters) {
|
|
|
|
this.postParameters = new HashMap<String, String>(postParameters);
|
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
public Map<String, List<String>> getResponseHeaders() {
|
|
|
|
return responseHeaders;
|
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
public Map<String, String> getPostParameters() {
|
|
|
|
return postParameters;
|
2008-06-02 15:12:28 -04:00
|
|
|
}
|
|
|
|
|
2012-12-02 04:36:59 -05:00
|
|
|
|
2008-11-19 11:22:31 -05:00
|
|
|
public Map<String, String> getRequestHeaders() {
|
|
|
|
return requestHeaders;
|
2008-06-02 15:12:28 -04:00
|
|
|
}
|
|
|
|
|
2008-02-13 15:08:06 -05:00
|
|
|
}
|