aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/http/www/RSS.java
blob: ab96221ece65fe68474d51d2a5babe119d17f12f (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
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * 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.http.www;

import com.juick.Message;
import com.juick.server.MessagesQueries;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

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

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

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

        response.setContentType("application/rss+xml; charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            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>");

            Iterator<Message> i = msgs.iterator();
            while (i.hasNext()) {
                Message msg = i.next();

                out.println("<item>");
                out.println("<link>http://juick.com/" + msg.User.UName + "/" + msg.MID + "</link>");
                out.println("<guid>http://juick.com/" + msg.User.UName + "/" + msg.MID + "</guid>");

                out.print("<title><![CDATA[@" + msg.User.UName + ":");
                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[" + PageTemplates.formatMessage(msg.Text) + "]]></description>");

                synchronized (sdfSQL) {
                    try {
                        Date date = sdfSQL.parse(msg.TimestampString);
                        out.println("<pubDate>" + sdfRSS.format(date) + "</pubDate>");
                    } catch (Exception e) {
                        System.err.println("PARSE EXCEPTION: " + msg.TimestampString);
                    }
                }

                out.println("<comments>http://juick.com/" + msg.User.UName + "/" + msg.MID + "</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.MID + ".jpg' type='image/jpeg'/>");
                        out.println("<media:thumbnail url='http://i.juick.com/ps/" + msg.MID + ".jpg'/>");
                    } else if (msg.AttachmentType.equals("png")) {
                        out.println("<media:content url='http://i.juick.com/photos-1024/" + msg.MID + ".png' type='image/png'/>");
                        out.println("<media:thumbnail url='http://i.juick.com/ps/" + msg.MID + ".png'/>");
                    }
                }
                out.println("<juick:user uid='" + msg.User.UID + "'/>");
                out.println("</item>");
            }

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