summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java
blob: bf185bd2b3512b1b23201e9763869e51466ca304 (plain) (tree)
1
2
3
4
5
6


                                      
                                     

                                  
















                                                
                                                         











                                                       





















                                                                                                




                                                
 







                                                                                                   
 
                                                                                                                     


                                                                                     
                                                             

                                                                   
                                                                                                                 


                                                     
                                                                                        
                                    

                                                                                                                

                                     
                                                     
















                                                                
package org.openslx.taskmanager.tasks;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
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 = 1024 * 1024;

	@Override
	protected boolean initTask()
	{
		this.setStatusObject( status );
		if ( this.url == null ) {
			status.error = "No URL given.";
			return false;
		}
		return true;
	}

	private String getError( URLConnection connection, boolean close )
	{
		if ( ! ( connection instanceof HttpURLConnection ) )
			return null;

		StringBuilder sb = new StringBuilder();
		InputStream es = ( (HttpURLConnection)connection ).getErrorStream();
		final byte data[] = new byte[ 9000 ];
		try {
			while ( es.read( data, 0, data.length ) != -1 ) {
				if ( sb.length() < 1024 ) {
					sb.append( new String( data, StandardCharsets.UTF_8 ) );
				}
			}
		} catch ( Exception e ) {
		}
		if ( close ) {
			Util.multiClose( es );
		}
		return sb.toString();
	}

	@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;
			}

			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 = baos.toString( StandardCharsets.UTF_8.name() );
			return true;
		} catch ( Exception e ) {
			status.error = "Download error: " + e.toString() + "\n" + getError( connection, false );
			return false;
		} finally {
			getError( connection, true );
			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;
	}

}