Porting Servlets to Portlets
Hi,
I want to port a servlet application to a portlet application. In particular, I want to port this method, which works with Servlets so that it works with Portlets:
public void jasperReport(String name, String type, ResultSet data, Map params) {
// Look up the compiled report design resource
ExternalContext econtext = getExternalContext();
InputStream stream = econtext.getResourceAsStream(PREFIX + name + SUFFIX);
// make sure cursor is in front of the first record
try {
data.beforeFirst();
} catch (Exception e) {
throw new FacesException(e);
}
// Fill the requested report with the specified data
JRResultSetDataSource ds = new JRResultSetDataSource(data);
JasperPrint jasperPrint = null;
try {
jasperPrint = JasperFillManager.fillReport(stream, params, ds);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new FacesException(e);
} finally {
try {
stream.close();
} catch (IOException e) {
;
}
}
// Configure the exporter to be used, along with the custom
// parameters specific to the exporter type
JRExporter exporter = null;
HttpServletResponse response = (HttpServletResponse)
econtext.getResponse();
FacesContext fcontext = FacesContext.getCurrentInstance();
try {
response.setContentType(type);
if ("application/pdf".equals(type)) {
exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
response.getOutputStream());
} else if ("text/html".equals(type)) {
exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,
response.getWriter());
// Make images available for the HTML output
HttpServletRequest request =
(HttpServletRequest)
fcontext.getExternalContext().getRequest();
request.getSession().setAttribute(
ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
jasperPrint);
exporter.setParameter(
JRHtmlExporterParameter.IMAGES_MAP, new HashMap());
// Requires mapping /image to the imageServlet in the web.xml
// This servlet serves up the px images for spacing
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image=");
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new FacesException(e);
}
// Enough with the preliminaries ... export the report already
try {
exporter.exportReport();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new FacesException(e);
}
// Tell JavaServer Faces that no output is required
fcontext.responseComplete();
}
Thanks,
Marc
Message was edited by:
marc_nikko

