summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java
diff options
context:
space:
mode:
authorSimon Rettberg2014-06-03 16:47:36 +0200
committerSimon Rettberg2014-06-03 16:47:36 +0200
commit32dc5354e2916387a2c62eadae0a4568023f1151 (patch)
tree7fd9a0173d6073e86d1d48e545646b1bc8c1a5eb /src/main/java/org/openslx/taskmanager/tasks/DownloadText.java
downloadtmlite-bwlp-32dc5354e2916387a2c62eadae0a4568023f1151.tar.gz
tmlite-bwlp-32dc5354e2916387a2c62eadae0a4568023f1151.tar.xz
tmlite-bwlp-32dc5354e2916387a2c62eadae0a4568023f1151.zip
Initial commit
Diffstat (limited to 'src/main/java/org/openslx/taskmanager/tasks/DownloadText.java')
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/DownloadText.java82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java b/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java
new file mode 100644
index 0000000..11b30cf
--- /dev/null
+++ b/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java
@@ -0,0 +1,82 @@
+package org.openslx.taskmanager.tasks;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
+
+import org.openslx.satserver.util.Util;
+import org.openslx.taskmanager.api.AbstractTask;
+
+import com.google.gson.annotations.Expose;
+
+public class DownloadText extends AbstractTask
+{
+
+ @Expose
+ private String url = null;
+
+ private Output status = new Output();
+
+ private static final long MAX_SIZE = 10000;
+
+ @Override
+ protected boolean initTask()
+ {
+ this.setStatusObject( status );
+ if ( this.url == null ) {
+ status.error = "No URL given.";
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ protected boolean execute()
+ {
+ URLConnection connection = null;
+ BufferedInputStream in = null;
+ try {
+ connection = new URL( this.url ).openConnection();
+ in = new BufferedInputStream( connection.getInputStream() );
+ status.size = connection.getContentLength();
+ if ( status.size > MAX_SIZE ) {
+ status.error = "Remote file too large: " + status.size + " bytes!";
+ return false;
+ }
+ StringBuilder sb = new StringBuilder( Math.max( 8, status.size ) );
+
+ final byte data[] = new byte[ 9000 ];
+ int count;
+ while ( ( count = in.read( data, 0, data.length ) ) != -1 ) {
+ sb.append( new String( data, 0, count, StandardCharsets.UTF_8 ) );
+ status.complete += count;
+ if ( status.complete > MAX_SIZE ) {
+ status.error = "Remote file too large: > " + status.size + " bytes!";
+ return false;
+ }
+ }
+ status.content = sb.toString();
+ return true;
+ } catch ( IOException e ) {
+ status.error = "Download error: " + e.toString();
+ return false;
+ } finally {
+ Util.multiClose( in );
+ }
+ }
+
+ /**
+ * Output - contains additional status data of this task
+ */
+ @SuppressWarnings( "unused" )
+ private static class Output
+ {
+ protected String error = null;
+ protected String content = null;
+ protected int size = -1;
+ protected int complete = 0;
+ }
+
+}