KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.SnipFormatter;
34 import org.snipsnap.snip.SnipLink;
35 import org.snipsnap.snip.SnipSpaceFactory;
36 import org.snipsnap.user.AuthenticationService;
37 import org.snipsnap.user.User;
38
39 import javax.servlet.RequestDispatcher JavaDoc;
40 import javax.servlet.ServletException JavaDoc;
41 import javax.servlet.http.HttpServlet JavaDoc;
42 import javax.servlet.http.HttpServletRequest JavaDoc;
43 import javax.servlet.http.HttpServletResponse JavaDoc;
44 import javax.servlet.http.HttpSession JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 /**
48  * Servlet to store comments.
49  *
50  * @author Matthias L. Jugel
51  * @version $Id: CommentStoreServlet.java 1801 2005-01-29 17:26:09Z leo $
52  */

53 public class CommentStoreServlet extends HttpServlet JavaDoc {
54   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
55           throws ServletException JavaDoc, IOException JavaDoc {
56     Configuration config = Application.get().getConfiguration();
57     // If this is not a multipart/form-data request continue
58
String JavaDoc 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 JavaDoc e) {
63         Logger.warn("CommentStoreServlet: multipart/form-data wrapper:" + e.getMessage());
64       }
65     }
66
67     String JavaDoc name = request.getParameter("comment");
68     String JavaDoc content = request.getParameter("content");
69     Snip snip = SnipSpaceFactory.getInstance().load(name);
70
71     if (request.getParameter("preview") != null) {
72       request.setAttribute("snip", snip);
73       request.setAttribute("preview", SnipFormatter.toXML(snip, content));
74       request.setAttribute("content", content);
75       request.setAttribute("comment", name);
76       RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/comment.jsp");
77       dispatcher.forward(request, response);
78       return;
79     } else if (request.getParameter("cancel") == null) {
80
81       HttpSession JavaDoc session = request.getSession();
82       if (session != null) {
83         User user = Application.get().getUser();
84         AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class);
85
86         if (snip != null && service.isAuthenticated(user)) {
87           snip.getComments().postComment(content);
88         } else {
89           response.sendError(HttpServletResponse.SC_FORBIDDEN);
90           return;
91         }
92       }
93     } else if (snip == null) {
94       // return to referrer if the snip cannot be found
95
response.sendRedirect(sanitize(request.getParameter("referer")));
96       return;
97     }
98
99     response.sendRedirect(config.getUrl("/comments/" + SnipLink.encode(name)));
100   }
101
102   private String JavaDoc sanitize(String JavaDoc parameter) {
103     if (null != parameter) {
104       return parameter.split("[\r\n]")[0];
105     }
106     return parameter;
107   }
108 }
109
Popular Tags