1 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 ; 35 import javax.servlet.http.HttpServlet ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.servlet.http.HttpSession ; 39 import java.io.BufferedWriter ; 40 import java.io.IOException ; 41 import java.io.OutputStreamWriter ; 42 import java.sql.Timestamp ; 43 import java.util.HashMap ; 44 45 51 public class CssHandlerServlet extends HttpServlet { 52 private final static String THEME_NAME = "snipsnap.theme.name"; 53 54 ApplicationAwareMap styleSheets = new ApplicationAwareMap(HashMap .class, HashMap .class); 55 ApplicationAwareMap timeStamps = new ApplicationAwareMap(HashMap .class, HashMap .class); 56 57 private String getStyleSheet(HttpServletRequest request) { 58 String theme = getTheme(request); 59 String id = request.getPathInfo(); 60 String themeId = theme + id; 61 Snip cssSnip = (Snip) styleSheets.getMap().get(themeId); 63 Timestamp cssTimestamp = (Timestamp ) timeStamps.getMap().get(themeId); 64 if (null == cssSnip || null == cssTimestamp || cssSnip.getMTime().after(cssTimestamp)) { 65 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 66 String 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 request) { 76 Timestamp ts = (Timestamp ) timeStamps.getMap().get(getTheme(request) + request.getPathInfo()); 77 return (ts != null ? ts.getTime() / 1000 * 1000 : super.getLastModified(request)); 78 } 79 80 private String getTheme(HttpServletRequest request) { 81 HttpSession session = request.getSession(); 82 String sessionTheme = (String ) session.getAttribute(THEME_NAME); 83 Configuration config = Application.get().getConfiguration(); 84 return sessionTheme == null ? config.getTheme() : sessionTheme; 85 } 86 87 public void doGet(HttpServletRequest request, HttpServletResponse response) 88 throws ServletException , IOException { 89 if (request.getParameter("theme") != null) { 90 HttpSession session = request.getSession(); 91 session.setAttribute(THEME_NAME, request.getParameter("theme")); 92 timeStamps.getMap().clear(); 93 String 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 content = getStyleSheet(request); 103 response.setContentType("text/css"); 104 response.setContentLength(content.length()); 105 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (response.getOutputStream())); 106 writer.write(content); 107 writer.flush(); 108 } catch (Exception e) { 109 response.sendError(HttpServletResponse.SC_NOT_FOUND); 110 return; 111 } 112 } 113 } 114 | Popular Tags |