Mercurial > hg > digilib-old
annotate servlet/src/digilib/servlet/PDFCache.java @ 499:87d2ed21bdec Root_digilibPDF
changed generation of documentID in PDFCache
author | cmielack |
---|---|
date | Fri, 13 Feb 2009 11:08:19 +0100 |
parents | 86b5589f38db |
children |
rev | line source |
---|---|
495 | 1 package digilib.servlet; |
2 | |
3 import java.io.BufferedInputStream; | |
4 import java.io.File; | |
5 import java.io.FileInputStream; | |
6 import java.io.IOException; | |
7 import java.util.Hashtable; | |
8 | |
9 import javax.servlet.ServletConfig; | |
10 import javax.servlet.ServletContext; | |
11 import javax.servlet.ServletException; | |
12 import javax.servlet.ServletOutputStream; | |
13 import javax.servlet.http.HttpServlet; | |
14 import javax.servlet.http.HttpServletRequest; | |
15 import javax.servlet.http.HttpServletResponse; | |
16 | |
17 import org.apache.log4j.Logger; | |
18 | |
19 | |
20 /** | |
21 * PDF request manager with caching capabilities. | |
22 * This is a very early and dirty but functional version and will | |
23 * (hopefully soon) be replaced by a parallelized and more sophisticated one. | |
24 * | |
25 * @author cmielack | |
26 * | |
27 */ | |
28 | |
29 public class PDFCache extends HttpServlet { | |
30 | |
31 private static final long serialVersionUID = 1L; | |
32 | |
33 | |
34 private static int STATUS_NONEXISTENT = 0; // the document does not exist and is not under construction | |
35 private static int STATUS_DONE = 1; // the document exists and can be downloaded | |
36 private static int STATUS_PENDING = 2; // the document is "under construction" | |
37 private static int STATUS_ERROR = 3; // an error occurred while processing the request | |
38 | |
39 private static String cache_directory = "cache/"; // the path (relative to the tomcat base-directory) where cached | |
40 // files are stored | |
41 | |
42 private static String cache_hash_id = "digilib.servlet.PDFCache"; | |
43 | |
44 | |
45 private static Logger logger = Logger.getLogger("digilib.servlet"); | |
46 | |
47 | |
48 public void init(ServletConfig config){ | |
49 // initialize the PDFCache | |
50 | |
51 logger.debug("Initializing PDFCache"); | |
52 | |
53 try { | |
54 super.init(config); | |
55 } catch (ServletException e) { | |
56 e.printStackTrace(); | |
57 } | |
58 | |
59 ServletContext context = this.getServletContext(); | |
60 | |
61 context.setAttribute(cache_hash_id, new Hashtable<String,Integer>()); | |
62 | |
63 Hashtable<String,Integer> cache_hash = (Hashtable<String,Integer>) context.getAttribute(cache_hash_id); | |
64 | |
65 if (cache_hash==null){ | |
66 cache_hash = new Hashtable<String,Integer>(); | |
67 context.setAttribute(cache_hash_id, cache_hash); | |
68 } | |
69 | |
70 // search the cache-directory for existing files and fill them into the Hashtable as STATUS_DONE | |
71 File cache_dir = new File(cache_directory); | |
72 String[] cached_files = cache_dir.list(); | |
73 | |
74 | |
75 for (String file: cached_files){ | |
76 if (file.endsWith(".pdf")){ | |
77 logger.debug("cache found "+file); | |
78 cache_hash.put(file, 1); | |
79 } | |
80 } | |
81 | |
82 } | |
83 | |
84 | |
85 public String getDocumentId(HttpServletRequest request){ | |
86 // generate an unambiguous ID from the request (this is used for filenames etc) | |
87 String id; | |
499 | 88 |
89 String fn = request.getParameter("fn"); | |
90 String dh = request.getParameter("dh"); | |
91 String pgs = request.getParameter("pgs"); | |
495 | 92 |
499 | 93 id = "fn=" + fn + "&dh=" + dh + "&pgs=" + pgs + ".pdf"; |
495 | 94 |
95 return id; | |
96 } | |
97 | |
98 | |
99 public void doGet(HttpServletRequest request, HttpServletResponse response){ | |
100 | |
101 // get the status of the Document specified by the request ... | |
102 int status = this.getStatus(request); | |
103 | |
104 | |
105 if (status == STATUS_DONE) { | |
106 // ... and if the file already exists, send it ... | |
107 try { | |
108 sendFile(request,response); | |
498
86b5589f38db
added a sendRedirect after the creation of a PDF so that the user that requested the document gets to download it upon completion
cmielack
parents:
497
diff
changeset
|
109 |
495 | 110 } catch (IOException e) { |
111 // TODO Auto-generated catch block | |
112 e.printStackTrace(); | |
113 } | |
114 } | |
115 | |
116 else if (status == STATUS_PENDING){ | |
117 // ... if it is in the works, notify the user about it ... | |
118 } | |
119 else if (status == STATUS_NONEXISTENT){ | |
120 // ... or else, generate the file and inform the user about the estimated generation-time | |
121 try { | |
498
86b5589f38db
added a sendRedirect after the creation of a PDF so that the user that requested the document gets to download it upon completion
cmielack
parents:
497
diff
changeset
|
122 createFile(request, response); |
499 | 123 response.sendRedirect(request.getRequestURI()+'?'+request.getQueryString()+"&done=true"); // refresh the browser after finishing the file |
495 | 124 } catch (ServletException e) { |
125 // TODO Auto-generated catch block | |
126 e.printStackTrace(); | |
127 } catch (IOException e) { | |
128 // TODO Auto-generated catch block | |
129 e.printStackTrace(); | |
498
86b5589f38db
added a sendRedirect after the creation of a PDF so that the user that requested the document gets to download it upon completion
cmielack
parents:
497
diff
changeset
|
130 } catch (InterruptedException e) { |
86b5589f38db
added a sendRedirect after the creation of a PDF so that the user that requested the document gets to download it upon completion
cmielack
parents:
497
diff
changeset
|
131 // TODO Auto-generated catch block |
86b5589f38db
added a sendRedirect after the creation of a PDF so that the user that requested the document gets to download it upon completion
cmielack
parents:
497
diff
changeset
|
132 e.printStackTrace(); |
495 | 133 } |
134 } | |
135 else { | |
136 // if an error occurred ... | |
137 } | |
138 } | |
139 | |
140 public int getStatus(HttpServletRequest request){ | |
141 | |
142 String documentId = getDocumentId(request); | |
143 | |
144 ServletContext context = this.getServletContext(); | |
145 Hashtable<String,Integer> documentStatus = (Hashtable<String,Integer>) context.getAttribute(cache_hash_id); | |
146 | |
147 int status = STATUS_NONEXISTENT; | |
148 | |
149 if (documentStatus.containsKey(documentId)){ | |
150 status = documentStatus.get(documentId); | |
151 } | |
152 | |
153 return status; | |
154 } | |
155 | |
156 public void sendFile(HttpServletRequest request, HttpServletResponse response) throws IOException{ | |
157 // send the file specified by the request to the response | |
158 | |
159 String filename = getDocumentId(request); | |
160 File cached_file = null; | |
161 FileInputStream fis = null; | |
162 ServletOutputStream sos = null; | |
163 BufferedInputStream bis = null; | |
164 | |
165 try { | |
166 cached_file = new File(this.cache_directory + filename); | |
167 fis = new FileInputStream(cached_file); | |
168 sos = response.getOutputStream(); | |
169 bis = new BufferedInputStream(fis); | |
170 int bytes = 0; | |
171 | |
497 | 172 String fn = request.getParameter("fn"); |
173 String pgs = request.getParameter("pgs"); | |
174 | |
495 | 175 response.setContentType("application/pdf"); |
497 | 176 response.addHeader("Content-Disposition", "attachment; filename="+fn+"_"+pgs+".pdf"); |
495 | 177 response.setContentLength( (int) cached_file.length()); |
178 | |
179 logger.debug("Sending document "+filename+" to the user."); | |
180 | |
181 while ((bytes = bis.read()) != -1){ | |
182 sos.write(bytes); | |
183 } | |
184 } | |
185 catch(Exception e){ | |
186 logger.error(e.getMessage()); | |
187 } | |
188 finally{ | |
189 // close all streams | |
190 if (fis != null) | |
191 fis.close(); | |
192 if (bis != null) | |
193 bis.close(); | |
194 if (sos != null) | |
195 sos.close(); | |
196 } | |
197 } | |
198 | |
498
86b5589f38db
added a sendRedirect after the creation of a PDF so that the user that requested the document gets to download it upon completion
cmielack
parents:
497
diff
changeset
|
199 public void createFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InterruptedException{ |
495 | 200 // use MakePDF to generate a new Document and put it into the cache-directory |
201 | |
202 // get global instance of MakePDF | |
203 MakePDF mpdf = (MakePDF) this.getServletContext().getAttribute("digilib.servlet.MakePDF"); | |
204 | |
205 | |
206 if (mpdf==null){ | |
207 mpdf = new MakePDF(); | |
208 logger.debug("didn't find MakePDF-Object"); | |
209 } | |
210 | |
211 String filename = this.cache_directory + this.getDocumentId(request); | |
212 | |
213 logger.debug("createFile is going to create file "+filename); | |
214 | |
215 mpdf.doCreate(request,response,filename); // set the parameters and ... | |
216 mpdf.run(); // ... start generating the pdf | |
498
86b5589f38db
added a sendRedirect after the creation of a PDF so that the user that requested the document gets to download it upon completion
cmielack
parents:
497
diff
changeset
|
217 |
495 | 218 //new Thread(mpdf,"MakePDF").start(); |
219 } | |
220 } |