|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2015 Slava Semushin <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | +package ru.mystamps.web.controller; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.PrintWriter; |
| 22 | +import java.text.DateFormat; |
| 23 | +import java.text.SimpleDateFormat; |
| 24 | +import java.util.Locale; |
| 25 | + |
| 26 | +import javax.servlet.http.HttpServletResponse; |
| 27 | + |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import org.springframework.stereotype.Controller; |
| 32 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 33 | + |
| 34 | +import ru.mystamps.web.service.dto.SitemapInfoDto; |
| 35 | +import ru.mystamps.web.service.SeriesService; |
| 36 | +import ru.mystamps.web.Url; |
| 37 | + |
| 38 | +import lombok.RequiredArgsConstructor; |
| 39 | + |
| 40 | +@Controller |
| 41 | +@RequiredArgsConstructor |
| 42 | +public class SitemapController { |
| 43 | + private static final Logger LOG = LoggerFactory.getLogger(SitemapController.class); |
| 44 | + |
| 45 | + // TODO: convert dates to UTC and omit server-specific timezone part |
| 46 | + // According to http://www.w3.org/TR/NOTE-datetime |
| 47 | + private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mmXXX"; |
| 48 | + |
| 49 | + private final SeriesService seriesService; |
| 50 | + |
| 51 | + @RequestMapping(Url.SITEMAP_XML) |
| 52 | + public void getSitemapXml(HttpServletResponse response) { |
| 53 | + response.setContentType("application/xml"); |
| 54 | + response.setCharacterEncoding("UTF-8"); |
| 55 | + |
| 56 | + DateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH); |
| 57 | + |
| 58 | + try { |
| 59 | + PrintWriter writer = response.getWriter(); |
| 60 | + |
| 61 | + writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
| 62 | + writer.println("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"); |
| 63 | + |
| 64 | + writer.print("\t<url>\t\t<loc>"); |
| 65 | + writer.print(Url.PUBLIC_URL); |
| 66 | + writer.print(Url.INDEX_PAGE); |
| 67 | + writer.println("</loc>\t</url>"); |
| 68 | + |
| 69 | + for (SitemapInfoDto item : seriesService.findAllForSitemap()) { |
| 70 | + writer.println("\t<url>"); |
| 71 | + |
| 72 | + writer.print("\t\t<loc>"); |
| 73 | + writer.print(createLocEntry(item)); |
| 74 | + writer.println("</loc>"); |
| 75 | + |
| 76 | + writer.print("\t\t<lastmod>"); |
| 77 | + writer.print(createLastModEntry(dateFormatter, item)); |
| 78 | + writer.println("</lastmod>"); |
| 79 | + |
| 80 | + writer.println("\t</url>"); |
| 81 | + } |
| 82 | + |
| 83 | + writer.println("</urlset>"); |
| 84 | + } catch (IOException ex) { |
| 85 | + LOG.error("Can't return sitemap.xml: {}", ex.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static String createLocEntry(SitemapInfoDto item) { |
| 90 | + return Url.PUBLIC_URL + Url.INFO_SERIES_PAGE.replace("{id}", String.valueOf(item.getId())); |
| 91 | + } |
| 92 | + |
| 93 | + private static String createLastModEntry(DateFormat dateFormatter, SitemapInfoDto item) { |
| 94 | + return dateFormatter.format(item.getUpdatedAt()); |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | + |
0 commit comments