I’ve got a resource aware spring mvc portlet that I’m using to serve a PDF. Previously our method for serving PDFs from a portlet has been to link to a servlet to actually write the PDF response. Our pattern in the servlets was basically this:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
OutputStream out = response.getOutputStream();
FileInputStream certIn = null;
try {
certIn = new FileInputStream(pdfFile);
if (certIn.available() > 0) {
while (certIn.available() > 0) {
out.write(certIn.read());
}
out.flush();
}
} catch (IOException e) {
response.reset();
response.setContentType("text/html");
getServletContext().getRequestDispatcher("/WEB-INF/jsp/error.jsp").include(request, response);
} finally {
if (certIn != null) {
try {
certIn.close();
} catch (IOException e) {
LOG.warn(
"Failed to close FileInputStream", e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
LOG.warn("Failed to close ServletOutputStream", e);
}
}
}
}
I’m now trying to replicate this in the resource aware portlet. The problem I’ve got is if we get an error, I can’t get it to redirect to the error jsp.
If I use the portletContext.getRequestDispatcher() to forward to a jsp, I get an error saying I can’t call getWriter() after getOuputStream(). I get the same error if I try to return a spring ModelAndView to the error.jsp.
Can anyone suggest how I can redirect the user to a jsp after calling getOutputStream() on the ResourceResponse?
Thanks.
Changed the order of what I was doing slightly in the end. Now don’t call getPortletOutputStream() until I know the file exists and is readable. If it fails whilst actually writing to the output stream it cannot serve the jsp, but that’s the case for the servlet too.