KickJava   Java API By Example, From Geeks To Geeks.

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


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.snip.Snip;
32 import org.snipsnap.snip.SnipSpace;
33 import org.snipsnap.snip.label.TypeLabel;
34 import org.snipsnap.user.AuthenticationService;
35 import org.snipsnap.user.Roles;
36 import org.snipsnap.user.User;
37 import org.snipsnap.util.URLEncoderDecoder;
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 java.io.IOException JavaDoc;
45 import java.util.Collection JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Map JavaDoc;
48
49 /**
50  * Load a snip to view.
51  *
52  * @author Matthias L. Jugel
53  * @version $Id: SnipViewServlet.java 1689 2004-06-25 13:55:39Z leo $
54  */

55 public class SnipViewServlet extends HttpServlet JavaDoc {
56   private final static Roles authRoles = new Roles(Roles.AUTHENTICATED);
57
58   protected void doHead(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
59           throws ServletException JavaDoc, IOException JavaDoc {
60     doGet(request, response);
61   }
62
63   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
64           throws IOException JavaDoc, ServletException JavaDoc {
65
66     Configuration config = Application.get().getConfiguration();
67     User user = Application.get().getUser();
68     AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class);
69
70     if (service.isAuthenticated(user)) {
71       user.lastAccess();
72     }
73
74     // handle the snip name
75
String JavaDoc name = request.getPathInfo();
76     if (null == name || "/".equals(name)) {
77       name = config.getStartSnip();
78     } else {
79       name = name.substring(1);
80     }
81     String JavaDoc encodedSpace = config.getEncodedSpace();
82     if (encodedSpace != null && encodedSpace.length() > 0) {
83       name = name.replace(encodedSpace.charAt(0), ' ');
84     }
85 // System.out.println("name='"+name+"'");
86

87     // load snip and set attributes for request
88
SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
89     Snip snip = space.load(name);
90
91     String JavaDoc subname = null;
92     if (null == snip) {
93       // handle attachments
94
int slashIndex = name.lastIndexOf('/');
95       if (slashIndex != -1) {
96         subname = name.substring(slashIndex + 1);
97         name = name.substring(0, slashIndex);
98         Logger.log(Logger.DEBUG, name + ": attachment: " + subname);
99       }
100       snip = space.load(name);
101     }
102
103     request.setAttribute("snip", snip);
104 // request.setAttribute("URI", request.getRequestURL().toString());
105

106     if (subname != null && subname.length() > 0) {
107       try {
108         request.setAttribute(FileDownloadServlet.FILENAME, subname);
109         RequestDispatcher JavaDoc dispatcher =
110                 getServletContext().getNamedDispatcher("org.snipsnap.net.FileDownloadServlet");
111         dispatcher.forward(request, response);
112         return;
113       } catch (ServletException JavaDoc e) {
114         // jump to the not found page
115
name = name + "/" + subname;
116         snip = null;
117       }
118     }
119
120     // stop special processing for HEAD requests
121
if ("HEAD".equals(request.getMethod())) {
122       response.setStatus(HttpServletResponse.SC_OK);
123       return;
124     }
125
126     // Snip does not exist
127
if (null == snip) {
128       if (config.allow(Configuration.APP_PERM_CREATESNIP)) {
129         response.sendRedirect(config.getUrl("/exec/edit?name=" + URLEncoderDecoder.encode(name, config.getEncoding())));
130       } else {
131         if ("snipsnap-notfound".equals(name)) {
132           response.sendError(HttpServletResponse.SC_NOT_FOUND,
133                              "Internal Error: could not find snipsnap-notfound page."
134                              + "This may indicate that either the installation has failed or the Database is corrupted.");
135           return;
136         }
137         response.sendRedirect(config.getUrl("/space/snipsnap-notfound?name=" + URLEncoderDecoder.encode(name, config.getEncoding())));
138       }
139       return;
140     }
141
142     String JavaDoc viewHandler = null;
143     String JavaDoc type = null;
144     Collection JavaDoc mimeTypes = snip.getLabels().getLabels("TypeLabel");
145     if (!mimeTypes.isEmpty()) {
146       Iterator JavaDoc handlerIt = mimeTypes.iterator();
147       while (handlerIt.hasNext()) {
148         TypeLabel typeLabel = (TypeLabel) handlerIt.next();
149         viewHandler = typeLabel.getViewHandler();
150         // search for default handler if non found
151
if (null == viewHandler) {
152           viewHandler = TypeLabel.getViewHandler(typeLabel.getTypeValue());
153         }
154
155         if (null != viewHandler) {
156           type = typeLabel.getTypeValue();
157           request.setAttribute("view_handler", viewHandler);
158           request.setAttribute("mime_type", type);
159           break;
160         }
161       }
162     }
163
164     Application app = Application.get();
165     Map JavaDoc params = app.getParameters();
166     params.put("viewed", snip);
167     params.put("RSS", params.get("RSS") + "?snip=" + snip.getNameEncoded());
168
169     snip.handle(request);
170     RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/snip.jsp");
171     dispatcher.forward(request, response);
172   }
173 }
174
Popular Tags