X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FStatisticsServlet.java;h=4917402ca9fa308be5b4ef377ce01c74e6286816;hb=c7708c4957a822b48e14570a0695b6572eb22469;hp=e79d47052497760726825449d740bebcc68f6148;hpb=c3e6bf5bde573fe733f5b5860610d3df73630014;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java index e79d4705..4917402c 100755 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java @@ -180,39 +180,8 @@ public class StatisticsServlet extends BaseServlet { outputType = req.getParameter("output_type"); } - try { - - String filterQuery = this.queryGeneretor(map); - eventlogger.debug("SQL Query for Statistics resultset. " + filterQuery); - - ResultSet rs = this.getRecordsForSQL(filterQuery); - - if (outputType.equals("csv")) { - resp.setContentType("application/octet-stream"); - Date date = new Date(); - SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss"); - resp.setHeader("Content-Disposition", - "attachment; filename=\"result:" + dateFormat.format(date) + ".csv\""); - eventlogger.info("Generating CSV file from Statistics resultset"); + this.getRecordsForSQL(map, outputType, out, resp); - rsToCSV(rs, out); - } else { - eventlogger.info("Generating JSON for Statistics resultset"); - this.rsToJson(rs, out); - } - } catch (IOException e) { - eventlogger.error("IOException - Generating JSON/CSV:" + e); - e.printStackTrace(); - } catch (JSONException e) { - eventlogger.error("JSONException - executing SQL query:" + e); - e.printStackTrace(); - } catch (SQLException e) { - eventlogger.error("SQLException - executing SQL query:" + e); - e.printStackTrace(); - } catch (ParseException e) { - eventlogger.error("ParseException - executing SQL query:" + e); - e.printStackTrace(); - } } @@ -565,44 +534,47 @@ public class StatisticsServlet extends BaseServlet { intlogger.info("Error parsing time=" + s); return -1; } - private ResultSet getRecordsForSQL(String sql) { - intlogger.debug(sql); + + private void getRecordsForSQL(Map map, String outputType, ServletOutputStream out, HttpServletResponse resp) { + try { + + String filterQuery = this.queryGeneretor(map); + eventlogger.debug("SQL Query for Statistics resultset. " + filterQuery); + intlogger.debug(filterQuery); long start = System.currentTimeMillis(); DB db = new DB(); - Connection conn = null; ResultSet rs = null; - Statement stmt = null; - PreparedStatement pst = null; - try { - conn = db.getConnection(); - stmt = conn.createStatement(); - pst = conn.prepareStatement(sql); + try (Connection conn = db.getConnection()) { + try (PreparedStatement pst = conn.prepareStatement(filterQuery)) { rs = pst.executeQuery(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - - try { - if (conn != null) { - db.release(conn); - } - if (stmt != null) { - stmt.close(); - } - if (pst != null) { - pst.close(); - } - if (rs != null) { - rs.close(); - } - - } catch (SQLException sqlException) { - intlogger.error("Exception in getting SQl Records",sqlException); + if (outputType.equals("csv")) { + resp.setContentType("application/octet-stream"); + Date date = new Date(); + SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss"); + resp.setHeader("Content-Disposition", + "attachment; filename=\"result:" + dateFormat.format(date) + ".csv\""); + eventlogger.info("Generating CSV file from Statistics resultset"); + + rsToCSV(rs, out); + } else { + eventlogger.info("Generating JSON for Statistics resultset"); + this.rsToJson(rs, out); } - - intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms"); - - return rs; + } + } catch (SQLException e) { + e.printStackTrace(); } + intlogger.debug("Time: " + (System.currentTimeMillis() - start) + " ms"); + } catch (IOException e) { + eventlogger.error("IOException - Generating JSON/CSV:" + e); + e.printStackTrace(); + } catch (JSONException e) { + eventlogger.error("JSONException - executing SQL query:" + e); + e.printStackTrace(); + } catch (ParseException e) { + eventlogger.error("ParseException - executing SQL query:" + e); + e.printStackTrace(); + } } } +