Simplify exception handling

This commit is contained in:
Knut Forkalsrud 2013-01-01 16:26:23 -08:00
parent 5415459087
commit bec21c49dd
2 changed files with 107 additions and 113 deletions

View file

@ -204,99 +204,93 @@ public class AlbumServlet
req.setAttribute("base", req.getContextPath() + req.getServletPath()); req.setAttribute("base", req.getContextPath() + req.getServletPath());
req.setAttribute("mapper", new Mapper()); req.setAttribute("mapper", new Mapper());
String pathInfo = req.getPathInfo(); 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")) { try {
pathInfo = pathInfo.substring(0, pathInfo.length() - ".album".length()); if (pathInfo != null && pathInfo.startsWith(basePrefix)) {
handleAlbum(req, res, (DirectoryEntry)resolveEntry(pathInfo)); pathInfo = pathInfo.substring(basePrefix.length());
return; } 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")) { if (pathInfo.endsWith(".album")) {
pathInfo = pathInfo.substring(0, pathInfo.length() - ".photo".length()); pathInfo = pathInfo.substring(0, pathInfo.length() - ".album".length());
handlePhoto(req, res, (FileEntry)resolveEntry(pathInfo)); handleAlbum(req, res, (DirectoryEntry)resolveEntry(pathInfo));
return; return;
} }
if (pathInfo.endsWith(".frame")) { if (pathInfo.endsWith(".photo")) {
pathInfo = pathInfo.substring(0, pathInfo.length() - ".frame".length()); pathInfo = pathInfo.substring(0, pathInfo.length() - ".photo".length());
handleMovieFrame(req, res, (FileEntry)resolveEntry(pathInfo)); handlePhoto(req, res, (FileEntry)resolveEntry(pathInfo));
return; return;
} }
if (pathInfo.endsWith(".movie")) { if (pathInfo.endsWith(".frame")) {
pathInfo = pathInfo.substring(0, pathInfo.length() - ".movie".length()); pathInfo = pathInfo.substring(0, pathInfo.length() - ".frame".length());
handleMovie(req, res, (FileEntry)resolveEntry(pathInfo)); handleMovieFrame(req, res, (FileEntry)resolveEntry(pathInfo));
return; return;
} }
if (pathInfo.endsWith(".edit")) { if (pathInfo.endsWith(".movie")) {
pathInfo = pathInfo.substring(0, pathInfo.length() - ".edit".length()); pathInfo = pathInfo.substring(0, pathInfo.length() - ".movie".length());
handleEdit(req, res, (FileEntry)resolveEntry(pathInfo)); handleMovie(req, res, (FileEntry)resolveEntry(pathInfo));
return; return;
} }
if (pathInfo.endsWith(".edit")) {
pathInfo = pathInfo.substring(0, pathInfo.length() - ".edit".length());
handleEdit(req, res, (FileEntry)resolveEntry(pathInfo));
return;
}
if (pathInfo.endsWith(".json")) { if (pathInfo.endsWith(".json")) {
pathInfo = pathInfo.substring(0, pathInfo.length() - ".json".length()); pathInfo = pathInfo.substring(0, pathInfo.length() - ".json".length());
handleJson(req, res, (DirectoryEntry)resolveEntry(pathInfo)); handleJson(req, res, (DirectoryEntry)resolveEntry(pathInfo));
return; return;
} }
File file = new File(base, pathInfo); File file = new File(base, pathInfo);
if (!file.canRead()) { if (!file.canRead()) {
res.setStatus(HttpServletResponse.SC_FORBIDDEN); res.setStatus(HttpServletResponse.SC_FORBIDDEN);
return; return;
} }
String size = req.getParameter("size"); String size = req.getParameter("size");
if (size != null) { if (size != null) {
try {
FileEntry e = (FileEntry)resolve(file); FileEntry e = (FileEntry)resolve(file);
procesScaledImageRequest(req, res, file, e.getThumbnail(), size); procesScaledImageRequest(req, res, file, e.getThumbnail(), size);
return; 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); res.setStatus(HttpServletResponse.SC_NOT_FOUND);
} }
void handlePhoto(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { void handlePhoto(HttpServletRequest req, HttpServletResponse res, FileEntry entry) throws Exception {
try { res.setContentType("text/html");
res.setContentType("text/html"); req.setAttribute("entry", entry);
req.setAttribute("entry", entry); req.setAttribute("thmb", new Integer(800));
req.setAttribute("thmb", new Integer(800)); RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm");
RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm"); rd.forward(req, res);
rd.forward(req, res);
} catch (Exception ex) {
throw new RuntimeException("sadness", ex);
}
} }
void handleAlbum(HttpServletRequest req, HttpServletResponse res, DirectoryEntry entry) { void handleAlbum(HttpServletRequest req, HttpServletResponse res, DirectoryEntry entry) throws Exception {
try { res.setContentType("text/html");
res.setContentType("text/html"); req.setAttribute("entry", entry);
req.setAttribute("entry", entry); req.setAttribute("thmb", new Integer(250));
req.setAttribute("thmb", new Integer(250)); req.setAttribute("full", new Integer(800));
req.setAttribute("full", new Integer(800)); req.setAttribute("D", "$");
req.setAttribute("D", "$"); RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/dynamic.vm");
RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/dynamic.vm"); rd.forward(req, res);
rd.forward(req, res);
} catch (Exception e) {
throw new RuntimeException("sadness", e);
}
} }
void handleMovieFrame(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { void handleMovieFrame(HttpServletRequest req, HttpServletResponse res, FileEntry entry) throws Exception {
File file = entry.getPath(); File file = entry.getPath();
if (notModified(req, file)) { if (notModified(req, file)) {
@ -326,6 +320,7 @@ public class AlbumServlet
thumbDb.store(key, cimg); thumbDb.store(key, cimg);
log.info(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + thumbDb.size() + " entries"); log.info(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + thumbDb.size() + " entries");
} catch (Exception e) { } catch (Exception e) {
e.fillInStackTrace();
throw new RuntimeException("sadness", e); throw new RuntimeException("sadness", e);
} }
} }
@ -381,6 +376,7 @@ public class AlbumServlet
out.println(" \"name\": " + jsStr(e.getName()) + ","); out.println(" \"name\": " + jsStr(e.getName()) + ",");
out.println(" \"path\": " + jsStr(mapper.map(e.getThumbnail().getPath())) + ","); out.println(" \"path\": " + jsStr(mapper.map(e.getThumbnail().getPath())) + ",");
out.println(" \"type\": " + jsStr(e.getType()) + ","); out.println(" \"type\": " + jsStr(e.getType()) + ",");
out.println(" \"thumbtype\": " + jsStr(e.getThumbnail().getType()) + ",");
out.println(" \"width\": " + e.getThumbnail().getSize().getWidth() + ","); out.println(" \"width\": " + e.getThumbnail().getSize().getWidth() + ",");
out.println(" \"height\": " + e.getThumbnail().getSize().getHeight() + ","); out.println(" \"height\": " + e.getThumbnail().getSize().getHeight() + ",");
out.println(" \"caption\": " + jsStr(e.getCaption())); out.println(" \"caption\": " + jsStr(e.getCaption()));
@ -398,53 +394,46 @@ public class AlbumServlet
return in == null ? "null" : "\"" + in.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\""; return in == null ? "null" : "\"" + in.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\"";
} }
void handleEdit(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { void handleEdit(HttpServletRequest req, HttpServletResponse res, FileEntry entry) throws Exception {
try { String value = req.getParameter("value");
String value = req.getParameter("value"); if (value != null) {
if (value != null) { File propertyFile = new File(entry.getPath().getParent(), "album.properties");
File propertyFile = new File(entry.getPath().getParent(), "album.properties"); Properties props = new Properties();
Properties props = new Properties(); if (propertyFile.exists()) {
if (propertyFile.exists()) { FileInputStream fis = new FileInputStream(propertyFile);
FileInputStream fis = new FileInputStream(propertyFile); props.load(fis);
props.load(fis); fis.close();
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;
} }
props.setProperty("file." + entry.getName() + ".caption", value);
FileOutputStream fos = new FileOutputStream(propertyFile);
props.store(fos, "online editor");
fos.close();
res.setContentType("text/html"); res.setContentType("text/html");
req.setAttribute("entry", entry); res.getWriter().println(HtmlUtils.htmlEscape(value));
req.setAttribute("thmb", new Integer(640)); clearDirCache();
RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/edit.vm"); return;
rd.forward(req, res);
} catch (Exception ex) {
throw new RuntimeException("sadness", ex);
} }
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); void handleSearch(HttpServletRequest req, HttpServletResponse res, DirectoryEntry entry) throws Exception {
SearchResults results = search.search(query); String query = req.getParameter("q");
res.setContentType("text/html"); SearchEngine search = new SearchEngine(entry);
req.setAttribute("search", query); SearchResults results = search.search(query);
req.setAttribute("entry", results);
req.setAttribute("thmb", new Integer(250)); res.setContentType("text/html");
req.setAttribute("full", new Integer(800)); req.setAttribute("search", query);
RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm"); req.setAttribute("entry", results);
rd.forward(req, res); req.setAttribute("thmb", new Integer(250));
} catch (Exception ex) { req.setAttribute("full", new Integer(800));
throw new RuntimeException("sadness", ex); RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm");
} rd.forward(req, res);
} }

View file

@ -88,7 +88,12 @@ public class PictureScaler {
@Override @Override
public CachedImage call() throws Exception { 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<CachedImage> future) { public void setFuture(Future<CachedImage> future) {