001package bradleyross.j2ee.servlets;
002import java.io.IOException;
003import java.io.StringWriter;
004import javax.servlet.http.HttpServletRequest;
005import javax.servlet.http.HttpServletResponse;
006import javax.servlet.ServletOutputStream;
007import bradleyross.j2ee.servlets.Servlet;
008import bradleyross.library.helpers.GenericPrinter;
009/**
010 * Superclass for classes downloading files from the server.
011 * 
012 * @author Bradley Ross
013 *
014 */
015public class DownloadItem extends Servlet 
016{
017        /**
018         * Added to satisfy serializable interface.
019         */
020        private static final long serialVersionUID = 1L;
021        /**
022         * This method retrieves the item to be downloaded.
023         * 
024         * <p>This method will be overridden to produce meaningful classes.</p>
025         * 
026         * @param thisPage Object containing information on this HTTP transaction
027         * @throws IOException if io errors
028         */
029        protected  void processor(ThisPage thisPage) throws IOException
030        {
031                StringWriter sw = new StringWriter();
032                GenericPrinter writer = new GenericPrinter(sw);
033                writer.println("This is a test of bradleyross.library.servlets.DownloadItem.");
034                writer.println("This page is produced by the method processor(ThisPage). ");
035                writer.println("The processor method would be subclassed to produce a useful page.");
036                thisPage.setMimeType("text/plain");
037                thisPage.setFileName("test.txt");
038                byte[] result = sw.toString().getBytes();
039                writer.close();
040                thisPage.setContents(result);
041        }
042        /**
043         * This method is called by the application server to service
044         * the HTTP transaction.
045         * 
046         * @param req HTTP request object
047         * @param res HTTP response object
048         * @throws IOException if io errors
049         */
050        public void service (HttpServletRequest req,
051                        HttpServletResponse res)
052        throws IOException
053        {
054                Servlet.ThisPage thisPage = new Servlet.ThisPage(req, res, getConfig());
055                starter(thisPage);
056                if (thisPage.getTerminateRequest())
057                {
058                        return;
059                }
060                processor(thisPage);
061                if (thisPage.getTerminateRequest())
062                {
063                        return;
064                }
065                ender(thisPage);
066                if (thisPage.getTerminateRequest())
067                {
068                        return;
069                }
070                if (thisPage.getContents() == null)
071                {
072                        thisPage.addMessage("There is no information to download");
073                        thisPage.errorMessage();
074                        return;
075                }
076                res.setContentType(thisPage.getMimeType());
077                String tempFilename = thisPage.getFileName();
078                if (tempFilename == null)
079                { ; }
080                else if (tempFilename.trim().length() == 0)
081                { ; }
082                else if (thisPage.getMode() == ThisPage.DOWNLOAD)
083                {
084                        res.setHeader("Content-Disposition", "attachment;filename=" + tempFilename);
085                }
086                else
087                {
088                        res.setHeader("Content-Disposition", "filename=" + tempFilename);
089                }
090                ServletOutputStream output = res.getOutputStream();
091                output.write(thisPage.getContents());
092        }
093}