Merge pull request #1886 from Accelerite/trafficsentinel

CLOUDSTACK-9728: Fixed traffic sentinel HTTP 414 error response
This commit is contained in:
Rajani Karuturi 2017-04-20 15:44:13 +05:30 committed by GitHub
commit 53b1c5b69e

View File

@ -22,6 +22,7 @@ package com.cloud.network.resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
@ -53,6 +54,7 @@ import com.cloud.agent.api.StartupTrafficMonitorCommand;
import com.cloud.host.Host;
import com.cloud.resource.ServerResource;
import com.cloud.utils.exception.ExecutionException;
import java.net.HttpURLConnection;
public class TrafficSentinelResource implements ServerResource {
@ -205,14 +207,25 @@ public class TrafficSentinelResource implements ServerResource {
_exclZones = cmd.getExcludeZones();
}
BufferedReader in = null;
OutputStream os = null;
try {
//Query traffic Sentinel
trafficSentinel =
new URL(_url + "/inmsf/Query?script=" + URLEncoder.encode(getScript(cmd.getPublicIps(), cmd.getStart(), cmd.getEnd()), "UTF-8") +
"&authenticate=basic&resultFormat=txt");
//Query traffic Sentinel using POST method. 3 parts to the connection call and subsequent writing.
BufferedReader in = new BufferedReader(new InputStreamReader(trafficSentinel.openStream()));
//Part 1 - Connect to the URL of the traffic sentinel's instance.
trafficSentinel = new URL(_url + "/inmsf/Query");
String postData = "script="+URLEncoder.encode(getScript(cmd.getPublicIps(), cmd.getStart(), cmd.getEnd()), "UTF-8")+"&authenticate=basic&resultFormat=txt";
HttpURLConnection con = (HttpURLConnection) trafficSentinel.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Length", String.valueOf(postData.length()));
con.setDoOutput(true);
//Part 2 - Write Data
os = con.getOutputStream();
os.write(postData.getBytes("UTF-8"));
//Part 3 - Read response of the request
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
@ -228,13 +241,19 @@ public class TrafficSentinelResource implements ServerResource {
answer.put(publicIp, bytesSentAndReceived);
}
}
in.close();
} catch (MalformedURLException e1) {
s_logger.info("Invalid Traffic Sentinel URL", e1);
throw new ExecutionException(e1.getMessage());
} catch (IOException e) {
s_logger.debug("Error in direct network usage accounting", e);
throw new ExecutionException(e.getMessage());
} finally {
if (os != null) {
os.close();
}
if (in != null) {
in.close();
}
}
} catch (Exception e) {
s_logger.debug(e);