2018-05-10 19:21:15 +02:00
|
|
|
package org.gcube.data.analysis.nlphub.legacy;
|
2018-03-15 15:40:50 +01:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.util.ArrayList;
|
2018-05-11 19:03:43 +02:00
|
|
|
|
|
|
|
|
2018-05-10 19:21:15 +02:00
|
|
|
import org.gcube.data.analysis.nlphub.nlp.NlpParameter;
|
2018-05-11 19:03:43 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-03-15 15:40:50 +01:00
|
|
|
|
|
|
|
public class AsyncHttpRequest extends Thread {
|
|
|
|
private String baseUrl, finalUrl, result, method;
|
|
|
|
private ArrayList<NlpParameter> parameters;
|
2018-05-11 19:03:43 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AsyncHttpRequest.class);
|
|
|
|
|
2018-03-15 15:40:50 +01:00
|
|
|
protected long elapsedTime;
|
|
|
|
|
|
|
|
public AsyncHttpRequest() {
|
|
|
|
this.baseUrl = null;
|
|
|
|
this.parameters = null;
|
|
|
|
this.method = "GET";
|
|
|
|
finalUrl = null;
|
2018-05-11 19:03:43 +02:00
|
|
|
elapsedTime = 0;
|
2018-03-15 15:40:50 +01:00
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-15 15:40:50 +01:00
|
|
|
public AsyncHttpRequest(String baseUrl, String method, ArrayList<NlpParameter> parameters) {
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
this.parameters = parameters;
|
2018-05-11 19:03:43 +02:00
|
|
|
if (method == null)
|
2018-03-15 15:40:50 +01:00
|
|
|
this.method = "GET";
|
|
|
|
else
|
|
|
|
this.method = (method.equalsIgnoreCase("GET") || method.equalsIgnoreCase("POST")) ? method : "GET";
|
|
|
|
setFinalUrl();
|
|
|
|
elapsedTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
elapsedTime = System.currentTimeMillis();
|
2018-05-11 19:03:43 +02:00
|
|
|
if (finalUrl == null)
|
2018-03-15 15:40:50 +01:00
|
|
|
finalUrl = baseUrl;
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
BufferedReader r = null;
|
|
|
|
try {
|
|
|
|
URL url = new URL(finalUrl);
|
|
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
connection.setUseCaches(false);
|
|
|
|
connection.setRequestMethod(method);
|
|
|
|
r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
String line = "";
|
|
|
|
result = "";
|
|
|
|
|
|
|
|
while (line != null) {
|
|
|
|
line = r.readLine();
|
|
|
|
if (line != null)
|
|
|
|
result += line.trim();
|
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-15 15:40:50 +01:00
|
|
|
asyncHttpRequestCallback();
|
|
|
|
|
2018-05-11 19:03:43 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getLocalizedMessage(),e);
|
2018-03-20 14:45:49 +01:00
|
|
|
asyncHttpRequestCallback();
|
2018-03-15 15:40:50 +01:00
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (r != null)
|
|
|
|
r.close();
|
|
|
|
if (connection != null)
|
|
|
|
connection.disconnect();
|
|
|
|
} catch (Exception e) {
|
2018-05-11 19:03:43 +02:00
|
|
|
logger.error(e.getLocalizedMessage(),e);
|
2018-03-15 15:40:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-15 15:40:50 +01:00
|
|
|
public String getResult() {
|
|
|
|
return result;
|
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-15 15:40:50 +01:00
|
|
|
public void asyncHttpRequestCallback() {
|
|
|
|
elapsedTime = System.currentTimeMillis() - elapsedTime;
|
|
|
|
}
|
2018-05-11 19:03:43 +02:00
|
|
|
|
2018-03-15 15:40:50 +01:00
|
|
|
public void setBaseUrl(String baseUrl) {
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMethod(String method) {
|
|
|
|
this.method = method;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setParameters(ArrayList<NlpParameter> parameters) {
|
|
|
|
this.parameters = parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setFinalUrl() {
|
|
|
|
finalUrl = baseUrl;
|
|
|
|
if (parameters != null) {
|
|
|
|
if (finalUrl.indexOf("?") < 0)
|
|
|
|
finalUrl += "?";
|
|
|
|
for (NlpParameter p : parameters) {
|
|
|
|
try {
|
|
|
|
finalUrl += p.getName() + "=" + URLEncoder.encode((String) p.getValue(), "UTF-8");
|
|
|
|
finalUrl += "&";
|
2018-05-11 19:03:43 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getLocalizedMessage(),e);
|
2018-03-15 15:40:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
finalUrl = finalUrl.substring(0, finalUrl.length() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|