KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.snipsnap.snip.SnipSpaceFactory;
36 import org.snipsnap.snip.label.Label;
37 import org.snipsnap.snip.label.LabelManager;
38
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.http.HttpServlet JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.util.Enumeration JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Map JavaDoc;
47
48
49 /**
50  * Adding a label to a snip
51  *
52  * @author Stephan J. Schmidt
53  * @version $Id: StoreLabelServlet.java 1782 2004-12-09 11:16:33Z leo $
54  */

55 public class StoreLabelServlet extends HttpServlet JavaDoc {
56
57   protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
58     Configuration config = Application.get().getConfiguration();
59     // If this is not a multipart/form-data request continue
60
String JavaDoc type = request.getHeader("Content-Type");
61     if (type != null && type.startsWith("multipart/form-data")) {
62       try {
63         request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8");
64       } catch (IllegalArgumentException JavaDoc e) {
65         Logger.warn("AddLabelServlet: multipart/form-data wrapper:" + e.getMessage());
66       }
67     }
68
69     doGet(request, response);
70   }
71
72   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
73           throws IOException JavaDoc, ServletException JavaDoc {
74     Configuration config = Application.get().getConfiguration();
75
76     String JavaDoc snipName = request.getParameter("snipname");
77
78     // cancel pressed
79
if (null != request.getParameter("cancel")) {
80       response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(snipName)));
81       return;
82     }
83
84     if (null == request.getParameter("back")) {
85       Snip snip = ((SnipSpace) Components.getComponent(SnipSpace.class)).load(snipName);
86       String JavaDoc labelType = request.getParameter("labeltype");
87
88       Label label = null;
89       if (null != labelType) {
90         LabelManager manager = (LabelManager) Components.getComponent(LabelManager.class);
91         label = manager.getLabel(labelType);
92         handleLabel(label, request);
93         snip.getLabels().addLabel(label);
94         SnipSpaceFactory.getInstance().store(snip);
95       }
96     }
97
98     response.sendRedirect(config.getUrl("/exec/labels?snipname=" + SnipLink.encode(snipName)));
99   }
100
101   private void handleLabel(Label label, HttpServletRequest JavaDoc request) {
102     Map JavaDoc params = new HashMap JavaDoc();
103     Enumeration JavaDoc enumeration = request.getParameterNames();
104     while (enumeration.hasMoreElements()) {
105       String JavaDoc name = (String JavaDoc) enumeration.nextElement();
106       params.put(name, request.getParameter(name));
107     }
108     label.handleInput(params);
109   }
110 }
Popular Tags