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.SnipLink; 34 import org.snipsnap.snip.SnipSpace; 35 36 import javax.servlet.RequestDispatcher ; 37 import javax.servlet.ServletException ; 38 import javax.servlet.http.HttpServlet ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 import java.io.IOException ; 42 import java.util.Arrays ; 43 44 50 public class SnipCopyServlet extends HttpServlet { 51 52 public void doPost(HttpServletRequest request, HttpServletResponse response) 53 throws IOException , ServletException { 54 doGet(request, response); 55 } 56 57 public void doGet(HttpServletRequest request, HttpServletResponse response) 58 throws IOException , ServletException { 59 Configuration config = Application.get().getConfiguration(); 60 String type = request.getHeader("Content-Type"); 62 if (type != null && type.startsWith("multipart/form-data")) { 63 try { 64 request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8"); 65 } catch (IllegalArgumentException e) { 66 Logger.warn("SnipCopyServlet: multipart/form-data wrapper:" + e.getMessage()); 67 } 68 } 69 70 String name = request.getParameter("snip"); 71 72 if (null != request.getParameter("cancel")) { 73 response.sendRedirect(config.getSnipUrl(name)); 74 return; 75 } 76 77 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 78 79 if (null != name && space.exists(name)) { 80 Snip snip = space.load(name); 81 if (request.getParameter("copy") != null) { 82 String newName = request.getParameter("name"); 83 if (newName != null && newName.endsWith("/")) { 84 newName = newName.substring(0, newName.length() - 2); 85 } 86 String [] subsnips = request.getParameterValues("subsnips"); 87 88 Snip newSnip = snip.copy(newName); 89 Logger.log("SnipCopyServlet: copied " + snip.getName() + " to " + newSnip.getName()); 90 for (int s = 0; subsnips != null && s < subsnips.length; s++) { 91 String subSnipName = subsnips[s]; 92 Snip subSnip = space.load(subSnipName); 93 if (subSnip != null && subSnipName.startsWith(name)) { 94 String newSubSnipName = newName + "/" + subsnips[s].substring(name.length() + 1); 95 Snip newSubSnip = subSnip.copy(newSubSnipName); 96 Logger.log("SnipCopyServlet: copied " + subSnip.getName() + " to " + newSubSnip.getName()); 97 } else { 98 Logger.warn("SnipCopyServlet: snip does not exist: " + subsnips[s]); 99 } 100 } 101 response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(newName))); 102 return; 103 } 104 request.setAttribute("name", name); 105 request.setAttribute("snip", snip); 106 request.setAttribute("subsnips", Arrays.asList(space.match(snip.getName() + "/"))); 107 108 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/copy.jsp"); 109 dispatcher.forward(request, response); 110 return; 111 } 112 113 String referer = sanitize(request.getHeader("REFERER")); 114 if (referer == null || referer.length() == 0) { 115 referer = config.getSnipUrl(config.getStartSnip()); 116 } 117 response.sendRedirect(referer); 118 } 119 120 private String sanitize(String parameter) { 121 if (null != parameter) { 122 return parameter.split("[\r\n]")[0]; 123 } 124 return parameter; 125 } 126 127 } 128 | Popular Tags |