aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/api/Users.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2015-10-31 01:36:15 +0300
committerGravatar Vitaly Takmazov2015-10-31 01:36:15 +0300
commita4897522174fb48864a4ef7d6276167f9da61f3b (patch)
tree882e8ac66cfd73b4ea1361976fe8be479d78f765 /src/main/java/com/juick/api/Users.java
parent0742847f430b02d2d9031f1a11799ac7076e9e26 (diff)
moved to Gradle
Diffstat (limited to 'src/main/java/com/juick/api/Users.java')
-rw-r--r--src/main/java/com/juick/api/Users.java120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/main/java/com/juick/api/Users.java b/src/main/java/com/juick/api/Users.java
new file mode 100644
index 00000000..cdd48fd6
--- /dev/null
+++ b/src/main/java/com/juick/api/Users.java
@@ -0,0 +1,120 @@
+package com.juick.api;
+
+import com.juick.User;
+import com.juick.server.UserQueries;
+import java.io.IOException;
+import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ *
+ * @author ugnich
+ */
+public class Users {
+
+ Connection sql;
+
+ public Users(Connection sql) {
+ this.sql = sql;
+ }
+
+ public void doGetUsers(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException {
+ ArrayList<com.juick.User> users = new ArrayList<User>();
+
+ String punames[] = request.getParameterValues("uname");
+ if (punames != null) {
+ ArrayList<String> unames = new ArrayList<String>(Arrays.asList(punames));
+ Iterator<String> i = unames.iterator();
+ while (i.hasNext()) {
+ if (!i.next().matches("^[a-zA-Z0-9\\-]{2,16}$")) {
+ i.remove();
+ }
+ }
+ if (!unames.isEmpty() && unames.size() < 20) {
+ users.addAll(UserQueries.getUsersByName(sql, unames));
+ }
+ }
+
+ String pjids[] = request.getParameterValues("jid");
+ if (pjids != null) {
+ ArrayList<String> jids = new ArrayList<String>(Arrays.asList(pjids));
+ Iterator<String> ii = jids.iterator();
+ while (ii.hasNext()) {
+ if (!ii.next().matches("^[a-zA-Z0-9\\-\\_\\@\\.]{6,64}$")) {
+ ii.remove();
+ }
+ }
+ if (!jids.isEmpty() && jids.size() < 20) {
+ users.addAll(UserQueries.getUsersByJID(sql, jids));
+ }
+ }
+
+ if (!users.isEmpty()) {
+ String json = com.juick.json.Users.arrayToString(users);
+ Main.replyJSON(request, response, json);
+ } else {
+ response.sendError(404);
+ }
+ }
+
+ public void doGetUserRead(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException {
+ int uid = 0;
+ String uname = request.getParameter("uname");
+ if (uname == null) {
+ uid = vuid;
+ } else {
+ if (UserQueries.checkUserNameValid(uname)) {
+ com.juick.User u = UserQueries.getUserByName(sql, uname);
+ if (u != null && u.UID > 0) {
+ uid = u.UID;
+ }
+ }
+ }
+
+ if (uid > 0) {
+ ArrayList<Integer> uids = UserQueries.getUserRead(sql, uid);
+ if (uids.size() > 0) {
+ ArrayList<com.juick.User> users = UserQueries.getUsersByID(sql, uids);
+ if (users.size() > 0) {
+ String json = com.juick.json.Users.arrayToString(users);
+ Main.replyJSON(request, response, json);
+ return;
+ }
+ }
+ }
+ response.sendError(404);
+ }
+
+ public void doGetUserReaders(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException {
+ int uid = 0;
+ String uname = request.getParameter("uname");
+ if (uname == null) {
+ uid = vuid;
+ } else {
+ if (UserQueries.checkUserNameValid(uname)) {
+ com.juick.User u = UserQueries.getUserByName(sql, uname);
+ if (u != null && u.UID > 0) {
+ uid = u.UID;
+ }
+ }
+ }
+
+ if (uid > 0) {
+ ArrayList<Integer> uids = UserQueries.getUserReaders(sql, uid);
+ if (uids.size() > 0) {
+ ArrayList<com.juick.User> users = UserQueries.getUsersByID(sql, uids);
+ if (users.size() > 0) {
+ String json = com.juick.json.Users.arrayToString(users);
+ Main.replyJSON(request, response, json);
+ return;
+ }
+ }
+ }
+ response.sendError(404);
+ }
+}