KickJava   Java API By Example, From Geeks To Geeks.

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


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.snipsnap.app.Application;
28 import org.snipsnap.config.Configuration;
29 import org.snipsnap.container.Components;
30 import org.snipsnap.snip.Snip;
31 import org.snipsnap.snip.SnipSpace;
32 import org.snipsnap.util.ApplicationAwareMap;
33
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.http.HttpServlet JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.servlet.http.HttpSession JavaDoc;
39 import java.io.BufferedWriter JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.OutputStreamWriter JavaDoc;
42 import java.sql.Timestamp JavaDoc;
43 import java.util.HashMap JavaDoc;
44
45 /**
46  * Handler for CSS Stylesheets (virtual)
47  *
48  * @author Matthias L. Jugel
49  * @version $Id: CssHandlerServlet.java 1743 2004-09-14 12:28:00Z leo $
50  */

51 public class CssHandlerServlet extends HttpServlet JavaDoc {
52   private final static String JavaDoc THEME_NAME = "snipsnap.theme.name";
53
54   ApplicationAwareMap styleSheets = new ApplicationAwareMap(HashMap JavaDoc.class, HashMap JavaDoc.class);
55   ApplicationAwareMap timeStamps = new ApplicationAwareMap(HashMap JavaDoc.class, HashMap JavaDoc.class);
56
57   private String JavaDoc getStyleSheet(HttpServletRequest JavaDoc request) {
58     String JavaDoc theme = getTheme(request);
59     String JavaDoc id = request.getPathInfo();
60     String JavaDoc themeId = theme + id;
61     // get cssSnip for the theme id
62
Snip cssSnip = (Snip) styleSheets.getMap().get(themeId);
63     Timestamp JavaDoc cssTimestamp = (Timestamp JavaDoc) timeStamps.getMap().get(themeId);
64     if (null == cssSnip || null == cssTimestamp || cssSnip.getMTime().after(cssTimestamp)) {
65       SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
66       String JavaDoc snipName = Configuration.SNIPSNAP_THEMES + "/" + theme + ("/default.css".equals(id) ? "/css" : id);
67       cssSnip = space.load(snipName);
68       styleSheets.getMap().put(themeId, cssSnip);
69       timeStamps.getMap().put(themeId, cssSnip.getMTime().clone());
70     }
71
72     return cssSnip.getContent();
73   }
74
75   protected long getLastModified(HttpServletRequest JavaDoc request) {
76     Timestamp JavaDoc ts = (Timestamp JavaDoc) timeStamps.getMap().get(getTheme(request) + request.getPathInfo());
77     return (ts != null ? ts.getTime() / 1000 * 1000 : super.getLastModified(request));
78   }
79
80   private String JavaDoc getTheme(HttpServletRequest JavaDoc request) {
81     HttpSession JavaDoc session = request.getSession();
82     String JavaDoc sessionTheme = (String JavaDoc) session.getAttribute(THEME_NAME);
83     Configuration config = Application.get().getConfiguration();
84     return sessionTheme == null ? config.getTheme() : sessionTheme;
85   }
86
87   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
88           throws ServletException JavaDoc, IOException JavaDoc {
89     if (request.getParameter("theme") != null) {
90       HttpSession JavaDoc session = request.getSession();
91       session.setAttribute(THEME_NAME, request.getParameter("theme"));
92       timeStamps.getMap().clear();
93       String JavaDoc referer = request.getHeader("REFERER");
94       if (referer == null || referer.length() == 0) {
95         Configuration config = Application.get().getConfiguration();
96         referer = config.getSnipUrl(config.getStartSnip());
97       }
98       response.sendRedirect(referer);
99       return;
100     }
101     try {
102       String JavaDoc content = getStyleSheet(request);
103       response.setContentType("text/css");
104       response.setContentLength(content.length());
105       BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(response.getOutputStream()));
106       writer.write(content);
107       writer.flush();
108     } catch (Exception JavaDoc e) {
109       response.sendError(HttpServletResponse.SC_NOT_FOUND);
110       return;
111     }
112   }
113 }
114
Popular Tags