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
|
|
70 // Utils instance with debuglevel
|
|
71 int debugLevel = servletOp.tryToGetInitParam("debug-level", 10);
|
|
72 util = new Utils(debugLevel);
|
|
73 // reset Util for ServletOps instance
|
|
74 servletOp.setUtils(util);
|
|
75 // image file to send in case of error
|
|
76 String errorImgFileName = servletOp.tryToGetInitParam("error-image", "/docuserver/images/icons/scalerror.gif");
|
|
77 errorImgFile = new File(errorImgFileName);
|
|
78 // image file to send if access is denied
|
|
79 String denyImgFileName = servletOp.tryToGetInitParam("denied-image", "/docuserver/images/icons/denied.gif");
|
|
80 denyImgFile = new File(denyImgFileName);
|
|
81 // base directories in order of preference (prescaled versions first)
|
|
82 String baseDirList = servletOp.tryToGetInitParam("basedir-list", "/docuserver/scaled/small:/docuserver/images:/docuserver/scans/quellen");
|
|
83 // split list into directories
|
|
84 StringTokenizer dirs = new StringTokenizer(baseDirList, ":");
|
|
85 int n = dirs.countTokens();
|
|
86 // add directories into array
|
|
87 baseDirs = new String[n];
|
|
88 for (int i = 0; i < n; i++) {
|
|
89 baseDirs[i] = dirs.nextToken();
|
|
90 }
|
|
91 // use authentication information
|
|
92 String useAuth = servletOp.tryToGetInitParam("use-authorization", "true");
|
|
93 if ((useAuth.indexOf("false") > 0)||(useAuth.indexOf("FALSE") > 0)) {
|
|
94 useAuthentication = false;
|
|
95 } else {
|
|
96 useAuthentication = true;
|
|
97 try {
|
|
98 // DB version
|
|
99 //authOp = new DBAuthOpsImpl(util);
|
|
100 // XML version
|
|
101 String cnfPath = servletOp.tryToGetInitParam("auth-file", "/docuserver/www/digitallibrary/WEB-INF/digilib-auth.xml");
|
|
102 authOp = new XMLAuthOps(util, cnfPath);
|
|
103 } catch (AuthOpException e) {
|
|
104 throw new ServletException(e);
|
|
105 }
|
|
106 }
|
|
107 // FileOps instance
|
|
108 fileOp = new FileOps(util);
|
|
109 // global DocuImage instance (don't reuse inside a request!)
|
|
110 globalImage = new JAIDocuImage(util);
|
|
111 // globalImage = new JIMIDocuImage(util);
|
|
112 //globalImage = new ImageLoaderDocuImage(util);
|
|
113
|
|
114 }
|
|
115
|
|
116 /**Process the HTTP Get request*/
|
|
117 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
118 util.dprintln(1, "The servlet has received a GET!");
|
|
119 processRequest(request, response);
|
|
120 }
|
|
121
|
|
122 /**Process the HTTP Post request*/
|
|
123 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
124 util.dprintln(1, "The servlet has received a POST!");
|
|
125 processRequest(request, response);
|
|
126 }
|
|
127
|
|
128 /**Clean up resources*/
|
|
129 public void destroy() {
|
|
130 }
|
|
131
|
|
132 /**********************************************************************
|
|
133 * main request handler
|
|
134 **********************************************************************/
|
|
135
|
|
136 void processRequest(HttpServletRequest request, HttpServletResponse response)
|
|
137 throws ServletException, IOException {
|
|
138
|
|
139 // time for benchmarking
|
|
140 long startTime = System.currentTimeMillis();
|
|
141 // output mime/type
|
|
142 String mimeType = "image/png";
|
|
143
|
|
144 /**
|
|
145 * parameters for a session
|
|
146 */
|
|
147
|
|
148 // scale the image file to fit window size
|
|
149 boolean scaleToFit = true;
|
|
150 // use heuristics (GIF?) to scale or not
|
|
151 boolean forcedScale = false;
|
|
152 // try prescaled images first
|
|
153 boolean preScaledFirst = true;
|
|
154 // interpolation to use for scaling
|
|
155 int scaleQual = 0;
|
|
156 // send html error message (or image file)
|
|
157 boolean errorMsgHtml = false;
|
|
158
|
|
159 /**
|
|
160 * request parameter
|
|
161 */
|
|
162
|
|
163 // file/dir to load
|
|
164 String param_fn = servletOp.tryToGetParam("fn", "", request);
|
|
165 // page number
|
|
166 int param_pn = servletOp.tryToGetParam("pn", 1, request);
|
|
167 // destination image width
|
|
168 int param_dw = servletOp.tryToGetParam("dw", 300, request);
|
|
169 // destination image height
|
|
170 int param_dh = servletOp.tryToGetParam("dh", 400, request);
|
|
171 // relative area x_offset (0..1)
|
|
172 float param_wx = servletOp.tryToGetParam("wx", 0f, request);
|
|
173 // relative area y_offset
|
|
174 float param_wy = servletOp.tryToGetParam("wy", 0f, request);
|
|
175 // relative area width (0..1)
|
|
176 float param_ww = servletOp.tryToGetParam("ww", 1f, request);
|
|
177 // relative area height
|
|
178 float param_wh = servletOp.tryToGetParam("wh", 1f, request);
|
|
179 // scale factor (additional to dw/width, dh/height)
|
|
180 float param_ws = servletOp.tryToGetParam("ws", 1f, request);
|
|
181 // operation mode: flags separated by "+"
|
|
182 String param_mo = servletOp.tryToGetParam("mo", "", request);
|
|
183 // operation mode: "fit": always fit to page, "file": send as-is
|
|
184 if (param_mo.indexOf("fit") >= 0) {
|
|
185 scaleToFit = true;
|
|
186 forcedScale = true;
|
|
187 } else if (param_mo.indexOf("file") >= 0) {
|
|
188 scaleToFit = false;
|
|
189 forcedScale = true;
|
|
190 }
|
|
191 // operation mode: "errtxt": error message in html, "errimg": error image
|
|
192 if (param_mo.indexOf("errtxt") >= 0) {
|
|
193 errorMsgHtml = true;
|
|
194 } else if (param_mo.indexOf("errimg") >= 0) {
|
|
195 errorMsgHtml = false;
|
|
196 }
|
|
197 // operation mode: "q0" - "q2": interpolation quality
|
|
198 if (param_mo.indexOf("q0") >= 0) {
|
|
199 scaleQual = 0;
|
|
200 } else if (param_mo.indexOf("q1") >= 0) {
|
|
201 scaleQual = 1;
|
|
202 } else if (param_mo.indexOf("q2") >= 0) {
|
|
203 scaleQual = 2;
|
|
204 }
|
|
205 // operation mode: "lores": try to use scaled image, "hires": unscaled image
|
|
206 if (param_mo.indexOf("lores") >= 0) {
|
|
207 preScaledFirst = true;
|
|
208 } else if (param_mo.indexOf("hires") >= 0) {
|
|
209 preScaledFirst = false;
|
|
210 }
|
|
211
|
|
212 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);
|
|
213
|
|
214 //"big" try for all file/image actions
|
|
215 try {
|
|
216
|
|
217 // DocuImage instance
|
|
218 DocuImage docuImage = new JAIDocuImage(util);
|
|
219 // DocuImage docuImage = new JIMIDocuImage(util);
|
|
220 //DocuImage docuImage = new ImageLoaderDocuImage(util);
|
|
221
|
|
222
|
|
223 /**
|
|
224 * find the file to load/send
|
|
225 */
|
|
226
|
|
227 String loadPathName = "";
|
|
228 // if there's PathInfo, append
|
|
229 if (request.getPathInfo() != null) {
|
|
230 loadPathName += request.getPathInfo();
|
|
231 }
|
|
232 // append fn parameter
|
|
233 loadPathName += param_fn;
|
|
234 // if it's zoomed, try hires version (to be optimized...)
|
|
235 if ((param_ww < 1f) || (param_wh < 1f)) {
|
|
236 preScaledFirst = false;
|
|
237 }
|
|
238
|
|
239 if (useAuthentication) {
|
|
240 // check permissions
|
|
241 List rolesRequired = authOp.rolesForPath(loadPathName, request);
|
|
242 if (rolesRequired != null) {
|
|
243 Utils.dprintln(1, "Role required: "+rolesRequired);
|
|
244 Utils.dprintln(2, "User: "+request.getRemoteUser());
|
|
245 if (! authOp.isRoleAuthorized(rolesRequired, request)) {
|
|
246 Utils.dprintln(1, "ERROR: access denied!");
|
|
247 if (errorMsgHtml) {
|
|
248 servletOp.htmlMessage("ERROR: Unauthorized access!", response);
|
|
249 } else {
|
|
250 docuImage.sendFile(denyImgFile, response);
|
|
251 }
|
|
252 return;
|
|
253 }
|
|
254 }
|
|
255 }
|
|
256
|
|
257 // find the file
|
|
258 File fileToLoad = fileOp.getFileVariant(baseDirs, loadPathName, param_pn, preScaledFirst);
|
|
259
|
|
260 Utils.dprintln(1, "Loading: "+fileToLoad);
|
|
261
|
|
262 // get the source image type (if it's known)
|
|
263 mimeType = fileOp.mimeForFile(fileToLoad);
|
|
264
|
|
265 // if not forced and source is GIF/PNG then send-as-is if not zoomed
|
|
266 if((!forcedScale && (mimeType == "image/gif" || mimeType == "image/png")
|
|
267 && (param_ww == 1f) && (param_wh == 1f)) || (forcedScale && !scaleToFit)) {
|
|
268
|
|
269 Utils.dprintln(1, "Sending File as is.");
|
|
270
|
|
271 docuImage.sendFile(fileToLoad, response);
|
|
272
|
|
273 Utils.dprintln(1, "Done in "+(System.currentTimeMillis()-startTime)+"ms");
|
|
274 return;
|
|
275 }
|
|
276
|
|
277 // load file
|
|
278 docuImage.loadImage(fileToLoad);
|
|
279
|
|
280 /**
|
|
281 * crop and scale the image
|
|
282 */
|
|
283
|
|
284 // get size
|
|
285 int imgWidth = docuImage.getWidth();
|
|
286 int imgHeight = docuImage.getHeight();
|
|
287
|
|
288 util.dprintln(2, "IMG: "+imgWidth+"x"+imgHeight);
|
|
289 util.dprintln(2, "time "+(System.currentTimeMillis()-startTime)+"ms");
|
|
290
|
|
291 // calculate absolute from relative coordinates
|
|
292 float areaXoff = param_wx * imgWidth;
|
|
293 float areaYoff = param_wy * imgHeight;
|
|
294 float areaWidth = param_ww * imgWidth;
|
|
295 float areaHeight = param_wh * imgHeight;
|
|
296 // calculate scaling factors
|
|
297 float scaleX = param_dw / areaWidth * param_ws;
|
|
298 float scaleY = param_dh / areaHeight * param_ws;
|
|
299 float scaleXY = (scaleX > scaleY) ? scaleY : scaleX;
|
|
300
|
|
301 util.dprintln(1, "Scale "+scaleXY+"("+scaleX+","+scaleY+") on "+areaXoff+","+areaYoff+" "+areaWidth+"x"+areaHeight);
|
|
302
|
|
303 // fit area to image
|
|
304 areaWidth = (areaXoff + areaWidth > imgWidth) ? imgWidth - areaXoff : areaWidth;
|
|
305 areaHeight = (areaYoff + areaHeight > imgHeight) ? imgHeight - areaYoff : areaHeight;
|
|
306
|
|
307 util.dprintln(2, "cropped: "+areaXoff+","+areaYoff+" "+areaWidth+"x"+areaHeight);
|
|
308
|
|
309 // check image parameters
|
|
310 if ((areaWidth < 1)||(areaHeight < 1)
|
|
311 ||(scaleXY * areaWidth < 2)||(scaleXY * areaHeight < 2)) {
|
|
312 Utils.dprintln(1, "ERROR: invalid scale parameter set!");
|
|
313 throw new ImageOpException("Invalid scale parameter set!");
|
|
314 }
|
|
315
|
|
316 // crop and scale image
|
|
317 docuImage.cropAndScale((int)areaXoff, (int)areaYoff, (int)areaWidth, (int)areaHeight,
|
|
318 scaleXY, scaleQual);
|
|
319
|
|
320 util.dprintln(2, "time "+(System.currentTimeMillis()-startTime)+"ms");
|
|
321
|
|
322 /**
|
|
323 * write the resulting image
|
|
324 */
|
|
325
|
|
326 // setup output -- if source is JPG then dest will be JPG else it's PNG
|
|
327 if (mimeType != "image/jpeg") {
|
|
328 mimeType="image/png";
|
|
329 }
|
|
330
|
|
331 // write the image
|
|
332 docuImage.writeImage(mimeType, response);
|
|
333
|
|
334 util.dprintln(1, "Done in "+(System.currentTimeMillis()-startTime)+"ms");
|
|
335
|
|
336 /**
|
|
337 * error handling
|
|
338 */
|
|
339
|
|
340 }//"big" try
|
|
341 catch (FileOpException e) {
|
|
342 util.dprintln(1, "ERROR: File IO Error: "+e);
|
|
343 try {
|
|
344 if (errorMsgHtml) {
|
|
345 servletOp.htmlMessage("ERROR: File IO Error: "+e, response);
|
|
346 } else {
|
|
347 globalImage.sendFile(errorImgFile, response);
|
|
348 }
|
|
349 } catch (FileOpException ex) {} // so we don't get a loop
|
|
350 return;
|
|
351 }
|
|
352 catch (AuthOpException e) {
|
|
353 Utils.dprintln(1, "ERROR: Authorization error: "+e);
|
|
354 try {
|
|
355 if (errorMsgHtml) {
|
|
356 servletOp.htmlMessage("ERROR: Authorization error: "+e, response);
|
|
357 } else {
|
|
358 globalImage.sendFile(errorImgFile, response);
|
|
359 }
|
|
360 } catch (FileOpException ex) {} // so we don't get a loop
|
|
361 return;
|
|
362 }
|
|
363 catch (ImageOpException e) {
|
|
364 Utils.dprintln(1, "ERROR: Image Error: "+e);
|
|
365 try {
|
|
366 if (errorMsgHtml) {
|
|
367 servletOp.htmlMessage("ERROR: Image Operation Error: "+e, response);
|
|
368 } else {
|
|
369 globalImage.sendFile(errorImgFile, response);
|
|
370 }
|
|
371 } catch (FileOpException ex) {} // so we don't get a loop
|
|
372 return;
|
|
373 }
|
|
374
|
|
375 }
|
|
376
|
|
377 }//Scaler class
|