1 25 26 package org.snipsnap.net; 27 28 import org.snipsnap.snip.Snip; 29 import org.snipsnap.snip.SnipLink; 30 import org.snipsnap.snip.SnipSpaceFactory; 31 import org.snipsnap.snip.label.Label; 32 import org.snipsnap.snip.label.LabelManager; 33 import org.snipsnap.snip.label.Labels; 34 import org.snipsnap.app.Application; 35 import org.snipsnap.config.Configuration; 36 import org.snipsnap.container.Components; 37 import org.snipsnap.net.filter.MultipartWrapper; 38 import org.radeox.util.logging.Logger; 39 40 import javax.servlet.RequestDispatcher ; 41 import javax.servlet.ServletException ; 42 import javax.servlet.http.HttpServlet ; 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import java.io.IOException ; 46 import java.util.Iterator ; 47 48 53 public class LabelsServlet extends HttpServlet { 54 protected 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("AddLabelServlet: multipart/form-data wrapper:" + e.getMessage()); 64 } 65 } 66 67 doGet(request, response); 68 } 69 70 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException { 71 Configuration config = Application.get().getConfiguration(); 72 73 String snipName = request.getParameter("snipname"); 74 if (null == snipName) { 75 response.sendRedirect(config.getUrl("/space/" + config.getStartSnip())); 76 return; 77 } 78 79 if (null != request.getParameter("cancel")) { 81 response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(snipName))); 82 return; 83 } 84 85 Snip snip = SnipSpaceFactory.getInstance().load(snipName); 86 request.setAttribute("snip", snip); 87 88 LabelManager manager = (LabelManager) Components.getComponent(LabelManager.class); 89 request.setAttribute("labelmanager", manager); 90 91 if(null != request.getParameter("add")) { 92 String labelType = request.getParameter("labeltype"); 93 Label label = manager.getLabel(labelType); 94 request.setAttribute("label", label); 95 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/addlabel.jsp"); 96 dispatcher.forward(request, response); 97 return; 98 } 99 100 if(null != request.getParameter("edit")) { 101 request.setAttribute("edit", "edit"); 102 String labelName = request.getParameter("labelname"); 103 String labelValue = request.getParameter("labelvalue"); 104 Label label = snip.getLabels().getLabel(labelName, labelValue); 105 if(null != label) { 106 request.setAttribute("label", label); 107 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/addlabel.jsp"); 108 dispatcher.forward(request, response); 109 return; 110 } 111 } 112 113 if(null != request.getParameter("delete")) { 114 String [] labels = request.getParameterValues("label"); 115 for (int i = 0; i < labels.length; i++) { 116 String label[] = labels[i].split("\\|"); 117 snip.getLabels().removeLabel(label[0], label[1]); 118 } 119 } 120 121 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/showlabels.jsp"); 122 dispatcher.forward(request, response); 123 } 124 } 125 | Popular Tags |