Mercurial > hg > digilib-old
annotate servlet3/src/main/java/digilib/servlet/DocumentBean.java @ 1130:f523a1c8edaa
make frontend page for digilibUrl action configurable.
author | robcast |
---|---|
date | Mon, 12 Nov 2012 13:04:58 +0100 |
parents | 7779b37d1d05 |
children |
rev | line source |
---|---|
246 | 1 /* |
2 * DocumentBean -- Access control bean for JSP | |
338 | 3 * |
246 | 4 * Digital Image Library servlet components |
338 | 5 * |
246 | 6 * Copyright (C) 2001, 2002, 2003 Robert Casties (robcast@mail.berlios.de) |
338 | 7 * |
246 | 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. | |
338 | 12 * |
246 | 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 | |
338 | 15 * |
246 | 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 | |
338 | 19 * |
246 | 20 */ |
1 | 21 |
22 package digilib.servlet; | |
23 | |
347 | 24 import java.util.List; |
1 | 25 |
153 | 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 | |
181 | 32 import org.apache.log4j.Logger; |
33 | |
153 | 34 import digilib.auth.AuthOpException; |
35 import digilib.auth.AuthOps; | |
36 import digilib.io.DocuDirCache; | |
37 import digilib.io.DocuDirectory; | |
563 | 38 import digilib.io.FileOps.FileClass; |
576
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
39 import digilib.io.ImageInput; |
574 | 40 import digilib.io.ImageSet; |
596 | 41 import digilib.util.ImageSize; |
1 | 42 |
73 | 43 public class DocumentBean { |
44 | |
181 | 45 // general logger |
297
b74c914b48a9
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
252
diff
changeset
|
46 private static Logger logger = Logger.getLogger("digilib.docubean"); |
246 | 47 |
73 | 48 // AuthOps object to check authorization |
49 private AuthOps authOp; | |
246 | 50 |
73 | 51 // use authorization database |
52 private boolean useAuthentication = true; | |
246 | 53 |
153 | 54 // path to add for authenticated access |
55 private String authURLPath = ""; | |
246 | 56 |
86 | 57 // DocuDirCache |
58 private DocuDirCache dirCache = null; | |
73 | 59 |
60 // DigilibConfiguration object | |
903 | 61 private DigilibServletConfiguration dlConfig; |
1 | 62 |
246 | 63 // DigilibRequest object |
903 | 64 private DigilibServletRequest dlRequest = null; |
246 | 65 |
73 | 66 /** |
67 * Constructor for DocumentBean. | |
68 */ | |
69 public DocumentBean() { | |
338 | 70 logger.debug("new DocumentBean"); |
73 | 71 } |
1 | 72 |
73 | 73 public DocumentBean(ServletConfig conf) { |
338 | 74 logger.debug("new DocumentBean"); |
73 | 75 try { |
76 setConfig(conf); | |
77 } catch (Exception e) { | |
181 | 78 logger.fatal("ERROR: Unable to read config: ", e); |
73 | 79 } |
80 } | |
1 | 81 |
73 | 82 public void setConfig(ServletConfig conf) throws ServletException { |
181 | 83 logger.debug("setConfig"); |
73 | 84 // get our ServletContext |
85 ServletContext context = conf.getServletContext(); | |
86 // see if there is a Configuration instance | |
903 | 87 dlConfig = (DigilibServletConfiguration) context |
246 | 88 .getAttribute("digilib.servlet.configuration"); |
73 | 89 if (dlConfig == null) { |
90 // create new Configuration | |
297
b74c914b48a9
Servlet version 1.5.0b -- the beginning of the next generation :-)
robcast
parents:
252
diff
changeset
|
91 throw new ServletException("ERROR: No configuration!"); |
73 | 92 } |
93 | |
86 | 94 // get cache |
153 | 95 dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); |
1 | 96 |
73 | 97 /* |
246 | 98 * authentication |
73 | 99 */ |
153 | 100 useAuthentication = dlConfig.getAsBoolean("use-authorization"); |
101 authOp = (AuthOps) dlConfig.getValue("servlet.auth.op"); | |
102 authURLPath = dlConfig.getAsString("auth-url-path"); | |
196 | 103 if (useAuthentication && (authOp == null)) { |
246 | 104 throw new ServletException( |
105 "ERROR: use-authorization configured but no AuthOp!"); | |
196 | 106 } |
73 | 107 } |
108 | |
109 /** | |
246 | 110 * check if the request must be authorized to access filepath |
73 | 111 */ |
903 | 112 public boolean isAuthRequired(DigilibServletRequest request) |
246 | 113 throws AuthOpException { |
181 | 114 logger.debug("isAuthRequired"); |
73 | 115 return useAuthentication ? authOp.isAuthRequired(request) : false; |
116 } | |
117 | |
118 /** | |
246 | 119 * check if the request is allowed to access filepath |
73 | 120 */ |
903 | 121 public boolean isAuthorized(DigilibServletRequest request) throws AuthOpException { |
181 | 122 logger.debug("isAuthorized"); |
73 | 123 return useAuthentication ? authOp.isAuthorized(request) : true; |
124 } | |
1 | 125 |
73 | 126 /** |
246 | 127 * return a list of authorization roles needed for request to access the |
128 * specified path | |
73 | 129 */ |
903 | 130 public List<String> rolesForPath(DigilibServletRequest request) throws AuthOpException { |
181 | 131 logger.debug("rolesForPath"); |
73 | 132 return useAuthentication ? authOp.rolesForPath(request) : null; |
133 } | |
1 | 134 |
73 | 135 /** |
136 * check request authorization against a list of roles | |
137 */ | |
903 | 138 public boolean isRoleAuthorized(List<String> roles, DigilibServletRequest request) { |
181 | 139 logger.debug("isRoleAuthorized"); |
246 | 140 return useAuthentication ? authOp.isRoleAuthorized(roles, request) |
141 : true; | |
73 | 142 } |
1 | 143 |
73 | 144 /** |
145 * check for authenticated access and redirect if necessary | |
146 */ | |
246 | 147 public boolean doAuthentication(HttpServletResponse response) |
148 throws Exception { | |
338 | 149 logger.debug("doAuthenication-Method"); |
246 | 150 return doAuthentication(dlRequest, response); |
151 } | |
152 | |
153 /** | |
154 * check for authenticated access and redirect if necessary | |
155 */ | |
903 | 156 public boolean doAuthentication(DigilibServletRequest request, |
246 | 157 HttpServletResponse response) throws Exception { |
181 | 158 logger.debug("doAuthentication"); |
86 | 159 if (!useAuthentication) { |
79
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
160 // shortcut if no authentication |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
161 return true; |
63c8186455c1
Servlet version 1.6b. Further cleanup and new functionality:
robcast
parents:
73
diff
changeset
|
162 } |
73 | 163 // check if we are already authenticated |
246 | 164 if (((HttpServletRequest) request.getServletRequest()).getRemoteUser() == null) { |
181 | 165 logger.debug("unauthenticated so far"); |
73 | 166 // if not maybe we must? |
167 if (isAuthRequired(request)) { | |
181 | 168 logger.debug("auth required, redirect"); |
73 | 169 // we are not yet authenticated -> redirect |
246 | 170 response.sendRedirect(authURLPath |
73 | 171 + ((HttpServletRequest) request.getServletRequest()) |
246 | 172 .getServletPath() |
73 | 173 + "?" |
174 + ((HttpServletRequest) request.getServletRequest()) | |
246 | 175 .getQueryString()); |
73 | 176 } |
177 } | |
178 return true; | |
179 } | |
1 | 180 |
73 | 181 /** |
252 | 182 * Sets the current DigilibRequest. Also completes information in the request. |
183 * | |
184 * @param dlRequest | |
185 * The dlRequest to set. | |
186 */ | |
903 | 187 public void setRequest(DigilibServletRequest dlRequest) throws Exception { |
252 | 188 this.dlRequest = dlRequest; |
189 if (dirCache == null) { | |
190 return; | |
191 } | |
192 String fn = dlRequest.getFilePath(); | |
193 // get information about the file | |
574 | 194 ImageSet fileset = (ImageSet) dirCache.getFile(fn, dlRequest |
563 | 195 .getAsInt("pn"), FileClass.IMAGE); |
252 | 196 if (fileset == null) { |
197 return; | |
198 } | |
199 // add file name | |
576
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
200 dlRequest.setValue("img.fn", fileset); |
252 | 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) { | |
347 | 207 // add pt |
252 | 208 dlRequest.setValue("pt", dd.size()); |
209 } | |
347 | 210 // get original pixel size |
576
dad720e9b12b
try: DocuDirent as interface, ImageFile inherits from ImageInput and implements DocuDirent
robcast
parents:
574
diff
changeset
|
211 ImageInput origfile = fileset.getBiggest(); |
585 | 212 // check image for size (TODO: just if mo=hires?) |
347 | 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 } | |
252 | 219 } |
220 | |
221 /** | |
246 | 222 * get the first page number in the directory (not yet functional) |
73 | 223 */ |
903 | 224 public int getFirstPage(DigilibServletRequest request) { |
181 | 225 logger.debug("getFirstPage"); |
73 | 226 return 1; |
227 } | |
1 | 228 |
73 | 229 /** |
246 | 230 * get the number of pages/files in the directory |
231 */ | |
232 public int getNumPages() throws Exception { | |
233 return getNumPages(dlRequest); | |
234 } | |
235 | |
563 | 236 /** |
237 * get the number of image pages/files in the directory | |
238 */ | |
903 | 239 public int getNumPages(DigilibServletRequest request) throws Exception { |
563 | 240 return getNumPages(request, FileClass.IMAGE); |
241 } | |
242 | |
246 | 243 /** |
563 | 244 * get the number of pages/files of type fc in the directory |
73 | 245 */ |
903 | 246 public int getNumPages(DigilibServletRequest request, FileClass fc) throws Exception { |
181 | 247 logger.debug("getNumPages"); |
246 | 248 DocuDirectory dd = (dirCache != null) ? dirCache.getDirectory(request |
249 .getFilePath()) : null; | |
86 | 250 if (dd != null) { |
563 | 251 return dd.size(fc); |
86 | 252 } |
253 return 0; | |
73 | 254 } |
1 | 255 |
73 | 256 /** |
257 * Returns the dlConfig. | |
246 | 258 * |
73 | 259 * @return DigilibConfiguration |
260 */ | |
903 | 261 public DigilibServletConfiguration getDlConfig() { |
73 | 262 return dlConfig; |
263 } | |
1 | 264 |
246 | 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 */ | |
903 | 312 public DigilibServletRequest getRequest() { |
246 | 313 return dlRequest; |
314 } | |
315 | |
338 | 316 } |