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.SnipSpace; 36 import org.snipsnap.snip.SnipSpaceFactory; 37 import org.snipsnap.user.AuthenticationService; 38 import org.snipsnap.user.Permissions; 39 import org.snipsnap.user.Security; 40 import org.snipsnap.user.User; 41 42 import javax.servlet.RequestDispatcher ; 43 import javax.servlet.ServletException ; 44 import javax.servlet.http.HttpServlet ; 45 import javax.servlet.http.HttpServletRequest ; 46 import javax.servlet.http.HttpServletResponse ; 47 import javax.servlet.http.HttpSession ; 48 import java.io.IOException ; 49 50 56 public class SnipStoreServlet extends HttpServlet { 57 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 58 response.sendRedirect(Application.get().getConfiguration().getUrl()); 59 } 60 61 public void doPost(HttpServletRequest request, HttpServletResponse response) 62 throws ServletException , IOException { 63 Configuration config = Application.get().getConfiguration(); 64 String type = request.getHeader("Content-Type"); 66 if (type != null && type.startsWith("multipart/form-data")) { 67 try { 68 request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8"); 69 } catch (IllegalArgumentException e) { 70 Logger.warn("SnipStoreServlet: multipart/form-data wrapper:" + e.getMessage()); 71 } 72 } 73 74 String name = request.getParameter("name"); 75 String parent = request.getParameter("parent"); 76 String content = request.getParameter("content"); 77 78 if (request.getParameter("cancel") != null) { 80 if (null == name || "".equals(name)) { 81 response.sendRedirect(sanitize(request.getParameter("referer"))); 82 } else { 83 response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(name))); 84 } 85 return; 86 } 87 88 if (null == name || "".equals(name)) { 90 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/edit"); 91 request.setAttribute("error", "snip.name.empty"); 92 dispatcher.forward(request, response); 93 return; 94 } 95 96 if (parent != null && !"".equals(parent)) { 97 name = parent + "/" + name; 98 } 99 100 SnipSpace space = SnipSpaceFactory.getInstance(); 101 Snip snip = space.load(name); 102 103 RequestDispatcher dispatcher; 104 105 if (request.getParameter("preview") != null) { 107 request.setAttribute("preview", SnipFormatter.toXML(snip, content)); 108 dispatcher = request.getRequestDispatcher("/exec/edit"); 109 dispatcher.forward(request, response); 110 return; 111 } 112 113 if (request.getParameter("copy.template") != null) { 115 dispatcher = request.getRequestDispatcher("/exec/edit"); 116 dispatcher.forward(request, response); 117 return; 118 } 119 120 HttpSession session = request.getSession(); 122 if (session != null) { 123 User user = Application.get().getUser(); 124 AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class); 125 String storeHandler = request.getParameter("store_handler"); 126 if (service.isAuthenticated(user) && (null == snip || Security.checkPermission(Permissions.EDIT_SNIP, user, snip))) { 127 if (null != storeHandler) { 128 dispatcher = request.getRequestDispatcher("/plugin/" + storeHandler); 129 try { 130 dispatcher.forward(request, response); 131 } catch (Exception e) { 132 Logger.warn("error while forwarding to store handler", e); 133 request.setAttribute("error", "snip.store.handler.error"); 134 request.setAttribute("error_msg", e.getLocalizedMessage()); 135 dispatcher = request.getRequestDispatcher("/exec/edit"); 136 dispatcher.forward(request, response); 137 } 138 return; 139 } else { 140 if (snip != null) { 142 snip.setContent(content); 143 space.store(snip); 144 } else { 145 snip = space.create(name, content); 146 } 147 } 148 } 149 } 150 151 if (null == snip && !space.exists(name)) { 152 response.sendRedirect(sanitize(request.getParameter("referer"))); 153 return; 154 } 155 156 response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(name))); 157 } 158 159 private String sanitize(String parameter) { 160 if (null != parameter) { 161 return parameter.split("[\r\n]")[0]; 162 } 163 return parameter; 164 } 165 } 166 | Popular Tags |