Mercurial > hg > digilib-old
comparison servlet2/src/main/java/digilib/servlet/ScalerNoThread.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 package digilib.servlet; | |
| 2 | |
| 3 import java.io.File; | |
| 4 import java.io.IOException; | |
| 5 import java.util.List; | |
| 6 | |
| 7 import javax.servlet.ServletConfig; | |
| 8 import javax.servlet.ServletContext; | |
| 9 import javax.servlet.ServletException; | |
| 10 import javax.servlet.http.HttpServlet; | |
| 11 import javax.servlet.http.HttpServletRequest; | |
| 12 import javax.servlet.http.HttpServletResponse; | |
| 13 | |
| 14 import org.apache.log4j.Logger; | |
| 15 | |
| 16 import digilib.auth.AuthOpException; | |
| 17 import digilib.auth.AuthOps; | |
| 18 import digilib.image.DocuImage; | |
| 19 import digilib.image.ImageJobDescription; | |
| 20 import digilib.image.ImageOpException; | |
| 21 import digilib.image.ImageWorker; | |
| 22 import digilib.io.DocuDirCache; | |
| 23 import digilib.io.DocuDirectory; | |
| 24 import digilib.io.ImageInput; | |
| 25 | |
| 26 /** | |
| 27 * Version of Scaler servlet that doesn't use a thread pool. | |
| 28 */ | |
| 29 public class ScalerNoThread extends HttpServlet { | |
| 30 | |
| 31 private static final long serialVersionUID = 1450947819851623306L; | |
| 32 | |
| 33 /** digilib servlet version (for all components) */ | |
| 34 public static final String version = "1.9.0a5 nothread"; | |
| 35 | |
| 36 /** servlet error codes */ | |
| 37 public static enum Error {UNKNOWN, AUTH, FILE, IMAGE}; | |
| 38 | |
| 39 /** type of error message */ | |
| 40 public static enum ErrMsg {IMAGE, TEXT, CODE}; | |
| 41 | |
| 42 /** logger for accounting requests */ | |
| 43 protected static Logger accountlog = Logger.getLogger("account.request"); | |
| 44 | |
| 45 /** gengeral logger for this class */ | |
| 46 protected static Logger logger = Logger.getLogger("digilib.scaler"); | |
| 47 | |
| 48 /** logger for authentication related */ | |
| 49 protected static Logger authlog = Logger.getLogger("digilib.auth"); | |
| 50 | |
| 51 /** DocuDirCache instance */ | |
| 52 protected DocuDirCache dirCache; | |
| 53 | |
| 54 /** authentication error image file */ | |
| 55 public static File denyImgFile; | |
| 56 | |
| 57 /** image error image file */ | |
| 58 public static File errorImgFile; | |
| 59 | |
| 60 /** not found error image file */ | |
| 61 public static File notfoundImgFile; | |
| 62 | |
| 63 /** send files as is? */ | |
| 64 protected boolean sendFileAllowed = true; | |
| 65 | |
| 66 /** DigilibConfiguration instance */ | |
| 67 protected DigilibServletConfiguration dlConfig; | |
| 68 | |
| 69 /** use authorization database */ | |
| 70 protected boolean useAuthorization = true; | |
| 71 | |
| 72 /** AuthOps instance */ | |
| 73 protected AuthOps authOp; | |
| 74 | |
| 75 /** | |
| 76 * Initialisation on first run. | |
| 77 * | |
| 78 * @throws ServletException | |
| 79 * | |
| 80 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) | |
| 81 */ | |
| 82 public void init(ServletConfig config) throws ServletException { | |
| 83 super.init(config); | |
| 84 | |
| 85 System.out | |
| 86 .println("***** Digital Image Library Image Scaler Servlet (version " | |
| 87 + version + ") *****"); | |
| 88 // say hello in the log file | |
| 89 logger.info("***** Digital Image Library Image Scaler Servlet (version " | |
| 90 + version + ") *****"); | |
| 91 | |
| 92 // get our ServletContext | |
| 93 ServletContext context = config.getServletContext(); | |
| 94 // see if there is a Configuration instance | |
| 95 dlConfig = (DigilibServletConfiguration) context | |
| 96 .getAttribute("digilib.servlet.configuration"); | |
| 97 if (dlConfig == null) { | |
| 98 // no Configuration | |
| 99 throw new ServletException("No Configuration!"); | |
| 100 } | |
| 101 // set our AuthOps | |
| 102 useAuthorization = dlConfig.getAsBoolean("use-authorization"); | |
| 103 authOp = (AuthOps) dlConfig.getValue("servlet.auth.op"); | |
| 104 | |
| 105 // DocuDirCache instance | |
| 106 dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); | |
| 107 | |
| 108 denyImgFile = ServletOps.getFile( | |
| 109 (File) dlConfig.getValue("denied-image"), context); | |
| 110 errorImgFile = ServletOps.getFile( | |
| 111 (File) dlConfig.getValue("error-image"), context); | |
| 112 notfoundImgFile = ServletOps.getFile( | |
| 113 (File) dlConfig.getValue("notfound-image"), context); | |
| 114 sendFileAllowed = dlConfig.getAsBoolean("sendfile-allowed"); | |
| 115 } | |
| 116 | |
| 117 /** Returns modification time relevant to the request for caching. | |
| 118 * | |
| 119 * @see javax.servlet.http.HttpServlet#getLastModified(javax.servlet.http.HttpServletRequest) | |
| 120 */ | |
| 121 public long getLastModified(HttpServletRequest request) { | |
| 122 accountlog.debug("GetLastModified from " + request.getRemoteAddr() | |
| 123 + " for " + request.getQueryString()); | |
| 124 long mtime = -1; | |
| 125 // create new request | |
| 126 DigilibServletRequest dlReq = new DigilibServletRequest(request); | |
| 127 DocuDirectory dd = dirCache.getDirectory(dlReq.getFilePath()); | |
| 128 if (dd != null) { | |
| 129 mtime = dd.getDirMTime() / 1000 * 1000; | |
| 130 } | |
| 131 logger.debug(" returns "+mtime); | |
| 132 return mtime; | |
| 133 } | |
| 134 | |
| 135 /* (non-Javadoc) | |
| 136 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) | |
| 137 */ | |
| 138 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { | |
| 139 accountlog.info("GET from " + request.getRemoteAddr()); | |
| 140 this.processRequest(request, response); | |
| 141 } | |
| 142 | |
| 143 | |
| 144 /* (non-Javadoc) | |
| 145 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) | |
| 146 */ | |
| 147 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException { | |
| 148 accountlog.info("POST from " + request.getRemoteAddr()); | |
| 149 this.processRequest(request, response); | |
| 150 } | |
| 151 | |
| 152 | |
| 153 protected void doHead(HttpServletRequest req, HttpServletResponse resp) | |
| 154 throws ServletException, IOException { | |
| 155 logger.debug("HEAD from "+req.getRemoteAddr()); | |
| 156 super.doHead(req, resp); | |
| 157 } | |
| 158 | |
| 159 protected void doOptions(HttpServletRequest req, HttpServletResponse resp) | |
| 160 throws ServletException, IOException { | |
| 161 logger.debug("OPTIONS from "+req.getRemoteAddr()); | |
| 162 super.doOptions(req, resp); | |
| 163 } | |
| 164 | |
| 165 /** Service this request using the response. | |
| 166 * @param request | |
| 167 * @param response | |
| 168 * @throws ServletException | |
| 169 */ | |
| 170 public void processRequest(HttpServletRequest request, | |
| 171 HttpServletResponse response) throws ServletException { | |
| 172 | |
| 173 if (dlConfig == null) { | |
| 174 logger.error("ERROR: No Configuration!"); | |
| 175 throw new ServletException("NO VALID digilib CONFIGURATION!"); | |
| 176 } | |
| 177 | |
| 178 accountlog.debug("request: " + request.getQueryString()); | |
| 179 logger.debug("request: " + request.getQueryString()); | |
| 180 long startTime = System.currentTimeMillis(); | |
| 181 | |
| 182 // parse request | |
| 183 DigilibServletRequest dlRequest = new DigilibServletRequest(request); | |
| 184 // extract the job information | |
| 185 ImageJobDescription jobTicket = ImageJobDescription.getInstance(dlRequest, dlConfig); | |
| 186 | |
| 187 // type of error reporting | |
| 188 ErrMsg errMsgType = ErrMsg.IMAGE; | |
| 189 if (dlRequest.hasOption("errtxt")) { | |
| 190 errMsgType = ErrMsg.TEXT; | |
| 191 } else if (dlRequest.hasOption("errcode")) { | |
| 192 errMsgType = ErrMsg.CODE; | |
| 193 } | |
| 194 | |
| 195 try { | |
| 196 /* | |
| 197 * check if we can fast-track without scaling | |
| 198 */ | |
| 199 ImageInput fileToLoad = (ImageInput) jobTicket.getInput(); | |
| 200 | |
| 201 // check permissions | |
| 202 if (useAuthorization) { | |
| 203 // get a list of required roles (empty if no restrictions) | |
| 204 List<String> rolesRequired = authOp.rolesForPath( | |
| 205 jobTicket.getFilePath(), request); | |
| 206 if (rolesRequired != null) { | |
| 207 authlog.debug("Role required: " + rolesRequired); | |
| 208 authlog.debug("User: " + request.getRemoteUser()); | |
| 209 // is the current request/user authorized? | |
| 210 if (!authOp.isRoleAuthorized(rolesRequired, request)) { | |
| 211 // send deny answer and abort | |
| 212 throw new AuthOpException(); | |
| 213 } | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 // if requested, send image as a file | |
| 218 if (sendFileAllowed && jobTicket.getSendAsFile()) { | |
| 219 String mt = null; | |
| 220 if (jobTicket.hasOption("rawfile")) { | |
| 221 mt = "application/octet-stream"; | |
| 222 } | |
| 223 logger.debug("Sending RAW File as is."); | |
| 224 ServletOps.sendFile(fileToLoad.getFile(), mt, null, response, logger); | |
| 225 logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms"); | |
| 226 return; | |
| 227 } | |
| 228 | |
| 229 // if possible, send the image without actually having to transform it | |
| 230 if (! jobTicket.isTransformRequired()) { | |
| 231 logger.debug("Sending File as is."); | |
| 232 ServletOps.sendFile(fileToLoad.getFile(), null, null, response, logger); | |
| 233 logger.info("Done in " + (System.currentTimeMillis() - startTime) + "ms"); | |
| 234 return; | |
| 235 } | |
| 236 | |
| 237 // create job | |
| 238 ImageWorker job = new ImageWorker(dlConfig, jobTicket); | |
| 239 // get result immediately | |
| 240 DocuImage img = job.call(); | |
| 241 // forced destination image type | |
| 242 String mt = null; | |
| 243 if (jobTicket.hasOption("jpg")) { | |
| 244 mt = "image/jpeg"; | |
| 245 } else if (jobTicket.hasOption("png")) { | |
| 246 mt = "image/png"; | |
| 247 } | |
| 248 // send image | |
| 249 ServletOps.sendImage(img, mt, response, logger); | |
| 250 logger.debug("Job Processing Time: " | |
| 251 + (System.currentTimeMillis() - startTime) + "ms"); | |
| 252 | |
| 253 } catch (ImageOpException e) { | |
| 254 logger.error(e.getClass() + ": " + e.getMessage()); | |
| 255 digilibError(errMsgType, Error.IMAGE, null, response); | |
| 256 } catch (IOException e) { | |
| 257 logger.error(e.getClass() + ": " + e.getMessage()); | |
| 258 digilibError(errMsgType, Error.FILE, null, response); | |
| 259 } catch (AuthOpException e) { | |
| 260 logger.error(e.getClass() + ": " + e.getMessage()); | |
| 261 digilibError(errMsgType, Error.AUTH, null, response); | |
| 262 } | |
| 263 | |
| 264 } | |
| 265 | |
| 266 /** | |
| 267 * Sends an error to the client as text or image. | |
| 268 * | |
| 269 * @param type | |
| 270 * @param error | |
| 271 * @param msg | |
| 272 * @param response | |
| 273 */ | |
| 274 public static void digilibError(ErrMsg type, Error error, String msg, | |
| 275 HttpServletResponse response) { | |
| 276 try { | |
| 277 File img = null; | |
| 278 int status = 0; | |
| 279 if (error == Error.AUTH) { | |
| 280 if (msg == null) { | |
| 281 msg = "ERROR: Unauthorized access!"; | |
| 282 } | |
| 283 img = denyImgFile; | |
| 284 status = HttpServletResponse.SC_FORBIDDEN; | |
| 285 } else if (error == Error.FILE) { | |
| 286 if (msg == null) { | |
| 287 msg = "ERROR: Image file not found!"; | |
| 288 } | |
| 289 img = notfoundImgFile; | |
| 290 status = HttpServletResponse.SC_NOT_FOUND; | |
| 291 } else { | |
| 292 if (msg == null) { | |
| 293 msg = "ERROR: Other image error!"; | |
| 294 } | |
| 295 img = errorImgFile; | |
| 296 status = HttpServletResponse.SC_BAD_REQUEST; | |
| 297 } | |
| 298 if (response.isCommitted()) { | |
| 299 // response already committed | |
| 300 logger.error("Unable to send error: " + msg); | |
| 301 return; | |
| 302 } | |
| 303 if (type == ErrMsg.TEXT) { | |
| 304 ServletOps.htmlMessage(msg, response); | |
| 305 } else if (type == ErrMsg.CODE) { | |
| 306 response.sendError(status, msg); | |
| 307 } else if (img != null) { | |
| 308 // default: image | |
| 309 ServletOps.sendFile(img, null, null, response, logger); | |
| 310 } | |
| 311 } catch (Exception e) { | |
| 312 logger.error("Error sending error!", e); | |
| 313 } | |
| 314 | |
| 315 } | |
| 316 | |
| 317 public static String getVersion() { | |
| 318 return version; | |
| 319 } | |
| 320 | |
| 321 } |
