Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close csv & writer properly when downloading report (#3526) #3527

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)

- #3471 - EmailService not working due to unsatisfied reference to MailTemplateManager in AEM on prem
- #3497 - Redirect Manager: allow creating redirect configurations in a nested hierarchy
- #3526 - Downloading report causes "Writer already closed" error

## 6.9.10 - 2024-12-13

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
Expand Down Expand Up @@ -79,14 +78,16 @@ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHtt
+ ".csv");

Writer writer = null;
Csv csv = null;

try {
writer = response.getWriter();

// write the BOM to indicate this is a UTF-8 file
writer.write("\uFEFF");

// initialize the csv
final Csv csv = new Csv();
csv = new Csv();
csv.setFieldSeparatorWrite(delimiterConfiguration.getFieldDelimiter());
csv.writeInit(writer);

Expand All @@ -106,14 +107,15 @@ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHtt
log.warn("Unable to export report for configuration: {}", config);
}
}
csv.close();
} else {
throw new IOException("No configurations found for " + request.getResource());
}
} catch (ReportException e) {
throw new ServletException("Exception extracting report to CSV", e);
} finally {
IOUtils.closeQuietly(writer);
if (csv != null) {
csv.close();
}
}
}

Expand Down
Loading