diff options
-rw-r--r-- | nbproject/project.properties | 2 | ||||
-rw-r--r-- | src/java/com/juick/rss/Main.java | 58 |
2 files changed, 32 insertions, 28 deletions
diff --git a/nbproject/project.properties b/nbproject/project.properties index 94d97248..7c8e7334 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -30,7 +30,7 @@ includes=** j2ee.compile.on.save=true j2ee.deploy.on.save=true j2ee.platform=1.6-web -j2ee.platform.classpath=${j2ee.server.home}/lib/catalina-ant.jar:${j2ee.server.home}/lib/tomcat-jdbc.jar:${j2ee.server.home}/lib/jasper-el.jar:${j2ee.server.home}/lib/tomcat-i18n-fr.jar:${j2ee.server.home}/lib/jsp-api.jar:${j2ee.server.home}/lib/catalina.jar:${j2ee.server.home}/lib/tomcat-i18n-ja.jar:${j2ee.server.home}/lib/el-api.jar:${j2ee.server.home}/lib/catalina-tribes.jar:${j2ee.server.home}/lib/tomcat-coyote.jar:${j2ee.server.home}/lib/ecj-3.7.1.jar:${j2ee.server.home}/lib/jasper.jar:${j2ee.server.home}/lib/tomcat-util.jar:${j2ee.server.home}/lib/tomcat-api.jar:${j2ee.server.home}/lib/tomcat-dbcp.jar:${j2ee.server.home}/lib/servlet-api.jar:${j2ee.server.home}/lib/tomcat-i18n-es.jar:${j2ee.server.home}/lib/catalina-ha.jar:${j2ee.server.home}/lib/annotations-api.jar +j2ee.platform.classpath=${j2ee.server.home}/lib/catalina-ant.jar:${j2ee.server.home}/lib/tomcat-jdbc.jar:${j2ee.server.home}/lib/jasper-el.jar:${j2ee.server.home}/lib/tomcat-i18n-fr.jar:${j2ee.server.home}/lib/tomcat7-websocket.jar:${j2ee.server.home}/lib/jsp-api.jar:${j2ee.server.home}/lib/catalina.jar:${j2ee.server.home}/lib/ecj-4.2.2.jar:${j2ee.server.home}/lib/tomcat-i18n-ja.jar:${j2ee.server.home}/lib/websocket-api.jar:${j2ee.server.home}/lib/el-api.jar:${j2ee.server.home}/lib/catalina-tribes.jar:${j2ee.server.home}/lib/tomcat-coyote.jar:${j2ee.server.home}/lib/jasper.jar:${j2ee.server.home}/lib/tomcat-util.jar:${j2ee.server.home}/lib/tomcat-api.jar:${j2ee.server.home}/lib/tomcat-dbcp.jar:${j2ee.server.home}/lib/servlet-api.jar:${j2ee.server.home}/lib/tomcat-i18n-es.jar:${j2ee.server.home}/lib/catalina-ha.jar:${j2ee.server.home}/lib/annotations-api.jar j2ee.server.type=Tomcat jar.compress=false javac.classpath=\ diff --git a/src/java/com/juick/rss/Main.java b/src/java/com/juick/rss/Main.java index 65169fbb..827ecf39 100644 --- a/src/java/com/juick/rss/Main.java +++ b/src/java/com/juick/rss/Main.java @@ -48,26 +48,26 @@ import javax.servlet.http.HttpServletResponse; */ @WebServlet(name = "Main", urlPatterns = {"/"}) public class Main extends HttpServlet { - + private static SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SimpleDateFormat sdfRSS = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); Connection sql; - + @Override public void init() throws ServletException { super.init(); try { Properties conf = new Properties(); conf.load(new FileInputStream("/etc/juick/rss.conf")); - + Class.forName("com.mysql.jdbc.Driver"); sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); - + } catch (Exception e) { log(null, e); } } - + @Override public void destroy() { super.destroy(); @@ -80,13 +80,13 @@ public class Main extends HttpServlet { } } } - + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getCharacterEncoding() == null) { request.setCharacterEncoding("UTF-8"); } - + String uri = request.getRequestURI(); if (uri.equals("/")) { int hours = Utils.parseInt(request.getParameter("hours"), 0); @@ -109,8 +109,12 @@ public class Main extends HttpServlet { int uid = UserQueries.getUIDbyName(sql, uname); if (uid > 0) { ArrayList<Integer> mids = MessagesQueries.getUserBlog(sql, uid, 0, 0); - ArrayList<Message> msgs = MessagesQueries.getMessages(sql, mids); - responseMessages(response, uid, uname, msgs); + if (!mids.isEmpty()) { + ArrayList<Message> msgs = MessagesQueries.getMessages(sql, mids); + responseMessages(response, uid, uname, msgs); + } else { + response.sendError(404); + } } else { response.sendError(404); } @@ -118,10 +122,10 @@ public class Main extends HttpServlet { response.sendError(404); } } - + private void responseMessages(HttpServletResponse response, int uid, String uname, ArrayList<Message> msgs) throws IOException { response.setContentType("application/rss+xml; charset=UTF-8"); - + PrintWriter out = response.getWriter(); try { out.println("<?xml version='1.0' encoding='utf-8'?>"); @@ -138,15 +142,15 @@ public class Main extends HttpServlet { out.println("<link>http://juick.com/</link>"); out.println("<description>The latest messages at Juick</description>"); } - + 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++) { @@ -155,13 +159,13 @@ public class Main extends HttpServlet { } out.println("]]></title>"); out.println("<description><![CDATA[" + formatMessage(msg.Text) + "]]></description>"); - + try { Date date = sdfSQL.parse(msg.TimestampString); out.println("<pubDate>" + sdfRSS.format(date) + "</pubDate>"); } catch (ParseException e) { } - + 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++) { @@ -180,26 +184,26 @@ public class Main extends HttpServlet { out.println("<juick:user uid='" + msg.User.UID + "'/>"); out.println("</item>"); } - + out.println("</channel></rss>"); } finally { out.close(); } } - + private void responseReplies(HttpServletResponse response, int hours) throws IOException { 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:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:ya=\"http://blogs.yandex.ru/yarss/\" xmlns:media=\"http://search.yahoo.com/mrss/\">"); out.println("<channel>"); out.println("<title>Juick</title>"); out.println("<link>http://juick.com/</link>"); out.println("<description>The latest comments at Juick</description>"); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -212,7 +216,7 @@ public class Main extends HttpServlet { int mid = rs.getInt(2); int rid = rs.getInt(3); String uname = rs.getString(4); - + out.println("<item>"); out.println("<ya:post>http://juick.com/" + muname + "/" + mid + "</ya:post>"); out.println("<link>http://juick.com/" + muname + "/" + mid + "#" + rid + "</link>"); @@ -238,16 +242,16 @@ public class Main extends HttpServlet { } finally { Utils.finishSQL(rs, stmt); } - + out.println("</channel></rss>"); } finally { out.close(); } } - + private ArrayList<Integer> getLastMessages(int hours) { ArrayList<Integer> mids = new ArrayList<Integer>(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -266,7 +270,7 @@ public class Main extends HttpServlet { return mids; } private static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?<!>))*)([\\]\\}]|>)"); - + public static String formatMessage(String msg) { msg = msg.replaceAll("&", "&"); msg = msg.replaceAll("<", "<"); @@ -326,7 +330,7 @@ public class Main extends HttpServlet { // > citate msg = msg.replaceAll("(?:(?<=\\n)|(?<=\\A))> *(.*)?(\\n|(?=\\Z))", "<blockquote>$1</blockquote>"); msg = msg.replaceAll("</blockquote><blockquote>", "\n"); - + msg = msg.replaceAll("\n", "<br/>\n"); return msg; } |