From 915e804c097ed97e39eebc498f1339cab53109cd Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Mon, 28 Nov 2016 20:04:05 +0700 Subject: WebUtils utilite class ; some inprovements; tests now run successfully --- .../main/java/com/juick/api/controllers/Users.java | 40 +++++++--------------- 1 file changed, 13 insertions(+), 27 deletions(-) (limited to 'juick-api/src/main/java/com') diff --git a/juick-api/src/main/java/com/juick/api/controllers/Users.java b/juick-api/src/main/java/com/juick/api/controllers/Users.java index 75dea5f5..396a716c 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Users.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Users.java @@ -5,6 +5,7 @@ import com.juick.api.util.HttpForbiddenException; import com.juick.api.util.HttpNotFoundException; import com.juick.service.UserService; import com.juick.util.UserUtils; +import com.juick.util.WebUtils; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @@ -15,12 +16,9 @@ import org.springframework.web.bind.annotation.ResponseBody; import javax.inject.Inject; import java.security.Principal; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; import java.util.List; /** - * * @author ugnich */ @Controller @@ -32,39 +30,27 @@ public class Users { @RequestMapping(value = "/users", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public List doGetUsers( - @RequestParam(value = "uname", required = false) String[] punames, - @RequestParam(value = "jid", required = false) String[] pjids) { + @RequestParam(value = "uname", required = false) List unames, + @RequestParam(value = "jid", required = false) List jids) { List users = new ArrayList<>(); - 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) { + if (unames != null) { + unames.removeIf(WebUtils::isNotUserName); + + if (!unames.isEmpty() && unames.size() < 20) users.addAll(userService.getUsersByName(unames)); - } } - if (pjids != null) { - List 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) { + if (jids != null) { + jids.removeIf(WebUtils::isNotJid); + + if (!jids.isEmpty() && jids.size() < 20) users.addAll(userService.getUsersByJID(jids)); - } } - if (!users.isEmpty()) { + if (!users.isEmpty()) return users; - } + throw new HttpNotFoundException(); } -- cgit v1.2.3