Mercurial > hg > digilib-old
annotate servlet/src/digilib/servlet/Scaler.java @ 531:9cedd170b581 digilibPDF
* PDF generation works now even with subdirectories
* genericsification and clean up
author | robcast |
---|---|
date | Fri, 05 Feb 2010 20:58:38 +0100 |
parents | 30a3d828a98c |
children | e758a49258e8 |
rev | line source |
---|---|
1 | 1 package digilib.servlet; |
2 | |
500 | 3 import java.io.File; |
4 import java.io.IOException; | |
5 import java.io.OutputStream; | |
6 import java.util.List; | |
7 | |
8 import javax.servlet.ServletConfig; | |
9 import javax.servlet.ServletContext; | |
10 import javax.servlet.ServletException; | |
11 import javax.servlet.http.HttpServletRequest; | |
12 import javax.servlet.http.HttpServletResponse; | |
1 | 13 |
500 | 14 import digilib.auth.AuthOpException; |
15 import digilib.auth.AuthOps; | |
16 import digilib.image.ImageOpException; | |
17 import digilib.io.DocuDirCache; | |
503
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
18 import digilib.io.DocuDirectory; |
500 | 19 import digilib.io.DocuDirent; |
502 | 20 import digilib.io.FileOpException; |
500 | 21 import digilib.io.FileOps; |
22 import digilib.io.ImageFile; | |
1 | 23 |
511 | 24 |
25 // TODO digilibError is not used anymore and may need to get reintegrated | |
26 | |
500 | 27 public class Scaler extends RequestHandler { |
28 | |
29 /** digilib servlet version (for all components) */ | |
527 | 30 public static final String dlVersion = "1.8.0a"; |
1 | 31 |
500 | 32 /** general error code */ |
33 public static final int ERROR_UNKNOWN = 0; | |
34 | |
35 /** error code for authentication error */ | |
36 public static final int ERROR_AUTH = 1; | |
37 | |
38 /** error code for file operation error */ | |
39 public static final int ERROR_FILE = 2; | |
40 | |
41 /** error code for image operation error */ | |
42 public static final int ERROR_IMAGE = 3; | |
43 | |
44 /** DocuDirCache instance */ | |
45 DocuDirCache dirCache; | |
1 | 46 |
500 | 47 /** authentication error image file */ |
48 File denyImgFile; | |
1 | 49 |
500 | 50 /** image error image file */ |
51 File errorImgFile; | |
52 | |
53 /** not found error image file */ | |
54 File notfoundImgFile; | |
1 | 55 |
500 | 56 /** subsampling before scaling */ |
57 float minSubsample = 2f; | |
1 | 58 |
500 | 59 /** send files as is? */ |
60 boolean sendFileAllowed = true; | |
61 | |
62 /** default scaling quality */ | |
63 int defaultQuality = 1; | |
1 | 64 |
500 | 65 /** DigilibConfiguration instance */ |
66 DigilibConfiguration dlConfig; | |
67 | |
68 /** use authorization database */ | |
69 boolean useAuthorization = true; | |
70 | |
71 /** AuthOps instance */ | |
72 AuthOps authOp; | |
73 | |
74 // EXPRIMENTAL | |
75 /** try to enlarge cropping area for "oblique" angles */ | |
76 boolean wholeRotArea = false; | |
1 | 77 |
503
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
78 |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
79 protected long getLastModified(HttpServletRequest request) { |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
80 accountlog.debug("GetLastModified from " + request.getRemoteAddr() |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
81 + " for " + request.getQueryString()); |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
82 long mtime = -1; |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
83 // create new request with defaults |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
84 DigilibRequest dlReq = new DigilibRequest(); |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
85 // set with request parameters |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
86 dlReq.setWithRequest(request); |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
87 // find the requested file |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
88 DocuDirent f = findFile(dlReq); |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
89 if (f != null) { |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
90 DocuDirectory dd = (DocuDirectory) f.getParent(); |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
91 mtime = dd.getDirMTime() / 1000 * 1000; |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
92 } |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
93 return mtime; |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
94 } |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
95 /** |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
96 * Returns the DocuDirent corresponding to the DigilibRequest. |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
97 * |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
98 * @param dlRequest |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
99 * @return |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
100 */ |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
101 public DocuDirent findFile(DigilibRequest dlRequest) { |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
102 // find the file(set) |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
103 DocuDirent f = dirCache.getFile(dlRequest.getFilePath(), dlRequest |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
104 .getAsInt("pn"), FileOps.CLASS_IMAGE); |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
105 return f; |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
106 } |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
107 |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
108 |
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
109 |
500 | 110 /** |
111 * Initialisation on first run. | |
503
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
112 * @throws ServletException |
500 | 113 * |
114 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) | |
115 */ | |
116 public void init(ServletConfig config) throws ServletException { | |
117 super.init(config); | |
118 | |
119 System.out | |
120 .println("***** Digital Image Library Image Scaler Servlet (version " | |
121 + dlVersion + ") *****"); | |
122 // say hello in the log file | |
123 logger | |
124 .info("***** Digital Image Library Image Scaler Servlet (version " | |
125 + dlVersion + ") *****"); | |
1 | 126 |
500 | 127 // get our ServletContext |
128 ServletContext context = config.getServletContext(); | |
129 // see if there is a Configuration instance | |
130 dlConfig = (DigilibConfiguration) context | |
131 .getAttribute("digilib.servlet.configuration"); | |
132 if (dlConfig == null) { | |
133 // no Configuration | |
134 throw new ServletException("No Configuration!"); | |
135 } | |
136 // set our AuthOps | |
137 useAuthorization = dlConfig.getAsBoolean("use-authorization"); | |
138 authOp = (AuthOps) dlConfig.getValue("servlet.auth.op"); | |
1 | 139 |
500 | 140 // DocuDirCache instance |
141 dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); | |
142 denyImgFile = ServletOps.getFile((File) dlConfig.getValue("denied-image"), config); | |
143 errorImgFile = ServletOps.getFile((File) dlConfig.getValue("error-image"), config); | |
144 notfoundImgFile = ServletOps.getFile((File) dlConfig.getValue("notfound-image"), config); | |
145 sendFileAllowed = dlConfig.getAsBoolean("sendfile-allowed"); | |
146 minSubsample = dlConfig.getAsFloat("subsample-minimum"); | |
147 defaultQuality = dlConfig.getAsInt("default-quality"); | |
148 } | |
1 | 149 |
511 | 150 |
151 | |
152 | |
500 | 153 @Override |
154 public void processRequest(HttpServletRequest request, | |
155 HttpServletResponse response) throws ServletException, ImageOpException { | |
1 | 156 |
500 | 157 |
158 if (dlConfig == null) { | |
159 throw new ServletException("ERROR: No Configuration!"); | |
160 } | |
502 | 161 |
500 | 162 accountlog.debug("request: " + request.getQueryString()); |
163 logger.debug("request: " + request.getQueryString()); | |
164 | |
165 | |
166 // define the job information | |
503
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
167 ImageJobInformation jobdeclaration = new ImageJobInformation(dlConfig); |
500 | 168 jobdeclaration.setWithRequest(request); |
169 | |
502 | 170 ImageFile fileToLoad = null; |
171 try { | |
172 fileToLoad = jobdeclaration.get_fileToLoad(); | |
173 } catch (IOException e2) { | |
174 // TODO Auto-generated catch block | |
175 e2.printStackTrace(); | |
176 return; | |
177 } | |
500 | 178 |
179 | |
502 | 180 // if requested, send image as a file |
181 if(sendFileAllowed && jobdeclaration.checkSendAsFile()){ | |
182 String mt = null; | |
183 if (jobdeclaration.hasOption("mo", "rawfile")) { | |
184 mt = "application/octet-stream"; | |
185 } | |
186 logger.debug("Sending RAW File as is."); | |
187 try { | |
188 ServletOps.sendFile(fileToLoad.getFile(), mt, response); | |
189 } catch (FileOpException e) { | |
190 e.printStackTrace(); | |
191 } | |
192 | |
193 return; | |
194 } | |
195 | |
500 | 196 |
197 | |
502 | 198 // if possible, send the image without actually having to transform it |
199 if(jobdeclaration.noTransformRequired()){ | |
200 logger.debug("Sending File as is."); | |
201 | |
202 try { | |
203 ServletOps.sendFile(fileToLoad.getFile(), null, response); | |
204 } catch (FileOpException e) { | |
205 e.printStackTrace(); | |
206 } | |
207 | |
208 //logger.info("Done in " | |
209 // + (System.currentTimeMillis() - startTime) + "ms"); | |
210 return; | |
211 } | |
212 | |
213 | |
214 | |
500 | 215 |
216 OutputStream outputstream = null; | |
217 try { | |
218 outputstream = response.getOutputStream(); | |
219 } catch (IOException e1) { | |
220 // TODO Auto-generated catch block | |
221 e1.printStackTrace(); | |
222 logger.error(e1.getMessage()); | |
223 } | |
502 | 224 |
225 if (! DigilibWorker.canRun()) { | |
226 logger.error("Servlet overloaded!"); | |
227 try { | |
228 response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); | |
229 } catch (IOException e) { | |
230 e.printStackTrace(); | |
231 } | |
232 return; | |
233 } | |
234 | |
235 | |
500 | 236 DigilibWorker job=null; |
237 try { | |
238 | |
239 long startTime = System.currentTimeMillis(); | |
1 | 240 |
501 | 241 /* check permissions */ |
242 if (useAuthorization) { | |
243 // get a list of required roles (empty if no restrictions) | |
531
9cedd170b581
* PDF generation works now even with subdirectories
robcast
parents:
527
diff
changeset
|
244 List<String> rolesRequired; |
501 | 245 try { |
246 rolesRequired = authOp.rolesForPath(jobdeclaration.getFilePath(), request); | |
247 if (rolesRequired != null) { | |
248 authlog.debug("Role required: " + rolesRequired); | |
249 authlog.debug("User: " + request.getRemoteUser()); | |
250 // is the current request/user authorized? | |
251 if (!authOp.isRoleAuthorized(rolesRequired, request)) { | |
252 // send deny answer and abort | |
253 throw new AuthOpException(); | |
254 } | |
255 } | |
256 | |
257 } catch (AuthOpException e) { | |
258 // TODO Auto-generated catch block | |
259 e.printStackTrace(); | |
260 } | |
261 } | |
262 | |
500 | 263 |
503
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
264 job = new DigilibImageWorker(dlConfig, outputstream , jobdeclaration); |
1 | 265 |
500 | 266 job.run(); |
1 | 267 |
503
fdb824bd57ab
first functional version of PDFCache after restructuring the code
cmielack
parents:
502
diff
changeset
|
268 |
500 | 269 if (job.hasError()) { |
270 throw new ImageOpException(job.getError().toString()); | |
271 } | |
1 | 272 |
500 | 273 try { |
274 outputstream.flush(); | |
275 logger.debug("Job Processing Time: "+ (System.currentTimeMillis()-startTime) + "ms"); | |
276 } catch (IOException e) { | |
277 e.printStackTrace(); | |
278 logger.error(e.getMessage()); | |
279 response.sendError(1); | |
280 } | |
1 | 281 |
500 | 282 |
283 } catch (IOException e) { | |
284 e.printStackTrace(); | |
285 logger.error(e.getClass()+": "+ e.getMessage()); | |
286 //response.sendError(1); | |
287 } catch (ImageOpException e) { | |
288 e.printStackTrace(); | |
289 logger.error(e.getClass()+": "+ e.getMessage()); | |
290 //response.sendError(1); | |
291 } | |
1 | 292 |
500 | 293 |
511 | 294 /* boolean errorMsgHtml = false; |
500 | 295 |
296 if(jobdeclaration.hasOption("mo","errtxt")){ | |
297 errorMsgHtml = true; | |
298 } else if (jobdeclaration.hasOption("mo","errimg")) { | |
299 errorMsgHtml = true; | |
511 | 300 } */ |
500 | 301 |
302 } | |
303 | |
304 | |
305 /** | |
306 * Sends an error to the client as text or image. | |
307 * | |
308 * @param asHTML | |
309 * @param type | |
310 * @param msg | |
311 * @param response | |
312 */ | |
313 public void digilibError(boolean asHTML, int type, String msg, | |
314 HttpServletResponse response) { | |
315 try { | |
316 File img = null; | |
317 if (type == ERROR_AUTH) { | |
318 if (msg == null) { | |
319 msg = "ERROR: Unauthorized access!"; | |
320 } | |
321 img = denyImgFile; | |
322 } else if (type == ERROR_FILE) { | |
323 if (msg == null) { | |
324 msg = "ERROR: Image file not found!"; | |
325 } | |
326 img = notfoundImgFile; | |
327 } else { | |
328 if (msg == null) { | |
329 msg = "ERROR: Other image error!"; | |
330 } | |
331 img = this.errorImgFile; | |
332 } | |
333 if (asHTML && (img != null)) { | |
334 ServletOps.htmlMessage(msg, response); | |
335 } else { | |
336 ServletOps.sendFile(img, null, response); | |
337 } | |
338 } catch (IOException e) { | |
339 logger.error("Error sending error!", e); | |
340 } | |
1 | 341 |
500 | 342 } |
1 | 343 |
500 | 344 public static String getVersion(){ |
345 return dlVersion; | |
346 } | |
347 | |
1 | 348 |
500 | 349 } |