KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > SnipEditServlet


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
41 import javax.servlet.ServletException JavaDoc;
42 import javax.servlet.http.HttpServlet JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50
51 /**
52  * Load a snip to edit. Loads the snip into the request context. In case
53  * the snip is newly created put the name into "snip_name".
54  *
55  * @author Matthias L. Jugel
56  * @version $Id: SnipEditServlet.java 1801 2005-01-29 17:26:09Z leo $
57  */

58 public class SnipEditServlet extends HttpServlet JavaDoc {
59
60   private final static Roles authRoles = new Roles(Roles.AUTHENTICATED);
61
62   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
63           throws IOException JavaDoc, ServletException JavaDoc {
64     doGet(request, response);
65   }
66
67   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
68           throws IOException JavaDoc, ServletException JavaDoc {
69
70     String JavaDoc name = request.getParameter("name");
71     String JavaDoc content = request.getParameter("content");
72     String JavaDoc type = request.getParameter("type");
73     String JavaDoc 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       // get all mime types associated with the snip
80
Collection JavaDoc snipTypes = snip.getLabels().getLabels("TypeLabel");
81       if (!snipTypes.isEmpty()) {
82         Iterator JavaDoc 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           // check that an edit handler is set
90
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       // handle new snips (they can get a parent and a template)
105
String JavaDoc parent = request.getParameter("parent");
106       String JavaDoc 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     // copy a template into the content if it was requested
119
String JavaDoc 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     // set the attributes to transport snip and snip name (used when nonexisting snip)
127
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 JavaDoc 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 JavaDoc dispatcher = request.getRequestDispatcher("/exec/edit.jsp");
150     dispatcher.forward(request, response);
151   }
152
153   private List JavaDoc getTemplates() {
154     List JavaDoc templates = new ArrayList JavaDoc();
155
156     SnipSpace snipspace = (SnipSpace) Components.getComponent(SnipSpace.class);
157     List JavaDoc snipList = snipspace.getAll();
158
159     Iterator JavaDoc 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 JavaDoc labelsCat = labels.getLabels("TypeLabel");
167         if (!labelsCat.isEmpty()) {
168           Iterator JavaDoc 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 JavaDoc sanitize(String JavaDoc parameter) {
182     if (null != parameter) {
183       return parameter.split("[\r\n]")[0];
184     }
185     return parameter;
186   }
187 }
188
Popular Tags