903
|
1 /*
|
|
2 * DigilibRequest.java
|
|
3 *
|
|
4 * lightweight class carrying all parameters for a request to digilib
|
|
5 *
|
|
6
|
|
7 Digital Image Library servlet components
|
|
8
|
|
9 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de),
|
|
10 Christian Luginbuehl
|
|
11
|
|
12 This program is free software; you can redistribute it and/or modify it
|
|
13 under the terms of the GNU General Public License as published by the
|
|
14 Free Software Foundation; either version 2 of the License, or (at your
|
|
15 option) any later version.
|
|
16
|
|
17 Please read license.txt for the full details. A copy of the GPL
|
|
18 may be found at http://www.gnu.org/copyleft/lgpl.html
|
|
19
|
|
20 You should have received a copy of the GNU General Public License
|
|
21 along with this program; if not, write to the Free Software
|
|
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23
|
|
24 * Created on 27. August 2002, 19:43
|
|
25 */
|
|
26
|
|
27 package digilib.servlet;
|
|
28
|
|
29 import java.io.UnsupportedEncodingException;
|
|
30 import java.net.URLDecoder;
|
|
31 import java.util.Enumeration;
|
|
32 import java.util.StringTokenizer;
|
|
33
|
|
34 import javax.servlet.ServletRequest;
|
|
35 import javax.servlet.http.HttpServletRequest;
|
|
36
|
|
37 import digilib.image.DocuImage;
|
|
38 import digilib.io.FileOps;
|
|
39 import digilib.util.OptionsSet;
|
|
40 import digilib.util.Parameter;
|
|
41
|
|
42 /**
|
|
43 * Class holding the parameters of a digilib user request. The parameters are
|
|
44 * mostly named like the servlet parameters: <br>
|
|
45 * request_path: url of the page/document. <br>
|
|
46 * fn: url of the page/document. <br>
|
|
47 * pn: page number. <br>
|
|
48 * dw: width of result window in pixels. <br>
|
|
49 * dh: height of result window in pixels. <br>
|
|
50 * wx: left edge of image area (float from 0 to 1). <br>
|
|
51 * wy: top edge of image area (float from 0 to 1). <br>
|
|
52 * ww: width of image area(float from 0 to 1). <br>
|
|
53 * wh: height of image area(float from 0 to 1). <br>
|
|
54 * ws: scale factor. <br>
|
|
55 * mo: special options like 'fit' for gifs. <br>
|
|
56 * mk: list of marks. <br>
|
|
57 * pt: total number of pages (generated by sevlet). <br>
|
|
58 * baseURL: base URL (from http:// to below /servlet). <br>
|
|
59 * ...et alii
|
|
60 *
|
|
61 * @author casties
|
|
62 *
|
|
63 */
|
|
64 public class DigilibServletRequest extends DigilibRequest {
|
|
65
|
1157
|
66 protected DocuImage image; // internal DocuImage instance for this request
|
903
|
67
|
1157
|
68 protected HttpServletRequest servletRequest; // internal ServletRequest
|
903
|
69
|
|
70 /**
|
|
71 * Creates a new instance of DigilibRequest with parameters from a
|
|
72 * ServletRequest. All undefined parameters are set to default values.
|
|
73 *
|
|
74 * @param request
|
|
75 */
|
|
76 public DigilibServletRequest(HttpServletRequest request) {
|
|
77 setWithRequest(request);
|
|
78 initOptions();
|
|
79 }
|
|
80
|
1157
|
81 /**
|
|
82 * set up parameters.
|
|
83 *
|
|
84 */
|
|
85 protected void initParams() {
|
|
86 /*
|
|
87 * Definition of parameters and default values. Parameter of type 's'
|
|
88 * are for the servlet.
|
|
89 */
|
903
|
90
|
1157
|
91 // url of the page/document (second part)
|
|
92 newParameter("fn", "", null, 's');
|
|
93 // page number
|
|
94 newParameter("pn", new Integer(1), null, 's');
|
|
95 // width of client in pixels
|
|
96 newParameter("dw", new Integer(0), null, 's');
|
|
97 // height of client in pixels
|
|
98 newParameter("dh", new Integer(0), null, 's');
|
|
99 // left edge of image (float from 0 to 1)
|
|
100 newParameter("wx", new Float(0), null, 's');
|
|
101 // top edge in image (float from 0 to 1)
|
|
102 newParameter("wy", new Float(0), null, 's');
|
|
103 // width of image (float from 0 to 1)
|
|
104 newParameter("ww", new Float(1), null, 's');
|
|
105 // height of image (float from 0 to 1)
|
|
106 newParameter("wh", new Float(1), null, 's');
|
|
107 // scale factor
|
|
108 newParameter("ws", new Float(1), null, 's');
|
|
109 // special options like 'fit' for gifs
|
|
110 newParameter("mo", this.options, null, 's');
|
|
111 // rotation angle (degree)
|
|
112 newParameter("rot", new Float(0), null, 's');
|
|
113 // contrast enhancement factor
|
|
114 newParameter("cont", new Float(0), null, 's');
|
|
115 // brightness enhancement factor
|
|
116 newParameter("brgt", new Float(0), null, 's');
|
|
117 // color multiplicative factors
|
|
118 newParameter("rgbm", "0/0/0", null, 's');
|
|
119 // color additive factors
|
|
120 newParameter("rgba", "0/0/0", null, 's');
|
|
121 // display dpi resolution (total)
|
|
122 newParameter("ddpi", new Float(0), null, 's');
|
|
123 // display dpi X resolution
|
|
124 newParameter("ddpix", new Float(0), null, 's');
|
|
125 // display dpi Y resolution
|
|
126 newParameter("ddpiy", new Float(0), null, 's');
|
|
127 // scale factor for mo=ascale
|
|
128 newParameter("scale", new Float(1), null, 's');
|
|
129 // color conversion operation
|
|
130 newParameter("colop", "", null, 's');
|
903
|
131
|
1157
|
132 /*
|
|
133 * Parameters of type 'i' are not exchanged between client and server,
|
|
134 * but are for the servlets or JSPs internal use.
|
|
135 */
|
903
|
136
|
1157
|
137 // url of the page/document (first part, may be empty)
|
|
138 newParameter("request.path", "", null, 'i');
|
|
139 // base URL (from http:// to below /servlet)
|
|
140 newParameter("base.url", null, null, 'i');
|
|
141 // DocuImage instance for this request
|
|
142 newParameter("docu.image", image, null, 'i');
|
|
143 image = null;
|
|
144 // HttpServletRequest for this request
|
|
145 newParameter("servlet.request", servletRequest, null, 'i');
|
|
146 servletRequest = null;
|
903
|
147
|
1157
|
148 /*
|
|
149 * Parameters of type 'c' are for the clients use
|
|
150 */
|
903
|
151
|
1157
|
152 // "real" filename
|
|
153 newParameter("img.fn", "", null, 'c');
|
|
154 // image dpi x
|
|
155 newParameter("img.dpix", new Integer(0), null, 'c');
|
|
156 // image dpi y
|
|
157 newParameter("img.dpiy", new Integer(0), null, 'c');
|
|
158 // hires image size x
|
|
159 newParameter("img.pix_x", new Integer(0), null, 'c');
|
|
160 // hires image size y
|
|
161 newParameter("img.pix_y", new Integer(0), null, 'c');
|
|
162 // total number of pages
|
|
163 newParameter("pt", new Integer(0), null, 'c');
|
|
164 // marks
|
|
165 newParameter("mk", "", null, 'c');
|
|
166 }
|
903
|
167
|
1157
|
168 /*
|
|
169 * (non-Javadoc)
|
|
170 *
|
903
|
171 * @see digilib.servlet.ParameterMap#initOptions()
|
|
172 */
|
|
173 @Override
|
|
174 protected void initOptions() {
|
|
175 options = (OptionsSet) getValue("mo");
|
|
176 }
|
|
177
|
1157
|
178 /**
|
|
179 * Populate the request object with data from a ServletRequest.
|
|
180 *
|
|
181 *
|
|
182 * @param request
|
|
183 */
|
|
184 public void setWithRequest(HttpServletRequest request) {
|
|
185 servletRequest = request;
|
|
186 // decide if it's old-style or new-style
|
|
187 String qs = ((HttpServletRequest) request).getQueryString();
|
|
188 if (qs != null) {
|
|
189 if (qs.indexOf("&") > -1) {
|
|
190 // & separator
|
|
191 setWithParamString(qs, "&");
|
|
192 } else if (qs.indexOf(";") > -1) {
|
|
193 // ; separator
|
|
194 setWithParamString(qs, ";");
|
|
195 } else if (request.getParameter("fn") != null || request.getParameter("dw") != null
|
|
196 || request.getParameter("dh") != null) {
|
|
197 // standard '&' parameters
|
|
198 setWithParamRequest(request);
|
|
199 } else {
|
|
200 setWithOldString(qs);
|
|
201 }
|
|
202 }
|
|
203 setValue("servlet.request", request);
|
|
204 // add path from request
|
|
205 setValue("request.path", ((HttpServletRequest) request).getPathInfo());
|
|
206 // set the baseURL
|
|
207 setBaseURL((HttpServletRequest) request);
|
|
208 }
|
903
|
209
|
1157
|
210 /**
|
|
211 * Populate a request from a string in the old "++++" parameter form.
|
|
212 *
|
|
213 * @param queryString
|
|
214 * String with paramters in the old "+++" form.
|
|
215 */
|
|
216 public void setWithOldString(String queryString) {
|
|
217 if (queryString == null) {
|
|
218 return;
|
|
219 }
|
|
220 // enable the passing of delimiter to get empty parameters
|
|
221 StringTokenizer query = new StringTokenizer(queryString, "+", true);
|
|
222 String token;
|
|
223 // first parameter FN
|
|
224 if (query.hasMoreTokens()) {
|
|
225 token = query.nextToken();
|
|
226 if (!token.equals("+")) {
|
|
227 setValueFromString("fn", token);
|
|
228 if (query.hasMoreTokens()) {
|
|
229 query.nextToken();
|
|
230 }
|
|
231 }
|
|
232 }
|
|
233 // second parameter PN
|
|
234 if (query.hasMoreTokens()) {
|
|
235 token = query.nextToken();
|
|
236 if (!token.equals("+")) {
|
|
237 setValueFromString("pn", token);
|
|
238 if (query.hasMoreTokens()) {
|
|
239 query.nextToken();
|
|
240 }
|
|
241 }
|
|
242 }
|
|
243 // third parameter WS
|
|
244 if (query.hasMoreTokens()) {
|
|
245 token = query.nextToken();
|
|
246 if (!token.equals("+")) {
|
|
247 setValueFromString("ws", token);
|
|
248 if (query.hasMoreTokens()) {
|
|
249 query.nextToken();
|
|
250 }
|
|
251 }
|
|
252 }
|
|
253 // fourth parameter MO
|
|
254 if (query.hasMoreTokens()) {
|
|
255 token = query.nextToken();
|
|
256 if (!token.equals("+")) {
|
|
257 setValueFromString("mo", token);
|
|
258 if (query.hasMoreTokens()) {
|
|
259 query.nextToken();
|
|
260 }
|
|
261 }
|
|
262 }
|
|
263 // fifth parameter MK
|
|
264 if (query.hasMoreTokens()) {
|
|
265 token = query.nextToken();
|
|
266 if (!token.equals("+")) {
|
|
267 setValueFromString("mk", token);
|
|
268 if (query.hasMoreTokens()) {
|
|
269 query.nextToken();
|
|
270 }
|
|
271 }
|
|
272 }
|
|
273 // sixth parameter WX
|
|
274 if (query.hasMoreTokens()) {
|
|
275 token = query.nextToken();
|
|
276 if (!token.equals("+")) {
|
|
277 setValueFromString("wx", token);
|
|
278 if (query.hasMoreTokens()) {
|
|
279 query.nextToken();
|
|
280 }
|
|
281 }
|
|
282 }
|
|
283 // seventh parameter WY
|
|
284 if (query.hasMoreTokens()) {
|
|
285 token = query.nextToken();
|
|
286 if (!token.equals("+")) {
|
|
287 setValueFromString("wy", token);
|
|
288 if (query.hasMoreTokens()) {
|
|
289 query.nextToken();
|
|
290 }
|
|
291 }
|
|
292 }
|
|
293 // eigth parameter WW
|
|
294 if (query.hasMoreTokens()) {
|
|
295 token = query.nextToken();
|
|
296 if (!token.equals("+")) {
|
|
297 setValueFromString("ww", token);
|
|
298 if (query.hasMoreTokens()) {
|
|
299 query.nextToken();
|
|
300 }
|
|
301 }
|
|
302 }
|
|
303 // ninth parameter WH
|
|
304 if (query.hasMoreTokens()) {
|
|
305 token = query.nextToken();
|
|
306 if (!token.equals("+")) {
|
|
307 setValueFromString("wh", token);
|
|
308 if (query.hasMoreTokens()) {
|
|
309 query.nextToken();
|
|
310 }
|
|
311 }
|
|
312 }
|
|
313 }
|
903
|
314
|
1157
|
315 /**
|
|
316 * Return the request parameters as a String in the parameter form
|
|
317 * 'fn=/icons&pn=1'. Empty (undefined) fields are not included.
|
|
318 *
|
|
319 * @return String of request parameters in parameter form.
|
|
320 */
|
|
321 public String getAsString() {
|
|
322 return getAsString(0);
|
|
323 }
|
903
|
324
|
1157
|
325 /**
|
|
326 * Return the request parameters of a given type type as a String in the
|
|
327 * parameter form 'fn=/icons&pn=1'. Empty (undefined) fields are not
|
|
328 * included.
|
|
329 *
|
|
330 * @return String of request parameters in parameter form.
|
|
331 */
|
|
332 public String getAsString(int type) {
|
|
333 StringBuffer s = new StringBuffer(50);
|
|
334 // go through all values
|
|
335 for (Parameter p : params.values()) {
|
|
336 if ((type > 0) && (p.getType() != type)) {
|
|
337 // skip the wrong types
|
|
338 continue;
|
|
339 }
|
|
340 String name = p.getName();
|
|
341 /*
|
|
342 * handling special cases
|
|
343 */
|
|
344 // request_path adds to fn
|
|
345 if (name.equals("fn")) {
|
|
346 s.append("&fn=" + getAsString("request.path") + getAsString("fn"));
|
|
347 continue;
|
|
348 }
|
|
349 /*
|
|
350 * the rest is sent with its name
|
|
351 */
|
|
352 // parameters that are not set or internal are not sent
|
|
353 if ((!p.hasValue()) || (p.getType() == 'i')) {
|
|
354 continue;
|
|
355 }
|
|
356 s.append("&" + name + "=" + p.getAsString());
|
|
357 }
|
|
358 // kill first "&"
|
|
359 s.deleteCharAt(0);
|
|
360 return s.toString();
|
|
361 }
|
903
|
362
|
1157
|
363 /**
|
|
364 * Returns request parameters in old '++++' form.
|
|
365 *
|
|
366 * @return String with parameters in old '++++' form.
|
|
367 */
|
|
368 public String getAsOldString() {
|
|
369 StringBuffer s = new StringBuffer(50);
|
|
370 s.append(getAsString("request.path"));
|
|
371 s.append(getAsString("fn"));
|
|
372 s.append("+" + getAsString("pn"));
|
|
373 s.append("+" + getAsString("ws"));
|
|
374 s.append("+" + getAsString("mo"));
|
|
375 s.append("+" + getAsString("mk"));
|
|
376 s.append("+" + getAsString("wx"));
|
|
377 s.append("+" + getAsString("wy"));
|
|
378 s.append("+" + getAsString("ww"));
|
|
379 s.append("+" + getAsString("wh"));
|
|
380 return s.toString();
|
|
381 }
|
903
|
382
|
1157
|
383 /**
|
|
384 * Set request parameters from javax.servlet.ServletRequest. Uses the
|
|
385 * Requests getParameter methods for 'fn=foo' style parameters.
|
|
386 *
|
|
387 * @param request
|
|
388 * ServletRequest to get parameters from.
|
|
389 */
|
|
390 // @SuppressWarnings("unchecked") // ServletRequest.getParameterNames()
|
|
391 // returns naked Enumeration
|
903
|
392 public void setWithParamRequest(ServletRequest request) {
|
1157
|
393 setValue("servlet.request", request);
|
|
394 // go through all request parameters
|
|
395 for (Enumeration<String> i = request.getParameterNames(); i.hasMoreElements();) {
|
|
396 String name = (String) i.nextElement();
|
|
397 // is this a known parameter?
|
|
398 if (params.containsKey(name)) {
|
|
399 Parameter p = (Parameter) this.get(name);
|
|
400 // internal parameters are not set
|
|
401 if (p.getType() == 'i') {
|
|
402 continue;
|
|
403 }
|
|
404 p.setValueFromString(request.getParameter(name));
|
|
405 continue;
|
|
406 }
|
|
407 // unknown parameters are just added with type 'r'
|
|
408 newParameter(name, null, request.getParameter(name), 'r');
|
|
409 }
|
|
410 // add path from request
|
|
411 setValue("request.path", ((HttpServletRequest) request).getPathInfo());
|
|
412 }
|
903
|
413
|
1157
|
414 /**
|
|
415 * Set request parameters from query string. Uses the separator string qs to
|
|
416 * get 'fn=foo' style parameters.
|
|
417 *
|
|
418 * @param qs
|
|
419 * query string
|
|
420 * @param sep
|
|
421 * parameter-separator string
|
|
422 */
|
|
423 public void setWithParamString(String qs, String sep) {
|
|
424 // go through all request parameters
|
|
425 String[] qa = qs.split(sep);
|
|
426 for (int i = 0; i < qa.length; i++) {
|
|
427 // split names and values on "="
|
|
428 String[] nv = qa[i].split("=");
|
|
429 try {
|
|
430 String name = URLDecoder.decode(nv[0], "UTF-8");
|
|
431 String val = URLDecoder.decode(nv[1], "UTF-8");
|
|
432 // is this a known parameter?
|
|
433 if (params.containsKey(name)) {
|
|
434 Parameter p = (Parameter) this.get(name);
|
|
435 // internal parameters are not set
|
|
436 if (p.getType() == 'i') {
|
|
437 continue;
|
|
438 }
|
|
439 p.setValueFromString(val);
|
|
440 continue;
|
|
441 }
|
|
442 // unknown parameters are just added with type 'r'
|
|
443 newParameter(name, null, val, 'r');
|
|
444 } catch (UnsupportedEncodingException e) {
|
|
445 // this shouldn't happen anyway
|
|
446 e.printStackTrace();
|
|
447 }
|
|
448 }
|
|
449 }
|
903
|
450
|
1157
|
451 /**
|
|
452 * Test if option string <code>opt</code> is set. Checks if the substring
|
|
453 * <code>opt</code> is contained in the options string <code>param</code>.
|
|
454 * Deprecated! use hasOption(String opt) for "mo"-options.
|
|
455 *
|
|
456 * @param opt
|
|
457 * Option string to be tested.
|
|
458 * @return boolean
|
|
459 */
|
|
460 public boolean hasOption(String param, String opt) {
|
|
461 String s = getAsString(param);
|
|
462 if (s != null) {
|
|
463 StringTokenizer i = new StringTokenizer(s, ",");
|
|
464 while (i.hasMoreTokens()) {
|
|
465 if (i.nextToken().equals(opt)) {
|
|
466 return true;
|
|
467 }
|
|
468 }
|
|
469 }
|
|
470 return false;
|
|
471 }
|
903
|
472
|
1157
|
473 /**
|
|
474 * The image file path to be accessed.
|
|
475 *
|
|
476 * The mage file path is assembled from the servlets RequestPath and
|
|
477 * Parameter fn and normalized.
|
|
478 *
|
|
479 * @return String the effective filepath.
|
|
480 */
|
|
481 public String getFilePath() {
|
|
482 String s = getAsString("request.path");
|
|
483 s += getAsString("fn");
|
|
484 return FileOps.normalName(s);
|
|
485 }
|
903
|
486
|
1157
|
487 /* Property getter and setter */
|
903
|
488
|
1157
|
489 /**
|
|
490 * Set the requests base URL parameter from a
|
|
491 * javax.sevlet.http.HttpServletRequest.
|
|
492 *
|
|
493 * @param request
|
|
494 * HttpServletRequest to set the base URL.
|
|
495 */
|
|
496 public void setBaseURL(javax.servlet.http.HttpServletRequest request) {
|
|
497 String baseURL = null;
|
|
498 // calculate base URL string from request (minus last part)
|
|
499 String s = request.getRequestURL().toString();
|
|
500 int eop = s.lastIndexOf("/");
|
|
501 if (eop > 0) {
|
|
502 baseURL = s.substring(0, eop);
|
|
503 } else {
|
|
504 // fall back
|
|
505 baseURL = "http://" + request.getServerName() + "/docuserver/digitallibrary";
|
|
506 }
|
|
507 setValue("base.url", baseURL);
|
|
508 }
|
903
|
509
|
1157
|
510 /**
|
|
511 * Returns the image.
|
|
512 *
|
|
513 * @return DocuImage
|
|
514 */
|
|
515 public DocuImage getImage() {
|
|
516 return image;
|
|
517 }
|
903
|
518
|
1157
|
519 /**
|
|
520 * Sets the image.
|
|
521 *
|
|
522 * @param image
|
|
523 * The image to set
|
|
524 */
|
|
525 public void setImage(DocuImage image) {
|
|
526 this.image = image;
|
|
527 setValue("docu.image", image);
|
|
528 }
|
903
|
529
|
1157
|
530 /**
|
|
531 * @return
|
|
532 */
|
|
533 public HttpServletRequest getServletRequest() {
|
|
534 return servletRequest;
|
|
535 }
|
903
|
536
|
|
537 }
|