Hi,
Note the @Resource private HttpServletRequest httpRequest field. Is
this the best way to do this? Is the field thread safe?
Yes, it is a thread-safe proxy...
By the way, starting from 2.2.5 it is possible to use a RequestDispatcherProvider [1] which implements JAXRS MessageBodyWriter to
redirect to other servlets. You can configure RequestDispatcherProvider to store the response object either as a request parameter
or request session parameter and you can tell it what is the name of the key.
Ex, you can have a method returning an instance of jasperPrint and this provider storing it as an
ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE
I'm not sure if it can help in your specific case, but I'd like to figure out how RequestDispatcherProvider can be enhanced, if
needed, for users be able to avoid dealing with ResourceUtils.getClasspathResourceStream, unless you only used it for testing...
thanks, Sergey
[1] http://cxf.apache.org/docs/jax-rs.html#JAX-RS-Redirection
----- Original Message -----
From: "Matthew Shaw" <[email protected]>
To: <[email protected]>
Sent: Wednesday, November 25, 2009 11:53 PM
Subject: REST and Jasper
Hi,
I'd like to integrate jasper reports. All going well, except for how
Jasper locates images in a web environment, using an ImageServlet. The
demo app from jasper requires that I set a map into the http session
which contains a cache of the images as they are called.
Here is a snippet of my code:
@Resource
private HttpServletRequest httpRequest;
public Response getCustomerList(ReportQuery reportQuery)
{
log.debug("Start - getCustomerList");
log.debug("Customer Id:
"+reportQuery.getCustomerId());
log.debug("Start from controller id:
"+reportQuery.getStartFromControllerId());
// TODO: this is an example report only
used to test the framework.
ByteArrayOutputStream baos = new
ByteArrayOutputStream();
try {
InputStream is =
ResourceUtils.getClasspathResourceStream("/WEB-INF/jasperreports/WebappR
eport.jasper", this.getClass(), BusFactory.getDefaultBus());
JasperReport
jasperReport = (JasperReport)JRLoader.loadObject(is);
Map parameters = new
HashMap();
parameters.put("ReportTitle", "Address Report");
JasperPrint jasperPrint
=
JasperFillManager.fillReport(
jasperReport,
parameters,
new DataSourceStub()
);
JRHtmlExporter exporter
= new JRHtmlExporter();
httpRequest.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_
SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
"image?image=");
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
return Response.ok(baos.toString(),
"text/html").build();
}
Note the @Resource private HttpServletRequest httpRequest field. Is
this the best way to do this? Is the field thread safe? As I potentially
have a lot of users requesting this service...
Cheers,
Matt.