blob: bf185bd2b3512b1b23201e9763869e51466ca304 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
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;
}
}
|