Mercurial > hg > digilib-old
annotate servlet/src/digilib/servlet/Scaler.java @ 62:1efc2a994f48
Fixed error converting wx in DigilibRequest.
Fixed error when use-authentication=false.
(ROC)
author | robcast |
---|---|
date | Wed, 28 Aug 2002 23:19:55 +0200 |
parents | 8d9a0abf3626 |
children | 5ea1999befd4 |
rev | line source |
---|---|
1 | 1 /* Scaler -- Scaler servlet main class |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) | |
6 | |
7 This program is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Please read license.txt for the full details. A copy of the GPL | |
13 may be found at http://www.gnu.org/copyleft/lgpl.html | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 */ | |
20 | |
21 package digilib.servlet; | |
22 | |
23 import javax.servlet.*; | |
24 import javax.servlet.http.*; | |
25 import java.io.*; | |
26 import java.util.*; | |
27 | |
28 import digilib.*; | |
29 import digilib.io.*; | |
30 import digilib.image.*; | |
31 import digilib.auth.*; | |
32 | |
33 | |
34 //public class Scaler extends HttpServlet implements SingleThreadModel { | |
35 public class Scaler extends HttpServlet { | |
36 | |
37 // Utils instance with debuglevel | |
38 Utils util; | |
39 // ServletOpss instance | |
40 ServletOps servletOp; | |
41 // FileOps instance | |
42 FileOps fileOp; | |
43 // AuthOps instance | |
44 AuthOps authOp; | |
45 // global DocuImage instance (don't reuse inside a request!) | |
46 DocuImage globalImage; | |
47 | |
48 // use authorization database | |
49 boolean useAuthentication = true; | |
50 // image file to send in case of error | |
51 File errorImgFile = new File("/docuserver/images/icons/scalerror.gif"); | |
52 // image file to send if access is denied | |
53 File denyImgFile = new File("/docuserver/images/icons/denied.gif"); | |
54 // base directories in order of preference (prescaled versions first) | |
55 String[] baseDirs = {"/docuserver/scaled/small", "/docuserver/images", "/docuserver/scans/quellen"}; | |
56 | |
57 | |
58 /********************************************************* | |
59 * Initialize global variables | |
60 *********************************************************/ | |
61 public void init(ServletConfig config) throws ServletException { | |
62 super.init(config); | |
63 | |
64 // first we need an Utils to setup ServletOps UGLY!! | |
65 util = new Utils(5); | |
66 // servletOps takes a ServletConfig to get the config file name | |
67 servletOp = new ServletOps(util, config); | |
68 // then we can start reading parameters UGLY!! | |
69 | |
59
8d9a0abf3626
Utils now has a setter for debugLevel. DocumentBean now properly sets
robcast
parents:
56
diff
changeset
|
70 // Utils with new debuglevel |
1 | 71 int debugLevel = servletOp.tryToGetInitParam("debug-level", 10); |
59
8d9a0abf3626
Utils now has a setter for debugLevel. DocumentBean now properly sets
robcast
parents:
56
diff
changeset
|
72 util.setDebugLevel(debugLevel); |
1 | 73 // image file to send in case of error |
74 String errorImgFileName = servletOp.tryToGetInitParam("error-image", "/docuserver/images/icons/scalerror.gif"); | |
75 errorImgFile = new File(errorImgFileName); | |
76 // image file to send if access is denied | |
77 String denyImgFileName = servletOp.tryToGetInitParam("denied-image", "/docuserver/images/icons/denied.gif"); | |
78 denyImgFile = new File(denyImgFileName); | |
79 // base directories in order of preference (prescaled versions first) | |
80 String baseDirList = servletOp.tryToGetInitParam("basedir-list", "/docuserver/scaled/small:/docuserver/images:/docuserver/scans/quellen"); | |
81 // split list into directories | |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
82 baseDirs = servletOp.tryToGetPathArray(baseDirList, baseDirs); |
1 | 83 // use authentication information |
84 String useAuth = servletOp.tryToGetInitParam("use-authorization", "true"); | |
62 | 85 if ((useAuth.indexOf("false") > -1)||(useAuth.indexOf("FALSE") > -1)) { |
1 | 86 useAuthentication = false; |
87 } else { | |
88 useAuthentication = true; | |
89 try { | |
90 // DB version | |
91 //authOp = new DBAuthOpsImpl(util); | |
92 // XML version | |
93 String cnfPath = servletOp.tryToGetInitParam("auth-file", "/docuserver/www/digitallibrary/WEB-INF/digilib-auth.xml"); | |
94 authOp = new XMLAuthOps(util, cnfPath); | |
95 } catch (AuthOpException e) { | |
96 throw new ServletException(e); | |
97 } | |
98 } | |
99 // FileOps instance | |
100 fileOp = new FileOps(util); | |
101 // global DocuImage instance (don't reuse inside a request!) | |
102 globalImage = new JAIDocuImage(util); | |
103 // globalImage = new JIMIDocuImage(util); | |
104 //globalImage = new ImageLoaderDocuImage(util); | |
105 | |
106 } | |
107 | |
108 /**Process the HTTP Get request*/ | |
109 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
110 util.dprintln(1, "The servlet has received a GET!"); | |
111 processRequest(request, response); | |
112 } | |
113 | |
114 /**Process the HTTP Post request*/ | |
115 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
116 util.dprintln(1, "The servlet has received a POST!"); | |
117 processRequest(request, response); | |
118 } | |
119 | |
120 /**Clean up resources*/ | |
121 public void destroy() { | |
122 } | |
123 | |
124 /********************************************************************** | |
125 * main request handler | |
126 **********************************************************************/ | |
127 | |
128 void processRequest(HttpServletRequest request, HttpServletResponse response) | |
129 throws ServletException, IOException { | |
130 | |
131 // time for benchmarking | |
132 long startTime = System.currentTimeMillis(); | |
133 // output mime/type | |
134 String mimeType = "image/png"; | |
135 | |
136 /** | |
137 * parameters for a session | |
138 */ | |
139 | |
140 // scale the image file to fit window size | |
141 boolean scaleToFit = true; | |
142 // use heuristics (GIF?) to scale or not | |
143 boolean forcedScale = false; | |
144 // try prescaled images first | |
145 boolean preScaledFirst = true; | |
146 // interpolation to use for scaling | |
147 int scaleQual = 0; | |
148 // send html error message (or image file) | |
149 boolean errorMsgHtml = false; | |
150 | |
151 /** | |
152 * request parameter | |
153 */ | |
154 | |
155 // file/dir to load | |
156 String param_fn = servletOp.tryToGetParam("fn", "", request); | |
157 // page number | |
158 int param_pn = servletOp.tryToGetParam("pn", 1, request); | |
159 // destination image width | |
160 int param_dw = servletOp.tryToGetParam("dw", 300, request); | |
161 // destination image height | |
162 int param_dh = servletOp.tryToGetParam("dh", 400, request); | |
163 // relative area x_offset (0..1) | |
164 float param_wx = servletOp.tryToGetParam("wx", 0f, request); | |
165 // relative area y_offset | |
166 float param_wy = servletOp.tryToGetParam("wy", 0f, request); | |
167 // relative area width (0..1) | |
168 float param_ww = servletOp.tryToGetParam("ww", 1f, request); | |
169 // relative area height | |
170 float param_wh = servletOp.tryToGetParam("wh", 1f, request); | |
171 // scale factor (additional to dw/width, dh/height) | |
172 float param_ws = servletOp.tryToGetParam("ws", 1f, request); | |
173 // operation mode: flags separated by "+" | |
174 String param_mo = servletOp.tryToGetParam("mo", "", request); | |
175 // operation mode: "fit": always fit to page, "file": send as-is | |
176 if (param_mo.indexOf("fit") >= 0) { | |
177 scaleToFit = true; | |
178 forcedScale = true; | |
179 } else if (param_mo.indexOf("file") >= 0) { | |
180 scaleToFit = false; | |
181 forcedScale = true; | |
182 } | |
183 // operation mode: "errtxt": error message in html, "errimg": error image | |
184 if (param_mo.indexOf("errtxt") >= 0) { | |
185 errorMsgHtml = true; | |
186 } else if (param_mo.indexOf("errimg") >= 0) { | |
187 errorMsgHtml = false; | |
188 } | |
189 // operation mode: "q0" - "q2": interpolation quality | |
190 if (param_mo.indexOf("q0") >= 0) { | |
191 scaleQual = 0; | |
192 } else if (param_mo.indexOf("q1") >= 0) { | |
193 scaleQual = 1; | |
194 } else if (param_mo.indexOf("q2") >= 0) { | |
195 scaleQual = 2; | |
196 } | |
197 // operation mode: "lores": try to use scaled image, "hires": unscaled image | |
198 if (param_mo.indexOf("lores") >= 0) { | |
199 preScaledFirst = true; | |
200 } else if (param_mo.indexOf("hires") >= 0) { | |
201 preScaledFirst = false; | |
202 } | |
203 | |
204 Utils.dprintln(1, "Parameter values: fn:"+param_fn+" pn:"+param_pn+" dw:"+param_dw+" dh:"+param_dh+" wx:"+param_wx+" wy:"+param_wy+" ww:"+param_ww+" wh:"+param_wh+" ws:"+param_ws+" mo:"+param_mo); | |
205 | |
206 //"big" try for all file/image actions | |
207 try { | |
208 | |
209 // DocuImage instance | |
210 DocuImage docuImage = new JAIDocuImage(util); | |
211 // DocuImage docuImage = new JIMIDocuImage(util); | |
212 //DocuImage docuImage = new ImageLoaderDocuImage(util); | |
213 | |
214 | |
215 /** | |
216 * find the file to load/send | |
217 */ | |
218 | |
219 String loadPathName = ""; | |
220 // if there's PathInfo, append | |
221 if (request.getPathInfo() != null) { | |
222 loadPathName += request.getPathInfo(); | |
223 } | |
224 // append fn parameter | |
225 loadPathName += param_fn; | |
226 // if it's zoomed, try hires version (to be optimized...) | |
227 if ((param_ww < 1f) || (param_wh < 1f)) { | |
228 preScaledFirst = false; | |
229 } | |
230 | |
231 if (useAuthentication) { | |
232 // check permissions | |
233 List rolesRequired = authOp.rolesForPath(loadPathName, request); | |
234 if (rolesRequired != null) { | |
235 Utils.dprintln(1, "Role required: "+rolesRequired); | |
236 Utils.dprintln(2, "User: "+request.getRemoteUser()); | |
237 if (! authOp.isRoleAuthorized(rolesRequired, request)) { | |
238 Utils.dprintln(1, "ERROR: access denied!"); | |
239 if (errorMsgHtml) { | |
240 servletOp.htmlMessage("ERROR: Unauthorized access!", response); | |
241 } else { | |
242 docuImage.sendFile(denyImgFile, response); | |
243 } | |
244 return; | |
245 } | |
246 } | |
247 } | |
248 | |
249 // find the file | |
250 File fileToLoad = fileOp.getFileVariant(baseDirs, loadPathName, param_pn, preScaledFirst); | |
251 | |
252 Utils.dprintln(1, "Loading: "+fileToLoad); | |
253 | |
254 // get the source image type (if it's known) | |
255 mimeType = fileOp.mimeForFile(fileToLoad); | |
256 | |
257 // if not forced and source is GIF/PNG then send-as-is if not zoomed | |
258 if((!forcedScale && (mimeType == "image/gif" || mimeType == "image/png") | |
259 && (param_ww == 1f) && (param_wh == 1f)) || (forcedScale && !scaleToFit)) { | |
260 | |
261 Utils.dprintln(1, "Sending File as is."); | |
262 | |
263 docuImage.sendFile(fileToLoad, response); | |
264 | |
265 Utils.dprintln(1, "Done in "+(System.currentTimeMillis()-startTime)+"ms"); | |
266 return; | |
267 } | |
268 | |
269 // load file | |
270 docuImage.loadImage(fileToLoad); | |
271 | |
272 /** | |
273 * crop and scale the image | |
274 */ | |
275 | |
276 // get size | |
277 int imgWidth = docuImage.getWidth(); | |
278 int imgHeight = docuImage.getHeight(); | |
279 | |
280 util.dprintln(2, "IMG: "+imgWidth+"x"+imgHeight); | |
281 util.dprintln(2, "time "+(System.currentTimeMillis()-startTime)+"ms"); | |
282 | |
283 // calculate absolute from relative coordinates | |
284 float areaXoff = param_wx * imgWidth; | |
285 float areaYoff = param_wy * imgHeight; | |
286 float areaWidth = param_ww * imgWidth; | |
287 float areaHeight = param_wh * imgHeight; | |
288 // calculate scaling factors | |
289 float scaleX = param_dw / areaWidth * param_ws; | |
290 float scaleY = param_dh / areaHeight * param_ws; | |
291 float scaleXY = (scaleX > scaleY) ? scaleY : scaleX; | |
292 | |
293 util.dprintln(1, "Scale "+scaleXY+"("+scaleX+","+scaleY+") on "+areaXoff+","+areaYoff+" "+areaWidth+"x"+areaHeight); | |
294 | |
295 // fit area to image | |
296 areaWidth = (areaXoff + areaWidth > imgWidth) ? imgWidth - areaXoff : areaWidth; | |
297 areaHeight = (areaYoff + areaHeight > imgHeight) ? imgHeight - areaYoff : areaHeight; | |
298 | |
299 util.dprintln(2, "cropped: "+areaXoff+","+areaYoff+" "+areaWidth+"x"+areaHeight); | |
300 | |
301 // check image parameters | |
302 if ((areaWidth < 1)||(areaHeight < 1) | |
303 ||(scaleXY * areaWidth < 2)||(scaleXY * areaHeight < 2)) { | |
304 Utils.dprintln(1, "ERROR: invalid scale parameter set!"); | |
305 throw new ImageOpException("Invalid scale parameter set!"); | |
306 } | |
307 | |
308 // crop and scale image | |
309 docuImage.cropAndScale((int)areaXoff, (int)areaYoff, (int)areaWidth, (int)areaHeight, | |
310 scaleXY, scaleQual); | |
311 | |
312 util.dprintln(2, "time "+(System.currentTimeMillis()-startTime)+"ms"); | |
313 | |
314 /** | |
315 * write the resulting image | |
316 */ | |
317 | |
318 // setup output -- if source is JPG then dest will be JPG else it's PNG | |
319 if (mimeType != "image/jpeg") { | |
320 mimeType="image/png"; | |
321 } | |
322 | |
323 // write the image | |
324 docuImage.writeImage(mimeType, response); | |
325 | |
326 util.dprintln(1, "Done in "+(System.currentTimeMillis()-startTime)+"ms"); | |
327 | |
328 /** | |
329 * error handling | |
330 */ | |
331 | |
332 }//"big" try | |
333 catch (FileOpException e) { | |
334 util.dprintln(1, "ERROR: File IO Error: "+e); | |
335 try { | |
336 if (errorMsgHtml) { | |
337 servletOp.htmlMessage("ERROR: File IO Error: "+e, response); | |
338 } else { | |
339 globalImage.sendFile(errorImgFile, response); | |
340 } | |
341 } catch (FileOpException ex) {} // so we don't get a loop | |
342 return; | |
343 } | |
344 catch (AuthOpException e) { | |
345 Utils.dprintln(1, "ERROR: Authorization error: "+e); | |
346 try { | |
347 if (errorMsgHtml) { | |
348 servletOp.htmlMessage("ERROR: Authorization error: "+e, response); | |
349 } else { | |
350 globalImage.sendFile(errorImgFile, response); | |
351 } | |
352 } catch (FileOpException ex) {} // so we don't get a loop | |
353 return; | |
354 } | |
355 catch (ImageOpException e) { | |
356 Utils.dprintln(1, "ERROR: Image Error: "+e); | |
357 try { | |
358 if (errorMsgHtml) { | |
359 servletOp.htmlMessage("ERROR: Image Operation Error: "+e, response); | |
360 } else { | |
361 globalImage.sendFile(errorImgFile, response); | |
362 } | |
363 } catch (FileOpException ex) {} // so we don't get a loop | |
364 return; | |
365 } | |
366 | |
367 } | |
368 | |
369 }//Scaler class |