package org.openslx.taskmanager.tasks; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; import org.openslx.satserver.util.ProxyHandler; 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 = 1024 * 1024; @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; // Before open connection, handle proxy settings. ProxyHandler.configProxy(); 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; } final ByteArrayOutputStream baos = new ByteArrayOutputStream( Math.max( 512, status.size ) ); final byte data[] = new byte[ 9000 ]; int count; while ( ( count = in.read( data, 0, data.length ) ) != -1 ) { baos.write( data, 0, count ); status.complete += count; if ( status.complete > MAX_SIZE ) { status.error = "Remote file too large: > " + status.complete + " bytes!"; return false; } } status.content = new String( baos.toByteArray(), StandardCharsets.UTF_8 ); 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; } }