1 25 package org.snipsnap.net; 26 27 import org.radeox.util.Encoder; 28 import org.radeox.util.logging.Logger; 29 import org.snipsnap.app.Application; 30 import org.snipsnap.container.Components; 31 import org.snipsnap.snip.Snip; 32 import org.snipsnap.snip.SnipSpace; 33 import org.snipsnap.snip.label.Label; 34 import org.snipsnap.snip.label.Labels; 35 import org.snipsnap.snip.label.TypeLabel; 36 import org.snipsnap.user.Permissions; 37 import org.snipsnap.user.Roles; 38 import org.snipsnap.user.Security; 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.ArrayList ; 47 import java.util.Collection ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 51 58 public class SnipEditServlet extends HttpServlet { 59 60 private final static Roles authRoles = new Roles(Roles.AUTHENTICATED); 61 62 public void doPost(HttpServletRequest request, HttpServletResponse response) 63 throws IOException , ServletException { 64 doGet(request, response); 65 } 66 67 public void doGet(HttpServletRequest request, HttpServletResponse response) 68 throws IOException , ServletException { 69 70 String name = request.getParameter("name"); 71 String content = request.getParameter("content"); 72 String type = request.getParameter("type"); 73 String editHandler = request.getParameter("handler"); 74 75 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 76 Snip snip = null; 77 if (name != null && space.exists(name)) { 78 snip = space.load(name); 79 Collection snipTypes = snip.getLabels().getLabels("TypeLabel"); 81 if (!snipTypes.isEmpty()) { 82 Iterator handlerIt = snipTypes.iterator(); 83 while (handlerIt.hasNext()) { 84 TypeLabel typeLabel = (TypeLabel) handlerIt.next(); 85 editHandler = typeLabel.getEditHandler(); 86 if (null == editHandler) { 87 editHandler = TypeLabel.getEditHandler(typeLabel.getTypeValue()); 88 } 89 if (null != editHandler && !"".equals(editHandler)) { 91 if (Security.checkPermission(Permissions.EDIT_SNIP, Application.get().getUser(), snip) && 92 Security.hasRoles(Application.get().getUser(), snip, authRoles)) { 93 Logger.log("SnipEditServlet: using edit handler '" + editHandler + "'"); 94 type = typeLabel.getTypeValue(); 95 } else { 96 editHandler = null; 97 } 98 break; 99 } 100 } 101 } 102 } else { 103 104 String parent = request.getParameter("parent"); 106 String parentBefore = request.getParameter("parentBefore"); 107 if (null == parentBefore) { 108 parentBefore = parent; 109 } 110 request.setAttribute("parent", parent); 111 request.setAttribute("parentBefore", parentBefore); 112 request.setAttribute("templates", getTemplates()); 113 if (type != null) { 114 editHandler = TypeLabel.getEditHandler(type); 115 } 116 } 117 118 String template = request.getParameter("template"); 120 boolean copyTemplate = request.getParameter("copy.template") != null; 121 if (copyTemplate && template != null) { 122 Snip templateSnip = space.load(template); 123 content = (content != null ? content : "") + templateSnip.getContent(); 124 } 125 126 request.setAttribute("snip", snip); 128 request.setAttribute("snip_name", name); 129 130 if (null != content) { 131 request.setAttribute("content", content); 132 } else { 133 request.setAttribute("content", snip != null ? snip.getContent() : ""); 134 } 135 136 if (null != editHandler && !"".equals(editHandler)) { 137 if (Security.hasRoles(Application.get().getUser(), snip, authRoles)) { 138 request.setAttribute("edit_handler", editHandler); 139 request.setAttribute("mime_type", type); 140 } 141 } 142 143 String referer = sanitize(request.getParameter("referer")); 144 if (null == referer && request.getHeader("REFERER") != null) { 145 referer = Encoder.escape(request.getHeader("REFERER")); 146 } 147 request.setAttribute("referer", referer == null ? "" : referer); 148 149 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/edit.jsp"); 150 dispatcher.forward(request, response); 151 } 152 153 private List getTemplates() { 154 List templates = new ArrayList (); 155 156 SnipSpace snipspace = (SnipSpace) Components.getComponent(SnipSpace.class); 157 List snipList = snipspace.getAll(); 158 159 Iterator iterator = snipList.iterator(); 160 while (iterator.hasNext()) { 161 Snip snip = (Snip) iterator.next(); 162 Labels labels = snip.getLabels(); 163 boolean noLabelsAll = labels.getAll().isEmpty(); 164 165 if (!noLabelsAll) { 166 Collection labelsCat = labels.getLabels("TypeLabel"); 167 if (!labelsCat.isEmpty()) { 168 Iterator iter = labelsCat.iterator(); 169 while (iter.hasNext()) { 170 Label label = (Label) iter.next(); 171 if (label.getValue().equals("Template")) { 172 templates.add(snip.getName()); 173 } 174 } 175 } 176 } 177 } 178 return templates; 179 } 180 181 private String sanitize(String parameter) { 182 if (null != parameter) { 183 return parameter.split("[\r\n]")[0]; 184 } 185 return parameter; 186 } 187 } 188 | Popular Tags |