From abb0bb10d34959b6091a701b7aaf4ab8cd699c53 Mon Sep 17 00:00:00 2001 From: labkey-jeckels Date: Sun, 7 Dec 2025 07:59:24 -0800 Subject: [PATCH] Fix BiologicsReportTest - don't offer files with no content --- .../reports/report/r/ParamReplacementSvc.java | 2 +- .../report/r/view/DownloadOutputView.java | 2 +- .../api/reports/report/r/view/HtmlOutput.java | 4 +- .../reports/report/r/view/ImageOutput.java | 4 +- .../api/reports/report/r/view/JsonOutput.java | 6 +- .../reports/report/r/view/ROutputView.java | 25 +-- .../api/reports/report/r/view/TextOutput.java | 4 +- .../api/reports/report/r/view/TsvOutput.java | 169 +++++++++--------- 8 files changed, 106 insertions(+), 110 deletions(-) diff --git a/api/src/org/labkey/api/reports/report/r/ParamReplacementSvc.java b/api/src/org/labkey/api/reports/report/r/ParamReplacementSvc.java index aaec41af51d..7986304942f 100644 --- a/api/src/org/labkey/api/reports/report/r/ParamReplacementSvc.java +++ b/api/src/org/labkey/api/reports/report/r/ParamReplacementSvc.java @@ -659,7 +659,7 @@ public void toFile(List outputSubst, FileLike file) throws Exc } } - public Collection fromFile(FileLike file) throws Exception + public Collection fromFile(FileLike file) throws IOException { Map outputSubstMap = new HashMap<>(); if (file.exists()) diff --git a/api/src/org/labkey/api/reports/report/r/view/DownloadOutputView.java b/api/src/org/labkey/api/reports/report/r/view/DownloadOutputView.java index 1a56443f632..457d1d72719 100644 --- a/api/src/org/labkey/api/reports/report/r/view/DownloadOutputView.java +++ b/api/src/org/labkey/api/reports/report/r/view/DownloadOutputView.java @@ -86,7 +86,7 @@ protected void renderInternal(Object model, PrintWriter out) throws IOException return; out.write(""); - renderTitle(model, out); + renderTitle(out); if (isCollapse()) out.write("
"); else diff --git a/api/src/org/labkey/api/reports/report/r/view/HtmlOutput.java b/api/src/org/labkey/api/reports/report/r/view/HtmlOutput.java index 2904d3a7283..70c4d37d1eb 100644 --- a/api/src/org/labkey/api/reports/report/r/view/HtmlOutput.java +++ b/api/src/org/labkey/api/reports/report/r/view/HtmlOutput.java @@ -98,7 +98,7 @@ public HtmlOutputView(ParamReplacement param, String label) @Override protected String renderInternalAsString(FileLike file) throws Exception { - if (file.exists()) + if (existsWithContent(file)) return PageFlowUtil.addScriptNonces(PageFlowUtil.getStreamContentsAsString(file.openInputStream())); return null; @@ -113,7 +113,7 @@ protected void renderInternal(Object model, PrintWriter out) throws Exception if (null != html) { out.write(""); - renderTitle(model, out); + renderTitle(out); if (isCollapse()) out.write("
"); else diff --git a/api/src/org/labkey/api/reports/report/r/view/ImageOutput.java b/api/src/org/labkey/api/reports/report/r/view/ImageOutput.java index 2027bd302be..8304493b076 100644 --- a/api/src/org/labkey/api/reports/report/r/view/ImageOutput.java +++ b/api/src/org/labkey/api/reports/report/r/view/ImageOutput.java @@ -123,7 +123,7 @@ protected String renderInternalAsString(FileLike file) throws IOException { String imgUrl = null; - if (file.exists()) + if (existsWithContent(file)) { FileLike imgFile; if (!_deleteFile) @@ -157,7 +157,7 @@ protected void renderInternal(Object model, PrintWriter out) throws IOException if (null != imgUrl) { out.write(""); - renderTitle(model, out); + renderTitle(out); if (isCollapse()) out.write("
"); else diff --git a/api/src/org/labkey/api/reports/report/r/view/JsonOutput.java b/api/src/org/labkey/api/reports/report/r/view/JsonOutput.java index b07e48b4f5d..21552583c9a 100644 --- a/api/src/org/labkey/api/reports/report/r/view/JsonOutput.java +++ b/api/src/org/labkey/api/reports/report/r/view/JsonOutput.java @@ -57,7 +57,7 @@ public JsonOutput() } @Override - public HttpView getView(ViewContext context) + public HttpView getView(ViewContext context) { return new JsonOutputView(this); } @@ -85,7 +85,7 @@ public JsonOutputView(ParamReplacement param) @Override protected String renderInternalAsString(FileLike file) throws IOException { - if (file.exists()) + if (existsWithContent(file)) return PageFlowUtil.getStreamContentsAsString(file.openInputStream()); return null; @@ -101,7 +101,7 @@ protected void renderInternal(Object model, PrintWriter out) throws IOException if (null != rawValue) { out.write(""); - renderTitle(model, out); + renderTitle(out); if (isCollapse()) out.write("
");
                     else
diff --git a/api/src/org/labkey/api/reports/report/r/view/ROutputView.java b/api/src/org/labkey/api/reports/report/r/view/ROutputView.java
index 71c0101625f..6ba467a370d 100644
--- a/api/src/org/labkey/api/reports/report/r/view/ROutputView.java
+++ b/api/src/org/labkey/api/reports/report/r/view/ROutputView.java
@@ -29,11 +29,8 @@
 import org.labkey.vfs.FileLike;
 
 import java.io.File;
-import java.io.FileFilter;
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.nio.file.Files;
-import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -128,7 +125,7 @@ protected String renderInternalAsString(FileLike file) throws Exception
         return null;
     }
 
-    protected void renderTitle(Object model, PrintWriter out)
+    protected void renderTitle(PrintWriter out)
     {
         StringBuilder sb = new StringBuilder();
 
@@ -164,7 +161,7 @@ protected FileLike moveToTemp(FileLike file) throws IOException
         return null;
     }
 
-    protected boolean exists(File file)
+    protected boolean existsWithContent(FileLike file)
     {
         long size = 0;
 
@@ -181,11 +178,7 @@ protected boolean exists(File file)
             if (ALLOW_REMOTE_FILESIZE_BYPASS && _isRemote)
                 return true;
 
-            try
-            {
-                size = Files.size(Paths.get(file.getAbsolutePath()));
-            }
-            catch(IOException ignore){}
+            size = file.getSize();
         }
 
         return (size > 0);
@@ -199,16 +192,12 @@ public static void cleanUpTemp(final long cutoff)
 
         if (tempDir.exists())
         {
-            File[] filesToDelete = tempDir.listFiles(new FileFilter(){
-                @Override
-                public boolean accept(File file)
+            File[] filesToDelete = tempDir.listFiles(file -> {
+                if (!file.isDirectory() && file.getName().startsWith(PREFIX))
                 {
-                    if (!file.isDirectory() && file.getName().startsWith(PREFIX))
-                    {
-                        return file.lastModified() < cutoff;
-                    }
-                    return false;
+                    return file.lastModified() < cutoff;
                 }
+                return false;
             });
             
             for (File file : filesToDelete)
diff --git a/api/src/org/labkey/api/reports/report/r/view/TextOutput.java b/api/src/org/labkey/api/reports/report/r/view/TextOutput.java
index 5b60f8d8144..9fc16a286a2 100644
--- a/api/src/org/labkey/api/reports/report/r/view/TextOutput.java
+++ b/api/src/org/labkey/api/reports/report/r/view/TextOutput.java
@@ -85,7 +85,7 @@ public TextOutputView(ParamReplacement param)
         @Override
         protected String renderInternalAsString(FileLike file) throws IOException
         {
-            if (file.exists())
+            if (existsWithContent(file))
                 return PageFlowUtil.getStreamContentsAsString(file.openInputStream());
 
             return null;
@@ -101,7 +101,7 @@ protected void renderInternal(Object model, PrintWriter out) throws IOException
                 if (null != rawValue)
                 {
                     out.write("");
-                    renderTitle(model, out);
+                    renderTitle(out);
                     if (isCollapse())
                         out.write("
");
                     else
diff --git a/api/src/org/labkey/api/reports/report/r/view/TsvOutput.java b/api/src/org/labkey/api/reports/report/r/view/TsvOutput.java
index 3528e1a624f..0b524a4219e 100644
--- a/api/src/org/labkey/api/reports/report/r/view/TsvOutput.java
+++ b/api/src/org/labkey/api/reports/report/r/view/TsvOutput.java
@@ -112,7 +112,7 @@ public static class TabReportView extends ROutputView
         @Override
         protected String renderInternalAsString(FileLike file) throws IOException
         {
-            if (file.exists())
+            if (existsWithContent(file))
                 return PageFlowUtil.getStreamContentsAsString(file.openInputStream());
 
             return null;
@@ -126,104 +126,111 @@ protected void renderInternal(Object model, PrintWriter out) throws Exception
                 TabLoader tabLoader = createTabLoader(file);
                 if (tabLoader != null)
                 {
-                    ColumnDescriptor[] cols = tabLoader.getColumns();
-                    List> data = tabLoader.load();
+                    try
+                    {
+                        ColumnDescriptor[] cols = tabLoader.getColumns();
+                        List> data = tabLoader.load();
 
-                    List display = new ArrayList<>();
-                    HashMap hrefs = new HashMap<>(tabLoader.getColumns().length * 2);
-                    HashMap styles = new HashMap<>(tabLoader.getColumns().length * 2);
+                        List display = new ArrayList<>();
+                        HashMap hrefs = new HashMap<>(tabLoader.getColumns().length * 2);
+                        HashMap styles = new HashMap<>(tabLoader.getColumns().length * 2);
 
-                    for (ColumnDescriptor col : cols)
-                        hrefs.put(col.name, null);
+                        for (ColumnDescriptor col : cols)
+                            hrefs.put(col.name, null);
 
-                    for (ColumnDescriptor col : cols)
-                    {
-                        if (col.name.endsWith(".href") || col.name.endsWith("_href"))
+                        for (ColumnDescriptor col : cols)
                         {
-                            String name = col.name.substring(0,col.name.length()-".href".length());
-                            if (hrefs.containsKey(name))
+                            if (col.name.endsWith(".href") || col.name.endsWith("_href"))
                             {
-                                hrefs.put(name,col);
-                                continue;
+                                String name = col.name.substring(0, col.name.length() - ".href".length());
+                                if (hrefs.containsKey(name))
+                                {
+                                    hrefs.put(name, col);
+                                    continue;
+                                }
                             }
-                        }
-                        if (col.name.endsWith(".style") || col.name.endsWith("_style"))
-                        {
-                            String name = col.name.substring(0,col.name.length()-".style".length());
-                            if (hrefs.containsKey(name))
+                            if (col.name.endsWith(".style") || col.name.endsWith("_style"))
                             {
-                                styles.put(name,col);
-                                continue;
+                                String name = col.name.substring(0, col.name.length() - ".style".length());
+                                if (hrefs.containsKey(name))
+                                {
+                                    styles.put(name, col);
+                                    continue;
+                                }
                             }
+                            display.add(col);
                         }
-                        display.add(col);
-                    }
 
-                    int row = 0;
-                    out.write("");
-                    renderTitle(model, out);
-                    if (isCollapse())
-                        out.write("
"); - else - out.write("
"); - out.write(""); - for (ColumnDescriptor col : display) - { - if (Number.class.isAssignableFrom(col.getDataClass())) - out.write(""); - out.write("
"); + int row = 0; + out.write(""); + renderTitle(out); + if (isCollapse()) + out.write("
"); else - out.write(""); - row++; - } - out.write(""); - - for (Map m : data) - { - if (row % 2 == 0) - out.write(""); - else - out.write(""); + out.write(""); + out.write("
"); - out.write(PageFlowUtil.filter(col.name, true, true)); - out.write("
"); + out.write(""); for (ColumnDescriptor col : display) { - Object colVal = m.get(col.name); - if ("NA".equals(colVal)) - colVal = null; - ColumnDescriptor hrefCol = hrefs.get(col.name); - String href = hrefCol == null ? null : ConvertUtils.convert((m.get(hrefCol.name))); - ColumnDescriptor styleCol = styles.get(col.name); - String style = styleCol == null ? null : ConvertUtils.convert((m.get(styleCol.name))); - - out.write(""); - if (null != href) - { - out.write(""); - } - if (null == colVal) - out.write(" "); + if (Number.class.isAssignableFrom(col.getDataClass())) + out.write(""); + row++; } out.write(""); - row++; + + for (Map m : data) + { + if (row % 2 == 0) + out.write(""); + else + out.write(""); + for (ColumnDescriptor col : display) + { + Object colVal = m.get(col.name); + if ("NA".equals(colVal)) + colVal = null; + ColumnDescriptor hrefCol = hrefs.get(col.name); + String href = hrefCol == null ? null : ConvertUtils.convert((m.get(hrefCol.name))); + ColumnDescriptor styleCol = styles.get(col.name); + String style = styleCol == null ? null : ConvertUtils.convert((m.get(styleCol.name))); + + out.write(""); + if (null != href) + { + out.write(""); + } + if (null == colVal) + out.write(" "); + else + out.write(PageFlowUtil.filter(ConvertUtils.convert(colVal), true, true)); + if (null != href) + out.write(""); + out.write(""); + } + out.write(""); + row++; + } + out.write("
"); else - out.write(PageFlowUtil.filter(ConvertUtils.convert(colVal), true, true)); - if (null != href) - out.write(""); + out.write(""); + out.write(PageFlowUtil.filter(col.name, true, true)); out.write("
"); + } + finally + { + tabLoader.close(); } - out.write("
"); } } }