001/* 002 * cilla - Blog Management System 003 * 004 * Copyright (C) 2012 Richard "Shred" Körber 005 * http://cilla.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU Affero General Public License as published 009 * by the Free Software Foundation, either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU Affero General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020package org.shredzone.cilla.plugin.sitemap; 021 022import java.io.IOException; 023import java.util.Date; 024import java.util.zip.GZIPOutputStream; 025 026import javax.annotation.Resource; 027import javax.servlet.http.HttpServletRequest; 028import javax.servlet.http.HttpServletResponse; 029 030import org.shredzone.cilla.core.model.Page; 031import org.shredzone.cilla.core.repository.PageDao; 032import org.shredzone.cilla.service.link.LinkService; 033import org.shredzone.commons.view.annotation.View; 034import org.shredzone.commons.view.annotation.ViewHandler; 035import org.shredzone.commons.view.exception.ViewException; 036import org.springframework.stereotype.Component; 037 038/** 039 * Views for generating a sitemap.xml. 040 * 041 * @author Richard "Shred" Körber 042 */ 043@ViewHandler 044@Component 045public class SitemapView { 046 047 private @Resource PageDao pageDao; 048 private @Resource LinkService linkService; 049 050 /** 051 * Renders a sitemap of all pages. 052 */ 053 @View(pattern = "/sitemap.xml.gz", name = "sitemap") 054 public void sitemapView(HttpServletRequest req, HttpServletResponse resp) 055 throws ViewException { 056 try { 057 resp.setContentType("text/xml"); 058 resp.setHeader("Content-Encoding", "gzip"); 059 060 try (GZIPOutputStream go = new GZIPOutputStream(resp.getOutputStream())) { 061 SitemapWriter writer = new SitemapWriter(go); 062 063 writer.writeHeader(); 064 writeHome(writer); 065 writePages(writer); 066 writer.writeFooter(); 067 writer.flush(); 068 069 go.finish(); 070 } 071 } catch (IOException ex) { 072 throw new ViewException(ex); 073 } 074 } 075 076 /** 077 * Generates a sitemap entry to the home page. 078 * 079 * @param writer 080 * {@link SitemapWriter} to write to 081 */ 082 private void writeHome(SitemapWriter writer) throws IOException { 083 Date[] minMaxDates = pageDao.fetchMinMaxModification(); 084 String homeUrl = linkService.linkTo().absolute().toString(); 085 writer.writeUrl(homeUrl, minMaxDates[1], null, 1.0f); 086 } 087 088 /** 089 * Generates a sitemap entry for all published pages 090 * 091 * @param writer 092 * {@link SitemapWriter} to write to 093 */ 094 private void writePages(SitemapWriter writer) throws IOException { 095 for (Page page : pageDao.fetchAllPublic()) { 096 String pageUrl = linkService.linkTo().page(page).absolute().toString(); 097 098 Float priority = null; 099 if (page.isHidden()) { 100 priority = 0.3f; 101 } 102 if (page.isSticky()) { 103 priority = 0.7f; 104 } 105 106 writer.writeUrl(pageUrl, page.getModification(), null, priority); 107 } 108 } 109 110}