diff options
author | Manoop Talasila <talasila@research.att.com> | 2018-09-12 16:14:12 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-09-12 16:14:12 +0000 |
commit | b55ec83f6513a3e9fe22ea94e91d47af04296fdd (patch) | |
tree | 75de70a7cc791797c5b89162abcc4f30becfa451 /ecomp-sdk/epsdk-analytics | |
parent | 46009a81d37a3c91634df13ee606144977055af3 (diff) | |
parent | df95f627e40ca892c60e37d83a96358fbbe21d22 (diff) |
Merge "Fix NPE & use try-with-resources issues"
Diffstat (limited to 'ecomp-sdk/epsdk-analytics')
-rw-r--r-- | ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/ReportHandler.java | 269 |
1 files changed, 100 insertions, 169 deletions
diff --git a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/ReportHandler.java b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/ReportHandler.java index 415edb0c..00e9dfea 100644 --- a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/ReportHandler.java +++ b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/ReportHandler.java @@ -505,13 +505,15 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { name = ""; } */ } - int cw = 0; - cw = name.trim().length() + 12; + //int cw = 0; + //cw = name.trim().length() + 12; // if(i!=cellWidth.size()-1) - if(sheet.getColumnWidth((short)0)< (short) name.trim().length()) - sheet.setColumnWidth((short)0, (short) name.trim().length()); - if(sheet.getColumnWidth((short)1)< (short) value.trim().length()) - sheet.setColumnWidth((short)1, (short) value.trim().length()); + if(name!=null && (sheet.getColumnWidth((short)0)< (short) name.trim().length())){ + sheet.setColumnWidth((short)0, (short) name.trim().length()); + } + if(sheet.getColumnWidth((short)1)< (short) value.trim().length()){ + sheet.setColumnWidth((short)1, (short) value.trim().length()); + } name = ""; value = ""; @@ -969,22 +971,16 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { SimpleDateFormat MMMMMDDYYYYHHMMSS = new SimpleDateFormat("MMMMM-dd-yyyy HH:mm:ss"); - - - ResultSet rs = null; - Connection conn = null; - Statement st = null; ResultSetMetaData rsmd = null; CreationHelper createHelper = wb.getCreationHelper(); if(nvl(sql_whole).length() >0 && rr.getReportType().equals(AppConstants.RT_LINEAR)) { - try { - conn = ConnectionUtils.getConnection(rr.getDbInfo()); - st = conn.createStatement(); + try(Connection conn = ConnectionUtils.getConnection(rr.getDbInfo()); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql_whole)) { System.out.println("************* Map Whole SQL *************"); System.out.println(sql_whole); System.out.println("*****************************************"); - rs = st.executeQuery(sql_whole); rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); HashMap colHash = new HashMap(); @@ -1655,18 +1651,7 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { } catch (Exception ex) { if(!(ex.getCause() instanceof java.net.SocketException) ) throw new RaptorException (ex); - } finally { - try { - if(conn!=null) - conn.close(); - if(st!=null) - st.close(); - if(rs!=null) - rs.close(); - } catch (SQLException ex) { - throw new RaptorException(ex); - } - } + } /*if(Globals.getShowDisclaimer() && !Globals.disclaimerPositionedTopInCSVExcel()) { rowNum += 1; @@ -2793,8 +2778,8 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { logger.debug(EELFLoggerDelegate.debugLogger, ("Xls File name " + AppUtils.getTempFolderPath() + xlsFName)); - FileOutputStream xlsOut = new FileOutputStream(FilenameUtils.normalize(AppUtils.getTempFolderPath() - + xlsFName)); + try(FileOutputStream xlsOut = new FileOutputStream(FilenameUtils.normalize(AppUtils.getTempFolderPath() + + xlsFName))){ // BufferedWriter xlsOut = new BufferedWriter(new // FileWriter(AppUtils // .getTempFolderPath() @@ -2832,7 +2817,7 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { // + user_id + ".xls"); wb.write(xlsOut); xlsOut.flush(); - xlsOut.close(); + } return xlsFName; } catch (Exception e) { e.printStackTrace(); @@ -3269,8 +3254,6 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>(); HttpSession session = request.getSession(); - ServletOutputStream sos = null; - BufferedInputStream buf = null; XSSFWorkbook wb = null; String formattedReportName = new HtmlStripper().stripSpecialCharacters(rr.getReportName()); String formattedDate = new SimpleDateFormat("MMddyyyyHHmm").format(new Date()); @@ -3324,7 +3307,6 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { String sheetRef = null; - FileOutputStream os = null; //template file File templateFile = null; if(isDashboard) { @@ -3344,7 +3326,7 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { count++; Map.Entry entry = (Entry) iter.next(); ReportRuntime rrDashRep = (ReportRuntime) entry.getValue(); - os = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx")); + try(FileOutputStream os = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx"))){ if(count==1) { if(nvl(rr.getTemplateFile()).length()>0) { @@ -3374,21 +3356,18 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { os.flush(); if(nvl(rr.getTemplateFile()).length()>0) { readTemplate.close(); - } - os.close(); - - FileInputStream inF = new FileInputStream(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx")); - FileOutputStream outStream = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"."+ nvls(extension, "xlsx")); - copyStream(inF, outStream); - outStream.flush(); - outStream.close(); - inF.close(); + } + + try(FileInputStream inF = new FileInputStream(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx")); + FileOutputStream outStream = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"."+ nvls(extension, "xlsx"))){ + copyStream(inF, outStream); + outStream.flush(); + } } - + } FileInputStream xlsIn = null; POIFSFileSystem fileSystem = null; - buf = null; FileOutputStream xlsOut = null; formattedDate = new SimpleDateFormat("MMddyyyyHHmm").format(new Date()); String xlsFName = "dashboard"+formattedDate+user_id+".xls"; @@ -3417,39 +3396,37 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { //Step 2. Generate XML file. File tmp = File.createTempFile("sheet", ".xml"); - FileOutputStream fileOutTemp = new FileOutputStream(tmp); - Writer fw = new OutputStreamWriter(fileOutTemp, XML_ENCODING); - String sql_whole = rrDashRep.getWholeSQL(); + try(FileOutputStream fileOutTemp = new FileOutputStream(tmp); + Writer fw = new OutputStreamWriter(fileOutTemp, XML_ENCODING)){ - SpreadsheetWriter sw = new SpreadsheetWriter(fw); - sw.beginSheet(); - - - generate(wb, sw, styles, rdDashRep, sql_whole, rrDashRep, request, sheet); - - - sw.endSheet(); - - fw.flush(); - fw.close(); - fileOutTemp.flush(); - fileOutTemp.close(); - + String sql_whole = rrDashRep.getWholeSQL(); + + SpreadsheetWriter sw = new SpreadsheetWriter(fw); + sw.beginSheet(); + + + generate(wb, sw, styles, rdDashRep, sql_whole, rrDashRep, request, sheet); + + + sw.endSheet(); + + fw.flush(); + fileOutTemp.flush(); + } //Step 3. Substitute the template entry with the generated data - FileOutputStream outF = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"."+ nvls(extension, "xlsx")); - templateFile = new File(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx")); - substitute(templateFile, tmp, sheetRef.substring(1), outF); - outF.flush(); - outF.close(); + try(FileOutputStream outF = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"."+ nvls(extension, "xlsx"))){ + templateFile = new File(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx")); + substitute(templateFile, tmp, sheetRef.substring(1), outF); + outF.flush(); + } - FileInputStream inF = new FileInputStream(AppUtils.getTempFolderPath()+ filename+"."+ nvls(extension, "xlsx")); - FileOutputStream outStream = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx")); + try(FileInputStream inF = new FileInputStream(AppUtils.getTempFolderPath()+ filename+"."+ nvls(extension, "xlsx")); + FileOutputStream outStream = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx"))){ copyStream(inF, outStream); outStream.flush(); - outStream.close(); - inF.close(); + } } } } else { @@ -3459,12 +3436,12 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { extension = templateFilename.substring(templateFilename.lastIndexOf(".")+1); filename = formattedReportName+formattedDate+user_id; //filename = templateFilename.substring(0, templateFilename.lastIndexOf("."))+"_"+formattedDate+user_id; - } else + } else { filename = formattedReportName+formattedDate+user_id; - + } if(nvl(rr.getTemplateFile()).length()<=0) { - os = new FileOutputStream(AppUtils.getTempFolderPath()+"template"+formattedDate+user_id+".xlsx"); + try(FileOutputStream os = new FileOutputStream(AppUtils.getTempFolderPath()+"template"+formattedDate+user_id+".xlsx")){ wb=new XSSFWorkbook(); //Load customized styles if (rr != null) @@ -3490,11 +3467,11 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { wb.write(os); os.flush(); //wb = null; - os.close(); + } } else { - os = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx")); - FileInputStream readTemplate = new FileInputStream(org.onap.portalsdk.analytics.system.AppUtils.getExcelTemplatePath()+rr.getTemplateFile()); + try(FileOutputStream os = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx")); + FileInputStream readTemplate = new FileInputStream(org.onap.portalsdk.analytics.system.AppUtils.getExcelTemplatePath()+rr.getTemplateFile());){ wb=new XSSFWorkbook(readTemplate); if (rr != null) styles = loadXSSFStyles(rr, wb, styles); @@ -3507,15 +3484,14 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { sheetRef = sheet.getPackagePart().getPartName().getName(); wb.write(os); os.flush(); - readTemplate.close(); //wb = null; - os.close(); + } } //Step 2. Generate XML file. File tmp = File.createTempFile("sheet", ".xml"); - FileOutputStream fileOutTemp = new FileOutputStream(tmp); - Writer fw = new OutputStreamWriter(fileOutTemp, XML_ENCODING); + try(FileOutputStream fileOutTemp = new FileOutputStream(tmp); + Writer fw = new OutputStreamWriter(fileOutTemp, XML_ENCODING)){ //String sql_whole = (String) request.getAttribute(AppConstants.RI_REPORT_SQL_WHOLE); String sql_whole = ""; @@ -3541,30 +3517,29 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { sw.endSheet(); fw.flush(); - fw.close(); fileOutTemp.flush(); - fileOutTemp.close(); - - + } //Step 3. Substitute the template entry with the generated data - FileOutputStream outF = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"."+ nvls(extension, "xlsx")); + try(FileOutputStream outF = new FileOutputStream(AppUtils.getTempFolderPath()+ filename+"."+ nvls(extension, "xlsx"))){ if(nvl(rr.getTemplateFile()).length()>0) { templateFile = new File(AppUtils.getTempFolderPath()+ filename+"T."+ nvls(extension, "xlsx")); - } else + } else { templateFile = new File(AppUtils.getTempFolderPath()+"template"+formattedDate+user_id+".xlsx"); - + } substitute(templateFile, tmp, sheetRef.substring(1), outF); outF.flush(); - outF.close(); + } } //get servlet output stream response.reset(); - sos = response.getOutputStream(); + try(BufferedInputStream buf = new BufferedInputStream(new FileInputStream(AppUtils.getTempFolderPath()+filename + "."+ nvls(extension, "xlsx"))); + ServletOutputStream sos = response.getOutputStream()){ + String mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; if(extension.equals("xlsm")) mime_type = "application/vnd.ms-excel.sheet.macroEnabled.12"; @@ -3572,16 +3547,16 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { response.setHeader("Content-disposition", "attachment;filename="+filename+"."+ nvls(extension, "xlsx")); - buf = new BufferedInputStream(new FileInputStream(AppUtils.getTempFolderPath()+filename + "."+ nvls(extension, "xlsx"))); + int readBytes = 0; //read from the file; write to the ServletOutputStream while ((readBytes = buf.read()) != -1) sos.write(readBytes); - buf.close(); + sos.flush(); - sos.close(); + } logger.debug(EELFLoggerDelegate.debugLogger, ("ENDING..DOWNLOADING XLSX...")); logger.debug(EELFLoggerDelegate.debugLogger, ("##### Heap utilization statistics [MB] #####")); logger.debug(EELFLoggerDelegate.debugLogger, ("Used Memory:" @@ -3601,9 +3576,10 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { * @param out the stream to write the result to */ private static void substitute(File zipfile, File tmpfile, String entry, OutputStream out) throws IOException { - ZipFile zip = new ZipFile(zipfile); + try(ZipFile zip = new ZipFile(zipfile); + ZipOutputStream zos = new ZipOutputStream(out)){ - ZipOutputStream zos = new ZipOutputStream(out); + @SuppressWarnings("unchecked") Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) zip.entries(); @@ -3611,18 +3587,17 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { ZipEntry ze = en.nextElement(); if(!ze.getName().equals(entry)){ zos.putNextEntry(new ZipEntry(ze.getName())); - InputStream is = zip.getInputStream(ze); + try(InputStream is = zip.getInputStream(ze)){ copyStream(is, zos); - is.close(); + } } } zos.putNextEntry(new ZipEntry(entry)); - InputStream is = new FileInputStream(tmpfile); - copyStream(is, zos); - zos.flush(); - zos.close(); - is.close(); - zip.close(); + try(InputStream is = new FileInputStream(tmpfile)){ + copyStream(is, zos); + } + zos.flush(); + } } private static void copyStream(InputStream in, OutputStream out) throws IOException { @@ -3641,8 +3616,6 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { //String reportTitle = rr.getReportName(); //String reportDescr = rr.getReportDescr(); PrintWriter csvOut = new PrintWriter(out); - ServletOutputStream sos = null; - BufferedInputStream buf = null; String fileName = ""; String formattedReportName = new HtmlStripper().stripSpecialCharacters(rr.getReportName()); String formattedDate = new SimpleDateFormat("MMddyyyyHHmm").format(new Date()); @@ -3667,13 +3640,10 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { } } HtmlStripper strip = new HtmlStripper(); - ResultSet rs = null; //OracleConnection conn = null; //OracleStatement st = null; //Connection conO = null; //Statement stO = null; - Connection conn = null; - Statement st = null; ResultSetMetaData rsmd = null; ColumnHeaderRow chr = null; int mb = 1024*1024; @@ -3751,16 +3721,16 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { if(nvl(sql_whole).length()>0) { - try { - conn = ConnectionUtils.getConnection(rr.getDbInfo()); - st = conn.createStatement(); + try(Connection conn = ConnectionUtils.getConnection(rr.getDbInfo()); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql_whole)) { + //conn.setDefaultRowPrefetch(1000); //st.setFetchDirection(ResultSet.TYPE_FORWARD_ONLY); //st.setFetchSize(1000); System.out.println("************* Map Whole SQL *************"); System.out.println(sql_whole); System.out.println("*****************************************"); - rs = st.executeQuery(sql_whole); //st.setFetchSize(1000); rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); @@ -3852,17 +3822,6 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { throw new RaptorException(ex); } catch (Exception ex) { throw new RaptorException (ex); - } finally { - try { - if(conn!=null) - conn.close(); - if(st!=null) - st.close(); - if(rs!=null) - rs.close(); - } catch (SQLException ex) { - throw new RaptorException(ex); - } } if(!raw) { @@ -3990,8 +3949,9 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { csvOut.println(); } // if */ - if(true && !raw) { - try { + if(!raw) { + try(ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(AppUtils.getTempFolderPath()+""+zipFName)); + FileInputStream fis = new FileInputStream(fileName)) { //final int BUFFER = 2048; @@ -4002,9 +3962,7 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { //CRC32 crc = new CRC32(); //PrintStream fos = new PrintStream(new WriterOutputStream(out)); //BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); - //ZipOutputStream s = new ZipOutputStream(dest); - ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(AppUtils.getTempFolderPath()+""+zipFName)); - FileInputStream fis = new FileInputStream(fileName); + //ZipOutputStream s = new ZipOutputStream(dest); //s.setLevel(6); @@ -4032,7 +3990,7 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { response.reset(); java.io.File file = null; - if(true && !raw) { + if(!raw) { response.setContentType("application/octet-stream"); response.setHeader("Content-disposition","attachment;filename="+fName+".zip"); file = new java.io.File(AppUtils.getTempFolderPath()+""+fName+".zip"); @@ -4043,12 +4001,11 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { } - FileInputStream fileIn = null; int c; - try { - sos = response.getOutputStream(); - fileIn = new FileInputStream(file); - buf = new BufferedInputStream(fileIn); + try(ServletOutputStream sos = response.getOutputStream(); + FileInputStream fileIn = new FileInputStream(file); + BufferedInputStream buf = new BufferedInputStream(fileIn);) { + byte [] bOut = new byte [4096]; //read from the file; write to the ServletOutputStream //while ((readBytes = buf.read()) != -1) @@ -4062,18 +4019,6 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { ex.printStackTrace(); } catch (Exception e) { e.printStackTrace(); - } finally { - try { - if (sos != null) - sos.close(); - if (buf != null) - buf.close(); - if (fileIn!=null) { - fileIn.close(); - } - } catch (Exception e1) { - e1.printStackTrace(); - } } File f = new File (AppUtils.getTempFolderPath() @@ -4136,10 +4081,10 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { try { String xmlFName = AppUtils.generateUniqueFileName(request, reportName, AppConstants.FT_XML); - PrintWriter xmlOut = new PrintWriter(new BufferedWriter(new FileWriter(new File( - AppUtils.getTempFolderPath() + xmlFName)))); - xmlOut.println(reportXML); - xmlOut.close(); + try(PrintWriter xmlOut = new PrintWriter(new BufferedWriter(new FileWriter(new File( + AppUtils.getTempFolderPath() + xmlFName))))){ + xmlOut.println(reportXML); + } //return AppUtils.getTempFolderURL() // + java.net.URLEncoder.encode(java.net.URLDecoder.decode(xmlFName)); @@ -4810,20 +4755,17 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { dateFormat = createHelper.createDataFormat().getFormat("MMMMM-dd-yyyy HH:mm:ss"); dateFormatMap.put("MMMMMDDYYYYHHMMSS", new Short(dateFormat)); - ResultSet rs = null; - Connection conn = null; - Statement st = null; ResultSetMetaData rsmd = null; if(nvl(sql_whole).length() >0 && (rr.getReportType().equals(AppConstants.RT_LINEAR) || rr.getReportType().equals(AppConstants.RT_HIVE) )) { - try { - conn = ConnectionUtils.getConnection(rr.getDbInfo()); - st = conn.createStatement(); + try(Connection conn = ConnectionUtils.getConnection(rr.getDbInfo()); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql_whole)) { + logger.debug(EELFLoggerDelegate.debugLogger, ("************* Map Whole SQL *************")); logger.debug(EELFLoggerDelegate.debugLogger, (sql_whole)); logger.debug(EELFLoggerDelegate.debugLogger, ("*****************************************")); - rs = st.executeQuery(sql_whole); rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); HashMap colHash = new HashMap(); @@ -5492,18 +5434,7 @@ public class ReportHandler extends org.onap.portalsdk.analytics.RaptorObject { } catch (Exception ex) { if(!(ex.getCause() instanceof java.net.SocketException) ) throw new RaptorException (ex); - } finally { - try { - if(conn!=null) - conn.close(); - if(st!=null) - st.close(); - if(rs!=null) - rs.close(); - } catch (SQLException ex) { - throw new RaptorException(ex); - } - } + } String footer = (String) session.getAttribute("FOOTER_"+index); if(nvl(footer).length()>0) { |