HttpStringRetriever.java
6.08 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.util.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.io.IOUtils;
/**
* Utility class to fetch the contents of a URL into a string
*
* @author Adam Mummery-Smith
*/
public class HttpStringRetriever extends Thread
{
public static final String LINE_ENDING_LF = "\n";
public static final String LINE_ENDING_CR = "\r";
public static final String LINE_ENDING_CRLF = "\r\n";
/**
* URL to connect to
*/
private final String url;
/**
* Additional headers to pass, if required
*/
private final Map<String, String> headers;
private final String lineEnding;
/**
* Response code returned from the remote server (if any)
*/
private int httpResponseCode = 0;
private final Object resultLock = new Object();
/**
* String retrieved from the remote server
*/
private String string;
/**
* True when this retriever is complete, even if retrieval failed
*/
private volatile boolean done = false;
/**
* True if the fetch operation was a success
*/
private volatile boolean success = false;
/**
* Create a new string retriever for the specified URL, with the supplied
* headers and line end characters.
*
* @param url URL to download from
* @param headers Additional headers to add to the request
* @param lineEnding Line ending to use
*/
public HttpStringRetriever(String url, Map<String, String> headers, String lineEnding)
{
this.url = url;
this.headers = headers;
this.lineEnding = lineEnding;
}
/**
* Create a new string retriever for the specified URL, with the supplied
* headers.
*
* @param url URL to download from
* @param headers Additional headers to add to the request
*/
public HttpStringRetriever(String url, Map<String, String> headers)
{
this(url, headers, HttpStringRetriever.LINE_ENDING_LF);
}
/**
* Create a new string retriever for the specified URL
*
* @param url URL to download from
*/
public HttpStringRetriever(String url)
{
this(url, null);
}
/**
* Create a new string retriever to be used synchronously
*/
public HttpStringRetriever()
{
this(null, null);
}
/**
* Get the string which was retrieved
*/
public String getString()
{
synchronized (this.resultLock)
{
return this.string;
}
}
/**
* True if the download is complete, even if it failed
*/
public boolean isDone()
{
return this.done;
}
/**
* True if the download completed successfully
*/
public boolean getSuccess()
{
return this.success;
}
/**
* Get the response code from the HTTP request, -1 if a connection error
* occurred.
*/
public int getHttpResponseCode()
{
return this.httpResponseCode;
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
@Override
public void run()
{
try
{
String result = this.fetch(new URL(this.url));
synchronized (this.resultLock)
{
this.string = result;
}
}
catch (MalformedURLException ex)
{
ex.printStackTrace();
}
this.done = true;
}
/**
* Fetch a String in the current thread, normally this method is called by
* the run() method to fetch the resource in a new thread but can be called
* directly to fetch the result in the current thread.
*
* @param url URL to fetch
* @return retrieved string or empty string on failure
*/
public String fetch(URL url)
{
StringBuilder readString = new StringBuilder();
HttpURLConnection httpClient = null;
try
{
// Open a HTTP connection to the URL
httpClient = (HttpURLConnection)url.openConnection();
httpClient.setDoInput(true);
httpClient.setUseCaches(false);
httpClient.setRequestMethod("GET");
httpClient.setRequestProperty("Connection", "Close");
httpClient.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/21.0"); // For CloudFlare
if (this.headers != null)
{
for (Entry<String, String> header : this.headers.entrySet())
{
httpClient.addRequestProperty(header.getKey(), header.getValue());
}
}
this.httpResponseCode = httpClient.getResponseCode();
if (this.httpResponseCode >= 200 && this.httpResponseCode < 300)
{
InputStream httpStream = httpClient.getInputStream();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new InputStreamReader(httpStream));
String readLine;
while ((readLine = reader.readLine()) != null)
{
readString.append(readLine).append(this.lineEnding);
}
this.success = true;
}
catch (IOException ex)
{
}
finally
{
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(httpStream);
}
}
}
catch (IOException ex)
{
this.httpResponseCode = -1;
}
finally
{
if (httpClient != null) httpClient.disconnect();
}
return readString.toString();
}
}