From 91bf74631157d8e559686ca25e4980f155d88438 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 30 Aug 2013 13:10:14 +0700 Subject: PM --- lib/xpp3_min-1.1.4.jar | Bin 0 -> 24955 bytes nbproject/build-impl.xml | 11 ++++ nbproject/genfiles.properties | 6 +-- nbproject/project.properties | 8 ++- nbproject/project.xml | 16 ++++++ src/java/com/juick/api/Main.java | 75 ++++++++++++++++++++++++++- src/java/com/juick/api/Others.java | 45 ++++++++++++++++ src/java/com/juick/api/PM.java | 102 +++++++++++++++++++++++++++++++++++++ src/java/com/juick/api/Utils.java | 12 ++++- 9 files changed, 266 insertions(+), 9 deletions(-) create mode 100644 lib/xpp3_min-1.1.4.jar create mode 100644 src/java/com/juick/api/Others.java create mode 100644 src/java/com/juick/api/PM.java diff --git a/lib/xpp3_min-1.1.4.jar b/lib/xpp3_min-1.1.4.jar new file mode 100644 index 00000000..e02ea412 Binary files /dev/null and b/lib/xpp3_min-1.1.4.jar differ diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index 965b7552..7fcc9e67 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -538,6 +538,9 @@ exists or setup the property manually. For example like this: + + + @@ -549,6 +552,9 @@ exists or setup the property manually. For example like this: + + + @@ -696,7 +702,9 @@ exists or setup the property manually. For example like this: + + @@ -704,7 +712,9 @@ exists or setup the property manually. For example like this: + + @@ -1072,6 +1082,7 @@ exists or setup the property manually. For example like this: + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index 13678348..9fa27ff1 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -1,8 +1,8 @@ -build.xml.data.CRC32=8cb2a940 +build.xml.data.CRC32=0e286cb1 build.xml.script.CRC32=2226508e build.xml.stylesheet.CRC32=651128d4@1.33.1.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=8cb2a940 -nbproject/build-impl.xml.script.CRC32=9918d740 +nbproject/build-impl.xml.data.CRC32=0e286cb1 +nbproject/build-impl.xml.script.CRC32=4228b93d nbproject/build-impl.xml.stylesheet.CRC32=0cbf5bb7@1.33.1.1 diff --git a/nbproject/project.properties b/nbproject/project.properties index e57401a6..92f560c4 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,6 +1,5 @@ annotation.processing.enabled=true annotation.processing.enabled.in.editor=true -annotation.processing.processors.list= annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output build.classes.dir=${build.web.dir}/WEB-INF/classes @@ -26,6 +25,7 @@ dist.war=${dist.dir}/${war.name} endorsed.classpath=\ ${libs.javaee-endorsed-api-6.0.classpath} excludes= +file.reference.xpp3_min-1.1.4.jar=lib/xpp3_min-1.1.4.jar includes=** j2ee.compile.on.save=true j2ee.deploy.on.save=true @@ -37,7 +37,9 @@ javac.classpath=\ ${reference.com_juick.jar}:\ ${reference.com_juick_server.jar}:\ ${reference.com_juick_json.jar}:\ - ${libs.JSON.classpath} + ${reference.com_juick_xmpp.jar}:\ + ${libs.JSON.classpath}:\ + ${file.reference.xpp3_min-1.1.4.jar} # Space-separated list of extra javac options javac.compilerargs= javac.debug=true @@ -69,9 +71,11 @@ platform.active=default_platform project.com_juick=../com.juick project.com_juick_json=../com.juick.json project.com_juick_server=../com.juick.server +project.com_juick_xmpp=../com.juick.xmpp reference.com_juick.jar=${project.com_juick}/dist/com.juick.jar reference.com_juick_json.jar=${project.com_juick_json}/dist/com.juick.json.jar reference.com_juick_server.jar=${project.com_juick_server}/dist/com.juick.server.jar +reference.com_juick_xmpp.jar=${project.com_juick_xmpp}/dist/com.juick.xmpp.jar resource.dir=setup run.test.classpath=\ ${javac.test.classpath}:\ diff --git a/nbproject/project.xml b/nbproject/project.xml index 15c691a1..01e2f8c9 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -18,10 +18,18 @@ ${reference.com_juick_json.jar} WEB-INF/lib + + ${reference.com_juick_xmpp.jar} + WEB-INF/lib + ${libs.JSON.classpath} WEB-INF/lib + + ${file.reference.xpp3_min-1.1.4.jar} + WEB-INF/lib + @@ -56,6 +64,14 @@ clean jar + + com_juick_xmpp + jar + + jar + clean + jar + diff --git a/src/java/com/juick/api/Main.java b/src/java/com/juick/api/Main.java index 449c2a86..718ca249 100644 --- a/src/java/com/juick/api/Main.java +++ b/src/java/com/juick/api/Main.java @@ -17,9 +17,13 @@ */ package com.juick.api; +import com.juick.xmpp.JID; +import com.juick.xmpp.Stream; +import com.juick.xmpp.StreamComponent; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; +import java.net.Socket; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; @@ -35,11 +39,14 @@ import javax.servlet.http.HttpServletResponse; * @author Ugnich Anton */ @WebServlet(name = "Main", urlPatterns = {"/"}) -public class Main extends HttpServlet { +public class Main extends HttpServlet implements Stream.StreamListener { Connection sql; Connection sqlSearch; + Stream xmpp; Users users; + PM pm; + Others others; @Override public void init() throws ServletException { @@ -53,11 +60,44 @@ public class Main extends HttpServlet { sqlSearch = DriverManager.getConnection("jdbc:mysql://127.0.0.1:9306/juick?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000&relaxAutoCommit=true&user=root&password="); users = new Users(sql); + pm = new PM(sql); + others = new Others(sql); + + setupXmppComponent(conf.getProperty("xmpp_password")); + } catch (Exception e) { log(null, e); } } + public void setupXmppComponent(final String password) { + Thread thr = new Thread(new Runnable() { + + @Override + public void run() { + try { + Socket socket = new Socket("localhost", 5347); + xmpp = new StreamComponent(new JID("", "api.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), password); + xmpp.addListener(Main.this); + xmpp.startParsing(); + } catch (IOException e) { + System.err.println(e); + } + } + }); + 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(); @@ -93,6 +133,9 @@ public class Main extends HttpServlet { } int vuid = Utils.getHttpAuthUID(sql, request); + if (vuid == 0) { + vuid = Utils.getVisitorQueryStringUID(sql, request); + } String uri = request.getRequestURI(); if (uri.equals("/users")) { @@ -101,6 +144,18 @@ public class Main extends HttpServlet { users.doGetUserRead(request, response, vuid); } else if (uri.equals("/users/readers")) { users.doGetUserReaders(request, response, vuid); + } else if (uri.equals("/pm")) { + if (vuid > 0) { + pm.doGetPM(request, response, vuid); + } else { + response.sendError(401); + } + } else if (uri.equals("/groups_pms")) { + if (vuid > 0) { + others.doGetGroupsPMs(request, response, vuid); + } else { + response.sendError(401); + } } else { response.sendError(404); } @@ -115,8 +170,24 @@ public class Main extends HttpServlet { */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + if (request.getCharacterEncoding() == null) { + request.setCharacterEncoding("UTF-8"); + } + + int vuid = Utils.getHttpAuthUID(sql, request); + if (vuid == 0) { + vuid = Utils.getVisitorQueryStringUID(sql, request); + } + + String uri = request.getRequestURI(); if (uri.equals("/post")) { + } else if (uri.equals("/pm")) { + if (vuid > 0) { + pm.doPostPM(request, response, xmpp, vuid); + } else { + response.sendError(401); + } } else { response.sendError(405); } @@ -127,7 +198,7 @@ public class Main extends HttpServlet { response.setHeader("Access-Control-Allow-Origin", "*"); String callback = request.getParameter("callback"); - if (callback != null && (callback.length() > 64 || !callback.matches("[a-zA-Z0-9\\-]+"))) { + if (callback != null && (callback.length() > 64 || !callback.matches("[a-zA-Z0-9\\-\\_]+"))) { callback = null; } diff --git a/src/java/com/juick/api/Others.java b/src/java/com/juick/api/Others.java new file mode 100644 index 00000000..25df5bf4 --- /dev/null +++ b/src/java/com/juick/api/Others.java @@ -0,0 +1,45 @@ +package com.juick.api; + +import com.juick.server.PMQueries; +import java.io.IOException; +import java.sql.Connection; +import java.util.ArrayList; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author ugnich + */ +public class Others { + + Connection sql; + + public Others(Connection sql) { + this.sql = sql; + } + + public void doGetGroupsPMs(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + int cnt = 5; + try { + String cntStr = request.getParameter("cnt"); + cnt = Integer.parseInt(cntStr); + if (cnt < 3) { + cnt = 3; + } + if (cnt > 10) { + cnt = 10; + } + } catch (Exception e) { + } + + ArrayList lastconv = PMQueries.getPMLastConversationsUsers(sql, vuid, cnt); + if (lastconv != null && !lastconv.isEmpty()) { + String json = "{\"pms\":" + com.juick.json.Users.arrayToString(lastconv) + "}"; + Main.replyJSON(request, response, json); + } else { + response.sendError(404); + } + } +} diff --git a/src/java/com/juick/api/PM.java b/src/java/com/juick/api/PM.java new file mode 100644 index 00000000..285f9ff6 --- /dev/null +++ b/src/java/com/juick/api/PM.java @@ -0,0 +1,102 @@ +package com.juick.api; + +import com.juick.server.PMQueries; +import com.juick.server.UserQueries; +import com.juick.xmpp.JID; +import com.juick.xmpp.Message; +import com.juick.xmpp.Stream; +import com.juick.xmpp.extensions.JuickMessage; +import java.io.IOException; +import java.sql.Connection; +import java.util.ArrayList; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author ugnich + */ +public class PM { + + Connection sql; + + public PM(Connection sql) { + this.sql = sql; + } + + public void doGetPM(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + String uname = request.getParameter("uname"); + int uid = 0; + if (uname != null && uname.matches("^[a-zA-Z0-9\\-]{2,16}$")) { + uid = UserQueries.getUIDbyName(sql, uname); + } + + if (uid == 0) { + response.sendError(400); + return; + } + + ArrayList msgs = PMQueries.getPMMessages(sql, vuid, uid); + if (msgs != null && !msgs.isEmpty()) { + String json = com.juick.json.Messages.arrayToString(msgs); + Main.replyJSON(request, response, json); + } else { + response.sendError(404); + } + } + + public void doPostPM(HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) throws ServletException, IOException { + String uname = request.getParameter("uname"); + int uid = 0; + if (UserQueries.checkUserNameValid(uname)) { + uid = UserQueries.getUIDbyName(sql, uname); + } + + String body = request.getParameter("body"); + if (uid == 0 || body == null || body.length() < 1 || body.length() > 10240) { + response.sendError(400); + return; + } + + if (UserQueries.isInBL(sql, uid, vuid)) { + response.sendError(403); + return; + } + + if (PMQueries.createPM(sql, vuid, uid, body)) { + Main.replyJSON(request, response, ""); + + Message msg = new Message(); + msg.from = new JID("juick", "juick.com", null); + msg.to = new JID(Integer.toString(uid), "push.juick.com", null); + JuickMessage jmsg = new JuickMessage(); + jmsg.User = UserQueries.getUserByUID(sql, vuid); + jmsg.Text = body; + msg.childs.add(jmsg); + System.out.println("MESSAGE: " + msg); + xmpp.send(msg); + + msg.to.Host = "ws.juick.com"; + xmpp.send(msg); + + String jid = UserQueries.getJIDbyUID(sql, uid); + if (jid != null) { + Message mm = new Message(); + mm.to = new JID(jid); + mm.type = Message.Type.chat; + if (PMQueries.havePMinRoster(sql, vuid, jid)) { + mm.from = new JID(jmsg.User.UName, "juick.com", "Juick"); + mm.body = body; + } else { + mm.from = new JID("juick", "juick.com", "Juick"); + mm.body = "Private message from @" + jmsg.User.UName + ":\n" + body; + } + xmpp.send(mm); + } + + } else { + response.sendError(500); + } + } +} diff --git a/src/java/com/juick/api/Utils.java b/src/java/com/juick/api/Utils.java index cac5612d..b406c362 100644 --- a/src/java/com/juick/api/Utils.java +++ b/src/java/com/juick/api/Utils.java @@ -70,8 +70,8 @@ public class Utils { } public static int getHttpAuthUID(Connection sql, HttpServletRequest request) { - String auth = request.getHeader("HTTP_AUTHORIZATION"); - if (auth != null && auth.length() > 8) { + String auth = request.getHeader("Authorization"); + if (auth != null && auth.length() > 8 && auth.startsWith("Basic ")) { try { BASE64Decoder dec = new BASE64Decoder(); String loginpassw[] = new String(dec.decodeBuffer(auth.substring(6))).split(":", 2); @@ -84,6 +84,14 @@ public class Utils { return 0; } + public static int getVisitorQueryStringUID(Connection sql, HttpServletRequest request) { + String hash = request.getParameter("hash"); + if (hash != null && hash.length() == 16) { + return com.juick.server.UserQueries.getUIDbyHash(sql, hash); + } + return 0; + } + public static void sendPermanentRedirect(HttpServletResponse response, String location) { response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); response.setHeader("Location", location); -- cgit v1.2.3