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.net.filter.MultipartWrapper; 32 import org.snipsnap.snip.Snip; 33 import org.snipsnap.snip.SnipFormatter; 34 import org.snipsnap.snip.SnipLink; 35 import org.snipsnap.snip.SnipSpaceFactory; 36 import org.snipsnap.user.AuthenticationService; 37 import org.snipsnap.user.User; 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 javax.servlet.http.HttpSession ; 45 import java.io.IOException ; 46 47 53 public class CommentStoreServlet extends HttpServlet { 54 public void doPost(HttpServletRequest request, HttpServletResponse response) 55 throws ServletException , IOException { 56 Configuration config = Application.get().getConfiguration(); 57 String type = request.getHeader("Content-Type"); 59 if (type != null && type.startsWith("multipart/form-data")) { 60 try { 61 request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8"); 62 } catch (IllegalArgumentException e) { 63 Logger.warn("CommentStoreServlet: multipart/form-data wrapper:" + e.getMessage()); 64 } 65 } 66 67 String name = request.getParameter("comment"); 68 String content = request.getParameter("content"); 69 Snip snip = SnipSpaceFactory.getInstance().load(name); 70 71 if (request.getParameter("preview") != null) { 72 request.setAttribute("snip", snip); 73 request.setAttribute("preview", SnipFormatter.toXML(snip, content)); 74 request.setAttribute("content", content); 75 request.setAttribute("comment", name); 76 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/comment.jsp"); 77 dispatcher.forward(request, response); 78 return; 79 } else if (request.getParameter("cancel") == null) { 80 81 HttpSession session = request.getSession(); 82 if (session != null) { 83 User user = Application.get().getUser(); 84 AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class); 85 86 if (snip != null && service.isAuthenticated(user)) { 87 snip.getComments().postComment(content); 88 } else { 89 response.sendError(HttpServletResponse.SC_FORBIDDEN); 90 return; 91 } 92 } 93 } else if (snip == null) { 94 response.sendRedirect(sanitize(request.getParameter("referer"))); 96 return; 97 } 98 99 response.sendRedirect(config.getUrl("/comments/" + SnipLink.encode(name))); 100 } 101 102 private String sanitize(String parameter) { 103 if (null != parameter) { 104 return parameter.split("[\r\n]")[0]; 105 } 106 return parameter; 107 } 108 } 109 | Popular Tags |