KickJava   Java API By Example, From Geeks To Geeks.

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


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.SnipSpace;
36 import org.snipsnap.snip.SnipSpaceFactory;
37 import org.snipsnap.user.AuthenticationService;
38 import org.snipsnap.user.Permissions;
39 import org.snipsnap.user.Security;
40 import org.snipsnap.user.User;
41
42 import javax.servlet.RequestDispatcher JavaDoc;
43 import javax.servlet.ServletException JavaDoc;
44 import javax.servlet.http.HttpServlet JavaDoc;
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47 import javax.servlet.http.HttpSession JavaDoc;
48 import java.io.IOException JavaDoc;
49
50 /**
51  * Servlet to store snips into the database after they have been edited.
52  *
53  * @author Matthias L. Jugel
54  * @version $Id: SnipStoreServlet.java 1801 2005-01-29 17:26:09Z leo $
55  */

56 public class SnipStoreServlet extends HttpServlet JavaDoc {
57   protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
58     response.sendRedirect(Application.get().getConfiguration().getUrl());
59   }
60
61   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
62           throws ServletException JavaDoc, IOException JavaDoc {
63     Configuration config = Application.get().getConfiguration();
64     // If this is not a multipart/form-data request continue
65
String JavaDoc type = request.getHeader("Content-Type");
66     if (type != null && type.startsWith("multipart/form-data")) {
67       try {
68         request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8");
69       } catch (IllegalArgumentException JavaDoc e) {
70         Logger.warn("SnipStoreServlet: multipart/form-data wrapper:" + e.getMessage());
71       }
72     }
73
74     String JavaDoc name = request.getParameter("name");
75     String JavaDoc parent = request.getParameter("parent");
76     String JavaDoc content = request.getParameter("content");
77
78     // cancel pressed, return to snip or referer
79
if (request.getParameter("cancel") != null) {
80       if (null == name || "".equals(name)) {
81         response.sendRedirect(sanitize(request.getParameter("referer")));
82       } else {
83         response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(name)));
84       }
85       return;
86     }
87
88     // if the name is empty show an error
89
if (null == name || "".equals(name)) {
90       RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/edit");
91       request.setAttribute("error", "snip.name.empty");
92       dispatcher.forward(request, response);
93       return;
94     }
95
96     if (parent != null && !"".equals(parent)) {
97       name = parent + "/" + name;
98     }
99
100     SnipSpace space = SnipSpaceFactory.getInstance();
101     Snip snip = space.load(name);
102
103     RequestDispatcher JavaDoc dispatcher;
104
105     // handle preview requests
106
if (request.getParameter("preview") != null) {
107       request.setAttribute("preview", SnipFormatter.toXML(snip, content));
108       dispatcher = request.getRequestDispatcher("/exec/edit");
109       dispatcher.forward(request, response);
110       return;
111     }
112
113     // handle template copy requests (done by the edit servlet!)
114
if (request.getParameter("copy.template") != null) {
115       dispatcher = request.getRequestDispatcher("/exec/edit");
116       dispatcher.forward(request, response);
117       return;
118     }
119
120     // handle requests that store using their own handler and forward (plugin)
121
HttpSession JavaDoc session = request.getSession();
122     if (session != null) {
123       User user = Application.get().getUser();
124       AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class);
125       String JavaDoc storeHandler = request.getParameter("store_handler");
126       if (service.isAuthenticated(user) && (null == snip || Security.checkPermission(Permissions.EDIT_SNIP, user, snip))) {
127         if (null != storeHandler) {
128           dispatcher = request.getRequestDispatcher("/plugin/" + storeHandler);
129           try {
130             dispatcher.forward(request, response);
131           } catch (Exception JavaDoc e) {
132             Logger.warn("error while forwarding to store handler", e);
133             request.setAttribute("error", "snip.store.handler.error");
134             request.setAttribute("error_msg", e.getLocalizedMessage());
135             dispatcher = request.getRequestDispatcher("/exec/edit");
136             dispatcher.forward(request, response);
137           }
138           return;
139         } else {
140           // default storage handling
141
if (snip != null) {
142             snip.setContent(content);
143             space.store(snip);
144           } else {
145             snip = space.create(name, content);
146           }
147         }
148       }
149     }
150
151     if (null == snip && !space.exists(name)) {
152       response.sendRedirect(sanitize(request.getParameter("referer")));
153       return;
154     }
155
156     response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(name)));
157   }
158
159   private String JavaDoc sanitize(String JavaDoc parameter) {
160     if (null != parameter) {
161       return parameter.split("[\r\n]")[0];
162     }
163     return parameter;
164   }
165 }
166
Popular Tags