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