KickJava   Java API By Example, From Geeks To Geeks.

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


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.SnipLink;
34 import org.snipsnap.snip.SnipSpace;
35
36 import javax.servlet.RequestDispatcher JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import javax.servlet.http.HttpServlet JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.util.Arrays JavaDoc;
43
44 /**
45  * Copy Snips
46  *
47  * @author Matthias L. Jugel
48  * @version $Id: SnipCopyServlet.java 1828 2005-04-28 10:07:57Z leo $
49  */

50 public class SnipCopyServlet extends HttpServlet JavaDoc {
51
52   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
53           throws IOException JavaDoc, ServletException JavaDoc {
54     doGet(request, response);
55   }
56
57   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
58           throws IOException JavaDoc, ServletException JavaDoc {
59     Configuration config = Application.get().getConfiguration();
60     // If this is not a multipart/form-data request continue
61
String JavaDoc type = request.getHeader("Content-Type");
62     if (type != null && type.startsWith("multipart/form-data")) {
63       try {
64         request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8");
65       } catch (IllegalArgumentException JavaDoc e) {
66         Logger.warn("SnipCopyServlet: multipart/form-data wrapper:" + e.getMessage());
67       }
68     }
69
70     String JavaDoc name = request.getParameter("snip");
71
72     if (null != request.getParameter("cancel")) {
73       response.sendRedirect(config.getSnipUrl(name));
74       return;
75     }
76
77     SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
78
79     if (null != name && space.exists(name)) {
80       Snip snip = space.load(name);
81       if (request.getParameter("copy") != null) {
82         String JavaDoc newName = request.getParameter("name");
83         if (newName != null && newName.endsWith("/")) {
84           newName = newName.substring(0, newName.length() - 2);
85         }
86         String JavaDoc[] subsnips = request.getParameterValues("subsnips");
87
88         Snip newSnip = snip.copy(newName);
89         Logger.log("SnipCopyServlet: copied " + snip.getName() + " to " + newSnip.getName());
90         for (int s = 0; subsnips != null && s < subsnips.length; s++) {
91           String JavaDoc subSnipName = subsnips[s];
92           Snip subSnip = space.load(subSnipName);
93           if (subSnip != null && subSnipName.startsWith(name)) {
94             String JavaDoc newSubSnipName = newName + "/" + subsnips[s].substring(name.length() + 1);
95             Snip newSubSnip = subSnip.copy(newSubSnipName);
96             Logger.log("SnipCopyServlet: copied " + subSnip.getName() + " to " + newSubSnip.getName());
97           } else {
98             Logger.warn("SnipCopyServlet: snip does not exist: " + subsnips[s]);
99           }
100         }
101         response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(newName)));
102         return;
103       }
104       request.setAttribute("name", name);
105       request.setAttribute("snip", snip);
106       request.setAttribute("subsnips", Arrays.asList(space.match(snip.getName() + "/")));
107
108       RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/copy.jsp");
109       dispatcher.forward(request, response);
110       return;
111     }
112
113     String JavaDoc referer = sanitize(request.getHeader("REFERER"));
114     if (referer == null || referer.length() == 0) {
115       referer = config.getSnipUrl(config.getStartSnip());
116     }
117     response.sendRedirect(referer);
118   }
119
120   private String JavaDoc sanitize(String JavaDoc parameter) {
121     if (null != parameter) {
122       return parameter.split("[\r\n]")[0];
123     }
124     return parameter;
125   }
126
127 }
128
Popular Tags