[JBoss Seam] - Re: how to download a file via Seam& JSF,
by squ1rr3l
Ok, I have my code working now. There were a couple of things that didn't work:
1) Initially I tried using the
| JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
|
for passing the outputStream from the Response object to the JasperExportManager. AFAIK, that's what produced the "Writer not possible" error.
2) In the session EJB, using FacesContext.getCurrentInstance() does not provide a usable context - at least not one that can be used to obtain the Response object. So maybe that's usable in a POJO component, but not an EJB component, and that's possibly by design, I don't know. Scope doesn't really seem to matter.
Anyway, what works is this:
- In the session EJB:
| @In
| private FacesContext facesContext;
| private byte[] report1;
|
| ...
|
| String reportName = "/reports/myreport.jrxml";
| CreateReport cr = new CreateReport();
| report2 = cr.getReport(reportName);
| try {
| HttpServletResponse response = (HttpServletResponse)
| facesContext.getExternalContext().getResponse();
| response.setContentType("application/pdf");
| OutputStream os = response.getOutputStream();
| os.write(report2);
| os.flush();
| os.close();
| facesContext.responseComplete();
| } catch(Exception ex) {
| ex.printStackTrace();
| }
|
Just for completeness - the CreateReport.getReport method just does the jasper stuff to load and fill the report, then return the byte array:
| public byte[] getReport(String reportName)
| {
| try
| {
| ServletContext sCtxt = ServletLifecycle.getServletContext();
| File reportFile = new File(sCtxt.getRealPath(reportName));
| if( !reportFile.exists()) {
| throw new JRRuntimeException("File " + reportName + " not found.");
| }
|
| JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
| JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
| JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
|
| if (jasperPrint != null) {
| byte[] reportPDF = JasperExportManager.exportReportToPdf(jasperPrint);
| return reportPDF;
| } else {
| throw new JRRuntimeException("null jasperPrint");
| return null;
| }
| }
| catch (Exception e)
| {
| e.printStackTrace();
| }
| return null;
| }
|
Thanks for the help!
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4122875#4122875
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4122875
18 years, 6 months
[JBoss Seam] - Re: how to download a file via Seam& JSF,
by squ1rr3l
Well I'm not sure why this isn't working for Jasper output, then. My code is very similar (although there is more involved, of course). I was actually getting the context before using injection, which worked better. I'm also using a session EJB - I assume your test code is not.
In any case, using
| FacesContext context = FacesContext.getCurrentInstance();
|
causes errors even earlier. When I do
| HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
|
I get an exception:
| 20:00:28,098 ERROR [STDERR] java.lang.IllegalStateException
| 20:00:28,108 ERROR [STDERR] at com.sun.faces.context.FacesContextImpl.assertNotReleased(FacesContextImpl.java:428)
| 20:00:28,108 ERROR [STDERR] at com.sun.faces.context.FacesContextImpl.getExternalContext(FacesContextImpl.java:149)
| 20:00:28,108 ERROR [STDERR] at org.my.project.session.I9CertForm.generateCertificate(I9CertForm.java:145)
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4122868#4122868
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4122868
18 years, 6 months
[JBoss Seam] - Re: Servlets in long running conversations
by andrey.chorniy
I have some news. it is start working for me, but to achieve that I have to add cookie parameter JSESSIONID to identify the session in which that conversation was originally created.
ContextFilter and the underlying code uses the HTTP-Session to restore Conversation-Entries.
If I make request with plain http-get request like:
http://server-host/context/servlet/some-servlet?cid=2
from another browser - it doesn't work.
If I use two tabs of FireFox - in the first tab I open the conversation, in the second I make a servlet-request - IT WORKS.
So, it start working after I've added JSESSIONID cookie to my call servlet-request.
I make a programmatic call (not a browser reauest), so I have control over cookies.
But it seems that it is impossible to do it if you don't know the JSESSIONID value or have no controll over cookies. To do it via plain URL approach you have to turn-off cookies in the browser to add the jsessionid parameter to the get-request.
>From one side - it add some additional security, from the other side - you not always can use it, since you are not always can establish the HTTPSession for that
Here is the code which is used to load current Conversation context.
It is stored in the sessionContext, which is based on the HTTPSession object
org.jboss.seam.contexts.Lifecycle
| public static void beginRequest(ServletContext servletContext, HttpSession session, ServletRequest request)
| {
| log.debug( ">>> Begin web request" );
| Contexts.eventContext.set( new WebRequestContext( ContextAdaptor.getRequest(request) ) );
| Contexts.sessionContext.set( new WebSessionContext( ContextAdaptor.getSession(session) ) );
| Contexts.applicationContext.set( new WebApplicationContext(servletContext) );
| Contexts.conversationContext.set(null); //in case endRequest() was never called
| }
|
| org.jboss.seam.core.ConversationEntries
| public static ConversationEntries instance()
| {
| if ( !Contexts.isSessionContextActive() )
| {
| throw new IllegalStateException("No session context active");
| }
| return (ConversationEntries) Component.getInstance(ConversationEntries.class, ScopeType.SESSION);
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4122867#4122867
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4122867
18 years, 6 months