From bec21c49dd3d107e58c127b8e88091654cf2f04e Mon Sep 17 00:00:00 2001 From: Knut Forkalsrud Date: Tue, 1 Jan 2013 16:26:23 -0800 Subject: [PATCH] Simplify exception handling --- .../forkalsrud/album/web/AlbumServlet.java | 213 +++++++++--------- .../forkalsrud/album/web/PictureScaler.java | 7 +- 2 files changed, 107 insertions(+), 113 deletions(-) diff --git a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java index cbd8fd1..fd542fb 100644 --- a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java +++ b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java @@ -204,99 +204,93 @@ public class AlbumServlet req.setAttribute("base", req.getContextPath() + req.getServletPath()); req.setAttribute("mapper", new Mapper()); String pathInfo = req.getPathInfo(); - if (pathInfo != null && pathInfo.startsWith(basePrefix)) { - pathInfo = pathInfo.substring(basePrefix.length()); - } else if (pathInfo.equals("/search")) { - handleSearch(req, res, (DirectoryEntry)resolveEntry("/")); - return; - } else { - res.sendError(HttpServletResponse.SC_NOT_FOUND, "pathinfo=" + pathInfo); - return; - } - if (pathInfo.endsWith(".album")) { - pathInfo = pathInfo.substring(0, pathInfo.length() - ".album".length()); - handleAlbum(req, res, (DirectoryEntry)resolveEntry(pathInfo)); - return; - } + try { + if (pathInfo != null && pathInfo.startsWith(basePrefix)) { + pathInfo = pathInfo.substring(basePrefix.length()); + } else if (pathInfo.equals("/search")) { + handleSearch(req, res, (DirectoryEntry)resolveEntry("/")); + return; + } else { + res.sendError(HttpServletResponse.SC_NOT_FOUND, "pathinfo=" + pathInfo); + return; + } - if (pathInfo.endsWith(".photo")) { - pathInfo = pathInfo.substring(0, pathInfo.length() - ".photo".length()); - handlePhoto(req, res, (FileEntry)resolveEntry(pathInfo)); - return; - } + if (pathInfo.endsWith(".album")) { + pathInfo = pathInfo.substring(0, pathInfo.length() - ".album".length()); + handleAlbum(req, res, (DirectoryEntry)resolveEntry(pathInfo)); + return; + } - if (pathInfo.endsWith(".frame")) { - pathInfo = pathInfo.substring(0, pathInfo.length() - ".frame".length()); - handleMovieFrame(req, res, (FileEntry)resolveEntry(pathInfo)); - return; - } + if (pathInfo.endsWith(".photo")) { + pathInfo = pathInfo.substring(0, pathInfo.length() - ".photo".length()); + handlePhoto(req, res, (FileEntry)resolveEntry(pathInfo)); + return; + } - if (pathInfo.endsWith(".movie")) { - pathInfo = pathInfo.substring(0, pathInfo.length() - ".movie".length()); - handleMovie(req, res, (FileEntry)resolveEntry(pathInfo)); - return; - } + if (pathInfo.endsWith(".frame")) { + pathInfo = pathInfo.substring(0, pathInfo.length() - ".frame".length()); + handleMovieFrame(req, res, (FileEntry)resolveEntry(pathInfo)); + return; + } - if (pathInfo.endsWith(".edit")) { - pathInfo = pathInfo.substring(0, pathInfo.length() - ".edit".length()); - handleEdit(req, res, (FileEntry)resolveEntry(pathInfo)); - return; - } + if (pathInfo.endsWith(".movie")) { + pathInfo = pathInfo.substring(0, pathInfo.length() - ".movie".length()); + handleMovie(req, res, (FileEntry)resolveEntry(pathInfo)); + return; + } + + if (pathInfo.endsWith(".edit")) { + pathInfo = pathInfo.substring(0, pathInfo.length() - ".edit".length()); + handleEdit(req, res, (FileEntry)resolveEntry(pathInfo)); + return; + } - if (pathInfo.endsWith(".json")) { - pathInfo = pathInfo.substring(0, pathInfo.length() - ".json".length()); - handleJson(req, res, (DirectoryEntry)resolveEntry(pathInfo)); - return; - } + if (pathInfo.endsWith(".json")) { + pathInfo = pathInfo.substring(0, pathInfo.length() - ".json".length()); + handleJson(req, res, (DirectoryEntry)resolveEntry(pathInfo)); + return; + } - File file = new File(base, pathInfo); - if (!file.canRead()) { - res.setStatus(HttpServletResponse.SC_FORBIDDEN); - return; - } + File file = new File(base, pathInfo); + if (!file.canRead()) { + res.setStatus(HttpServletResponse.SC_FORBIDDEN); + return; + } - String size = req.getParameter("size"); - if (size != null) { + String size = req.getParameter("size"); + if (size != null) { - try { FileEntry e = (FileEntry)resolve(file); procesScaledImageRequest(req, res, file, e.getThumbnail(), size); return; - } catch (Exception e) { - throw new RuntimeException("sadness", e); } + } catch (Exception e) { + e.fillInStackTrace(); + throw new ServletException("sadness", e); } res.setStatus(HttpServletResponse.SC_NOT_FOUND); } - void handlePhoto(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { - try { - res.setContentType("text/html"); - req.setAttribute("entry", entry); - req.setAttribute("thmb", new Integer(800)); - RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm"); - rd.forward(req, res); - } catch (Exception ex) { - throw new RuntimeException("sadness", ex); - } + void handlePhoto(HttpServletRequest req, HttpServletResponse res, FileEntry entry) throws Exception { + res.setContentType("text/html"); + req.setAttribute("entry", entry); + req.setAttribute("thmb", new Integer(800)); + RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm"); + rd.forward(req, res); } - void handleAlbum(HttpServletRequest req, HttpServletResponse res, DirectoryEntry entry) { - try { - res.setContentType("text/html"); - req.setAttribute("entry", entry); - req.setAttribute("thmb", new Integer(250)); - req.setAttribute("full", new Integer(800)); - req.setAttribute("D", "$"); - RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/dynamic.vm"); - rd.forward(req, res); - } catch (Exception e) { - throw new RuntimeException("sadness", e); - } + void handleAlbum(HttpServletRequest req, HttpServletResponse res, DirectoryEntry entry) throws Exception { + res.setContentType("text/html"); + req.setAttribute("entry", entry); + req.setAttribute("thmb", new Integer(250)); + req.setAttribute("full", new Integer(800)); + req.setAttribute("D", "$"); + RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/dynamic.vm"); + rd.forward(req, res); } - void handleMovieFrame(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { + void handleMovieFrame(HttpServletRequest req, HttpServletResponse res, FileEntry entry) throws Exception { File file = entry.getPath(); if (notModified(req, file)) { @@ -326,6 +320,7 @@ public class AlbumServlet thumbDb.store(key, cimg); log.info(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + thumbDb.size() + " entries"); } catch (Exception e) { + e.fillInStackTrace(); throw new RuntimeException("sadness", e); } } @@ -381,6 +376,7 @@ public class AlbumServlet out.println(" \"name\": " + jsStr(e.getName()) + ","); out.println(" \"path\": " + jsStr(mapper.map(e.getThumbnail().getPath())) + ","); out.println(" \"type\": " + jsStr(e.getType()) + ","); + out.println(" \"thumbtype\": " + jsStr(e.getThumbnail().getType()) + ","); out.println(" \"width\": " + e.getThumbnail().getSize().getWidth() + ","); out.println(" \"height\": " + e.getThumbnail().getSize().getHeight() + ","); out.println(" \"caption\": " + jsStr(e.getCaption())); @@ -398,53 +394,46 @@ public class AlbumServlet return in == null ? "null" : "\"" + in.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\""; } - void handleEdit(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { - try { - String value = req.getParameter("value"); - if (value != null) { - File propertyFile = new File(entry.getPath().getParent(), "album.properties"); - Properties props = new Properties(); - if (propertyFile.exists()) { - FileInputStream fis = new FileInputStream(propertyFile); - props.load(fis); - fis.close(); - } - props.setProperty("file." + entry.getName() + ".caption", value); - FileOutputStream fos = new FileOutputStream(propertyFile); - props.store(fos, "online editor"); - fos.close(); - res.setContentType("text/html"); - res.getWriter().println(HtmlUtils.htmlEscape(value)); - clearDirCache(); - return; + void handleEdit(HttpServletRequest req, HttpServletResponse res, FileEntry entry) throws Exception { + String value = req.getParameter("value"); + if (value != null) { + File propertyFile = new File(entry.getPath().getParent(), "album.properties"); + Properties props = new Properties(); + if (propertyFile.exists()) { + FileInputStream fis = new FileInputStream(propertyFile); + props.load(fis); + fis.close(); } + props.setProperty("file." + entry.getName() + ".caption", value); + FileOutputStream fos = new FileOutputStream(propertyFile); + props.store(fos, "online editor"); + fos.close(); res.setContentType("text/html"); - req.setAttribute("entry", entry); - req.setAttribute("thmb", new Integer(640)); - RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/edit.vm"); - rd.forward(req, res); - } catch (Exception ex) { - throw new RuntimeException("sadness", ex); + res.getWriter().println(HtmlUtils.htmlEscape(value)); + clearDirCache(); + return; } + res.setContentType("text/html"); + req.setAttribute("entry", entry); + req.setAttribute("thmb", new Integer(640)); + RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/edit.vm"); + rd.forward(req, res); } - void handleSearch(HttpServletRequest req, HttpServletResponse res, DirectoryEntry entry) { - try { - String query = req.getParameter("q"); - SearchEngine search = new SearchEngine(entry); - SearchResults results = search.search(query); + void handleSearch(HttpServletRequest req, HttpServletResponse res, DirectoryEntry entry) throws Exception { + String query = req.getParameter("q"); - res.setContentType("text/html"); - req.setAttribute("search", query); - req.setAttribute("entry", results); - req.setAttribute("thmb", new Integer(250)); - req.setAttribute("full", new Integer(800)); - RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm"); - rd.forward(req, res); - } catch (Exception ex) { - throw new RuntimeException("sadness", ex); - } + SearchEngine search = new SearchEngine(entry); + SearchResults results = search.search(query); + + res.setContentType("text/html"); + req.setAttribute("search", query); + req.setAttribute("entry", results); + req.setAttribute("thmb", new Integer(250)); + req.setAttribute("full", new Integer(800)); + RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm"); + rd.forward(req, res); } diff --git a/src/main/java/org/forkalsrud/album/web/PictureScaler.java b/src/main/java/org/forkalsrud/album/web/PictureScaler.java index 2908a85..d63e673 100644 --- a/src/main/java/org/forkalsrud/album/web/PictureScaler.java +++ b/src/main/java/org/forkalsrud/album/web/PictureScaler.java @@ -88,7 +88,12 @@ public class PictureScaler { @Override public CachedImage call() throws Exception { - return scalePictureReally(file, thumbnail, size); + try { + return scalePictureReally(file, thumbnail, size); + } catch (Exception e) { + log.error("sadness", e); + return new CachedImage(); + } } public void setFuture(Future future) {