started working on templates and api
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package de.srsoftware.widerhall.web;
|
||||
|
||||
import de.srsoftware.widerhall.data.User;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -8,14 +10,47 @@ import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static de.srsoftware.widerhall.Constants.*;
|
||||
import static de.srsoftware.widerhall.Util.t;
|
||||
|
||||
public class Rest extends HttpServlet {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Rest.class);
|
||||
private static final String USER_LIST = "user/list";
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String method = req.getMethod();
|
||||
LOG.debug("GET {}"+method);
|
||||
String error = handleGet(req, resp);
|
||||
if (error != null) resp.sendError(400,error);
|
||||
}
|
||||
|
||||
public String handleGet(HttpServletRequest req, HttpServletResponse resp){
|
||||
Object o = req.getSession().getAttribute(USER);
|
||||
JSONObject json = new JSONObject();
|
||||
if (o instanceof User user){
|
||||
var path = req.getPathInfo();
|
||||
json.put(USER,safeMapUser(user));
|
||||
path = path == null ? INDEX : path.substring(1);
|
||||
switch (path) {
|
||||
case USER_LIST:
|
||||
json.put("users", (user.is(ADMIN) ? User.list() : List.of(user)).stream().map(Rest::safeMapUser).toList());
|
||||
break;
|
||||
|
||||
}
|
||||
} else {
|
||||
json.put(ERROR,"Not logged in!");
|
||||
}
|
||||
try {
|
||||
resp.getWriter().println(json.toJSONString());
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
return t("Failed to handle request: {}",e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String,String> safeMapUser(User user){
|
||||
return Map.of(NAME,user.name(),EMAIL,user.email(),PASSWORD,user.hashedPassword() == null ? "no" : "yes");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user