You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by Adam Shelley <as...@inlandkwbc.com> on 2003/04/16 00:58:47 UTC
servlet with encryption and url parameters
Hello,
I wanted to have a servlet with url parameters and encryption enabled the
would allow a document output via fop to keep bookmarks as well. i'm just
sending this code for the archive just in case any other non java people
need some working code to do as such.
this doesn't take any parameters for passwords or permission levels so it is
quite crude but the example i found on the archive to do encryption with
iText seemed to copy the whole document page by page and left out important
things like bookmarks so hopefully this will be of use to someone.
With this method the whole pdf is encrypted as is and returned to the
response output stream keeping the pdf intact. You will need iText
available in the servlet's environment ie: tomcat home/common/lib to use
this functionality. I simply over wrote the servlet class in the examples
directory and rebuilt the fop.war file with ant.
-Adam
/*
XURLPDF.java, v 1.0 2003/03/14 17:30:00 david
Abstract :Mixing code from FopServlet.java and ExampleXML2PDF.java,
this servlet obtains a pdf, passing xml and xsl parameters as url.
Author: David Bermudez Garcia
modified by adam shelley to include itext's pdf encryptor class
*/
import java.io.*;
//SERVLET
import javax.servlet.*;
import javax.servlet.http.*;
//SAX
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.SAXResult;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
//FOP
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;
//AVALON
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
//ITEXT
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class FopServlet extends HttpServlet {
public static final String XML_REQUEST_PARAM = "xml";
public static final String XSL_REQUEST_PARAM = "xsl";
Logger log = null;
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException
{
if(log == null) {
log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
MessageHandler.setScreenLogger(log);
}
try {
String xmlParam = request.getParameter(XML_REQUEST_PARAM);
String xslParam = request.getParameter(XSL_REQUEST_PARAM);
if((xmlParam != null) && (xslParam != null)) {
response.setContentType("application/pdf");
Driver driver = new Driver();
Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);
driver.setRenderer(Driver.RENDER_PDF);
ByteArrayOutputStream fopout = new ByteArrayOutputStream();
driver.setOutputStream(fopout);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new
StreamSource(xslParam));
Source src=new StreamSource(xmlParam);
Result res = new SAXResult(driver.getContentHandler());
transformer.transform(src, res);
PdfReader reader = new PdfReader(fopout.toByteArray());
// note that the following line has to be modified to include
propper permissions and passwords.
PdfEncryptor.encrypt(reader,response.getOutputStream(),true,null,null,0);
} else {
PrintWriter out = response.getWriter();
out.println("<html><head><title>Error</title></head><body><h1>FopServletErro
r</h1><h3>request param not given.</body></html>");
}
} catch (Exception ex) {
throw new ServletException(ex);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: fop-user-unsubscribe@xml.apache.org
For additional commands, e-mail: fop-user-help@xml.apache.org