PageRenderTime 32ms CodeModel.GetById 22ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/ingenieux/nullweblog/web/FeedServlet.java

https://bitbucket.org/aldrinleal/nullweblog
Java | 94 lines | 72 code | 22 blank | 0 comment | 7 complexity | edf4f2e8334b17dff540c11c084ce9ad MD5 | raw file
 1package com.ingenieux.nullweblog.web;
 2
 3import java.io.IOException;
 4import java.util.Collection;
 5import java.util.Date;
 6
 7import javax.servlet.ServletException;
 8import javax.servlet.http.HttpServlet;
 9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11
12import org.apache.abdera.Abdera;
13import org.apache.abdera.factory.Factory;
14import org.apache.abdera.model.Document;
15import org.apache.abdera.model.Entry;
16import org.apache.abdera.model.Feed;
17import org.apache.commons.lang.StringUtils;
18
19import com.google.inject.Inject;
20import com.google.inject.Singleton;
21import com.ingenieux.nullweblog.model.Post;
22import com.ingenieux.nullweblog.model.Usuario;
23import com.ingenieux.nullweblog.server.PostDao;
24import com.ingenieux.nullweblog.server.UsuarioDao;
25
26@Singleton
27public class FeedServlet extends HttpServlet {
28	private static final long serialVersionUID = 1124037781342748204L;
29
30	@Inject
31	PostDao postDao;
32
33	@Inject
34	UsuarioDao usuarioDao;
35
36	@Override
37	protected void service(HttpServletRequest req, HttpServletResponse resp)
38			throws ServletException, IOException {
39		String synthLogin = (String) req.getAttribute("synthLogin");
40		Long postId = Long.valueOf(StringUtils.defaultString(
41				(String) req.getAttribute("postId"), "0"));
42
43		Usuario usuario = usuarioDao.findBySynthLogin(synthLogin);
44
45		if (null == usuario) {
46			resp.sendError(440, "User not found");
47			return;
48		}
49
50		if (0 != postId) {
51			Post post = postDao.findByAutorAndId(usuario, postId);
52
53			resp.getWriter().write(post.getContent());
54		} else {
55			Document<Feed> doc = getFeed(usuario);
56
57			doc.writeTo(resp.getOutputStream());
58		}
59	}
60
61	public Document<Feed> getFeed(Usuario usuario) {
62		Collection<Post> posts = postDao.findByAutor(usuario.getId());
63		Factory abderaFactory = new Abdera().getFactory();
64		Feed feedObj = abderaFactory.newFeed();
65
66		feedObj.setId("nullweblog:2010:" + usuario.getId());
67		feedObj.setTitle("Feed for "
68				+ StringUtils.defaultString(usuario.getNome(),
69						usuario.getLogin()));
70
71		Date lastUpdated = usuario.getCreated();
72
73		for (Post post : posts) {
74			Entry entryObj = abderaFactory.newEntry();
75
76			entryObj.setId("nullweblog:2010:" + usuario.getId() + ":"
77					+ post.getId());
78			entryObj.setTitle(post.getTitle());
79			entryObj.setContent(post.getContent());
80			entryObj.setPublished(post.getCreated());
81			entryObj.setUpdated(post.getLastUpdated());
82
83			if (lastUpdated.before(post.getLastUpdated()))
84				lastUpdated = post.getLastUpdated();
85
86			feedObj.addEntry(entryObj);
87		}
88
89		feedObj.setUpdated(lastUpdated);
90
91		Document<Feed> doc = feedObj.getDocument();
92		return doc;
93	}
94}