diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/java/com/juick/api/Main.java | 75 | ||||
-rw-r--r-- | src/java/com/juick/api/Others.java | 45 | ||||
-rw-r--r-- | src/java/com/juick/api/PM.java | 102 | ||||
-rw-r--r-- | src/java/com/juick/api/Utils.java | 12 |
4 files changed, 230 insertions, 4 deletions
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<com.juick.User> 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<com.juick.Message> 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); |