From ba4ddf62ddd1df23a95bfe15fe48e03791d4754d Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 5 Aug 2014 04:55:16 +0700 Subject: pubDate bugfix --- src/java/com/juick/rss/Main.java | 66 +++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/java/com/juick/rss/Main.java b/src/java/com/juick/rss/Main.java index 827ecf39..9c0d803c 100644 --- a/src/java/com/juick/rss/Main.java +++ b/src/java/com/juick/rss/Main.java @@ -28,7 +28,6 @@ import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -48,26 +47,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"); + + 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"); 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 +79,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); @@ -122,10 +121,10 @@ public class Main extends HttpServlet { response.sendError(404); } } - + private void responseMessages(HttpServletResponse response, int uid, String uname, ArrayList msgs) throws IOException { response.setContentType("application/rss+xml; charset=UTF-8"); - + PrintWriter out = response.getWriter(); try { out.println(""); @@ -142,15 +141,15 @@ public class Main extends HttpServlet { out.println("http://juick.com/"); out.println("The latest messages at Juick"); } - + Iterator i = msgs.iterator(); while (i.hasNext()) { Message msg = i.next(); - + out.println(""); out.println("http://juick.com/" + msg.User.UName + "/" + msg.MID + ""); out.println("http://juick.com/" + msg.User.UName + "/" + msg.MID + ""); - + out.print("<![CDATA[@" + msg.User.UName + ":"); if (!msg.Tags.isEmpty()) { for (int n = 0; n < msg.Tags.size(); n++) { @@ -159,13 +158,16 @@ public class Main extends HttpServlet { } out.println("]]>"); out.println(""); - - try { - Date date = sdfSQL.parse(msg.TimestampString); - out.println("" + sdfRSS.format(date) + ""); - } catch (ParseException e) { + + synchronized (sdfSQL) { + try { + Date date = sdfSQL.parse(msg.TimestampString); + out.println("" + sdfRSS.format(date) + ""); + } catch (Exception e) { + System.err.println("PARSE EXCEPTION: " + msg.TimestampString); + } } - + out.println("http://juick.com/" + msg.User.UName + "/" + msg.MID + ""); if (!msg.Tags.isEmpty()) { for (int n = 0; n < msg.Tags.size(); n++) { @@ -184,26 +186,26 @@ public class Main extends HttpServlet { out.println(""); out.println(""); } - + out.println(""); } 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(""); out.println(""); out.println(""); out.println("Juick"); out.println("http://juick.com/"); out.println("The latest comments at Juick"); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -216,7 +218,7 @@ public class Main extends HttpServlet { int mid = rs.getInt(2); int rid = rs.getInt(3); String uname = rs.getString(4); - + out.println(""); out.println("http://juick.com/" + muname + "/" + mid + ""); out.println("http://juick.com/" + muname + "/" + mid + "#" + rid + ""); @@ -242,16 +244,16 @@ public class Main extends HttpServlet { } finally { Utils.finishSQL(rs, stmt); } - + out.println(""); } finally { out.close(); } } - + private ArrayList getLastMessages(int hours) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -270,7 +272,7 @@ public class Main extends HttpServlet { return mids; } private static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](? citate msg = msg.replaceAll("(?:(?<=\\n)|(?<=\\A))> *(.*)?(\\n|(?=\\Z))", "
$1
"); msg = msg.replaceAll("
", "\n"); - + msg = msg.replaceAll("\n", "
\n"); return msg; } -- cgit v1.2.3