Java tutorial
/* Copyright 2006 - 2010 Under Dusken Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package no.dusken.aranea.view; import com.sun.syndication.feed.synd.*; import com.sun.syndication.io.SyndFeedOutput; import no.dusken.aranea.model.Article; import no.dusken.aranea.model.Page; import no.dusken.aranea.model.Person; import no.dusken.aranea.model.Section; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.view.AbstractView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.Writer; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Map; /** * Created by IntelliJ IDEA. User: maedhros Date: Aug 30, 2006 Time: 8:31:36 PM * To change this template use File | Settings | File Templates. */ public class FeedView extends AbstractView { private static final String DEFAULT_HOSTNAME = "www.underdusken.no"; private static final String DEFAULT_FEEDTYPE = "rss_2.0"; private String hostname = DEFAULT_HOSTNAME; private String feedtype = DEFAULT_FEEDTYPE; private Logger log = LoggerFactory.getLogger(this.getClass()); protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("application/rss+xml"); response.setCharacterEncoding("UTF-8"); List<Page> pages = (List<Page>) map.get("pages"); Section section = (Section) map.get("section"); feedtype = (String) map.get("feedType"); log.debug("Feedtype is {}", feedtype); if (feedtype == null) feedtype = DEFAULT_FEEDTYPE; List<SyndEntry> entries = new ArrayList<SyndEntry>(); SyndFeed feed = new SyndFeedImpl(); feed.setFeedType(feedtype); feed.setEncoding("UTF-8"); if (section != null) { feed.setTitle(section.getName() + " | Under Dusken"); } else { feed.setTitle("Under Dusken"); } feed.setLink("http://" + hostname); feed.setDescription("Nyheter fra Under Dusken, studentavisa i Trondheim"); // log.debug(pages); if (pages != null) { for (Page page : pages) { SyndEntry entry = new SyndEntryImpl(); entry.setTitle(page.getTitle()); entry.setPublishedDate(page.getTimePublished().getTime()); if (page instanceof Article) { Article article = (Article) page; if (article.getIssue() != null) { entry.setLink("http://" + hostname + request.getContextPath() + "/" + article.getParent().getName().toLowerCase() + "/" + article.getIssue().getFromDate().get(Calendar.YEAR) + "/" + article.getIssue().getIssue() + "/" + article.getID() + "/" + article.getSafeTitle()); } else { entry.setLink("http://" + hostname + request.getContextPath() + "/" + article.getParent().getName().toLowerCase() + "/" + article.getID() + "/" + article.getSafeTitle()); } } else { entry.setLink("http://" + hostname + request.getContextPath() + "/" + page.getClass().getSimpleName() + "/" + page.getID() + "/" + page.getSafeTitle()); } // does not validate in feedvalidator: // http://feedvalidator.org/check.cgi?url=http%3A%2F%2Fwww.underdusken.no%2Fdusken%2Ffeed%2Fud.xml // entry.setUpdatedDate(page.getModified().getTime()); // people List<SyndPerson> authors = new ArrayList<SyndPerson>(); for (Person p : page.getAuthors()) { SyndPerson sp = new SyndPersonImpl(); sp.setName(p.getFirstname() + " " + p.getSurname()); sp.setEmail(p.getUsername() + "@underdusken.no"); authors.add(sp); } entry.setAuthors(authors); // description: SyndContent description = new SyndContentImpl(); description.setType("text/html"); //remove htmltags description .setValue(page.getSummary() == null ? "" : page.getSummary().replaceAll("\\<.*?\\>", "")); entry.setDescription(description); entries.add(entry); } } feed.setEntries(entries); Writer writer = response.getWriter(); SyndFeedOutput output = new SyndFeedOutput(); output.output(feed, writer); writer.close(); } public void setHostname(String hostname) { this.hostname = hostname; } public void setFeedtype(String feedtype) { this.feedtype = feedtype; } }