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