Skip to content

Commit 70422e7

Browse files
committed
CLOUDSTACK-9728: Fixed traffic sentinel HTTP 414 error response
1 parent bb274a1 commit 70422e7

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

core/src/com/cloud/network/resource/TrafficSentinelResource.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.BufferedReader;
2323
import java.io.IOException;
2424
import java.io.InputStreamReader;
25+
import java.io.OutputStream;
2526
import java.net.MalformedURLException;
2627
import java.net.URL;
2728
import java.net.URLEncoder;
@@ -53,6 +54,7 @@
5354
import com.cloud.host.Host;
5455
import com.cloud.resource.ServerResource;
5556
import com.cloud.utils.exception.ExecutionException;
57+
import java.net.HttpURLConnection;
5658

5759
public class TrafficSentinelResource implements ServerResource {
5860

@@ -205,14 +207,25 @@ private DirectNetworkUsageAnswer getPublicIpBytesSentAndReceived(DirectNetworkUs
205207
_exclZones = cmd.getExcludeZones();
206208
}
207209

210+
BufferedReader in = null;
211+
OutputStream os = null;
208212
try {
209-
//Query traffic Sentinel
210-
trafficSentinel =
211-
new URL(_url + "/inmsf/Query?script=" + URLEncoder.encode(getScript(cmd.getPublicIps(), cmd.getStart(), cmd.getEnd()), "UTF-8") +
212-
"&authenticate=basic&resultFormat=txt");
213-
214-
BufferedReader in = new BufferedReader(new InputStreamReader(trafficSentinel.openStream()));
215-
213+
//Query traffic Sentinel using POST method. 3 parts to the connection call and subsequent writing.
214+
215+
//Part 1 - Connect to the URL of the traffic sentinel's instance.
216+
trafficSentinel = new URL(_url + "/inmsf/Query");
217+
String postData = "script="+URLEncoder.encode(getScript(cmd.getPublicIps(), cmd.getStart(), cmd.getEnd()), "UTF-8")+"&authenticate=basic&resultFormat=txt";
218+
HttpURLConnection con = (HttpURLConnection) trafficSentinel.openConnection();
219+
con.setRequestMethod("POST");
220+
con.setRequestProperty("Content-Length", String.valueOf(postData.length()));
221+
con.setDoOutput(true);
222+
223+
//Part 2 - Write Data
224+
os = con.getOutputStream();
225+
os.write(postData.getBytes("UTF-8"));
226+
227+
//Part 3 - Read response of the request
228+
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
216229
String inputLine;
217230

218231
while ((inputLine = in.readLine()) != null) {
@@ -228,13 +241,19 @@ private DirectNetworkUsageAnswer getPublicIpBytesSentAndReceived(DirectNetworkUs
228241
answer.put(publicIp, bytesSentAndReceived);
229242
}
230243
}
231-
in.close();
232244
} catch (MalformedURLException e1) {
233245
s_logger.info("Invalid Traffic Sentinel URL", e1);
234246
throw new ExecutionException(e1.getMessage());
235247
} catch (IOException e) {
236248
s_logger.debug("Error in direct network usage accounting", e);
237249
throw new ExecutionException(e.getMessage());
250+
} finally {
251+
if (os != null) {
252+
os.close();
253+
}
254+
if (in != null) {
255+
in.close();
256+
}
238257
}
239258
} catch (Exception e) {
240259
s_logger.debug(e);

0 commit comments

Comments
 (0)