From 4b1ef499dbd28c1639b9723ee2e9c5ff80002dde Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Mon, 22 Jul 2013 03:44:01 +0700 Subject: /users/ --- src/java/com/juick/api/Main.java | 4 +++- src/java/com/juick/api/Users.java | 46 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) (limited to 'src/java') diff --git a/src/java/com/juick/api/Main.java b/src/java/com/juick/api/Main.java index ebbeb103..449c2a86 100644 --- a/src/java/com/juick/api/Main.java +++ b/src/java/com/juick/api/Main.java @@ -95,7 +95,9 @@ public class Main extends HttpServlet { int vuid = Utils.getHttpAuthUID(sql, request); String uri = request.getRequestURI(); - if (uri.equals("/users/read")) { + if (uri.equals("/users")) { + users.doGetUsers(request, response, vuid); + } else if (uri.equals("/users/read")) { users.doGetUserRead(request, response, vuid); } else if (uri.equals("/users/readers")) { users.doGetUserReaders(request, response, vuid); diff --git a/src/java/com/juick/api/Users.java b/src/java/com/juick/api/Users.java index 2c60a95f..cdd48fd6 100644 --- a/src/java/com/juick/api/Users.java +++ b/src/java/com/juick/api/Users.java @@ -1,9 +1,12 @@ 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; @@ -20,6 +23,45 @@ public class Users { this.sql = sql; } + public void doGetUsers(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + ArrayList users = new ArrayList(); + + String punames[] = request.getParameterValues("uname"); + if (punames != null) { + ArrayList unames = new ArrayList(Arrays.asList(punames)); + Iterator 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 jids = new ArrayList(Arrays.asList(pjids)); + Iterator 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"); @@ -27,7 +69,7 @@ public class Users { uid = vuid; } else { if (UserQueries.checkUserNameValid(uname)) { - com.juick.User u = UserQueries.getUserByNick(sql, uname); + com.juick.User u = UserQueries.getUserByName(sql, uname); if (u != null && u.UID > 0) { uid = u.UID; } @@ -55,7 +97,7 @@ public class Users { uid = vuid; } else { if (UserQueries.checkUserNameValid(uname)) { - com.juick.User u = UserQueries.getUserByNick(sql, uname); + com.juick.User u = UserQueries.getUserByName(sql, uname); if (u != null && u.UID > 0) { uid = u.UID; } -- cgit v1.2.3