KickJava   Java API By Example, From Geeks To Geeks.

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


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

27
28 import org.snipsnap.app.Application;
29 import org.snipsnap.config.Configuration;
30 import org.snipsnap.snip.Blog;
31 import org.snipsnap.snip.BlogKit;
32 import org.snipsnap.snip.SnipFormatter;
33 import org.snipsnap.snip.SnipLink;
34 import org.snipsnap.snip.SnipSpaceFactory;
35 import org.snipsnap.user.Roles;
36 import org.snipsnap.user.Security;
37 import org.snipsnap.user.User;
38 import org.snipsnap.net.filter.MultipartWrapper;
39 import org.radeox.util.logging.Logger;
40
41 import javax.servlet.RequestDispatcher JavaDoc;
42 import javax.servlet.ServletException JavaDoc;
43 import javax.servlet.http.HttpServlet JavaDoc;
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.http.HttpServletResponse JavaDoc;
46 import javax.servlet.http.HttpSession JavaDoc;
47 import java.io.IOException JavaDoc;
48
49 /**
50  * Servlet to store comments.
51  * @author Matthias L. Jugel
52  * @version $Id: PostStoreServlet.java 1606 2004-05-17 10:56:18Z leo $
53  */

54 public class PostStoreServlet extends HttpServlet JavaDoc {
55   private static Roles REQUIRED_ROLES;
56
57   static {
58     REQUIRED_ROLES = new Roles();
59     REQUIRED_ROLES.add(Roles.OWNER);
60     REQUIRED_ROLES.add(Roles.EDITOR);
61   }
62
63   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
64     throws ServletException JavaDoc, IOException JavaDoc {
65     Configuration config = Application.get().getConfiguration();
66     // If this is not a multipart/form-data request continue
67
String JavaDoc type = request.getHeader("Content-Type");
68     if (type != null && type.startsWith("multipart/form-data")) {
69       try {
70         request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8");
71       } catch (IllegalArgumentException JavaDoc e) {
72         Logger.warn("PostStoreServlet: multipart/form-data wrapper:" + e.getMessage());
73       }
74     }
75
76     String JavaDoc title = request.getParameter("title");
77     String JavaDoc content = request.getParameter("content");
78     String JavaDoc snipName = request.getParameter("name");
79     if (null == snipName || "".equals(snipName)) {
80       snipName = Application.get().getConfiguration().getStartSnip();
81     }
82
83     if (request.getParameter("preview") != null) {
84       // If there is a title, generate preview of snipName with title + content
85
if (null != title && !"".equals(title)) {
86         request.setAttribute("preview", SnipFormatter.toXML(null, BlogKit.getContent(title, content)));
87       } else {
88         request.setAttribute("preview", SnipFormatter.toXML(null, content));
89       }
90       request.setAttribute("content", content);
91       request.setAttribute("title", title);
92       request.setAttribute("param.name", snipName);
93       RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/post.jsp");
94       dispatcher.forward(request, response);
95       return;
96     } else if (request.getParameter("cancel") == null) {
97       HttpSession JavaDoc session = request.getSession();
98       if (session != null) {
99         User user = Application.get().getUser();
100 // AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class);
101
Blog blog = SnipSpaceFactory.getInstance().getBlog(snipName);
102
103         if (Security.hasRoles(user, blog.getSnip(), REQUIRED_ROLES)) {
104           if (null == title || "".equals(title)) {
105             blog.post(content);
106           } else {
107             blog.post(content, title);
108           }
109         } else {
110           response.sendError(HttpServletResponse.SC_FORBIDDEN);
111         }
112       }
113     }
114
115     response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(snipName)));
116   }
117 }
118
Popular Tags