diff options
author | Vitaly Takmazov | 2016-07-07 15:16:21 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-07-07 15:16:21 +0300 |
commit | e5c286017b59a0862f2ccc4d9f905d8a3b0008ac (patch) | |
tree | 6522c6c00f64491fb1c9aca7cc8f36ad3c581954 /src/main/java/com/juick/api/Users.java | |
parent | e5c8298beee5dde90ca98cc4707faac4bf0e2f0c (diff) | |
parent | a2d11294adca359bb6f7021a9b5db13647b5f504 (diff) |
merge api
Diffstat (limited to 'src/main/java/com/juick/api/Users.java')
-rw-r--r-- | src/main/java/com/juick/api/Users.java | 126 |
1 files changed, 126 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..e85c517a --- /dev/null +++ b/src/main/java/com/juick/api/Users.java @@ -0,0 +1,126 @@ +package com.juick.api; + +import com.juick.User; +import com.juick.json.UserSerializer; +import com.juick.server.UserQueries; +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +/** + * + * @author ugnich + */ +public class Users { + + JdbcTemplate sql; + + UserSerializer userSerializer = new UserSerializer(); + + public Users(JdbcTemplate sql) { + this.sql = sql; + } + + public void doGetUsers(HttpServletRequest request, + HttpServletResponse response, int vuid) + throws ServletException, IOException { + List<com.juick.User> users = new ArrayList<>(); + + String punames[] = request.getParameterValues("uname"); + if (punames != null) { + ArrayList<String> unames = new ArrayList<>(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) { + List<String> jids = new ArrayList<>(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 = userSerializer.serializeList(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.getUID() > 0) { + uid = u.getUID(); + } + } + } + + if (uid > 0) { + List<Integer> uids = UserQueries.getUserRead(sql, uid); + if (uids.size() > 0) { + List<com.juick.User> users = UserQueries.getUsersByID(sql, uids); + if (users.size() > 0) { + String json = userSerializer.serializeList(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.getUID() > 0) { + uid = u.getUID(); + } + } + } + + if (uid > 0) { + List<User> users = UserQueries.getUserReaders(sql, uid); + String json = userSerializer.serializeList(users); + Main.replyJSON(request, response, json); + return; + } + response.sendError(404); + } +} |