comparison servlet/src/digilib/servlet/Scaler.java @ 558:2e971b9f3022 digilibPDF

added some enums
author robcast
date Fri, 17 Dec 2010 00:32:18 +0100
parents 0885f5ca5b24
children 686086d6e6d6
comparison
equal deleted inserted replaced
557:0885f5ca5b24 558:2e971b9f3022
31 public class Scaler extends HttpServlet { 31 public class Scaler extends HttpServlet {
32 32
33 /** digilib servlet version (for all components) */ 33 /** digilib servlet version (for all components) */
34 public static final String dlVersion = "1.9.0a"; 34 public static final String dlVersion = "1.9.0a";
35 35
36 /** general error code */ 36 /** servlet error codes */
37 public static final int ERROR_UNKNOWN = 0; 37 public static enum Error {UNKNOWN, AUTH, FILE, IMAGE};
38 38
39 /** error code for authentication error */ 39 /** type of error message */
40 public static final int ERROR_AUTH = 1; 40 public static enum ErrMsg {IMAGE, TEXT, CODE};
41 41
42 /** error code for file operation error */
43 public static final int ERROR_FILE = 2;
44
45 /** error code for image operation error */
46 public static final int ERROR_IMAGE = 3;
47
48 /** logger for accounting requests */ 42 /** logger for accounting requests */
49 protected static Logger accountlog = Logger.getLogger("account.request"); 43 protected static Logger accountlog = Logger.getLogger("account.request");
50 44
51 /** gengeral logger for this class */ 45 /** gengeral logger for this class */
52 protected static Logger logger = Logger.getLogger("digilib.servlet"); 46 protected static Logger logger = Logger.getLogger("digilib.servlet");
186 // parse request 180 // parse request
187 DigilibRequest dlRequest = new DigilibRequest(request); 181 DigilibRequest dlRequest = new DigilibRequest(request);
188 // extract the job information 182 // extract the job information
189 ImageJobDescription jobTicket = ImageJobDescription.getInstance(dlRequest, dlConfig); 183 ImageJobDescription jobTicket = ImageJobDescription.getInstance(dlRequest, dlConfig);
190 184
185 // type of error reporting
186 ErrMsg errMsgType = ErrMsg.IMAGE;
187 if (dlRequest.hasOption("errtxt")) {
188 errMsgType = ErrMsg.TEXT;
189 } else if (dlRequest.hasOption("errcode")) {
190 errMsgType = ErrMsg.CODE;
191 }
192
191 try { 193 try {
192 /* 194 /*
193 * check if we can fast-track without scaling 195 * check if we can fast-track without scaling
194 */ 196 */
195 ImageFile fileToLoad = jobTicket.getFileToLoad(); 197 ImageFile fileToLoad = jobTicket.getFileToLoad();
247 logger.debug("Job Processing Time: " 249 logger.debug("Job Processing Time: "
248 + (System.currentTimeMillis() - startTime) + "ms"); 250 + (System.currentTimeMillis() - startTime) + "ms");
249 251
250 } catch (IOException e) { 252 } catch (IOException e) {
251 logger.error(e.getClass() + ": " + e.getMessage()); 253 logger.error(e.getClass() + ": " + e.getMessage());
252 digilibError(dlRequest.hasOption("errtxt"), dlRequest.hasOption("errimg"), dlRequest.hasOption("errcode"), ERROR_FILE, null, response); 254 digilibError(errMsgType, Error.FILE, null, response);
253 } catch (AuthOpException e) { 255 } catch (AuthOpException e) {
254 logger.error(e.getClass() + ": " + e.getMessage()); 256 logger.error(e.getClass() + ": " + e.getMessage());
255 digilibError(dlRequest.hasOption("errtxt"), dlRequest.hasOption("errimg"), dlRequest.hasOption("errcode"), ERROR_AUTH, null, response); 257 digilibError(errMsgType, Error.AUTH, null, response);
256 } catch (InterruptedException e) { 258 } catch (InterruptedException e) {
257 logger.error(e.getClass() + ": " + e.getMessage()); 259 logger.error(e.getClass() + ": " + e.getMessage());
258 } catch (ExecutionException e) { 260 } catch (ExecutionException e) {
259 logger.error(e.getClass() + ": " + e.getMessage()); 261 logger.error(e.getClass() + ": " + e.getMessage());
260 String causeMsg = e.getCause().getMessage(); 262 String causeMsg = e.getCause().getMessage();
261 logger.error("caused by: " + causeMsg); 263 logger.error("caused by: " + causeMsg);
262 digilibError(dlRequest.hasOption("errtxt"), dlRequest.hasOption("errimg"), dlRequest.hasOption("errcode"), ERROR_IMAGE, causeMsg, response); 264 digilibError(errMsgType, Error.IMAGE, causeMsg, response);
263 } 265 }
264 266
265 } 267 }
266 268
267 /** 269 /**
268 * Sends an error to the client as text or image. 270 * Sends an error to the client as text or image.
269 * 271 *
270 * @param asText
271 * @param type 272 * @param type
273 * @param error
272 * @param msg 274 * @param msg
273 * @param response 275 * @param response
274 */ 276 */
275 public void digilibError(boolean asText, boolean asImage, boolean asCode, int type, String msg, 277 public void digilibError(ErrMsg type, Error error, String msg,
276 HttpServletResponse response) { 278 HttpServletResponse response) {
277 try { 279 try {
278 File img = null; 280 File img = null;
279 int status = 0; 281 int status = 0;
280 if (type == ERROR_AUTH) { 282 if (error == Error.AUTH) {
281 if (msg == null) { 283 if (msg == null) {
282 msg = "ERROR: Unauthorized access!"; 284 msg = "ERROR: Unauthorized access!";
283 } 285 }
284 img = denyImgFile; 286 img = denyImgFile;
285 status = HttpServletResponse.SC_FORBIDDEN; 287 status = HttpServletResponse.SC_FORBIDDEN;
286 } else if (type == ERROR_FILE) { 288 } else if (error == Error.FILE) {
287 if (msg == null) { 289 if (msg == null) {
288 msg = "ERROR: Image file not found!"; 290 msg = "ERROR: Image file not found!";
289 } 291 }
290 img = notfoundImgFile; 292 img = notfoundImgFile;
291 status = HttpServletResponse.SC_NOT_FOUND; 293 status = HttpServletResponse.SC_NOT_FOUND;
294 msg = "ERROR: Other image error!"; 296 msg = "ERROR: Other image error!";
295 } 297 }
296 img = this.errorImgFile; 298 img = this.errorImgFile;
297 status = HttpServletResponse.SC_BAD_REQUEST; 299 status = HttpServletResponse.SC_BAD_REQUEST;
298 } 300 }
299 if (asText) { 301 if (type == ErrMsg.TEXT) {
300 ServletOps.htmlMessage(msg, response); 302 ServletOps.htmlMessage(msg, response);
301 } else if (asCode) { 303 } else if (type == ErrMsg.CODE) {
302 response.sendError(status, msg); 304 response.sendError(status, msg);
303 } else if (img != null) { 305 } else if (img != null) {
304 // default: image 306 // default: image
305 ServletOps.sendFile(img, null, null, response); 307 ServletOps.sendFile(img, null, null, response);
306 } 308 }