1 package info.magnolia.cms.servlets; 2 3 import info.magnolia.cms.beans.config.Server; 4 import info.magnolia.cms.core.CacheHandler; 5 import info.magnolia.cms.core.Content; 6 import info.magnolia.cms.core.HierarchyManager; 7 import info.magnolia.cms.gui.misc.Sources; 8 import info.magnolia.cms.i18n.MessagesManager; 9 import info.magnolia.cms.security.SessionAccessControl; 10 import info.magnolia.cms.util.MockCacheRequest; 11 12 import java.io.IOException ; 13 import java.io.PrintWriter ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 17 import javax.jcr.Workspace; 18 import javax.servlet.ServletException ; 19 import javax.servlet.http.HttpServlet ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 23 import org.apache.log4j.Logger; 24 25 26 30 public class CacheGeneratorServlet extends HttpServlet { 31 32 35 private static final long serialVersionUID = 222L; 36 37 private static final String CACHE_GENERATE_ACTION = "create.cache"; 38 39 42 private static Logger log = Logger.getLogger(CacheGeneratorServlet.class); 43 44 47 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 48 49 if (request.getParameter(CACHE_GENERATE_ACTION) == null) { 50 displayCacheForm(request, response.getWriter()); 51 } 52 else { 53 try { 54 HierarchyManager hm = SessionAccessControl.getHierarchyManager(request); 55 Workspace ws = hm.getWorkspace(); 56 cachePage(hm.getRoot(), request); 57 cachePage(hm.getRoot(), request); 58 response.getWriter().write(MessagesManager.get(request, "cacheservlet.success")); 59 60 } 61 catch (Exception e) { 62 throw new ServletException (e); 63 } 64 } 65 } 66 67 73 private void displayCacheForm(HttpServletRequest request, PrintWriter out) { 74 75 out.println("<html><head><title>Magnolia</title>"); out.println(new Sources(request.getContextPath()).getHtmlCss()); 77 out.println("</head><body class=\"mgnlBgLight mgnlImportExport\">"); 79 out.println("<h2>"); out.println(MessagesManager.get(request, "cacheservlet.title")); out.println("</h2>"); if (Server.isAdmin()) 83 out.println("<h3 style=\"color:red;\">" 84 + MessagesManager.get(request, "cacheservlet.warning") 85 + "</h3>"); 86 87 out.println("<form method=\"get\" action=\"\">"); out.println("<input type=\"submit\" name=\"" + CACHE_GENERATE_ACTION + "\" value=\"" + MessagesManager.get(request, "cacheservlet.generate") + "\" />"); 93 out.println("</form></body></html>"); } 95 96 private void cachePage(Content page, HttpServletRequest request) { 97 Collection children = page.getChildren(); 98 String rootURL = "http://" + request.getRemoteAddr() + request.getContextPath() + "/"; 99 Iterator iter = children.iterator(); 100 while (iter.hasNext()) { 101 Content item = (Content) iter.next(); 102 String node = item.getName(); 103 MockCacheRequest mock = new MockCacheRequest(request, item); 104 CacheHandler.cacheURI(mock); 105 if (log.isDebugEnabled()) 106 log.debug("Trying to cache request:" + mock.getRequestURL()); 107 cachePage(item, request); 108 } 109 110 } 111 112 116 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , 117 IOException { 118 doGet(request, response); 119 } 120 } 121 | Popular Tags |