kr.steelheart.site.blog.web.FeedController.java Source code

Java tutorial

Introduction

Here is the source code for kr.steelheart.site.blog.web.FeedController.java

Source

package kr.steelheart.site.blog.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.steelheart.site.blog.entity.Post;
import kr.steelheart.site.blog.entity.QPost;
import kr.steelheart.site.blog.repository.PostRepository;
import kr.steelheart.site.core.LocalizableController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rometools.rome.feed.atom.Feed;
import com.rometools.rome.feed.atom.Link;
import com.rometools.rome.feed.atom.Person;
import com.rometools.rome.feed.synd.SyndPerson;

@LocalizableController
// @Slf4j
public class FeedController {

    @Autowired
    private PostRepository postRepository;

    @RequestMapping(value = { "/rss", "/tc/rss" }, method = RequestMethod.GET)
    public void rss(final HttpServletResponse response) throws IOException {
        response.sendRedirect("/feed");
    }

    @RequestMapping(value = {
            "/feed" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    @ResponseBody
    public Callable<Void> feed(final HttpServletRequest request) {
        return new Callable<Void>() {

            @Override
            public Void call() {
                PageRequest pageable = new PageRequest(0, 20, Direction.DESC, "created");
                Page<Post> posts = postRepository.findAll(QPost.post.publish.isTrue(), pageable);

                String host = "http://" + request.getAttribute("host") + "/";

                Feed feed = new Feed();

                List<SyndPerson> authors = new ArrayList<>();
                Person author = new Person();
                author.setName("Sungha Jun");
                author.setUri(host);
                authors.add(author);

                feed.setId(host);
                feed.setTitle("steelheart*Rocks!");
                feed.setAuthors(authors);
                feed.setCopyright("http://creativecommons.org/licenses/by/3.0/deed.en");

                for (Post post : posts) {
                    Date updated = null;
                    if (post.getUpdated() == null) {
                        updated = post.getCreated();
                    } else {
                        updated = post.getUpdated();
                    }

                    if (feed.getUpdated() == null || updated.compareTo(feed.getUpdated()) > 0) {
                        feed.setUpdated(updated);
                    }
                }

                if (feed.getUpdated() == null) {
                    feed.setUpdated(new Date());
                }

                Link link = new Link();
                link.setType(MediaType.TEXT_HTML_VALUE);
                link.setHref(host + "feed");

                List<Link> links = new ArrayList<>();
                links.add(link);

                feed.setAlternateLinks(links);
                feed.setLogo(host + "img/logo@2x.png");

                return null;
            }
        };
    }

}

//
// List<Entry> entries = new ArrayList<Entry>();
//
// for (Post post : posts) {
// Entry entry = new Entry();
//
// entry.setId(host + "blog/" + post.getKey());
// entry.setTitle(post.getTitle());
// entry.setCreated(post.getCreated());
//
// if (post.getUpdated() == null) {
// entry.setUpdated(post.getCreated());
// }
// else {
// entry.setUpdated(post.getUpdated());
// }
//
//
//
// if (post.getSections() == null || post.getSections().isEmpty()) {
// // Content summary = new Content();
// // summary.setValue(post.getSummary());
// // entry.setSummary(summary);
//
// Content content = new Content();
// content.setValue("No Conent");
// content.setType(MediaType.TEXT_HTML_VALUE);
//
// entry.getContents().add(content);
// }
// else {
// for (Section section : post.getSections()) {
// Content content = new Content();
// content.setType(MediaType.TEXT_HTML_VALUE);
//
// entry.getContents().add(content);
// }
// }
//
// List<Category> categories = new ArrayList<>();
// for (Tag tag : post.getTags()) {
// Category category = new Category();
// // category.setLabel(tag.getName());
// category.setTerm(tag.getName());
// categories.add(category);
// }
// entry.setCategories(categories);
//
//
// Link link = new Link();
// link.setType(MediaType.TEXT_HTML_VALUE);
// link.setHref(host + "blog/" + post.getKey());
//
// List<Link> links = new ArrayList<>();
// links.add(link);
//
// entry.setAlternateLinks(links);
//
//
// entries.add(entry);
// }
//
// return entries;