aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/juick/http
diff options
context:
space:
mode:
authorGravatar Ugnich Anton2015-10-19 22:57:41 +0700
committerGravatar Ugnich Anton2015-10-19 22:57:41 +0700
commited878bfc46df42c56d13fb6f235f9e7f63a64d61 (patch)
tree1842d1fab6b77cb9a72c63eae01e4d6d7ce943d3 /src/java/com/juick/http
parent804ef16a1df1ecd8a948cdc0894d82cba77e8f42 (diff)
+ RSS, basic classes
Diffstat (limited to 'src/java/com/juick/http')
-rw-r--r--src/java/com/juick/http/www/Main.java39
-rw-r--r--src/java/com/juick/http/www/RSS.java113
-rw-r--r--src/java/com/juick/http/www/Reader.java111
-rw-r--r--src/java/com/juick/http/www/Utils.java1
4 files changed, 136 insertions, 128 deletions
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 <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();
+ }
+ }
+}
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 <http://www.gnu.org/licenses/>.
- */
-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 = "<meta name=\"robots\" content=\"noindex,nofollow\"/>";
- PageTemplates.pageHead(out, "Ссылки", head);
- PageTemplates.pageNavigation(out, visitor, null);
- out.println("<section id=\"content\" style=\"width: 95%; margin-left: 15px\">");
-
- out.println("<ul id=\"readerlinks\">");
-
- 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("<li><img src=\"//i.juick.com/rss-icons/" + rs.getInt(2) + ".png\" width=\"16\" height=\"16\"/><a href=\"" + rs.getString(3) + "\" data-lid=\""+rs.getInt(1)+"\" onmousedown=\"readerLinkReplace(this)\" rel=\"nofollow\" target=\"_blank\">" + rs.getString(4) + "</a></li>");
- }
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(rs, stmt);
- }
-
- out.println("</ul>");
-
- out.println("</section>");
- 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;
}
}