2008-02-13 20:08:06 +00:00
|
|
|
|
2008-03-23 22:41:25 +00:00
|
|
|
package net.sourceforge.tuned;
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.channels.Channels;
|
2008-11-19 16:22:31 +00:00
|
|
|
import java.nio.channels.ReadableByteChannel;
|
2008-02-13 20:08:06 +00:00
|
|
|
import java.nio.channels.WritableByteChannel;
|
|
|
|
import java.nio.charset.Charset;
|
2008-06-02 19:12:28 +00:00
|
|
|
import java.util.HashMap;
|
2008-11-19 16:22:31 +00:00
|
|
|
import java.util.List;
|
2008-02-13 20:08:06 +00:00
|
|
|
import java.util.Map;
|
2008-07-30 22:37:01 +00:00
|
|
|
import java.util.Map.Entry;
|
2008-02-13 20:08:06 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
import javax.swing.SwingWorker;
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
public class DownloadTask extends SwingWorker<ByteBuffer, Void> {
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
public static final String DOWNLOAD_STATE = "download state";
|
2008-05-03 20:43:15 +00:00
|
|
|
public static final String DOWNLOAD_PROGRESS = "download progress";
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
public static enum DownloadState {
|
2008-03-19 22:14:38 +00:00
|
|
|
PENDING,
|
|
|
|
CONNECTING,
|
|
|
|
DOWNLOADING,
|
2008-06-02 19:12:28 +00:00
|
|
|
DONE
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private URL url;
|
|
|
|
|
2008-11-22 15:30:33 +00:00
|
|
|
private long contentLength = -1;
|
2008-02-13 20:08:06 +00:00
|
|
|
private DownloadState state = DownloadState.PENDING;
|
|
|
|
|
2008-11-19 16:22:31 +00:00
|
|
|
private Map<String, String> postParameters;
|
|
|
|
private Map<String, String> requestHeaders;
|
|
|
|
private Map<String, List<String>> responseHeaders;
|
2008-06-02 19:12:28 +00:00
|
|
|
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
public DownloadTask(URL url) {
|
|
|
|
this.url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
protected HttpURLConnection createConnection() throws Exception {
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
2008-11-19 16:22:31 +00:00
|
|
|
if (requestHeaders != null) {
|
|
|
|
for (Entry<String, String> entry : requestHeaders.entrySet()) {
|
|
|
|
connection.addRequestProperty(entry.getKey(), entry.getValue());
|
|
|
|
}
|
2008-06-02 19:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return connection;
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ByteBuffer doInBackground() throws Exception {
|
|
|
|
setDownloadState(DownloadState.CONNECTING);
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
HttpURLConnection connection = createConnection();
|
2008-02-13 20:08:06 +00:00
|
|
|
|
2008-11-19 16:22:31 +00:00
|
|
|
if (postParameters != null) {
|
2008-11-22 15:30:33 +00:00
|
|
|
ByteBuffer postData = encodeParameters(postParameters);
|
|
|
|
|
|
|
|
// 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 20:08:06 +00:00
|
|
|
connection.setRequestMethod("POST");
|
|
|
|
connection.setDoOutput(true);
|
2008-11-22 15:30:33 +00:00
|
|
|
|
|
|
|
// write post data
|
2008-02-13 20:08:06 +00:00
|
|
|
WritableByteChannel out = Channels.newChannel(connection.getOutputStream());
|
2008-11-22 15:30:33 +00:00
|
|
|
out.write(postData);
|
2008-02-13 20:08:06 +00:00
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
2008-11-22 15:30:33 +00:00
|
|
|
contentLength = connection.getContentLength();
|
2008-02-13 20:08:06 +00:00
|
|
|
|
2008-11-19 16:22:31 +00:00
|
|
|
responseHeaders = connection.getHeaderFields();
|
2008-06-02 19:12:28 +00:00
|
|
|
|
2008-02-13 20:08:06 +00:00
|
|
|
setDownloadState(DownloadState.DOWNLOADING);
|
|
|
|
|
2008-11-19 16:22:31 +00:00
|
|
|
ReadableByteChannel in = Channels.newChannel(connection.getInputStream());
|
2008-11-22 15:30:33 +00:00
|
|
|
ByteBufferOutputStream buffer = new ByteBufferOutputStream((int) (contentLength > 0 ? contentLength : 32 * 1024));
|
2008-02-13 20:08:06 +00:00
|
|
|
|
|
|
|
try {
|
2008-11-22 15:30:33 +00:00
|
|
|
while (!isCancelled() && ((buffer.transferFrom(in)) >= 0)) {
|
2008-06-02 19:12:28 +00:00
|
|
|
|
2008-11-22 15:30:33 +00:00
|
|
|
firePropertyChange(DOWNLOAD_PROGRESS, null, buffer.position());
|
2008-06-02 19:12:28 +00:00
|
|
|
|
2008-11-22 15:30:33 +00:00
|
|
|
if (isContentLengthKnown()) {
|
|
|
|
setProgress((int) ((buffer.position() * 100) / contentLength));
|
2008-06-02 19:12:28 +00:00
|
|
|
}
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2008-11-22 15:30:33 +00: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 20:08:06 +00:00
|
|
|
throw e;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
in.close();
|
2008-11-19 16:22:31 +00:00
|
|
|
|
|
|
|
// download either finished or an exception is thrown
|
|
|
|
setDownloadState(DownloadState.DONE);
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
2008-11-19 16:22:31 +00:00
|
|
|
return buffer.getByteBuffer();
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
protected void setDownloadState(DownloadState state) {
|
2008-02-13 20:08:06 +00:00
|
|
|
this.state = state;
|
2008-06-02 19:12:28 +00:00
|
|
|
firePropertyChange(DOWNLOAD_STATE, null, state);
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DownloadState getDownloadState() {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
public URL getUrl() {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-22 15:30:33 +00:00
|
|
|
public boolean isContentLengthKnown() {
|
|
|
|
return contentLength >= 0;
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-22 15:30:33 +00:00
|
|
|
public long getContentLength() {
|
|
|
|
return contentLength;
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-19 16:22:31 +00:00
|
|
|
public void setRequestHeaders(Map<String, String> requestHeaders) {
|
|
|
|
this.requestHeaders = new HashMap<String, String>(requestHeaders);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setPostParameters(Map<String, String> postParameters) {
|
|
|
|
this.postParameters = new HashMap<String, String>(postParameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String, List<String>> getResponseHeaders() {
|
|
|
|
return responseHeaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String, String> getPostParameters() {
|
|
|
|
return postParameters;
|
2008-06-02 19:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-19 16:22:31 +00:00
|
|
|
public Map<String, String> getRequestHeaders() {
|
|
|
|
return requestHeaders;
|
2008-06-02 19:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-22 15:30:33 +00:00
|
|
|
protected ByteBuffer encodeParameters(Map<String, String> parameters) {
|
2008-03-19 22:14:38 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2008-02-13 20:08:06 +00:00
|
|
|
|
2008-07-30 22:37:01 +00:00
|
|
|
for (Entry<String, String> entry : parameters.entrySet()) {
|
2008-11-22 15:30:33 +00:00
|
|
|
if (sb.length() > 0)
|
2008-02-13 20:08:06 +00:00
|
|
|
sb.append("&");
|
|
|
|
|
2008-07-30 22:37:01 +00:00
|
|
|
sb.append(entry.getKey());
|
2008-02-13 20:08:06 +00:00
|
|
|
sb.append("=");
|
|
|
|
|
|
|
|
try {
|
2008-07-30 22:37:01 +00:00
|
|
|
sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
|
2008-02-13 20:08:06 +00:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
// will never happen
|
2009-03-14 10:20:59 +00:00
|
|
|
Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.toString(), e);
|
2008-02-13 20:08:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Charset.forName("UTF-8").encode(sb.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|