1 package org.snipsnap.net; 2 3 27 28 import org.snipsnap.app.Application; 29 import org.snipsnap.config.Configuration; 30 import org.snipsnap.snip.Blog; 31 import org.snipsnap.snip.BlogKit; 32 import org.snipsnap.snip.SnipFormatter; 33 import org.snipsnap.snip.SnipLink; 34 import org.snipsnap.snip.SnipSpaceFactory; 35 import org.snipsnap.user.Roles; 36 import org.snipsnap.user.Security; 37 import org.snipsnap.user.User; 38 import org.snipsnap.net.filter.MultipartWrapper; 39 import org.radeox.util.logging.Logger; 40 41 import javax.servlet.RequestDispatcher ; 42 import javax.servlet.ServletException ; 43 import javax.servlet.http.HttpServlet ; 44 import javax.servlet.http.HttpServletRequest ; 45 import javax.servlet.http.HttpServletResponse ; 46 import javax.servlet.http.HttpSession ; 47 import java.io.IOException ; 48 49 54 public class PostStoreServlet extends HttpServlet { 55 private static Roles REQUIRED_ROLES; 56 57 static { 58 REQUIRED_ROLES = new Roles(); 59 REQUIRED_ROLES.add(Roles.OWNER); 60 REQUIRED_ROLES.add(Roles.EDITOR); 61 } 62 63 public void doPost(HttpServletRequest request, HttpServletResponse response) 64 throws ServletException , IOException { 65 Configuration config = Application.get().getConfiguration(); 66 String type = request.getHeader("Content-Type"); 68 if (type != null && type.startsWith("multipart/form-data")) { 69 try { 70 request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8"); 71 } catch (IllegalArgumentException e) { 72 Logger.warn("PostStoreServlet: multipart/form-data wrapper:" + e.getMessage()); 73 } 74 } 75 76 String title = request.getParameter("title"); 77 String content = request.getParameter("content"); 78 String snipName = request.getParameter("name"); 79 if (null == snipName || "".equals(snipName)) { 80 snipName = Application.get().getConfiguration().getStartSnip(); 81 } 82 83 if (request.getParameter("preview") != null) { 84 if (null != title && !"".equals(title)) { 86 request.setAttribute("preview", SnipFormatter.toXML(null, BlogKit.getContent(title, content))); 87 } else { 88 request.setAttribute("preview", SnipFormatter.toXML(null, content)); 89 } 90 request.setAttribute("content", content); 91 request.setAttribute("title", title); 92 request.setAttribute("param.name", snipName); 93 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/post.jsp"); 94 dispatcher.forward(request, response); 95 return; 96 } else if (request.getParameter("cancel") == null) { 97 HttpSession session = request.getSession(); 98 if (session != null) { 99 User user = Application.get().getUser(); 100 Blog blog = SnipSpaceFactory.getInstance().getBlog(snipName); 102 103 if (Security.hasRoles(user, blog.getSnip(), REQUIRED_ROLES)) { 104 if (null == title || "".equals(title)) { 105 blog.post(content); 106 } else { 107 blog.post(content, title); 108 } 109 } else { 110 response.sendError(HttpServletResponse.SC_FORBIDDEN); 111 } 112 } 113 } 114 115 response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(snipName))); 116 } 117 } 118 | Popular Tags |