From ed878bfc46df42c56d13fb6f235f9e7f63a64d61 Mon Sep 17 00:00:00 2001
From: Ugnich Anton
Date: Mon, 19 Oct 2015 22:57:41 +0700
Subject: + RSS, basic classes
---
src/java/com/juick/http/www/Main.java | 39 ++++++-----
src/java/com/juick/http/www/RSS.java | 113 ++++++++++++++++++++++++++++++++
src/java/com/juick/http/www/Reader.java | 111 -------------------------------
src/java/com/juick/http/www/Utils.java | 1 +
4 files changed, 136 insertions(+), 128 deletions(-)
create mode 100644 src/java/com/juick/http/www/RSS.java
delete mode 100644 src/java/com/juick/http/www/Reader.java
(limited to 'src/java/com/juick/http/www')
diff --git a/src/java/com/juick/http/www/Main.java b/src/java/com/juick/http/www/Main.java
index 0fe93173..56aa439f 100644
--- a/src/java/com/juick/http/www/Main.java
+++ b/src/java/com/juick/http/www/Main.java
@@ -17,6 +17,7 @@
*/
package com.juick.http.www;
+import com.juick.server.UserQueries;
import com.juick.xmpp.JID;
import com.juick.xmpp.Stream;
import com.juick.xmpp.StreamComponent;
@@ -43,13 +44,12 @@ import ru.sape.Sape;
@WebServlet(name = "Main", urlPatterns = {"/"})
@MultipartConfig(fileSizeThreshold = 1024 * 1024, maxRequestSize = 1024 * 1024 * 10)
public class Main extends HttpServlet implements Stream.StreamListener {
-
+
Connection sql;
Connection sqlSearch;
Stream xmpp;
Home home = new Home();
Discover discover = new Discover();
- Reader reader = new Reader();
PM pm = new PM();
Login login = new Login();
Help help = new Help();
@@ -60,29 +60,30 @@ public class Main extends HttpServlet implements Stream.StreamListener {
VKontakteLogin loginVK = new VKontakteLogin();
SignUp signup = new SignUp();
Settings settings = new Settings();
-
+ RSS rss = new RSS();
+
@Override
public void init() throws ServletException {
super.init();
try {
Properties conf = new Properties();
conf.load(new FileInputStream("/etc/juick/www.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", ""));
sqlSearch = DriverManager.getConnection("jdbc:mysql://127.0.0.1:9306?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000", "", "");
-
+
setupXmppComponent(conf.getProperty("xmpp_password"));
-
+
PageTemplates.sape = new Sape(conf.getProperty("sape_user"), "juick.com", 2000, 3600);
} catch (Exception e) {
log(null, e);
}
}
-
+
public void setupXmppComponent(final String password) {
Thread thr = new Thread(new Runnable() {
-
+
@Override
public void run() {
try {
@@ -97,17 +98,17 @@ public class Main extends HttpServlet implements Stream.StreamListener {
});
thr.start();
}
-
+
@Override
public void onStreamFail(String msg) {
System.err.println("XMPP STREAM FAIL: " + msg);
}
-
+
@Override
public void onStreamReady() {
System.err.println("XMPP STREAM READY");
}
-
+
@Override
public void destroy() {
super.destroy();
@@ -142,7 +143,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
request.setCharacterEncoding("UTF-8");
}
String uri = request.getRequestURI();
-
+
if (uri.equals("/")) {
String tag = request.getParameter("tag");
if (tag != null) {
@@ -151,10 +152,6 @@ public class Main extends HttpServlet implements Stream.StreamListener {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
home.doGet(sql, sqlSearch, request, response, visitor);
}
- } else if (uri.equals("/reader")) {
- reader.doGet(sql, request, response);
- } else if (uri.equals("/_out")) {
- reader.doGetOut(sql, request, response);
} else if (uri.equals("/post")) {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
if (visitor != null) {
@@ -181,6 +178,14 @@ public class Main extends HttpServlet implements Stream.StreamListener {
Errors.doGet404(sql, request, response);
}
}
+ } else if (uri.startsWith("/rss/")) {
+ String uname = uri.substring(5);
+ int uid = UserQueries.getUIDbyName(sql, uname);
+ if (uid > 0) {
+ rss.doGet(sql, request, response, uid, uname);
+ } else {
+ response.sendError(404);
+ }
} else if (uri.equals("/logout")) {
login.doGetLogout(sql, request, response);
} else if (uri.equals("/settings")) {
@@ -274,7 +279,7 @@ public class Main extends HttpServlet implements Stream.StreamListener {
if (request.getCharacterEncoding() == null) {
request.setCharacterEncoding("UTF-8");
}
-
+
String uri = request.getRequestURI();
if (uri.equals("/post")) {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
diff --git a/src/java/com/juick/http/www/RSS.java b/src/java/com/juick/http/www/RSS.java
new file mode 100644
index 00000000..ab96221e
--- /dev/null
+++ b/src/java/com/juick/http/www/RSS.java
@@ -0,0 +1,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 .
+ */
+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 mids = MessagesQueries.getUserBlog(sql, uid, 0, 0);
+ if (mids.isEmpty()) {
+ response.sendError(404);
+ return;
+ }
+
+ ArrayList msgs = MessagesQueries.getMessages(sql, mids);
+
+ response.setContentType("application/rss+xml; charset=UTF-8");
+ PrintWriter out = response.getWriter();
+ try {
+ out.println("");
+ out.println("");
+ out.println("");
+ out.println("");
+ out.println("" + uname + " - Juick");
+ out.println("http://juick.com/" + uname + "/");
+ out.println("The latest messages by @" + uname + " at Juick");
+ out.println("http://i.juick.com/a/" + uid + ".png" + uname + " - Juickhttp://juick.com/" + uname + "/");
+
+ 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("");
+ out.println("");
+
+ 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++) {
+ out.println("" + msg.Tags.get(n) + "");
+ }
+ }
+ if (msg.AttachmentType != null) {
+ if (msg.AttachmentType.equals("jpg")) {
+ out.println("");
+ out.println("");
+ } else if (msg.AttachmentType.equals("png")) {
+ out.println("");
+ out.println("");
+ }
+ }
+ out.println("");
+ out.println("
");
+ }
+
+ out.println("");
+ } finally {
+ out.close();
+ }
+ }
+}
diff --git a/src/java/com/juick/http/www/Reader.java b/src/java/com/juick/http/www/Reader.java
deleted file mode 100644
index 75a0e774..00000000
--- a/src/java/com/juick/http/www/Reader.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Juick
- * Copyright (C) 2008-2011, Ugnich Anton
- *
- * 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 .
- */
-package com.juick.http.www;
-
-import com.juick.server.MessagesQueries;
-import com.juick.server.TagQueries;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.net.URLDecoder;
-import java.net.URLEncoder;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-/**
- *
- * @author Ugnich Anton
- */
-public class Reader {
-
- protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
-
- response.setContentType("text/html; charset=UTF-8");
- PrintWriter out = response.getWriter();
- try {
- String head = "";
- PageTemplates.pageHead(out, "Ссылки", head);
- PageTemplates.pageNavigation(out, visitor, null);
- out.println("");
-
- out.println("");
-
- PreparedStatement stmt = null;
- ResultSet rs = null;
- try {
- stmt = sql.prepareStatement("SELECT link_id,rss_id,url,title FROM reader_links ORDER BY ts DESC LIMIT 100");
- rs = stmt.executeQuery();
- rs.beforeFirst();
- while (rs.next()) {
- out.println("
" + rs.getString(4) + " ");
- }
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(rs, stmt);
- }
-
- out.println("
");
-
- out.println("");
- PageTemplates.pageFooter(request, out, visitor, true);
- PageTemplates.pageEnd(out);
- } finally {
- out.close();
- }
- }
-
- protected void doGetOut(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
- int lid = Utils.parseInt(request.getParameter("lid"), 0);
- if (lid == 0) {
- response.sendError(404);
- return;
- }
-
- String url = null;
-
- PreparedStatement stmt = null;
- ResultSet rs = null;
- try {
- stmt = sql.prepareStatement("SELECT url FROM reader_links WHERE link_id=?");
- stmt.setInt(1, lid);
- rs = stmt.executeQuery();
- if (rs.first()) {
- url = rs.getString(1);
- }
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(rs, stmt);
- }
-
- if (url == null) {
- response.sendError(404);
- return;
- }
-
- Utils.sendTemporaryRedirect(response, url);
- }
-}
diff --git a/src/java/com/juick/http/www/Utils.java b/src/java/com/juick/http/www/Utils.java
index e20be893..afaf131e 100644
--- a/src/java/com/juick/http/www/Utils.java
+++ b/src/java/com/juick/http/www/Utils.java
@@ -188,6 +188,7 @@ public class Utils {
in.close();
return b.toString();
} catch (Exception e) {
+ System.err.println("fetchURL: "+e.toString());
return null;
}
}
--
cgit v1.2.3