r17 - in trunk/src/main: java/org/chorem/bow webapp
Author: bbrossaud Date: 2010-05-11 11:14:13 +0200 (Tue, 11 May 2010) New Revision: 17 Url: http://chorem.org/repositories/revision/bow/17 Log: added the ControllerServelet comments Modified: trunk/src/main/java/org/chorem/bow/ControllerServlet.java trunk/src/main/java/org/chorem/bow/TokenActions.java trunk/src/main/webapp/generateToken.jsp trunk/src/main/webapp/home.jsp trunk/src/main/webapp/register.jsp trunk/src/main/webapp/search.jsp Modified: trunk/src/main/java/org/chorem/bow/ControllerServlet.java =================================================================== --- trunk/src/main/java/org/chorem/bow/ControllerServlet.java 2010-05-11 07:15:52 UTC (rev 16) +++ trunk/src/main/java/org/chorem/bow/ControllerServlet.java 2010-05-11 09:14:13 UTC (rev 17) @@ -11,7 +11,6 @@ import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.util.List; -import java.util.Locale; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -21,22 +20,8 @@ import org.sharengo.wikitty.Criteria; import org.sharengo.wikitty.WikittyProxy; import org.sharengo.wikitty.search.Search; -import org.nuiton.i18n.I18n; /** - * javascript: - * var server = new XMLHttpRequest(); - * var url=location.href; - * var nameAndTags = prompt('Entrez le nom du lien et la liste des tags sous la forme: name|tag1 tag2 tag3', document.title+'|'); - * server.onreadystatechange = function() { - * if(server.readyState == 4) { - * if (server.status != 200) { - * alert('Erreur d\'ajout au bookmark: '+ server.status + '\n\n' + url + '\n\n' + nameAndTags); - * } - * } - * }; - * server.open('GET', 'http://localhost:8080/bow/bow?action=addUrl&url='+encodeURIComponent(url)+'&nameAndTags='+encodeURIComponent(nameAndTags), true); - * server.send(null); * * @author bbrossaud */ @@ -48,50 +33,73 @@ protected User user = null; protected TokenActions tokenActions = new TokenActions(); - public ControllerServlet() { - I18n.init(Locale.FRANCE); - } - @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { this.doPost(request, response); } + /* @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet error occurs + */ @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - String action = request.getParameter("action"); - - response.setContentType("text/html"); - request.setAttribute("bookmarkActions", bookmarkActions); - request.setAttribute("tokenActions", tokenActions); - if (action != null) { - if (action.equals("register")) { - log.debug("Going to actionRegister"); - this.actionRegister(request, response); - } else if (action.equals("registration")) { - log.debug("Going to Register"); - request.getRequestDispatcher("register.jsp").forward(request, response); - } else if (action.equals("login")) { - log.debug("Going to actionLogin"); - this.actionLogin(request, response); - } else if (action.equals("addUrl")) { - log.debug("Going to actionAddUrl"); - this.actionAddUrl(request, response); - } else if (action.equals("search") && user != null) { - log.debug("Going to actionSearch"); - this.actionSearch(request, response); - } else if (action.equals("clickOnTag") && user != null) { - log.debug("Going to actionClickOnTag"); - this.actionClickOnTag(request, response); - } else if (action.equals("generateToken") && user != null) { - log.debug("Going to actionGenerateToken"); - try { - this.actionGenerateToken(request, response); - } catch (NoSuchAlgorithmException ex) { - log.error("No Such Algorithm", ex); - request.getRequestDispatcher("error.jsp").forward(request, response); + try { + String action = request.getParameter("action"); + response.setContentType("text/html"); + request.setAttribute("bookmarkActions", bookmarkActions); + request.setAttribute("tokenActions", tokenActions); + if (action != null) { + if (action.equals("register")) { + if (log.isDebugEnabled()) { + log.debug("Going to actionRegister"); + } + this.actionRegister(request, response); + } else if (action.equals("registration")) { + if (log.isDebugEnabled()) { + if (log.isDebugEnabled()) { + log.debug("Going to Register"); + } + } + request.getRequestDispatcher("register.jsp").forward(request, response); + } else if (action.equals("login")) { + if (log.isDebugEnabled()) { + log.debug("Going to actionLogin"); + } + this.actionLogin(request, response); + } else if (action.equals("addUrl")) { + if (log.isDebugEnabled()) { + log.debug("Going to actionAddUrl"); + } + this.actionAddUrl(request, response); + } else if (action.equals("search") && user != null) { + if (log.isDebugEnabled()) { + log.debug("Going to actionSearch"); + } + this.actionSearch(request, response); + } else if (action.equals("clickOnTag") && user != null) { + if (log.isDebugEnabled()) { + log.debug("Going to actionClickOnTag"); + } + this.actionClickOnTag(request, response); + } else if (action.equals("generateToken") && user != null) { + if (log.isDebugEnabled()) { + log.debug("Going to actionGenerateToken"); + } + try { + this.actionGenerateToken(request, response); + } catch (NoSuchAlgorithmException ex) { + log.error("No Such Algorithm", ex); + request.getRequestDispatcher("error.jsp").forward(request, response); + } + } else { + user = null; + if (log.isDebugEnabled()) { + log.debug("Going to home"); + } + request.getRequestDispatcher("home.jsp").forward(request, response); } } else { user = null; @@ -100,26 +108,26 @@ } request.getRequestDispatcher("home.jsp").forward(request, response); } - } else { - user = null; - if (log.isDebugEnabled()) { - log.debug("Going to home"); - } - request.getRequestDispatcher("home.jsp").forward(request, response); + } catch (Exception eee) { + request.getRequestDispatcher("error.jsp").forward(request, response); } } + /* @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet error occurs + */ protected void actionGenerateToken(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, NoSuchAlgorithmException { WikittyProxy proxy = model.getProxy(); - String token = tokenActions.generateToken(); + String token = tokenActions.generateToken(); // Generate an encoding MD5 token Criteria criteria = Search.query().eq(Token.FQ_FIELD_TOKEN, token).criteria(); - if (proxy.findByCriteria(User.class, criteria) == null) { + if (proxy.findByCriteria(Token.class, criteria) == null) { // Check if the token already exists TokenImpl newToken = new TokenImpl(); newToken.setToken(token); newToken.setEmail(user.getEmail()); - proxy.store((Token) newToken); + proxy.store((Token) newToken); // If the token doesn't exist, it is stored tokenActions.setToken(token); } else { tokenActions.setToken(""); @@ -127,23 +135,29 @@ request.getRequestDispatcher("generateToken.jsp").forward(request, response); } + /* @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet error occurs + */ protected void actionAddUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - String url = request.getParameter("url"); - String nameAndTags = request.getParameter("nameAndTags"); - String token = request.getParameter("token"); - log.debug("URL = {" + url + "}"); + String url = request.getParameter("url"); // url of the website + String nameAndTags = request.getParameter("nameAndTags"); // name and tags + String token = request.getParameter("token"); // token or not if (url != null && nameAndTags != null) { - if (user == null) { + if (user == null) { // If the user is null, it's maybe a bookmark addition by token user = checkToken(url, nameAndTags, token); } if (user != null) { if (!url.isEmpty() && !nameAndTags.isEmpty()) { Bookmark bookmark = bookmarkActions.createBookmark(url, nameAndTags, user); if (bookmark != null) { - model.getProxy().store(bookmark); - log.debug("Adding URL"); + WikittyProxy proxy = model.getProxy(); + proxy.store(bookmark); // store the bookmark if all is Ok + if (log.isDebugEnabled()) { + log.debug("Adding URL"); + } if (token == null) { this.actionSearch(request, response); } @@ -153,48 +167,63 @@ } } + /* @param url String which contains the bookmark url + * @param nameAndTags String which contains the name and the tags of the + bookmark separated by '|' ==> name|tag1 tag2... + * @param token String which contains the MD5 encoding token + * @return null the token doesn't exist + * @return User the token owner + */ protected User checkToken(String url, String nameAndTags, String token) { if (url != null && nameAndTags != null && token != null) { WikittyProxy proxy = model.getProxy(); Criteria criteria = Search.query().eq(Token.FQ_FIELD_TOKEN, token).criteria(); Token DbToken = proxy.findByCriteria(Token.class, criteria); - if (DbToken != null) { - String userEmail = DbToken.getEmail(); - criteria = Search.query().eq(User.FQ_FIELD_EMAIL, userEmail).criteria(); + if (DbToken != null) { // check if the token exists + String userEmail = DbToken.getEmail(); // the token owner user name (email) + criteria = Search.query().eq(User.FQ_FIELD_EMAIL, userEmail).criteria(); // retrieve user by token return proxy.findByCriteria(User.class, criteria); } } return null; } + /* @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet error occurs + */ protected void actionClickOnTag(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + WikittyProxy proxy = model.getProxy(); String tag = request.getParameter("tag"); bookmarkActions.getBookmarks().clear(); bookmarkActions.getTagsCloud().clear(); Criteria criteria = Search.query().eq(Bookmark.FQ_FIELD_EMAIL, user.getEmail()).criteria(); - List<Bookmark> bookList = model.getProxy(). - findAllByCriteria(Bookmark.class, criteria).getAll(); + List<Bookmark> bookList = proxy.findAllByCriteria(Bookmark.class, criteria).getAll(); // select all bookmarks by user if (bookList != null) { bookmarkActions.setBookmarks(bookList); } if (tag != null) { if (!tag.isEmpty()) { tag = tag.trim(); - bookmarkActions.addTag(tag); + bookmarkActions.addTag(tag); // if a new tag is selected, adding in tag list } } - bookmarkActions.createTagsCloud(); + bookmarkActions.createTagsCloud(); // create the tags cloud with the new informations request.getRequestDispatcher("search.jsp").forward(request, response); } + /* @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet error occurs + */ protected void actionSearch(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - String words = request.getParameter("words"); + WikittyProxy proxy = model.getProxy(); + String words = request.getParameter("words"); // retrieve informations taping in the search field bookmarkActions.reset(); Criteria criteria = Search.query().eq(Bookmark.FQ_FIELD_EMAIL, user.getEmail()).criteria(); - List<Bookmark> bookList = model.getProxy(). - findAllByCriteria(Bookmark.class, criteria).getAll(); + List<Bookmark> bookList = proxy.findAllByCriteria(Bookmark.class, criteria).getAll(); // select all bookmarks by user if (bookList != null) { bookmarkActions.setBookmarks(bookList); } @@ -202,83 +231,88 @@ if (!words.isEmpty()) { words = words.trim(); String[] tags = words.split("\\s+"); - bookmarkActions.addTags(tags); + bookmarkActions.addTags(tags); // add the new tags } } - bookmarkActions.createTagsCloud(); + bookmarkActions.createTagsCloud(); // create the tags cloud with the new informations request.getRequestDispatcher("search.jsp").forward(request, response); } + /* @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet error occurs + */ protected void actionRegister(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - try { - if (this.checkRegister(request, response)) { - request.getRequestDispatcher("register.jsp").forward(request, response); + String email = request.getParameter("email"); + String password = request.getParameter("password"); + if (this.checkRegister(email, password)) { // check if all is well + request.getRequestDispatcher("register.jsp").forward(request, response); + } else { + WikittyProxy proxy = model.getProxy(); + UserImpl newUser = new UserImpl(); + newUser.setPassword(request.getParameter("password")); + newUser.setEmail(request.getParameter("email")); + this.user = proxy.store((User) newUser); // store the new user + if (user == null) { + request.getRequestDispatcher("error.jsp").forward(request, response); } else { - UserImpl newUser = new UserImpl(); - newUser.setPassword(request.getParameter("password")); - newUser.setEmail(request.getParameter("email")); - this.user = model.getProxy().store((User) newUser); - if (user == null) { - log.debug("Not put in the Databases"); - request.getRequestDispatcher("error.jsp").forward(request, response); - } else { - log.debug("adding to the Databases"); - this.actionSearch(request, response); - } + this.actionSearch(request, response); } - } catch (Exception eee) { - request.getRequestDispatcher("error.jsp").forward(request, response); } } + /* @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet error occurs + */ protected void actionLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - try { - User user = this.checkLogin(request, response); + String email = request.getParameter("email"); + String password = request.getParameter("password"); + User login = this.checkLogin(email, password); // check if the user exists - if (user != null) { - log.debug("User exists"); - this.user = user; - this.actionSearch(request, response); - } else { - log.debug("User doesn't exist"); - request.getRequestDispatcher("home.jsp").forward(request, response); - } - } catch (Exception eee) { - request.getRequestDispatcher("error.jsp").forward(request, response); + if (login != null) { + this.user = login; + this.actionSearch(request, response); + } else { + request.getRequestDispatcher("home.jsp").forward(request, response); } - } - protected User checkLogin(HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException { - String email = request.getParameter("email"); - String password = request.getParameter("password"); + /* @param email String which contains the user name (email) + * @param password String which contains the user password + * @return null the user doesn't exist + * @return User the user exists + */ + protected User checkLogin(String email, String password) { - if (!email.isEmpty() && !password.isEmpty()) { - WikittyProxy proxy = model.getProxy(); + if (email != null && password != null) { + if (!email.isEmpty() && !password.isEmpty()) { + WikittyProxy proxy = model.getProxy(); - Criteria criteria = Search.query().eq(User.FQ_FIELD_EMAIL, email).eq(User.FQ_FIELD_PASSWORD, password).criteria(); - return proxy.findByCriteria(User.class, criteria); + Criteria criteria = Search.query().eq(User.FQ_FIELD_EMAIL, email). + eq(User.FQ_FIELD_PASSWORD, password).criteria(); + return proxy.findByCriteria(User.class, criteria); // retrieve user by user name (email) and password + } } - log.debug("Empty password or email"); return null; } - protected boolean checkRegister(HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException { - String email = request.getParameter("email"); - String password = request.getParameter("password"); - log.debug("email={" + email + "}"); - if (!email.isEmpty() && !password.isEmpty()) { - WikittyProxy proxy = model.getProxy(); + /* @param email String which contains the user name (email) + * @param password String which contains the user password + * @return bool false if the user doesn't exists or true + */ + protected boolean checkRegister(String email, String password) { + if (email != null && password != null) { + if (!email.isEmpty() && !password.isEmpty()) { + WikittyProxy proxy = model.getProxy(); - Criteria criteria = Search.query().eq(User.FQ_FIELD_EMAIL, email).criteria(); - if (proxy.findByCriteria(User.class, criteria) == null) { - log.debug("No user with this email"); - return false; + Criteria criteria = Search.query().eq(User.FQ_FIELD_EMAIL, email).criteria(); // retrieve user by user name (email) + if (proxy.findByCriteria(User.class, criteria) == null) { + return false; + } } } return true; Modified: trunk/src/main/java/org/chorem/bow/TokenActions.java =================================================================== --- trunk/src/main/java/org/chorem/bow/TokenActions.java 2010-05-11 07:15:52 UTC (rev 16) +++ trunk/src/main/java/org/chorem/bow/TokenActions.java 2010-05-11 09:14:13 UTC (rev 17) @@ -6,12 +6,12 @@ import java.security.NoSuchAlgorithmException; import org.nuiton.util.StringUtil; -import static org.nuiton.i18n.I18n._; /** * * @author bbrossaud */ + public class TokenActions { String token = ""; Modified: trunk/src/main/webapp/generateToken.jsp =================================================================== --- trunk/src/main/webapp/generateToken.jsp 2010-05-11 07:15:52 UTC (rev 16) +++ trunk/src/main/webapp/generateToken.jsp 2010-05-11 09:14:13 UTC (rev 17) @@ -4,14 +4,20 @@ String token = tokenActions.getToken(); if (token != null && token.isEmpty() == false) { %> +<html> + <body> + Use it to add new bookmarks without authentication : <br/><br/> -Use it to add new bookmarks without authentication : <br/><br/> + <a href="javascript:var%20url=location.href;var%20nameAndTags=prompt('Entrez%20le%20nom%20du%20lien%20et%20la%20liste%20des%20tags%20sous%20la%20forme:%20name|tag1%20tag2%20tag3',%20document.title+'|');var%20image=new%20Image();var%20link='http://localhost:8080/bow/bow?action=addUrl&token=<%=token%>&url='+encodeURIComponent(url)+'&nameAndTags='+encodeURIComponent(nameAndTags);image.src=link;document.body.appendChild(image);window.location=url;">Session</a> + <% + } else { + %> + Token not available, try again later ... + <% + } + %> + + <br/><br/><a href="bow?action=search">Return to the search page</a> -<a href="javascript:var%20url=location.href;var%20nameAndTags=prompt('Entrez%20le%20nom%20du%20lien%20et%20la%20liste%20des%20tags%20sous%20la%20forme:%20name|tag1%20tag2%20tag3',%20document.title+'|');var%20image=new%20Image();var%20link='http://localhost:8080/bow/bow?action=addUrl&token=<%=token%>&url='+encodeURIComponent(url)+'&nameAndTags='+encodeURIComponent(nameAndTags);image.src=link;document.body.appendChild(image);window.location=url;">Session</a> -<% - } else { -%> - Token not available, try again later ... -<% - } -%> \ No newline at end of file + </body> +</html> \ No newline at end of file Modified: trunk/src/main/webapp/home.jsp =================================================================== --- trunk/src/main/webapp/home.jsp 2010-05-11 07:15:52 UTC (rev 16) +++ trunk/src/main/webapp/home.jsp 2010-05-11 09:14:13 UTC (rev 17) @@ -4,8 +4,8 @@ <form method="POST" action="home?action=login"> email <input type="text" name="email" size="20"><br /> password <input type=password name="password" size="20"><br /> - <input type="submit" value="submit"> + <input type="submit" value="login"> </form> - <a href="main?action=registration">register</a> + <a href="bow?action=registration">register</a> </body> </html> \ No newline at end of file Modified: trunk/src/main/webapp/register.jsp =================================================================== --- trunk/src/main/webapp/register.jsp 2010-05-11 07:15:52 UTC (rev 16) +++ trunk/src/main/webapp/register.jsp 2010-05-11 09:14:13 UTC (rev 17) @@ -2,11 +2,11 @@ <body> <h1>Register</h1> - <form method="POST" action="register?action=register"> + <form method="POST" action="bow?action=register"> email <input type="text" name="email" size="20"><br /> password <input type=password name="password" size="20"><br /> - <input type="submit" value="submit"> + <input type="submit" value="Register"> </form> - + <br/><br/><a href="bow?action=home">Return to the login page<a> </body> </html> \ No newline at end of file Modified: trunk/src/main/webapp/search.jsp =================================================================== --- trunk/src/main/webapp/search.jsp 2010-05-11 07:15:52 UTC (rev 16) +++ trunk/src/main/webapp/search.jsp 2010-05-11 09:14:13 UTC (rev 17) @@ -11,22 +11,22 @@ <html> <body> + <a href="bow?action=logout">Logout</a><br/> <h1>Search</h1> - <form method="POST" action="search?action=addUrl"> + <form method="POST" action="bow?action=addUrl"> URL <input type="text" name="url" size="20" value="URL"><br /> Name and Tags <input type="text" name="nameAndTags" size="20" value="name|tag1 tag2..."><br /> <input type="submit" value="add"> </form> - <br/><br/><a href="search?action=generateToken">Generate Token</a><br/><br/> + <br/><br/><a href="bow?action=generateToken">Generate Token</a><br/><br/> - <form method="POST" action="search?action=search"> + <form method="POST" action="bow?action=search"> <input type="text" name="words" size="20" value="<%=bookmarkActions.getSearchLine()%>" ><br /> <input type="submit" value="Find"> </form> - <% if (bookmarkActions != null) { Map<String, Integer> tagsCloud = bookmarkActions.getTagsCloud(); Set<String> tags = tagsCloud.keySet(); @@ -35,7 +35,7 @@ int font = bookmarkActions.getFont(value); %> -<FONT SIZE="<%=font%>" ><a href="search?action=clickOnTag&tag=<%=tag%>"><%=tag%></a></FONT> +<FONT SIZE="<%=font%>" ><a href="bow?action=clickOnTag&tag=<%=tag%>"><%=tag%></a></FONT> <% }
participants (1)
-
bbrossaudï¼ users.chorem.org