KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 package org.snipsnap.net;
27
28 import org.snipsnap.snip.Snip;
29 import org.snipsnap.snip.SnipLink;
30 import org.snipsnap.snip.SnipSpaceFactory;
31 import org.snipsnap.snip.label.Label;
32 import org.snipsnap.snip.label.LabelManager;
33 import org.snipsnap.snip.label.Labels;
34 import org.snipsnap.app.Application;
35 import org.snipsnap.config.Configuration;
36 import org.snipsnap.container.Components;
37 import org.snipsnap.net.filter.MultipartWrapper;
38 import org.radeox.util.logging.Logger;
39
40 import javax.servlet.RequestDispatcher JavaDoc;
41 import javax.servlet.ServletException JavaDoc;
42 import javax.servlet.http.HttpServlet JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 /**
49  * Showing the labels of a snip
50  * @author Marco Mosconi
51  * @version $Id: LabelsServlet.java 1610 2004-05-18 14:09:44Z leo $
52  */

53 public class LabelsServlet extends HttpServlet JavaDoc {
54   protected 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("AddLabelServlet: multipart/form-data wrapper:" + e.getMessage());
64       }
65     }
66
67     doGet(request, response);
68   }
69
70   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
71     Configuration config = Application.get().getConfiguration();
72
73     String JavaDoc snipName = request.getParameter("snipname");
74     if (null == snipName) {
75       response.sendRedirect(config.getUrl("/space/" + config.getStartSnip()));
76       return;
77     }
78
79     // cancel pressed
80
if (null != request.getParameter("cancel")) {
81       response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(snipName)));
82       return;
83     }
84
85     Snip snip = SnipSpaceFactory.getInstance().load(snipName);
86     request.setAttribute("snip", snip);
87
88     LabelManager manager = (LabelManager) Components.getComponent(LabelManager.class);
89     request.setAttribute("labelmanager", manager);
90
91     if(null != request.getParameter("add")) {
92       String JavaDoc labelType = request.getParameter("labeltype");
93       Label label = manager.getLabel(labelType);
94       request.setAttribute("label", label);
95       RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/addlabel.jsp");
96       dispatcher.forward(request, response);
97       return;
98     }
99
100     if(null != request.getParameter("edit")) {
101       request.setAttribute("edit", "edit");
102       String JavaDoc labelName = request.getParameter("labelname");
103       String JavaDoc labelValue = request.getParameter("labelvalue");
104       Label label = snip.getLabels().getLabel(labelName, labelValue);
105       if(null != label) {
106         request.setAttribute("label", label);
107         RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/addlabel.jsp");
108         dispatcher.forward(request, response);
109         return;
110       }
111     }
112
113     if(null != request.getParameter("delete")) {
114       String JavaDoc[] labels = request.getParameterValues("label");
115       for (int i = 0; i < labels.length; i++) {
116         String JavaDoc label[] = labels[i].split("\\|");
117         snip.getLabels().removeLabel(label[0], label[1]);
118       }
119     }
120
121     RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/showlabels.jsp");
122     dispatcher.forward(request, response);
123   }
124 }
125
Popular Tags