aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/RSS.java
blob: 2312014040596b75763d69dd9d592311fde5d0a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Juick
 * Copyright (C) 2008-2013, ugnich
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.juick.www;

import com.juick.Message;
import com.juick.server.MessagesQueries;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 * @author ugnich
 */
public class RSS {

    private static final SimpleDateFormat sdfRSS = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");

    protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, int uid, String uname) throws ServletException, IOException {
        List<Integer> mids = MessagesQueries.getUserBlog(sql, uid, 0, 0);
        if (mids.isEmpty()) {
            response.sendError(404);
            return;
        }

        List<Message> msgs = MessagesQueries.getMessages(sql, mids);

        response.setContentType("application/rss+xml; charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.println("<?xml version='1.0' encoding='utf-8'?>");
            out.println("<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom' xmlns:slash='http://purl.org/rss/1.0/modules/slash/' xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:media='http://search.yahoo.com/mrss/'  xmlns:juick='http://juick.com/'>");
            out.println("<channel>");
            out.println("<atom:link href='http://rss.juick.com/" + uname + "/blog' rel='self' type='application/rss+xml'/>");
            out.println("<title>" + uname + " - Juick</title>");
            out.println("<link>http://juick.com/" + uname + "/</link>");
            out.println("<description>The latest messages by @" + uname + " at Juick</description>");
            out.println("<image><url>http://i.juick.com/a/" + uid + ".png</url><title>" + uname + " - Juick</title><link>http://juick.com/" + uname + "/</link></image>");

            for (Message msg : msgs) {
                out.println("<item>");
                out.println("<link>http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + "</link>");
                out.println("<guid>http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + "</guid>");

                out.print("<title><![CDATA[@" + msg.getUser().getUName() + ":");
                if (!msg.Tags.isEmpty()) {
                    for (int n = 0; n < msg.Tags.size(); n++) {
                        out.print(" *" + msg.Tags.get(n));
                    }
                }
                out.println("]]></title>");
                out.println("<description><![CDATA[" + Utils.formatMessage(msg.getText()) + "]]></description>");

                Date date = msg.getDate();
                out.println("<pubDate>" + sdfRSS.format(date) + "</pubDate>");


                out.println("<comments>http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + "</comments>");
                if (!msg.Tags.isEmpty()) {
                    for (int n = 0; n < msg.Tags.size(); n++) {
                        out.println("<category>" + msg.Tags.get(n) + "</category>");
                    }
                }
                if (msg.AttachmentType != null) {
                    if (msg.AttachmentType.equals("jpg")) {
                        out.println("<media:content url='http://i.juick.com/photos-1024/" + msg.getMID() + ".jpg' type='image/jpeg'/>");
                        out.println("<media:thumbnail url='http://i.juick.com/ps/" + msg.getMID() + ".jpg'/>");
                    } else if (msg.AttachmentType.equals("png")) {
                        out.println("<media:content url='http://i.juick.com/photos-1024/" + msg.getMID() + ".png' type='image/png'/>");
                        out.println("<media:thumbnail url='http://i.juick.com/ps/" + msg.getMID() + ".png'/>");
                    }
                }
                out.println("<juick:user uid='" + msg.getUser().getUID() + "'/>");
                out.println("</item>");
            }

            out.println("</channel></rss>");
        }
    }
}