Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

FileDownloader.java

Go to the documentation of this file.
00001 package cedar.jetweb.servlet;
00002 
00003 import java.util.HashMap;
00004 
00005 import javax.servlet.*;
00006 import javax.servlet.http.*;
00007 import java.io.*;
00008 
00009 import cedar.jetweb.*;
00010 import cedar.jetweb.model.*;
00011 import cedar.jetweb.html.*;
00012 import cedar.jetweb.generator.Generator;
00013 
00014 import org.apache.tools.tar.*;
00015 import org.apache.tools.ant.taskdefs.Tar.TarFileSet;
00016 import org.apache.tools.ant.taskdefs.Tar;
00017 
00018 
00023 public class FileDownloader extends HttpServlet {
00024 
00025     private String destinationDirName;
00026     private File destinationDir;
00027 
00028     private PrintWriter out;
00029     private int paperId;
00030 
00031     public void init(ServletConfig config) throws ServletException {
00032     super.init(config);
00033     // Set up configuration with servlet mode on. 
00034     JetWebConfig.init();
00035     }
00036 
00041     protected void doPost(HttpServletRequest request, 
00042               HttpServletResponse response)
00043     throws ServletException, java.io.IOException {
00044         download(request,response);
00045     }
00046 
00051     protected void doGet(HttpServletRequest request, 
00052               HttpServletResponse response)
00053     throws ServletException, java.io.IOException {
00054         download(request,response);
00055     }
00056 
00057 
00062     public void download(HttpServletRequest request, HttpServletResponse res) {
00063       
00064     String tarRoot;
00065     File   tarDir;
00066 
00067     // Could test on the servlet request here and make different tarballs as required.
00068 
00069     try{
00070 
00071         if (true) {
00072         tarRoot = "HZSteerCards";
00073         tarDir = makeCards(request,tarRoot);
00074         }
00075 
00076         // Set the headers.
00077         res.setContentType("application/x-download");
00078         res.setHeader("Content-Disposition", "attachment; filename=" + tarRoot+".tar");     
00079 
00080         // Send the file.
00081         OutputStream out = res.getOutputStream();
00082         TarOutputStream tos = new TarOutputStream(out);
00083 
00084         TarEntry te = new TarEntry(tarDir);
00085         for (TarEntry newte : te.getDirectoryEntries()){
00086         for (TarEntry newte2 : newte.getDirectoryEntries()){
00087 
00088             if (!newte2.isDirectory()) {
00089             TarEntry go = new TarEntry(tarRoot+"/"
00090                            +newte2.getFile().getParentFile().getName()
00091                            +"/"+newte2.getFile().getName());
00092             go.setSize(newte2.getFile().length());
00093             tos.putNextEntry(go);
00094             //System.out.println("Loop1 "+go.getName());
00095             FileInputStream fIn = new FileInputStream(newte2.getFile());
00096             byte[] buffer = new byte[8 * 1024];
00097             int count = 0;
00098             System.out.println(go.getName());
00099             do {
00100                 tos.write(buffer, 0, count);
00101                 count = fIn.read(buffer, 0, buffer.length);
00102             } while (count != -1);
00103             tos.closeEntry();           
00104             }
00105         }
00106         }
00107 
00108         tos.close();
00109 
00110     } catch (Exception e) {
00111         try {
00112         res.setContentType("text/html");
00113         res.setHeader("Content-Disposition","");        
00114         e.printStackTrace(System.out);
00115         e.printStackTrace(res.getWriter());
00116         HTMLErrorWriter.write(res.getWriter(),e);
00117         } catch (IOException io) {
00118         System.out.println(io);
00119         }
00120     }
00121     }
00122 
00123     // Build a tarball of card files based upon the request.
00124     private static File makeCards(HttpServletRequest request, String tarRoot) 
00125     throws IOException, JetWebException {
00126 
00127     File workDir = File.createTempFile(tarRoot,"");
00128     workDir.delete();
00129     workDir.mkdir();
00130     
00131     // loop over all known generators
00132     for (String name : Generator.allNames()){
00133         
00134         // make a directory called name-version
00135         for (String version : Generator.allVersions(name)){
00136         
00137         File genDir = new File(workDir.getPath(),name+"-"+version);
00138         genDir.mkdir();
00139         
00140         Model model = new Model();
00141         model.getProtonPDFList().add(JetWebConfig.defaultProtonPDF);
00142         model.getPhotonPDFList().add(JetWebConfig.defaultPhotonPDF);
00143         
00144         Generator gen = Generator.Maker(name,version);
00145         gen.setSoftDefaults();
00146         
00147         // loop over all known valid processtypes and write a card file
00148         for (MCProcessType proc: MCProcessType.getAll(true)){
00149             String cardName = genDir.getPath()+"/"+proc+".input";
00150             
00151             BufferedWriter cards = 
00152             new BufferedWriter(new FileWriter(cardName) );
00153             gen.writeSteering(cards,model,proc);
00154             cards.newLine();
00155             cards.close();
00156         }
00157         }
00158     }
00159     // end loops
00160     return workDir;
00161     }
00162 }
00163 
00164 
00165                 

Generated Wed Jan 17 09:14:27 GMT 2007