comparison common/src/main/java/digilib/servlet/DigilibRequest.java @ 903:7779b37d1d05

refactored into maven modules per servlet type. can build servlet-api 2.3 and 3.0 via profile now!
author robcast
date Tue, 26 Apr 2011 20:24:31 +0200
parents
children 52598b5e5518
comparison
equal deleted inserted replaced
902:89ba3ffcf552 903:7779b37d1d05
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.StringTokenizer;
32
33 import javax.servlet.http.HttpServletRequest;
34
35 import digilib.image.DocuImage;
36 import digilib.io.FileOps;
37 import digilib.util.OptionsSet;
38 import digilib.util.Parameter;
39 import digilib.util.ParameterMap;
40
41 /**
42 * Class holding the parameters of a digilib user request. The parameters are
43 * mostly named like the servlet parameters: <br>
44 * request_path: url of the page/document. <br>
45 * fn: url of the page/document. <br>
46 * pn: page number. <br>
47 * dw: width of result window in pixels. <br>
48 * dh: height of result window in pixels. <br>
49 * wx: left edge of image area (float from 0 to 1). <br>
50 * wy: top edge of image area (float from 0 to 1). <br>
51 * ww: width of image area(float from 0 to 1). <br>
52 * wh: height of image area(float from 0 to 1). <br>
53 * ws: scale factor. <br>
54 * mo: special options like 'fit' for gifs. <br>
55 * mk: list of marks. <br>
56 * pt: total number of pages (generated by sevlet). <br>
57 * baseURL: base URL (from http:// to below /servlet). <br>
58 * ...et alii
59 *
60 * @author casties
61 *
62 */
63 public abstract class DigilibRequest extends ParameterMap {
64
65 protected DocuImage image; // internal DocuImage instance for this request
66
67 public DigilibRequest() {
68 super(30);
69 }
70
71 /** set up parameters.
72 *
73 */
74 protected void initParams() {
75 /*
76 * Definition of parameters and default values. Parameter of type 's'
77 * are for the servlet.
78 */
79
80 // url of the page/document (second part)
81 newParameter("fn", "", null, 's');
82 // page number
83 newParameter("pn", new Integer(1), null, 's');
84 // width of client in pixels
85 newParameter("dw", new Integer(0), null, 's');
86 // height of client in pixels
87 newParameter("dh", new Integer(0), null, 's');
88 // left edge of image (float from 0 to 1)
89 newParameter("wx", new Float(0), null, 's');
90 // top edge in image (float from 0 to 1)
91 newParameter("wy", new Float(0), null, 's');
92 // width of image (float from 0 to 1)
93 newParameter("ww", new Float(1), null, 's');
94 // height of image (float from 0 to 1)
95 newParameter("wh", new Float(1), null, 's');
96 // scale factor
97 newParameter("ws", new Float(1), null, 's');
98 // special options like 'fit' for gifs
99 newParameter("mo", this.options, null, 's');
100 // rotation angle (degree)
101 newParameter("rot", new Float(0), null, 's');
102 // contrast enhancement factor
103 newParameter("cont", new Float(0), null, 's');
104 // brightness enhancement factor
105 newParameter("brgt", new Float(0), null, 's');
106 // color multiplicative factors
107 newParameter("rgbm", "0/0/0", null, 's');
108 // color additive factors
109 newParameter("rgba", "0/0/0", null, 's');
110 // display dpi resolution (total)
111 newParameter("ddpi", new Float(0), null, 's');
112 // display dpi X resolution
113 newParameter("ddpix", new Float(0), null, 's');
114 // display dpi Y resolution
115 newParameter("ddpiy", new Float(0), null, 's');
116 // scale factor for mo=ascale
117 newParameter("scale", new Float(1), null, 's');
118 // color conversion operation
119 newParameter("colop", "", null, 's');
120
121 /*
122 * Parameters of type 'i' are not exchanged between client and server,
123 * but are for the servlets or JSPs internal use.
124 */
125
126 // url of the page/document (first part, may be empty)
127 newParameter("request.path", "", null, 'i');
128 // base URL (from http:// to below /servlet)
129 newParameter("base.url", null, null, 'i');
130 // DocuImage instance for this request
131 newParameter("docu.image", image, null, 'i');
132 image = null;
133 /*
134 * Parameters of type 'c' are for the clients use
135 */
136
137 // "real" filename
138 newParameter("img.fn", "", null, 'c');
139 // image dpi x
140 newParameter("img.dpix", new Integer(0), null, 'c');
141 // image dpi y
142 newParameter("img.dpiy", new Integer(0), null, 'c');
143 // hires image size x
144 newParameter("img.pix_x", new Integer(0), null, 'c');
145 // hires image size y
146 newParameter("img.pix_y", new Integer(0), null, 'c');
147 // total number of pages
148 newParameter("pt", new Integer(0), null, 'c');
149 // display level of digilib (0 = just image, 1 = one HTML page
150 // 2 = in frameset, 3 = XUL-'frameset'
151 // 4 = XUL-Sidebar )
152 newParameter("lv", new Integer(2), null, 'c');
153 // marks
154 newParameter("mk", "", null, 'c');
155 }
156
157 /* (non-Javadoc)
158 * @see digilib.servlet.ParameterMap#initOptions()
159 */
160 @Override
161 protected void initOptions() {
162 options = (OptionsSet) getValue("mo");
163 }
164
165 /**
166 * Return the request parameters as a String in the parameter form
167 * 'fn=/icons&amp;pn=1'. Empty (undefined) fields are not included.
168 *
169 * @return String of request parameters in parameter form.
170 */
171 public String getAsString() {
172 return getAsString(0);
173 }
174
175 /**
176 * Return the request parameters of a given type type as a String in the
177 * parameter form 'fn=/icons&amp;pn=1'. Empty (undefined) fields are not
178 * included.
179 *
180 * @return String of request parameters in parameter form.
181 */
182 public String getAsString(int type) {
183 StringBuffer s = new StringBuffer(50);
184 // go through all values
185 for (Parameter p: params.values()) {
186 if ((type > 0) && (p.getType() != type)) {
187 // skip the wrong types
188 continue;
189 }
190 String name = p.getName();
191 /*
192 * handling special cases
193 */
194 // request_path adds to fn
195 if (name.equals("fn")) {
196 s.append("&fn=" + getAsString("request.path")
197 + getAsString("fn"));
198 continue;
199 }
200 /*
201 * the rest is sent with its name
202 */
203 // parameters that are not set or internal are not sent
204 if ((!p.hasValue()) || (p.getType() == 'i')) {
205 continue;
206 }
207 s.append("&" + name + "=" + p.getAsString());
208 }
209 // kill first "&"
210 s.deleteCharAt(0);
211 return s.toString();
212 }
213
214 /**
215 * Set request parameters from query string. Uses the separator string qs to
216 * get 'fn=foo' style parameters.
217 *
218 * @param qs
219 * query string
220 * @param sep
221 * parameter-separator string
222 */
223 public void setWithParamString(String qs, String sep) {
224 // go through all request parameters
225 String[] qa = qs.split(sep);
226 for (int i = 0; i < qa.length; i++) {
227 // split names and values on "="
228 String[] nv = qa[i].split("=");
229 try {
230 String name = URLDecoder.decode(nv[0], "UTF-8");
231 String val = URLDecoder.decode(nv[1], "UTF-8");
232 // is this a known parameter?
233 if (params.containsKey(name)) {
234 Parameter p = (Parameter) this.get(name);
235 // internal parameters are not set
236 if (p.getType() == 'i') {
237 continue;
238 }
239 p.setValueFromString(val);
240 continue;
241 }
242 // unknown parameters are just added with type 'r'
243 newParameter(name, null, val, 'r');
244 } catch (UnsupportedEncodingException e) {
245 // this shouldn't happen anyway
246 e.printStackTrace();
247 }
248 }
249 }
250
251 /**
252 * Test if option string <code>opt</code> is set. Checks if the substring
253 * <code>opt</code> is contained in the options string <code>param</code>.
254 * Deprecated! use hasOption(String opt) for "mo"-options.
255 *
256 * @param opt
257 * Option string to be tested.
258 * @return boolean
259 */
260 public boolean hasOption(String param, String opt) {
261 String s = getAsString(param);
262 if (s != null) {
263 StringTokenizer i = new StringTokenizer(s, ",");
264 while (i.hasMoreTokens()) {
265 if (i.nextToken().equals(opt)) {
266 return true;
267 }
268 }
269 }
270 return false;
271 }
272
273 /**
274 * The image file path to be accessed.
275 *
276 * The mage file path is assembled from the servlets RequestPath and
277 * Parameter fn and normalized.
278 *
279 * @return String the effective filepath.
280 */
281 public String getFilePath() {
282 String s = getAsString("request.path");
283 s += getAsString("fn");
284 return FileOps.normalName(s);
285 }
286
287 /**
288 * Returns the image.
289 *
290 * @return DocuImage
291 */
292 public DocuImage getImage() {
293 return image;
294 }
295
296 /**
297 * Sets the image.
298 *
299 * @param image
300 * The image to set
301 */
302 public void setImage(DocuImage image) {
303 this.image = image;
304 setValue("docu.image", image);
305 }
306
307 public HttpServletRequest getServletRequest() {
308 // TODO Auto-generated method stub
309 return null;
310 }
311
312 }