aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/juick/api/Main.java
diff options
context:
space:
mode:
authorGravatar Ugnich Anton2013-08-30 13:10:14 +0700
committerGravatar Ugnich Anton2013-08-30 13:10:14 +0700
commit91bf74631157d8e559686ca25e4980f155d88438 (patch)
treed04790f407eba507aa3e9a6e17d9bc109b7fd982 /src/java/com/juick/api/Main.java
parent4b1ef499dbd28c1639b9723ee2e9c5ff80002dde (diff)
PM
Diffstat (limited to 'src/java/com/juick/api/Main.java')
-rw-r--r--src/java/com/juick/api/Main.java75
1 files changed, 73 insertions, 2 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;
}