1 25 package org.snipsnap.net; 26 27 import org.radeox.util.logging.Logger; 28 import org.snipsnap.app.Application; 29 import org.snipsnap.config.Configuration; 30 import org.snipsnap.container.Components; 31 import org.snipsnap.snip.Snip; 32 import org.snipsnap.snip.SnipSpace; 33 import org.snipsnap.snip.label.TypeLabel; 34 import org.snipsnap.user.AuthenticationService; 35 import org.snipsnap.user.Roles; 36 import org.snipsnap.user.User; 37 import org.snipsnap.util.URLEncoderDecoder; 38 39 import javax.servlet.RequestDispatcher ; 40 import javax.servlet.ServletException ; 41 import javax.servlet.http.HttpServlet ; 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.http.HttpServletResponse ; 44 import java.io.IOException ; 45 import java.util.Collection ; 46 import java.util.Iterator ; 47 import java.util.Map ; 48 49 55 public class SnipViewServlet extends HttpServlet { 56 private final static Roles authRoles = new Roles(Roles.AUTHENTICATED); 57 58 protected void doHead(HttpServletRequest request, HttpServletResponse response) 59 throws ServletException , IOException { 60 doGet(request, response); 61 } 62 63 public void doGet(HttpServletRequest request, HttpServletResponse response) 64 throws IOException , ServletException { 65 66 Configuration config = Application.get().getConfiguration(); 67 User user = Application.get().getUser(); 68 AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class); 69 70 if (service.isAuthenticated(user)) { 71 user.lastAccess(); 72 } 73 74 String name = request.getPathInfo(); 76 if (null == name || "/".equals(name)) { 77 name = config.getStartSnip(); 78 } else { 79 name = name.substring(1); 80 } 81 String encodedSpace = config.getEncodedSpace(); 82 if (encodedSpace != null && encodedSpace.length() > 0) { 83 name = name.replace(encodedSpace.charAt(0), ' '); 84 } 85 87 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 89 Snip snip = space.load(name); 90 91 String subname = null; 92 if (null == snip) { 93 int slashIndex = name.lastIndexOf('/'); 95 if (slashIndex != -1) { 96 subname = name.substring(slashIndex + 1); 97 name = name.substring(0, slashIndex); 98 Logger.log(Logger.DEBUG, name + ": attachment: " + subname); 99 } 100 snip = space.load(name); 101 } 102 103 request.setAttribute("snip", snip); 104 106 if (subname != null && subname.length() > 0) { 107 try { 108 request.setAttribute(FileDownloadServlet.FILENAME, subname); 109 RequestDispatcher dispatcher = 110 getServletContext().getNamedDispatcher("org.snipsnap.net.FileDownloadServlet"); 111 dispatcher.forward(request, response); 112 return; 113 } catch (ServletException e) { 114 name = name + "/" + subname; 116 snip = null; 117 } 118 } 119 120 if ("HEAD".equals(request.getMethod())) { 122 response.setStatus(HttpServletResponse.SC_OK); 123 return; 124 } 125 126 if (null == snip) { 128 if (config.allow(Configuration.APP_PERM_CREATESNIP)) { 129 response.sendRedirect(config.getUrl("/exec/edit?name=" + URLEncoderDecoder.encode(name, config.getEncoding()))); 130 } else { 131 if ("snipsnap-notfound".equals(name)) { 132 response.sendError(HttpServletResponse.SC_NOT_FOUND, 133 "Internal Error: could not find snipsnap-notfound page." 134 + "This may indicate that either the installation has failed or the Database is corrupted."); 135 return; 136 } 137 response.sendRedirect(config.getUrl("/space/snipsnap-notfound?name=" + URLEncoderDecoder.encode(name, config.getEncoding()))); 138 } 139 return; 140 } 141 142 String viewHandler = null; 143 String type = null; 144 Collection mimeTypes = snip.getLabels().getLabels("TypeLabel"); 145 if (!mimeTypes.isEmpty()) { 146 Iterator handlerIt = mimeTypes.iterator(); 147 while (handlerIt.hasNext()) { 148 TypeLabel typeLabel = (TypeLabel) handlerIt.next(); 149 viewHandler = typeLabel.getViewHandler(); 150 if (null == viewHandler) { 152 viewHandler = TypeLabel.getViewHandler(typeLabel.getTypeValue()); 153 } 154 155 if (null != viewHandler) { 156 type = typeLabel.getTypeValue(); 157 request.setAttribute("view_handler", viewHandler); 158 request.setAttribute("mime_type", type); 159 break; 160 } 161 } 162 } 163 164 Application app = Application.get(); 165 Map params = app.getParameters(); 166 params.put("viewed", snip); 167 params.put("RSS", params.get("RSS") + "?snip=" + snip.getNameEncoded()); 168 169 snip.handle(request); 170 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/snip.jsp"); 171 dispatcher.forward(request, response); 172 } 173 } 174 | Popular Tags |