Mercurial > hg > digilib-old
comparison servlet/src/digilib/servlet/PDFCache.java @ 503:fdb824bd57ab digilibPDF
first functional version of PDFCache after restructuring the code
author | cmielack |
---|---|
date | Mon, 02 Mar 2009 17:11:26 +0100 |
parents | b2325b33b77b |
children | 06d7e8c09b11 |
comparison
equal
deleted
inserted
replaced
502:157d4c7d2343 | 503:fdb824bd57ab |
---|---|
1 package digilib.servlet; | 1 package digilib.servlet; |
2 | 2 |
3 import java.io.BufferedInputStream; | |
4 import java.io.File; | |
5 import java.io.FileInputStream; | |
6 import java.io.IOException; | |
3 import java.io.InputStream; | 7 import java.io.InputStream; |
4 | 8 import java.util.HashMap; |
9 | |
10 import javax.servlet.ServletConfig; | |
11 import javax.servlet.ServletContext; | |
12 import javax.servlet.ServletException; | |
13 import javax.servlet.ServletOutputStream; | |
5 import javax.servlet.http.HttpServletRequest; | 14 import javax.servlet.http.HttpServletRequest; |
6 import javax.servlet.http.HttpServletResponse; | 15 import javax.servlet.http.HttpServletResponse; |
7 | 16 |
8 public class PDFCache extends RequestHandler { | 17 public class PDFCache extends RequestHandler { |
9 | 18 |
19 private DigilibConfiguration dlConfig = null; | |
20 | |
21 public static String cache_directory = "cache/"; // TODO set using dlConfig | |
22 | |
23 public static String cache_hash_id = "digilib.servlet.PDFCache"; | |
24 | |
25 HashMap<String,Integer> cache_hash = null; | |
26 | |
10 | 27 |
11 public static Integer STATUS_DONE = 0; // document exists in cache | 28 public static Integer STATUS_DONE = 0; // document exists in cache |
12 | 29 |
13 public static Integer STATUS_WIP = 1; // document is "work in progress" | 30 public static Integer STATUS_WIP = 1; // document is "work in progress" |
14 | 31 |
15 public static Integer STATUS_NONEXISTENT = 2; // document does not exist in cache and is not in progress | 32 public static Integer STATUS_NONEXISTENT = 2; // document does not exist in cache and is not in progress |
16 | 33 |
17 public static Integer STATUS_ERROR = 3; // an error occurred while processing the request | 34 public static Integer STATUS_ERROR = 3; // an error occurred while processing the request |
18 | 35 |
36 public static String version = "0.2"; | |
37 | |
38 // TODO functionality for the pre-generation of complete books/chapters using default values | |
39 // TODO use DLConfig for default values | |
40 // TODO use JSPs for automatically refreshing waiting-pages and download-pages | |
41 // TODO register the PDFCache instance globally and implement getters for cache_dir | |
42 | |
43 | |
44 public void init(ServletConfig config) throws ServletException{ | |
45 | |
46 super.init(config); | |
47 | |
48 | |
49 | |
50 logger.info("initialized PDFCache v."+version); | |
51 | |
52 // create and register hashtable | |
53 ServletContext context = config.getServletContext(); | |
54 | |
55 dlConfig = (DigilibConfiguration) context.getAttribute("digilib.servlet.configuration"); | |
56 | |
57 if (dlConfig == null) { | |
58 // no Configuration | |
59 throw new ServletException("No Configuration!"); | |
60 } | |
61 | |
62 context.setAttribute(cache_hash_id, new HashMap<String,Integer>()); | |
63 | |
64 cache_hash = (HashMap<String,Integer>) context.getAttribute(cache_hash_id); | |
65 | |
66 if (cache_hash==null){ | |
67 cache_hash = new HashMap<String,Integer>(); | |
68 context.setAttribute(cache_hash_id, cache_hash); | |
69 } | |
70 | |
71 // scan the directory | |
72 scanCacheDirectory(); | |
73 | |
74 } | |
75 | |
76 | |
77 public void scanCacheDirectory(){ | |
78 // search the cache-directory for existing files and fill them into the Hashtable as STATUS_DONE | |
79 | |
80 ServletContext context = this.getServletContext(); | |
81 HashMap<String,Integer> cache_hash = (HashMap<String,Integer>) context.getAttribute(cache_hash_id); | |
82 | |
83 File cache_dir = new File(cache_directory); | |
84 String[] cached_files = cache_dir.list(); | |
85 | |
86 | |
87 for (String file: cached_files){ | |
88 String docid = file.substring(0,file.length()-4); | |
89 logger.debug("docid = "+docid); | |
90 if (file.endsWith(".pdf") && !cache_hash.containsKey(docid)){ | |
91 logger.debug("PDFCache reads in "+file); | |
92 cache_hash.put(file, STATUS_DONE); | |
93 } | |
94 } | |
95 } | |
19 | 96 |
20 | 97 |
21 | 98 |
22 @Override | 99 @Override |
23 public void processRequest(HttpServletRequest request, | 100 public void processRequest(HttpServletRequest request, |
24 HttpServletResponse response) { | 101 HttpServletResponse response) { |
25 | 102 |
26 // evaluate request ( make a PDFJobDeclaration , get the DocumentId) | 103 // evaluate request ( make a PDFJobDeclaration , get the DocumentId) |
27 | 104 PDFJobInformation pdfji = new PDFJobInformation(dlConfig); |
28 // check, which state the requested document is in (ready, work in progress, non existent) | 105 pdfji.setWithRequest(request); |
29 | 106 |
30 // if necessary, initialize document generation (and notify the user) | 107 |
31 | 108 String docid = pdfji.getDocumentId(); |
32 // send the document | 109 |
33 } | 110 |
34 | 111 int status = getStatus(docid); |
35 @Override | 112 |
36 public void sendFile(InputStream is, HttpServletResponse response, String filename) { | 113 |
37 | 114 |
38 } | 115 if(status == STATUS_NONEXISTENT){ |
39 | 116 createNewPdfDocument(pdfji, docid); |
40 public String getDocumentId(PDFJobDeclaration jobdeclaration){ | 117 informUser(status, docid, response); |
41 // generate an unambiguous ID from the request (this is used for filenames etc) | 118 } |
42 String id; | 119 else if (status == STATUS_DONE){ |
43 | 120 try { |
44 String fn = jobdeclaration.getAsString("fn"); | 121 sendFile(docid, downloadFilename(pdfji), response); |
45 String dh = jobdeclaration.getAsString("dh"); | 122 } catch (IOException e) { |
46 String pgs = jobdeclaration.getAsString("pgs"); | 123 e.printStackTrace(); |
47 | 124 logger.error(e.getMessage()); |
48 id = "fn=" + fn + "&dh=" + dh + "&pgs=" + pgs + ".pdf"; | 125 } |
49 | 126 } |
50 return id; | 127 else { |
128 informUser(status, docid, response); | |
129 } | |
130 } | |
131 | |
132 | |
133 public void informUser(int status, String documentid, HttpServletResponse response){ | |
134 // depending on the documents status, redirect the user to an appropriate waiting- or download-site | |
135 // TODO | |
136 | |
137 if(status == STATUS_NONEXISTENT){ | |
138 // tell the user that the document has to be created before he/she can download it | |
139 logger.debug("PDFCache: "+documentid+" has STATUS_NONEXISTENT."); | |
140 } | |
141 else if(status == STATUS_WIP){ | |
142 logger.debug("PDFCache: "+documentid+" has STATUS_WIP."); | |
143 | |
144 // estimate remaining work time | |
145 // tell the user he/she has to wait | |
146 } | |
147 else if(status == STATUS_DONE){ | |
148 logger.debug("PDFCache: "+documentid+" has STATUS_DONE."); | |
149 | |
150 // do nothing or refresh | |
151 } | |
152 else { | |
153 logger.debug("PDFCache: "+documentid+" has STATUS_ERROR."); | |
154 | |
155 // status == STATUS_ERROR | |
156 // an error must have occured; show error page | |
157 } | |
158 | |
51 } | 159 } |
52 | 160 |
53 | 161 |
54 /** check the status of the document corresponding to the documentid */ | 162 /** check the status of the document corresponding to the documentid */ |
55 public Integer getStatus(String documentid){ | 163 public Integer getStatus(String documentid){ |
56 | 164 // rescan directory? might be useful if more than one instance uses the same cache directory ; Problem: wip-files occur in the list |
57 | 165 if(cache_hash.containsKey(documentid)) |
58 return 0; | 166 return cache_hash.get(documentid); |
167 else | |
168 return STATUS_NONEXISTENT; | |
169 } | |
170 | |
171 | |
172 public void createNewPdfDocument(PDFJobInformation pdfji, String filename){ | |
173 // start new worker | |
174 PDFMaker pdf_maker = new PDFMaker(dlConfig, cache_hash, pdfji,filename); | |
175 new Thread(pdf_maker, "PDFMaker").start(); | |
176 } | |
177 | |
178 | |
179 public String downloadFilename(PDFJobInformation pdfji){ | |
180 String filename; | |
181 filename = "digilib_"; | |
182 filename += pdfji.getImageJobInformation().getAsString("fn"); | |
183 filename += "_pgs" + pdfji.getAsString("pgs"); | |
184 filename += ".pdf"; | |
185 | |
186 return filename; | |
187 } | |
188 | |
189 /** | |
190 * sends a document to the user | |
191 * | |
192 * @param cachefile The filename of the document in cache. | |
193 * @param filename The filename used for downloading. | |
194 * @param response | |
195 * @throws IOException | |
196 */ | |
197 public void sendFile(String cachefile, String filename, HttpServletResponse response) throws IOException{ | |
198 File cached_file = null; | |
199 FileInputStream fis = null; | |
200 ServletOutputStream sos = null; | |
201 BufferedInputStream bis = null; | |
202 | |
203 try { | |
204 cached_file = new File(cache_directory + cachefile); | |
205 fis = new FileInputStream(cached_file); | |
206 sos = response.getOutputStream(); | |
207 bis = new BufferedInputStream(fis); | |
208 | |
209 int bytes = 0; | |
210 | |
211 response.setContentType("application/pdf"); | |
212 response.addHeader("Content-Disposition", "attachment; filename="+filename); | |
213 response.setContentLength( (int) cached_file.length()); | |
214 | |
215 while ((bytes = bis.read()) != -1){ | |
216 sos.write(bytes); | |
217 } | |
218 } | |
219 catch(Exception e){ | |
220 logger.error(e.getMessage()); | |
221 } | |
222 finally{ | |
223 // close all streams | |
224 if (fis != null) | |
225 fis.close(); | |
226 if (bis != null) | |
227 bis.close(); | |
228 if (sos != null) | |
229 sos.close(); | |
230 } | |
231 | |
59 } | 232 } |
60 } | 233 } |