Commenting-Server/src/main/java/net/mindoverflow/comments/webapp/controllers/CommentController.java

60 lines
1.5 KiB
Java

package net.mindoverflow.comments.webapp.controllers;
import com.auth0.jwt.JWT;
import net.mindoverflow.comments.webapp.WebServer;
import ro.pippo.controller.Controller;
import ro.pippo.controller.GET;
import ro.pippo.controller.POST;
import ro.pippo.controller.Path;
import ro.pippo.controller.extractor.Param;
import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.Map;
@Path("/comment")
public class CommentController extends Controller
{
@GET
public void getCommentPage()
{
Map<String, Object> model = new HashMap<>();
getRouteContext().render("comment", model);
}
@POST
public void addComment(@Param("comment") String comment, @Param("commentbtn") String commentbtn)
{
Cookie session = getRequest().getCookie("session");
if(session == null)
{
System.out.println("null cookie");
return;
}
String jwtFromCookie = session.getValue();
if(jwtFromCookie == null)
{
System.out.println("null jwt");
return;
}
String username = WebServer.jwtAndUser.get(jwtFromCookie);
if(username == null)
{
System.out.println("null user");
return;
}
// check if jwt is null, if saved in hashmap, and finally verify it with JWT.verify()
if(commentbtn != null)
{
WebServer.userAndComment.put(username, comment);
}
}
}