[Logo] OLD FORUM - Use new one: https://www.CaptainCasa.online/forum
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Messages posted by: Anonymous  XML
Profile for Anonymous -> Messages posted by Anonymous [4]
Author Message
First integrate a download component to your JSP, e.g. t:filedownloadbutton
Code:
<t:filedownloadbutton
 	fileextensions="pdf" 
 	filename="report.pdf" 
 	openimmediately="true" 
 	opensupported="true" 
 	text="Print PDF" 
 	url="#{d.PrintUI.printReportUrl}"
 />


In the referenced managed bean PrintUI, coding could look like that
Code:
public class PrintUI extends WorkpageDispatchedBean implements Serializable {
 	DefaultBufferedContent report;
 	...
 
 	// Constructor
 	public PrintUI(IWorkpageDispatcher dispatcher) {
 		super(dispatcher);
 		...
 		// add print functionallity
 		report = new MyReport();
 		// set close operation - to remove report from memory
 		getWorkpage().addLifecycleListener(
 				new WorkpageDefaultLifecycleListener() {
 					public void reactOnDestroyed() {
 						super.reactOnDestroyed();
 						if(report!=null) BufferedContentMgr.remove(report);
 					}
 				});		
 
 	}
 
 	public String getPrintReportUrl() { return report.getURL(); }
 
 	private static class MyReport extends DefaultBufferedContent {
 		@Override
 		public byte[] getContent() {
 			ByteArrayOutputStream out = new ByteArrayOutputStream();
 			// build PDF with iText			
 			Document document = new Document();
 			PdfWriter writer = PdfWriter.getInstance(document, out);
 			document.open();
 			// create content
 			...
 			document.close();
 			return out.toByteArray();				
 		}
 
 		@Override
 		public String getContentType() {
 			return "application/pdf";
 		}
 	}
 }


I didn't test the coding, but I hope there's no fatal error
There's a simple way to set a limit for max. opened pages.

You have to override some methods of your Dispatcher class which inherits from WorkpageDispatcher.
Code:
/**
  * This constructor is called initially 
  */
 public Dispatcher() {
   super();
   // Limit nr. of max opened pages
   getWorkpageContainer().setMaxNumberOfWorkpages(Constants.MAXWORKPAGES);
   //getWorkpageContainer().setMaxNumberOfWorkpagesExceededMessage("My own message!");
 }
 
 /**
  * This constructor is called when a new workpage is opened
  * @param workpageContainer
  */
 public Dispatcher(IWorkpageContainer workpageContainer) {
   super(workpageContainer);
   int openedPages = workpageContainer.getAllWorkpages().size();
   System.out.println(openedPages + " Workpages opened!");
   for(Object obj : workpageContainer.getAllWorkpages()) {
     Workpage wp = (Workpage)obj;
     System.out.println(wp.getTitle());
   }		
   if(openedPages >= Constants.MAXWORKPAGES) {
     Workpage wp = (Workpage)workpageContainer.getAllWorkpages().get(0);
     workpageContainer.getAllWorkpages().remove(0);
     System.out.println("Close " + wp.getTitle());
     wp.closeForced();
   }	
 }

If you just want to limit the amount, it's enough to override Dispatcher(), when the user exceeds the number of pages, he gets a popup.
If you want to do further manipulations in your workplace you can override Dispatcher(IWorkpageContainer), this constructor is called everytime a new page is opened!
GJLipX <a href="http://kiezoosvoxjy.com/">kiezoosvoxjy</a>, ivgvozhzkyvz, [link=http://odsgtpdfumaz.com/]odsgtpdfumaz[/link], http://mxlqynecfawl.com/
Sometimes you need the FacesContext.getCurrentInstnace() in a servlet, that is no a FacesServlet, e.g. when passing a URL to the FILEDOWNLOAD* components.

Code:
 package org.eclnt.jsfserver.bufferedcontent;
 
 import java.io.IOException;
 
 import javax.faces.FactoryFinder;
 import javax.faces.context.FacesContext;
 import javax.faces.context.FacesContextFactory;
 import javax.faces.lifecycle.Lifecycle;
 import javax.faces.lifecycle.LifecycleFactory;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
 import org.eclnt.util.log.CLog;
 
 public class BufferedContentServlet extends HttpServlet
 {
     // ------------------------------------------------------------------------
     // members
     // ------------------------------------------------------------------------
     
     private FacesContextFactory m_facesContextFactory;
     private Lifecycle m_lifecycle;
     
     // ------------------------------------------------------------------------
     // public usage
     // ------------------------------------------------------------------------
 
     @Override
     public void init() throws ServletException
     {
         super.init();
         try
         {
             LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
             m_facesContextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
             m_lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
         }
         catch (Throwable t)
         {
             CLog.L.log(CLog.LL_INF,"Problems in init() of BufferedContentServlet - only relevant if accessing FacesContext during servlet processing",t);
         }
     }
 
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
             throws ServletException, IOException
     {
         // create faces context
         FacesContext context = null;
         try
         {
             context = m_facesContextFactory.getFacesContext(getServletContext(),req,resp,m_lifecycle);
         }
         catch (Throwable t)
         {
             CLog.L.log(CLog.LL_INF,"Problems in init() of BufferedContentServlet - only relevant if accessing FacesContext during servlet processing",t);
         }
         ...
         ...
         ...
         normal servlet processing
         ...
         ...
         ...
         // clean up
         if (context != null)
         {
             context.release();
         }
     }
 
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
             throws ServletException, IOException
     {
         doGet(req,resp);
     }
 
 }
 


I am not 100% sure is this is a "hack"... Of course you need to be aware that all the typical things like JSF request lifecylcle etc. are not executed... But maybe you require the context for the resolution of some value bindings.

Björn
 
Profile for Anonymous -> Messages posted by Anonymous [4]
Go to:   
Powered by JForum 2.1.6 © JForum Team