Package org.apache.roller.weblogger.util.cache

Examples of org.apache.roller.weblogger.util.cache.CachedContent


        } else {
            cacheKey = weblogFeedCache.generateKey(feedRequest);
        }

        // cached content checking
        CachedContent cachedContent = null;
        if(isSiteWide) {
            cachedContent = (CachedContent) siteWideCache.get(cacheKey);
        } else {
            cachedContent = (CachedContent) weblogFeedCache.get(cacheKey, lastModified);
        }

        if(cachedContent != null) {
            log.debug("HIT "+cacheKey);

            response.setContentLength(cachedContent.getContent().length);
            response.getOutputStream().write(cachedContent.getContent());
            return;

        } else {
            log.debug("MISS "+cacheKey);
        }
       
       
        // validation.  make sure that request input makes sense.
        boolean invalid = false;
        if(feedRequest.getLocale() != null) {
           
            // locale view only allowed if weblog has enabled it
            if(!feedRequest.getWeblog().isEnableMultiLang()) {
                invalid = true;
            }
           
        }
        if(feedRequest.getWeblogCategoryName() != null) {
           
            // category specified.  category must exist.
            if(feedRequest.getWeblogCategory() == null) {
                invalid = true;
            }
           
        } else if(feedRequest.getTags() != null && feedRequest.getTags().size() > 0) {
           
            try {
                // tags specified.  make sure they exist.
                WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
                invalid = !wmgr.getTagComboExists(feedRequest.getTags(), (isSiteWide) ? null : weblog);
            } catch (WebloggerException ex) {
                invalid = true;
            }
        }
       
        if(invalid) {
            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
       
        // do we need to force a specific locale for the request?
        if(feedRequest.getLocale() == null && !weblog.isShowAllLangs()) {
            feedRequest.setLocale(weblog.getLocale());
        }
       
        // looks like we need to render content
        HashMap model = new HashMap();
        String pageId = null;
        try {
            // determine what template to render with
            if (WebloggerRuntimeConfig.isSiteWideWeblog(weblog.getHandle())) {
                pageId = "templates/feeds/site-"+feedRequest.getType()+"-"+feedRequest.getFormat()+".vm";
            } else {
                pageId = "templates/feeds/weblog-"+feedRequest.getType()+"-"+feedRequest.getFormat()+".vm";
            }

            // populate the rendering model
            Map initData = new HashMap();
            initData.put("parsedRequest", feedRequest);

            // define url strategy
            initData.put("urlStrategy", WebloggerFactory.getWeblogger().getUrlStrategy());
           
            // Load models for feeds
            String feedModels = WebloggerConfig.getProperty("rendering.feedModels");
            ModelLoader.loadModels(feedModels, model, initData, true);

            // Load special models for site-wide blog

            if(WebloggerRuntimeConfig.isSiteWideWeblog(weblog.getHandle())) {
                String siteModels = WebloggerConfig.getProperty("rendering.siteModels");
                ModelLoader.loadModels(siteModels, model, initData, true);
            }

            // Load weblog custom models
            ModelLoader.loadCustomModels(weblog, model, initData);
           
            if("entries".equals(feedRequest.getType()) && feedRequest.getTerm() != null) {
                pageId = "templates/feeds/weblog-search-atom.vm";               
                ModelLoader.loadModels(SearchResultsFeedModel.class.getName(), model, initData, true);
            }                       

        } catch (WebloggerException ex) {
            log.error("ERROR loading model for page", ex);

            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }


        // lookup Renderer we are going to use
        Renderer renderer = null;
        try {
            log.debug("Looking up renderer");
            Template template = new StaticTemplate(pageId, "velocity");
            renderer = RendererManager.getRenderer(template);
        } catch(Exception e) {
            // nobody wants to render my content :(

            // TODO: this log message has been disabled because it fills up
            // the logs with useless errors due to the fact that the way these
            // template ids are formed comes directly from the request and it
            // often gets bunk data causing invalid template ids.
            // at some point we should have better validation on the input so
            // that we can quickly dispatch invalid feed requests and only
            // get this far if we expect the template to be found
            //log.error("Couldn't find renderer for page "+pageId, e);

            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        // render content.  use default size of about 24K for a standard page
        CachedContent rendererOutput = new CachedContent(24567);
        try {
            log.debug("Doing rendering");
            renderer.render(model, rendererOutput.getCachedWriter());

            // flush rendered output and close
            rendererOutput.flush();
            rendererOutput.close();
        } catch(Exception e) {
            // bummer, error during rendering
            log.error("Error during rendering for page "+pageId, e);

            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }


        // post rendering process

        // flush rendered content to response
        log.debug("Flushing response output");
        response.setContentLength(rendererOutput.getContent().length);
        response.getOutputStream().write(rendererOutput.getContent());

        // cache rendered content.  only cache if user is not logged in?
        log.debug("PUT "+cacheKey);
        if(isSiteWide) {
            siteWideCache.put(cacheKey, rendererOutput);
View Full Code Here


        }

        // cached content checking
        if ((!this.excludeOwnerPages || !pageRequest.isLoggedIn()) && request.getAttribute("skipCache") == null) {

            CachedContent cachedContent = null;
            if (isSiteWide) {
                cachedContent = (CachedContent) siteWideCache.get(cacheKey);
            } else {
                cachedContent = (CachedContent) weblogPageCache.get(cacheKey, lastModified);
            }

            if (cachedContent != null) {
                log.debug("HIT " + cacheKey);

                // allow for hit counting
                if (!isSiteWide) {
                    this.processHit(weblog, request.getRequestURL().toString(), request.getHeader("referer"));
                }

                response.setContentLength(cachedContent.getContent().length);
                response.setContentType(cachedContent.getContentType());
                response.getOutputStream().write(cachedContent.getContent());
                return;
            } else {
                log.debug("MISS " + cacheKey);
            }
        }

        log.debug("Looking for template to use for rendering");

        // figure out what template to use
        ThemeTemplate page = null;

        // If this is a popup request, then deal with it specially
        // TODO: do we really need to keep supporting this?
        if (request.getParameter("popup") != null) {
            try {
                // Does user have a popupcomments page?
                page = weblog.getTheme().getTemplateByName("_popupcomments");
            } catch (Exception e) {
                // ignored ... considered page not found
            }

            // User doesn't have one so return the default
            if (page == null) {
                page = new StaticThemeTemplate("templates/weblog/popupcomments.vm", "velocity");
            }

            // If request specified the page, then go with that
        } else if ("page".equals(pageRequest.getContext())) {
            page = pageRequest.getWeblogPage();

            // if we don't have this page then 404, we don't let
            // this one fall through to the default template
            if (page == null) {
                if (!response.isCommitted()) {
                    response.reset();
                }
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }

            // If request specified tags section index, then look for custom template
        } else if ("tags".equals(pageRequest.getContext()) && pageRequest.getTags() == null) {
            try {
                page = weblog.getTheme().getTemplateByAction(ThemeTemplate.ACTION_TAGSINDEX);
            } catch (Exception e) {
                log.error("Error getting weblog page for action 'tagsIndex'", e);
            }

            // if we don't have a custom tags page then 404, we don't let
            // this one fall through to the default template
            if (page == null) {
                if (!response.isCommitted()) {
                    response.reset();
                }
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }

            // If this is a permalink then look for a permalink template
        } else if (pageRequest.getWeblogAnchor() != null) {
            try {
                page = weblog.getTheme().getTemplateByAction(ThemeTemplate.ACTION_PERMALINK);
            } catch (Exception e) {
                log.error("Error getting weblog page for action 'permalink'", e);
            }
        }

        // if we haven't found a page yet then try our default page
        if (page == null) {
            try {
                page = weblog.getTheme().getDefaultTemplate();
            } catch (Exception e) {
                log.error("Error getting default page for weblog = " + weblog.getHandle(), e);
            }
        }

        // Still no page?  Then that is a 404
        if (page == null) {
            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        log.debug("page found, dealing with it");

        // validation.  make sure that request input makes sense.
        boolean invalid = false;
        if (pageRequest.getWeblogPageName() != null && page.isHidden()) {
            invalid = true;
        }
        if (pageRequest.getLocale() != null) {

            // locale view only allowed if weblog has enabled it
            if (!pageRequest.getWeblog().isEnableMultiLang()) {
                invalid = true;
            }
        }
        if (pageRequest.getWeblogAnchor() != null) {

            // permalink specified.
            // entry must exist, be published before current time, and locale must match
            WeblogEntry entry = pageRequest.getWeblogEntry();
            if (entry == null) {
                invalid = true;
            } else if (pageRequest.getLocale() != null && !entry.getLocale().startsWith(pageRequest.getLocale())) {
                invalid = true;
            } else if (!entry.isPublished()) {
                invalid = true;
            } else if (new Date().before(entry.getPubTime())) {
                invalid = true;
            }
        } else if (pageRequest.getWeblogCategoryName() != null) {

            // category specified.  category must exist.
            if (pageRequest.getWeblogCategory() == null) {
                invalid = true;
            }
        } else if (pageRequest.getTags() != null && pageRequest.getTags().size() > 0) {

            try {
                // tags specified.  make sure they exist.
                WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
                invalid = !wmgr.getTagComboExists(pageRequest.getTags(), (isSiteWide) ? null : weblog);
            } catch (WebloggerException ex) {
                invalid = true;
            }
        }


        if (invalid) {
            log.debug("page failed validation, bailing out");
            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }


        // do we need to force a specific locale for the request?
        if (pageRequest.getLocale() == null && !weblog.isShowAllLangs()) {
            pageRequest.setLocale(weblog.getLocale());
        }


        // allow for hit counting
        if (!isSiteWide) {
            this.processHit(weblog, request.getRequestURL().toString(), request.getHeader("referer"));
        }


        // looks like we need to render content
        // set the content type
        String contentType = "text/html; charset=utf-8";
        if (StringUtils.isNotEmpty(page.getOutputContentType())) {
            contentType = page.getOutputContentType() + "; charset=utf-8";
        } else {
            String mimeType = RollerContext.getServletContext().getMimeType(page.getLink());
            if (mimeType != null) {
                // we found a match ... set the content type
                contentType = mimeType + "; charset=utf-8";
            } else {
                contentType = "text/html; charset=utf-8";
            }
        }

        HashMap model = new HashMap();
        try {
            PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(this, request, response, "", false, 8192, true);

            // special hack for menu tag
            request.setAttribute("pageRequest", pageRequest);

            // populate the rendering model
            Map initData = new HashMap();
            initData.put("requestParameters", request.getParameterMap());
            initData.put("parsedRequest", pageRequest);
            initData.put("pageContext", pageContext);

            // define url strategy
            initData.put("urlStrategy", WebloggerFactory.getWeblogger().getUrlStrategy());

            // if this was a comment posting, check for comment form
            WeblogEntryCommentForm commentForm = (WeblogEntryCommentForm) request.getAttribute("commentForm");
            if (commentForm != null) {
                initData.put("commentForm", commentForm);
            }

            // Load models for pages
            String pageModels = WebloggerConfig.getProperty("rendering.pageModels");
            ModelLoader.loadModels(pageModels, model, initData, true);

            // Load special models for site-wide blog
            if (WebloggerRuntimeConfig.isSiteWideWeblog(weblog.getHandle())) {
                String siteModels = WebloggerConfig.getProperty("rendering.siteModels");
                ModelLoader.loadModels(siteModels, model, initData, true);
            }

            // Load weblog custom models
            ModelLoader.loadCustomModels(weblog, model, initData);

            // ick, gotta load pre-3.0 model stuff as well :(
            ModelLoader.loadOldModels(model, request, response, pageContext, pageRequest, WebloggerFactory.getWeblogger().getUrlStrategy());
        } catch (WebloggerException ex) {
            log.error("Error loading model objects for page", ex);

            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }


        // lookup Renderer we are going to use
        Renderer renderer = null;
        try {
            log.debug("Looking up renderer");
            renderer = RendererManager.getRenderer(page);
        } catch (Exception e) {
            // nobody wants to render my content :(
            log.error("Couldn't find renderer for page " + page.getId(), e);

            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        // render content.  use size of about 24K for a standard page
        CachedContent rendererOutput = new CachedContent(24567, contentType);
        try {
            log.debug("Doing rendering");
            renderer.render(model, rendererOutput.getCachedWriter());

            // flush rendered output and close
            rendererOutput.flush();
            rendererOutput.close();
        } catch (Exception e) {
            // bummer, error during rendering
            log.error("Error during rendering for page " + page.getId(), e);

            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }


        // post rendering process
        // flush rendered content to response
        log.debug("Flushing response output");
        response.setContentType(contentType);
        response.setContentLength(rendererOutput.getContent().length);
        response.getOutputStream().write(rendererOutput.getContent());

        // cache rendered content.  only cache if user is not logged in?
        if ((!this.excludeOwnerPages || !pageRequest.isLoggedIn()) && request.getAttribute("skipCache") == null) {
            log.debug("PUT " + cacheKey);
View Full Code Here

            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
        // render content
        CachedContent rendererOutput = new CachedContent(4096);
        try {
            log.debug("Doing rendering");
            renderer.render(model, rendererOutput.getCachedWriter());
           
            // flush rendered output and close
            rendererOutput.flush();
            rendererOutput.close();
        } catch(Exception e) {
            // bummer, error during rendering
            log.error("Error during rendering for rsd template", e);
           
            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
       
        // post rendering process
       
        // flush rendered content to response
        log.debug("Flushing response output");
        response.setContentLength(rendererOutput.getContent().length);
        response.getOutputStream().write(rendererOutput.getContent());
       
        log.debug("Exiting");
    }
View Full Code Here

                Template template = new StaticTemplate("templates/feeds/weblog-"+type+"-"+format+".vm", "velocity");
                renderer = RendererManager.getRenderer(template);
               
               
                // render content.  use default size of about 24K for a standard page
                CachedContent rendererOutput = new CachedContent(24567);
                renderer.render(model, rendererOutput.getCachedWriter());
               
               
                // flush rendered output and close
                rendererOutput.flush();
                rendererOutput.close();
               
                // now just put it in the cache
                String key = feedCache.generateKey(feedRequest);
                feedCache.put(key, rendererOutput);
               
View Full Code Here

            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
        // render content
        CachedContent rendererOutput = new CachedContent(4096);
        try {
            log.debug("Doing rendering");
            renderer.render(model, rendererOutput.getCachedWriter());
           
            // flush rendered output and close
            rendererOutput.flush();
            rendererOutput.close();
        } catch(Exception e) {
            // bummer, error during rendering
            log.error("Error during rendering for rsd template", e);
           
            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
       
        // post rendering process
       
        // flush rendered content to response
        log.debug("Flushing response output");
        response.setContentLength(rendererOutput.getContent().length);
        response.getOutputStream().write(rendererOutput.getContent());
       
        log.debug("Exiting");
    }
View Full Code Here

        // set last-modified date
        ModDateHeaderUtil.setLastModifiedHeader(response, lastModified.getTime());

        // cached content checking
        String cacheKey = PlanetCache.CACHE_ID + ":" + this.generateKey(planetRequest);
        CachedContent entry = (CachedContent) planetCache.get(cacheKey);
        if (entry != null) {
            response.setContentLength(entry.getContent().length);
            response.getOutputStream().write(entry.getContent());
            return;
        }


        // looks like we need to render content
        @SuppressWarnings("unchecked")
        HashMap<String, Object> model = new HashMap();
        try {
            // populate the rendering model
            if (request.getParameter("group") != null) {
                Planet planetObject = planet.getPlanet("default");
                model.put("group", planet.getGroup(planetObject, request.getParameter("group")));
            }
            model.put("planet", planet);
            model.put("date", new Date());
            model.put("utils", new UtilitiesModel());
            model.put("siteName", PlanetRuntimeConfig.getProperty("site.name"));
            model.put("siteDescription", PlanetRuntimeConfig.getProperty("site.description"));
            model.put("lastModified", lastModified);
            if (StringUtils.isNotEmpty(PlanetRuntimeConfig.getProperty("site.absoluteurl"))) {
                model.put("absoluteSite", PlanetRuntimeConfig.getProperty("site.absoluteurl"));
            } else {
                model.put("absoluteSite", WebloggerRuntimeConfig.getAbsoluteContextURL());
            }
            model.put("feedStyle", new Boolean(WebloggerRuntimeConfig.getBooleanProperty("site.newsfeeds.styledFeeds")));

            int numEntries = WebloggerRuntimeConfig.getIntProperty("site.newsfeeds.defaultEntries");
            int entryCount = numEntries;
            String sCount = request.getParameter("count");
            if (sCount != null) {
                try {
                    entryCount = Integer.parseInt(sCount);
                } catch (NumberFormatException e) {
                    log.warn("Improperly formatted count parameter");
                }
                if (entryCount > numEntries) {
                    entryCount = numEntries;
                }
                if (entryCount < 0) {
                    entryCount = 0;
                }
            }
            model.put("entryCount", new Integer(entryCount));
        } catch (Exception ex) {
            log.error("Error loading model objects for page", ex);

            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }


        // lookup Renderer we are going to use
        Renderer renderer = null;
        try {
            log.debug("Looking up renderer");
            Template template = new StaticTemplate("templates/planet/planetrss.vm", "velocity");
            renderer = RendererManager.getRenderer(template);
        } catch (Exception e) {
            // nobody wants to render my content :(
            log.error("Couldn't find renderer for planet rss", e);

            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        // render content.  use default size of about 24K for a standard page
        CachedContent rendererOutput = new CachedContent(24567);
        try {
            log.debug("Doing rendering");
            renderer.render(model, rendererOutput.getCachedWriter());

            // flush rendered output and close
            rendererOutput.flush();
            rendererOutput.close();
        } catch (Exception e) {
            // bummer, error during rendering
            log.error("Error during rendering for planet rss", e);

            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }


        // post rendering process
        // flush rendered content to response
        log.debug("Flushing response output");
        response.setContentLength(rendererOutput.getContent().length);
        response.getOutputStream().write(rendererOutput.getContent());

        // cache rendered content.
        this.planetCache.put(cacheKey, rendererOutput);

        log.debug("Exiting");
View Full Code Here

            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
        // render content
        CachedContent rendererOutput = new CachedContent(4096);
        try {
            log.debug("Doing rendering");
            renderer.render(model, rendererOutput.getCachedWriter());
           
            // flush rendered output and close
            rendererOutput.flush();
            rendererOutput.close();
        } catch(Exception e) {
            // bummer, error during rendering
            log.error("Error during rendering for rsd template", e);
           
            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
       
        // post rendering process
       
        // flush rendered content to response
        log.debug("Flushing response output");
        response.setContentLength(rendererOutput.getContent().length);
        response.getOutputStream().write(rendererOutput.getContent());
       
        log.debug("Exiting");
    }
View Full Code Here

        } else {
            cacheKey = weblogFeedCache.generateKey(feedRequest);
        }

        // cached content checking
        CachedContent cachedContent = null;
        if(isSiteWide) {
            cachedContent = (CachedContent) siteWideCache.get(cacheKey);
        } else {
            cachedContent = (CachedContent) weblogFeedCache.get(cacheKey, lastModified);
        }

        if(cachedContent != null) {
            log.debug("HIT "+cacheKey);

            response.setContentLength(cachedContent.getContent().length);
            response.getOutputStream().write(cachedContent.getContent());
            return;

        } else {
            log.debug("MISS "+cacheKey);
        }
       
       
        // validation.  make sure that request input makes sense.
        boolean invalid = false;
        if(feedRequest.getLocale() != null) {
           
            // locale view only allowed if weblog has enabled it
            if(!feedRequest.getWeblog().isEnableMultiLang()) {
                invalid = true;
            }
           
        }
        if(feedRequest.getWeblogCategoryName() != null) {
           
            // category specified.  category must exist.
            if(feedRequest.getWeblogCategory() == null) {
                invalid = true;
            }
           
        } else if(feedRequest.getTags() != null && feedRequest.getTags().size() > 0) {
           
            try {
                // tags specified.  make sure they exist.
                WeblogEntryManager wmgr = WebloggerFactory.getWeblogger().getWeblogEntryManager();
                invalid = !wmgr.getTagComboExists(feedRequest.getTags(), (isSiteWide) ? null : weblog);
            } catch (WebloggerException ex) {
                invalid = true;
            }
        }
       
        if(invalid) {
            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
       
        // do we need to force a specific locale for the request?
        if(feedRequest.getLocale() == null && !weblog.isShowAllLangs()) {
            feedRequest.setLocale(weblog.getLocale());
        }
       
        // looks like we need to render content
        HashMap model = new HashMap();
        String pageId = null;
        try {
            // determine what template to render with
            boolean siteWide = WebloggerRuntimeConfig.isSiteWideWeblog(weblog.getHandle());
           if (siteWide && "entries".equals(feedRequest.getType()) && feedRequest.getTerm() != null) {
                pageId = "templates/feeds/site-search-atom.vm";

           } else if ("entries".equals(feedRequest.getType()) && feedRequest.getTerm() != null) {
                pageId = "templates/feeds/weblog-search-atom.vm";

            } else if (siteWide) {
                pageId = "templates/feeds/site-"+feedRequest.getType()+"-"+feedRequest.getFormat()+".vm";

            } else {
                pageId = "templates/feeds/weblog-"+feedRequest.getType()+"-"+feedRequest.getFormat()+".vm";
            }

            // populate the rendering model
            Map initData = new HashMap();
            initData.put("parsedRequest", feedRequest);

            // define url strategy
            initData.put("urlStrategy", WebloggerFactory.getWeblogger().getUrlStrategy());
           
            // Load models for feeds
            String feedModels = WebloggerConfig.getProperty("rendering.feedModels");
            ModelLoader.loadModels(feedModels, model, initData, true);

            // Load special models for site-wide blog

            if (siteWide) {
                String siteModels = WebloggerConfig.getProperty("rendering.siteModels");
                ModelLoader.loadModels(siteModels, model, initData, true);
            }

            // Load weblog custom models
            ModelLoader.loadCustomModels(weblog, model, initData);
           
            // Load search models if search feed
            if ("entries".equals(feedRequest.getType()) && feedRequest.getTerm() != null) {              
                ModelLoader.loadModels(SearchResultsFeedModel.class.getName(), model, initData, true);
            }                       

        } catch (WebloggerException ex) {
            log.error("ERROR loading model for page", ex);

            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }


        // lookup Renderer we are going to use
        Renderer renderer = null;
        try {
            log.debug("Looking up renderer");
            Template template = new StaticTemplate(pageId, "velocity");
            renderer = RendererManager.getRenderer(template);
        } catch(Exception e) {
            // nobody wants to render my content :(

            // TODO: this log message has been disabled because it fills up
            // the logs with useless errors due to the fact that the way these
            // template ids are formed comes directly from the request and it
            // often gets bunk data causing invalid template ids.
            // at some point we should have better validation on the input so
            // that we can quickly dispatch invalid feed requests and only
            // get this far if we expect the template to be found
            //log.error("Couldn't find renderer for page "+pageId, e);

            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        // render content.  use default size of about 24K for a standard page
        CachedContent rendererOutput = new CachedContent(24567);
        try {
            log.debug("Doing rendering");
            renderer.render(model, rendererOutput.getCachedWriter());

            // flush rendered output and close
            rendererOutput.flush();
            rendererOutput.close();
        } catch(Exception e) {
            // bummer, error during rendering
            log.error("Error during rendering for page "+pageId, e);

            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }


        // post rendering process

        // flush rendered content to response
        log.debug("Flushing response output");
        response.setContentLength(rendererOutput.getContent().length);
        response.getOutputStream().write(rendererOutput.getContent());

        // cache rendered content.  only cache if user is not logged in?
        log.debug("PUT "+cacheKey);
        if(isSiteWide) {
            siteWideCache.put(cacheKey, rendererOutput);
View Full Code Here

            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
        // render content.  use default size of about 24K for a standard page
        CachedContent rendererOutput = new CachedContent(24567);
        try {
            log.debug("Doing rendering");
            renderer.render(model, rendererOutput.getCachedWriter());
           
            // flush rendered output and close
            rendererOutput.flush();
            rendererOutput.close();
        } catch(Exception e) {
            // bummer, error during rendering
            log.error("Error during rendering for page "+page.getId(), e);
           
            if(!response.isCommitted()) response.reset();
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
       
       
        // post rendering process
       
        // flush rendered content to response
        log.debug("Flushing response output");
        response.setContentType(contentType);
        response.setContentLength(rendererOutput.getContent().length);
        response.getOutputStream().write(rendererOutput.getContent());
       
        log.debug("Exiting");
    }
View Full Code Here

    }

        // cached content checking
        if ((!this.excludeOwnerPages || !pageRequest.isLoggedIn()) && request.getAttribute("skipCache") == null) {

            CachedContent cachedContent = null;
            if (isSiteWide) {
                cachedContent = (CachedContent) siteWideCache.get(cacheKey);
            } else {
                cachedContent = (CachedContent) weblogPageCache.get(cacheKey, lastModified);
            }

            if (cachedContent != null) {
                log.debug("HIT " + cacheKey);

                // allow for hit counting
                if (!isSiteWide) {
                    this.processHit(weblog, request.getRequestURL().toString(), request.getHeader("referer"));
                }

                response.setContentLength(cachedContent.getContent().length);
                response.setContentType(cachedContent.getContentType());
                response.getOutputStream().write(cachedContent.getContent());
                return;
            } else {
                log.debug("MISS " + cacheKey);
            }
        }

        log.debug("Looking for template to use for rendering");

        // figure out what template to use
        ThemeTemplate page = null;

        // If this is a popup request, then deal with it specially
        // TODO: do we really need to keep supporting this?
        if (request.getParameter("popup") != null) {
            try {
                // Does user have a popupcomments page?
                page = weblog.getTheme().getTemplateByName("_popupcomments");
            } catch (Exception e) {
                // ignored ... considered page not found
            }

            // User doesn't have one so return the default
            if (page == null) {
                page = new StaticThemeTemplate("templates/weblog/popupcomments.vm", "velocity");
            }

            // If request specified the page, then go with that
        } else if ("page".equals(pageRequest.getContext())) {
            page = pageRequest.getWeblogPage();

            // if we don't have this page then 404, we don't let
            // this one fall through to the default template
            if (page == null) {
                if (!response.isCommitted()) {
                    response.reset();
                }
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }

            // If request specified tags section index, then look for custom template
        } else if ("tags".equals(pageRequest.getContext()) && pageRequest.getTags() != null) {
            try {
                page = weblog.getTheme().getTemplateByAction(ThemeTemplate.ACTION_TAGSINDEX);
            } catch (Exception e) {
                log.error("Error getting weblog page for action 'tagsIndex'", e);
            }

            // if we don't have a custom tags page then 404, we don't let
            // this one fall through to the default template
            if (page == null) {
                if (!response.isCommitted()) {
                    response.reset();
                }
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }

            // If this is a permalink then look for a permalink template
        } else if (pageRequest.getWeblogAnchor() != null) {
            try {
                page = weblog.getTheme().getTemplateByAction(ThemeTemplate.ACTION_PERMALINK);
            } catch (Exception e) {
                log.error("Error getting weblog page for action 'permalink'", e);
            }
        }

        // if we haven't found a page yet then try our default page
        if (page == null) {
            try {
                page = weblog.getTheme().getDefaultTemplate();
            } catch (Exception e) {
                log.error("Error getting default page for weblog = " + weblog.getHandle(), e);
            }
        }

        // Still no page?  Then that is a 404
        if (page == null) {
            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        log.debug("page found, dealing with it");

        // validation.  make sure that request input makes sense.
        boolean invalid = false;
        if (pageRequest.getWeblogPageName() != null && page.isHidden()) {
            invalid = true;
        }
        if (pageRequest.getLocale() != null) {

            // locale view only allowed if weblog has enabled it
            if (!pageRequest.getWeblog().isEnableMultiLang()) {
                invalid = true;
            }
        }
        if (pageRequest.getWeblogAnchor() != null) {

            // permalink specified.
            // entry must exist, be published before current time, and locale must match
            WeblogEntry entry = pageRequest.getWeblogEntry();
            if (entry == null) {
                invalid = true;
            } else if (pageRequest.getLocale() != null && !entry.getLocale().startsWith(pageRequest.getLocale())) {
                invalid = true;
            } else if (!entry.isPublished()) {
                invalid = true;
            } else if (new Date().before(entry.getPubTime())) {
                invalid = true;
            }
        } else if (pageRequest.getWeblogCategoryName() != null) {

            // category specified.  category must exist.
            if (pageRequest.getWeblogCategory() == null) {
                invalid = true;
            }
        } else if (pageRequest.getTags() != null && pageRequest.getTags().size() > 0) {

            try {
                // tags specified.  make sure they exist.
                WeblogEntryManager wmgr = WebloggerFactory.getWeblogger().getWeblogEntryManager();
                invalid = !wmgr.getTagComboExists(pageRequest.getTags(), (isSiteWide) ? null : weblog);
            } catch (WebloggerException ex) {
                invalid = true;
            }
        }


        if (invalid) {
            log.debug("page failed validation, bailing out");
            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }


        // do we need to force a specific locale for the request?
        if (pageRequest.getLocale() == null && !weblog.isShowAllLangs()) {
            pageRequest.setLocale(weblog.getLocale());
        }


        // allow for hit counting
        if (!isSiteWide) {
            this.processHit(weblog, request.getRequestURL().toString(), request.getHeader("referer"));
        }


        // looks like we need to render content
        // set the content type
        String contentType = "text/html; charset=utf-8";
        if (StringUtils.isNotEmpty(page.getOutputContentType())) {
            contentType = page.getOutputContentType() + "; charset=utf-8";
        } else {
            String mimeType = RollerContext.getServletContext().getMimeType(page.getLink());
            if (mimeType != null) {
                // we found a match ... set the content type
                contentType = mimeType + "; charset=utf-8";
            } else {
                contentType = "text/html; charset=utf-8";
            }
        }

        HashMap model = new HashMap();
        try {
            PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(this, request, response, "", false, 8192, true);

            // special hack for menu tag
            request.setAttribute("pageRequest", pageRequest);

            // populate the rendering model
            Map initData = new HashMap();
            initData.put("requestParameters", request.getParameterMap());
            initData.put("parsedRequest", pageRequest);
            initData.put("pageContext", pageContext);

            // define url strategy
            initData.put("urlStrategy", WebloggerFactory.getWeblogger().getUrlStrategy());

            // if this was a comment posting, check for comment form
            WeblogEntryCommentForm commentForm = (WeblogEntryCommentForm) request.getAttribute("commentForm");
            if (commentForm != null) {
                initData.put("commentForm", commentForm);
            }

            // Load models for pages
            String pageModels = WebloggerConfig.getProperty("rendering.pageModels");
            ModelLoader.loadModels(pageModels, model, initData, true);

            // Load special models for site-wide blog
            if (WebloggerRuntimeConfig.isSiteWideWeblog(weblog.getHandle())) {
                String siteModels = WebloggerConfig.getProperty("rendering.siteModels");
                ModelLoader.loadModels(siteModels, model, initData, true);
            }

            // Load weblog custom models
            ModelLoader.loadCustomModels(weblog, model, initData);

            // ick, gotta load pre-3.0 model stuff as well :(
            ModelLoader.loadOldModels(model, request, response, pageContext, pageRequest, WebloggerFactory.getWeblogger().getUrlStrategy());
        } catch (WebloggerException ex) {
            log.error("Error loading model objects for page", ex);

            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }


        // lookup Renderer we are going to use
        Renderer renderer = null;
        try {
            log.debug("Looking up renderer");
            renderer = RendererManager.getRenderer(page);
        } catch (Exception e) {
            // nobody wants to render my content :(
            log.error("Couldn't find renderer for page " + page.getId(), e);

            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        // render content.  use size of about 24K for a standard page
        CachedContent rendererOutput = new CachedContent(24567, contentType);
        try {
            log.debug("Doing rendering");
            renderer.render(model, rendererOutput.getCachedWriter());

            // flush rendered output and close
            rendererOutput.flush();
            rendererOutput.close();
        } catch (Exception e) {
            // bummer, error during rendering
            log.error("Error during rendering for page " + page.getId(), e);

            if (!response.isCommitted()) {
                response.reset();
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }


        // post rendering process
        // flush rendered content to response
        log.debug("Flushing response output");
        response.setContentType(contentType);
        response.setContentLength(rendererOutput.getContent().length);
        response.getOutputStream().write(rendererOutput.getContent());

        // cache rendered content.  only cache if user is not logged in?
        if ((!this.excludeOwnerPages || !pageRequest.isLoggedIn()) && request.getAttribute("skipCache") == null) {
            log.debug("PUT " + cacheKey);
View Full Code Here

TOP

Related Classes of org.apache.roller.weblogger.util.cache.CachedContent

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.