2222import java .io .BufferedReader ;
2323import java .io .IOException ;
2424import java .io .InputStreamReader ;
25+ import java .io .OutputStream ;
2526import java .net .MalformedURLException ;
2627import java .net .URL ;
2728import java .net .URLEncoder ;
5354import com .cloud .host .Host ;
5455import com .cloud .resource .ServerResource ;
5556import com .cloud .utils .exception .ExecutionException ;
57+ import java .net .HttpURLConnection ;
5658
5759public 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