Mercurial > hg > digilib-old
comparison servlet2/src/main/java/digilib/servlet/DocumentBean.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 |
comparison
equal
deleted
inserted
replaced
902:89ba3ffcf552 | 903:7779b37d1d05 |
---|---|
1 /* | |
2 * DocumentBean -- Access control bean for JSP | |
3 * | |
4 * Digital Image Library servlet components | |
5 * | |
6 * Copyright (C) 2001, 2002, 2003 Robert Casties (robcast@mail.berlios.de) | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify it under | |
9 * the terms of the GNU General Public License as published by the Free Software | |
10 * Foundation; either version 2 of the License, or (at your option) any later | |
11 * version. | |
12 * | |
13 * Please read license.txt for the full details. A copy of the GPL may be found | |
14 * at http://www.gnu.org/copyleft/lgpl.html | |
15 * | |
16 * You should have received a copy of the GNU General Public License along with | |
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
18 * Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
22 package digilib.servlet; | |
23 | |
24 import java.util.List; | |
25 | |
26 import javax.servlet.ServletConfig; | |
27 import javax.servlet.ServletContext; | |
28 import javax.servlet.ServletException; | |
29 import javax.servlet.http.HttpServletRequest; | |
30 import javax.servlet.http.HttpServletResponse; | |
31 | |
32 import org.apache.log4j.Logger; | |
33 | |
34 import digilib.auth.AuthOpException; | |
35 import digilib.auth.AuthOps; | |
36 import digilib.io.DocuDirCache; | |
37 import digilib.io.DocuDirectory; | |
38 import digilib.io.FileOps.FileClass; | |
39 import digilib.io.ImageInput; | |
40 import digilib.io.ImageSet; | |
41 import digilib.util.ImageSize; | |
42 | |
43 public class DocumentBean { | |
44 | |
45 // general logger | |
46 private static Logger logger = Logger.getLogger("digilib.docubean"); | |
47 | |
48 // AuthOps object to check authorization | |
49 private AuthOps authOp; | |
50 | |
51 // use authorization database | |
52 private boolean useAuthentication = true; | |
53 | |
54 // path to add for authenticated access | |
55 private String authURLPath = ""; | |
56 | |
57 // DocuDirCache | |
58 private DocuDirCache dirCache = null; | |
59 | |
60 // DigilibConfiguration object | |
61 private DigilibServletConfiguration dlConfig; | |
62 | |
63 // DigilibRequest object | |
64 private DigilibServletRequest dlRequest = null; | |
65 | |
66 /** | |
67 * Constructor for DocumentBean. | |
68 */ | |
69 public DocumentBean() { | |
70 logger.debug("new DocumentBean"); | |
71 } | |
72 | |
73 public DocumentBean(ServletConfig conf) { | |
74 logger.debug("new DocumentBean"); | |
75 try { | |
76 setConfig(conf); | |
77 } catch (Exception e) { | |
78 logger.fatal("ERROR: Unable to read config: ", e); | |
79 } | |
80 } | |
81 | |
82 public void setConfig(ServletConfig conf) throws ServletException { | |
83 logger.debug("setConfig"); | |
84 // get our ServletContext | |
85 ServletContext context = conf.getServletContext(); | |
86 // see if there is a Configuration instance | |
87 dlConfig = (DigilibServletConfiguration) context | |
88 .getAttribute("digilib.servlet.configuration"); | |
89 if (dlConfig == null) { | |
90 // create new Configuration | |
91 throw new ServletException("ERROR: No configuration!"); | |
92 } | |
93 | |
94 // get cache | |
95 dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); | |
96 | |
97 /* | |
98 * authentication | |
99 */ | |
100 useAuthentication = dlConfig.getAsBoolean("use-authorization"); | |
101 authOp = (AuthOps) dlConfig.getValue("servlet.auth.op"); | |
102 authURLPath = dlConfig.getAsString("auth-url-path"); | |
103 if (useAuthentication && (authOp == null)) { | |
104 throw new ServletException( | |
105 "ERROR: use-authorization configured but no AuthOp!"); | |
106 } | |
107 } | |
108 | |
109 /** | |
110 * check if the request must be authorized to access filepath | |
111 */ | |
112 public boolean isAuthRequired(DigilibServletRequest request) | |
113 throws AuthOpException { | |
114 logger.debug("isAuthRequired"); | |
115 return useAuthentication ? authOp.isAuthRequired(request) : false; | |
116 } | |
117 | |
118 /** | |
119 * check if the request is allowed to access filepath | |
120 */ | |
121 public boolean isAuthorized(DigilibServletRequest request) throws AuthOpException { | |
122 logger.debug("isAuthorized"); | |
123 return useAuthentication ? authOp.isAuthorized(request) : true; | |
124 } | |
125 | |
126 /** | |
127 * return a list of authorization roles needed for request to access the | |
128 * specified path | |
129 */ | |
130 public List<String> rolesForPath(DigilibServletRequest request) throws AuthOpException { | |
131 logger.debug("rolesForPath"); | |
132 return useAuthentication ? authOp.rolesForPath(request) : null; | |
133 } | |
134 | |
135 /** | |
136 * check request authorization against a list of roles | |
137 */ | |
138 public boolean isRoleAuthorized(List<String> roles, DigilibServletRequest request) { | |
139 logger.debug("isRoleAuthorized"); | |
140 return useAuthentication ? authOp.isRoleAuthorized(roles, request) | |
141 : true; | |
142 } | |
143 | |
144 /** | |
145 * check for authenticated access and redirect if necessary | |
146 */ | |
147 public boolean doAuthentication(HttpServletResponse response) | |
148 throws Exception { | |
149 logger.debug("doAuthenication-Method"); | |
150 return doAuthentication(dlRequest, response); | |
151 } | |
152 | |
153 /** | |
154 * check for authenticated access and redirect if necessary | |
155 */ | |
156 public boolean doAuthentication(DigilibServletRequest request, | |
157 HttpServletResponse response) throws Exception { | |
158 logger.debug("doAuthentication"); | |
159 if (!useAuthentication) { | |
160 // shortcut if no authentication | |
161 return true; | |
162 } | |
163 // check if we are already authenticated | |
164 if (((HttpServletRequest) request.getServletRequest()).getRemoteUser() == null) { | |
165 logger.debug("unauthenticated so far"); | |
166 // if not maybe we must? | |
167 if (isAuthRequired(request)) { | |
168 logger.debug("auth required, redirect"); | |
169 // we are not yet authenticated -> redirect | |
170 response.sendRedirect(authURLPath | |
171 + ((HttpServletRequest) request.getServletRequest()) | |
172 .getServletPath() | |
173 + "?" | |
174 + ((HttpServletRequest) request.getServletRequest()) | |
175 .getQueryString()); | |
176 } | |
177 } | |
178 return true; | |
179 } | |
180 | |
181 /** | |
182 * Sets the current DigilibRequest. Also completes information in the request. | |
183 * | |
184 * @param dlRequest | |
185 * The dlRequest to set. | |
186 */ | |
187 public void setRequest(DigilibServletRequest dlRequest) throws Exception { | |
188 this.dlRequest = dlRequest; | |
189 if (dirCache == null) { | |
190 return; | |
191 } | |
192 String fn = dlRequest.getFilePath(); | |
193 // get information about the file | |
194 ImageSet fileset = (ImageSet) dirCache.getFile(fn, dlRequest | |
195 .getAsInt("pn"), FileClass.IMAGE); | |
196 if (fileset == null) { | |
197 return; | |
198 } | |
199 // add file name | |
200 dlRequest.setValue("img.fn", fileset); | |
201 // add dpi | |
202 dlRequest.setValue("img.dpix", new Double(fileset.getResX())); | |
203 dlRequest.setValue("img.dpiy", new Double(fileset.getResY())); | |
204 // get number of pages in directory | |
205 DocuDirectory dd = dirCache.getDirectory(fn); | |
206 if (dd != null) { | |
207 // add pt | |
208 dlRequest.setValue("pt", dd.size()); | |
209 } | |
210 // get original pixel size | |
211 ImageInput origfile = fileset.getBiggest(); | |
212 // check image for size (TODO: just if mo=hires?) | |
213 ImageSize pixsize = origfile.getSize(); | |
214 if (pixsize != null) { | |
215 // add pixel size | |
216 dlRequest.setValue("img.pix_x", new Integer(pixsize.getWidth())); | |
217 dlRequest.setValue("img.pix_y", new Integer(pixsize.getHeight())); | |
218 } | |
219 } | |
220 | |
221 /** | |
222 * get the first page number in the directory (not yet functional) | |
223 */ | |
224 public int getFirstPage(DigilibServletRequest request) { | |
225 logger.debug("getFirstPage"); | |
226 return 1; | |
227 } | |
228 | |
229 /** | |
230 * get the number of pages/files in the directory | |
231 */ | |
232 public int getNumPages() throws Exception { | |
233 return getNumPages(dlRequest); | |
234 } | |
235 | |
236 /** | |
237 * get the number of image pages/files in the directory | |
238 */ | |
239 public int getNumPages(DigilibServletRequest request) throws Exception { | |
240 return getNumPages(request, FileClass.IMAGE); | |
241 } | |
242 | |
243 /** | |
244 * get the number of pages/files of type fc in the directory | |
245 */ | |
246 public int getNumPages(DigilibServletRequest request, FileClass fc) throws Exception { | |
247 logger.debug("getNumPages"); | |
248 DocuDirectory dd = (dirCache != null) ? dirCache.getDirectory(request | |
249 .getFilePath()) : null; | |
250 if (dd != null) { | |
251 return dd.size(fc); | |
252 } | |
253 return 0; | |
254 } | |
255 | |
256 /** | |
257 * Returns the dlConfig. | |
258 * | |
259 * @return DigilibConfiguration | |
260 */ | |
261 public DigilibServletConfiguration getDlConfig() { | |
262 return dlConfig; | |
263 } | |
264 | |
265 /** | |
266 * returns if the zoom area in the request can be moved | |
267 * | |
268 * @return | |
269 */ | |
270 public boolean canMoveRight() { | |
271 float ww = dlRequest.getAsFloat("ww"); | |
272 float wx = dlRequest.getAsFloat("wx"); | |
273 return (ww + wx < 1.0); | |
274 } | |
275 | |
276 /** | |
277 * returns if the zoom area in the request can be moved | |
278 * | |
279 * @return | |
280 */ | |
281 public boolean canMoveLeft() { | |
282 float ww = dlRequest.getAsFloat("ww"); | |
283 float wx = dlRequest.getAsFloat("wx"); | |
284 return ((ww < 1.0) && (wx > 0)); | |
285 } | |
286 | |
287 /** | |
288 * returns if the zoom area in the request can be moved | |
289 * | |
290 * @return | |
291 */ | |
292 public boolean canMoveUp() { | |
293 float wh = dlRequest.getAsFloat("wh"); | |
294 float wy = dlRequest.getAsFloat("wy"); | |
295 return ((wh < 1.0) && (wy > 0)); | |
296 } | |
297 | |
298 /** | |
299 * returns if the zoom area in the request can be moved | |
300 * | |
301 * @return | |
302 */ | |
303 public boolean canMoveDown() { | |
304 float wh = dlRequest.getAsFloat("wh"); | |
305 float wy = dlRequest.getAsFloat("wy"); | |
306 return (wh + wy < 1.0); | |
307 } | |
308 | |
309 /** | |
310 * @return Returns the dlRequest. | |
311 */ | |
312 public DigilibServletRequest getRequest() { | |
313 return dlRequest; | |
314 } | |
315 | |
316 } |