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 import org.snipsnap.snip.SnipSpaceFactory; 36 import org.snipsnap.snip.label.Label; 37 import org.snipsnap.snip.label.LabelManager; 38 39 import javax.servlet.ServletException ; 40 import javax.servlet.http.HttpServlet ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import java.io.IOException ; 44 import java.util.Enumeration ; 45 import java.util.HashMap ; 46 import java.util.Map ; 47 48 49 55 public class StoreLabelServlet extends HttpServlet { 56 57 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 58 Configuration config = Application.get().getConfiguration(); 59 String type = request.getHeader("Content-Type"); 61 if (type != null && type.startsWith("multipart/form-data")) { 62 try { 63 request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8"); 64 } catch (IllegalArgumentException e) { 65 Logger.warn("AddLabelServlet: multipart/form-data wrapper:" + e.getMessage()); 66 } 67 } 68 69 doGet(request, response); 70 } 71 72 public void doGet(HttpServletRequest request, HttpServletResponse response) 73 throws IOException , ServletException { 74 Configuration config = Application.get().getConfiguration(); 75 76 String snipName = request.getParameter("snipname"); 77 78 if (null != request.getParameter("cancel")) { 80 response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(snipName))); 81 return; 82 } 83 84 if (null == request.getParameter("back")) { 85 Snip snip = ((SnipSpace) Components.getComponent(SnipSpace.class)).load(snipName); 86 String labelType = request.getParameter("labeltype"); 87 88 Label label = null; 89 if (null != labelType) { 90 LabelManager manager = (LabelManager) Components.getComponent(LabelManager.class); 91 label = manager.getLabel(labelType); 92 handleLabel(label, request); 93 snip.getLabels().addLabel(label); 94 SnipSpaceFactory.getInstance().store(snip); 95 } 96 } 97 98 response.sendRedirect(config.getUrl("/exec/labels?snipname=" + SnipLink.encode(snipName))); 99 } 100 101 private void handleLabel(Label label, HttpServletRequest request) { 102 Map params = new HashMap (); 103 Enumeration enumeration = request.getParameterNames(); 104 while (enumeration.hasMoreElements()) { 105 String name = (String ) enumeration.nextElement(); 106 params.put(name, request.getParameter(name)); 107 } 108 label.handleInput(params); 109 } 110 } | Popular Tags |