r13 - in trunk/src/main: java/org/chorem/bow webapp
Author: bbrossaud Date: 2010-05-10 14:45:14 +0200 (Mon, 10 May 2010) New Revision: 13 Url: http://chorem.org/repositories/revision/bow/13 Log: the bookmark addition works, the token generation works but the javascript doesn't work Removed: trunk/src/main/java/org/chorem/bow/Login.java Modified: trunk/src/main/java/org/chorem/bow/BookmarkActions.java trunk/src/main/java/org/chorem/bow/ControllerServlet.java trunk/src/main/webapp/home.jsp trunk/src/main/webapp/search.jsp Modified: trunk/src/main/java/org/chorem/bow/BookmarkActions.java =================================================================== --- trunk/src/main/java/org/chorem/bow/BookmarkActions.java 2010-05-07 13:29:16 UTC (rev 12) +++ trunk/src/main/java/org/chorem/bow/BookmarkActions.java 2010-05-10 12:45:14 UTC (rev 13) @@ -26,22 +26,28 @@ protected int tmax = -1; protected int tmin = -1; - public Bookmark createBookmark(String query, User user) { - Bookmark bookmark = (Bookmark) new BookmarkImpl(); + public Bookmark createBookmark(String url, String nameAndTags, User user) { - int linkIndex = query.indexOf('|'); - if (linkIndex < 0) { + int nameIndex = nameAndTags.indexOf('|'); + if (nameIndex < 0) { return null; } - String link = query.substring(0, linkIndex); - link = link.replaceAll("\\s*|", ""); - query = query.substring(linkIndex + 1); - query = query.trim(); - String[] tags = query.split("\\s+"); - for (int i = 0; i < tags.length; ++i) { - bookmark.addTags(tags[i]); + Bookmark bookmark = (Bookmark) new BookmarkImpl(); + + String link = url.trim(); + bookmark.setLink(link); + + String name = nameAndTags.substring(0, nameIndex); + name = name.trim(); + bookmark.setDescription(name); + + String tags = nameAndTags.substring(nameIndex + 1); + tags = tags.trim(); + String[] tagsTab = tags.split("\\s+"); + for (int i = 0; i < tagsTab.length; ++i) { + bookmark.addTags(tagsTab[i]); } - bookmark.setLink(link); + bookmark.setEmail(user.getEmail()); bookmark.setDate(new Date()); return bookmark; Modified: trunk/src/main/java/org/chorem/bow/ControllerServlet.java =================================================================== --- trunk/src/main/java/org/chorem/bow/ControllerServlet.java 2010-05-07 13:29:16 UTC (rev 12) +++ trunk/src/main/java/org/chorem/bow/ControllerServlet.java 2010-05-10 12:45:14 UTC (rev 13) @@ -9,8 +9,9 @@ * @author bbrossaud */ import java.io.IOException; -import java.util.ArrayList; +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; @@ -20,33 +21,39 @@ import org.sharengo.wikitty.Criteria; import org.sharengo.wikitty.WikittyProxy; import org.sharengo.wikitty.search.Search; +import org.nuiton.i18n.I18n; +/** + * javascript: + * 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("Error d'ajout au bookmark: "+ server.status + "\n\n" + url + "\n\n" + nameAndTags); + * }; + * server.open("GET", 'http://localhost:8080/bow?action=addUrl&url='+encodeURIComponent(url)+'&nameAndtags='+encodeURIComponent(nameAndTags), true); + * server.send(null); + * + * @author bbrossaud + */ public class ControllerServlet extends HttpServlet { private static final Log log = LogFactory.getLog(ControllerServlet.class); protected Model model = new Model(); protected BookmarkActions bookmarkActions = new BookmarkActions(); 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 { - String action = request.getParameter("action"); - - response.setContentType("text/html"); - request.setAttribute("bookmarkActions", bookmarkActions); - if (action != null) { - if (action.equals("register")) { - request.getRequestDispatcher("register.jsp").forward(request, response); - } else if (action.equals("clickontag")) { - log.debug("Going to actionClickOnTag"); - this.actionClickOnTag(request, response); - } else { - request.getRequestDispatcher("home.jsp").forward(request, response); - } - } else { - request.getRequestDispatcher("home.jsp").forward(request, response); - } + this.doPost(request, response); } @Override @@ -56,42 +63,106 @@ 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")) { + } 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); + } } else { - log.debug("Going to home"); + user = null; + if (log.isDebugEnabled()) { + log.debug("Going to home"); + } 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); } } + protected void actionGenerateToken(HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException, NoSuchAlgorithmException { + + WikittyProxy proxy = model.getProxy(); + String token = tokenActions.generateToken(); + Criteria criteria = Search.query().eq(Token.FQ_FIELD_TOKEN, token).criteria(); + if (proxy.findByCriteria(User.class, criteria) == null) { + TokenImpl newToken = new TokenImpl(); + newToken.setToken(token); + newToken.setEmail(user.getEmail()); + proxy.store((Token) newToken); + tokenActions.setToken(token); + } else { + tokenActions.setToken(""); + } + request.getRequestDispatcher("generateToken.jsp").forward(request, response); + } + 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 + "}"); + if (url != null && nameAndTags != null) { + if (user == null) { + 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"); + if (token == null) { + this.actionSearch(request, response); + } + } + } + } + } + } - if (url != null) { - - log.debug("URL = {" + url + "}"); - Bookmark bookmark = bookmarkActions.createBookmark(url, user); - if (bookmark != null) { - model.getProxy().store(bookmark); - log.debug("Adding URL"); + 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(); + return proxy.findByCriteria(User.class, criteria); } } - request.getRequestDispatcher("search.jsp").forward(request, response); + return null; } protected void actionClickOnTag(HttpServletRequest request, HttpServletResponse response) @@ -99,8 +170,9 @@ 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, null).getAll(); + findAllByCriteria(Bookmark.class, criteria).getAll(); if (bookList != null) { bookmarkActions.setBookmarks(bookList); } @@ -118,8 +190,9 @@ throws IOException, ServletException { String words = request.getParameter("words"); bookmarkActions.reset(); + Criteria criteria = Search.query().eq(Bookmark.FQ_FIELD_EMAIL, user.getEmail()).criteria(); List<Bookmark> bookList = model.getProxy(). - findAllByCriteria(Bookmark.class, null).getAll(); + findAllByCriteria(Bookmark.class, criteria).getAll(); if (bookList != null) { bookmarkActions.setBookmarks(bookList); } @@ -149,13 +222,7 @@ request.getRequestDispatcher("error.jsp").forward(request, response); } else { log.debug("adding to the Databases"); - List<Bookmark> bookList = model.getProxy(). - findAllByCriteria(Bookmark.class, null).getAll(); - if (bookList != null) { - bookmarkActions.setBookmarks(bookList); - } - bookmarkActions.createTagsCloud(); - request.getRequestDispatcher("search.jsp").forward(request, response); + this.actionSearch(request, response); } } } catch (Exception eee) { @@ -171,13 +238,7 @@ if (user != null) { log.debug("User exists"); this.user = user; - List<Bookmark> bookList = model.getProxy(). - findAllByCriteria(Bookmark.class, null).getAll(); - if (bookList != null) { - bookmarkActions.setBookmarks(bookList); - } - bookmarkActions.createTagsCloud(); - request.getRequestDispatcher("search.jsp").forward(request, response); + this.actionSearch(request, response); } else { log.debug("User doesn't exist"); request.getRequestDispatcher("home.jsp").forward(request, response); Deleted: trunk/src/main/java/org/chorem/bow/Login.java =================================================================== --- trunk/src/main/java/org/chorem/bow/Login.java 2010-05-07 13:29:16 UTC (rev 12) +++ trunk/src/main/java/org/chorem/bow/Login.java 2010-05-10 12:45:14 UTC (rev 13) @@ -1,55 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - -package org.chorem.bow; - -/** - * - * @author bbrossaud - */ - -import java.io.IOException; -import java.io.PrintWriter; -import javax.servlet.ServletConfig; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - - -public class Login extends HttpServlet { - - - @Override - public void init(ServletConfig config) throws ServletException { - - } - - @Override - public void init() throws ServletException { - - } - - - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException - { - String password; - String email; - - password = request.getParameter("password"); - email = request.getParameter("email"); - if (!password.isEmpty() && !email.isEmpty()) - { - - } - else - { - response.setContentType("text/html"); - request.getRequestDispatcher("auth.jsp").forward(request, response); - } - } -} Modified: trunk/src/main/webapp/home.jsp =================================================================== --- trunk/src/main/webapp/home.jsp 2010-05-07 13:29:16 UTC (rev 12) +++ trunk/src/main/webapp/home.jsp 2010-05-10 12:45:14 UTC (rev 13) @@ -6,6 +6,6 @@ password <input type=password name="password" size="20"><br /> <input type="submit" value="submit"> </form> - <a href="main?action=register">register</a> + <a href="main?action=registration">register</a> </body> </html> \ No newline at end of file Modified: trunk/src/main/webapp/search.jsp =================================================================== --- trunk/src/main/webapp/search.jsp 2010-05-07 13:29:16 UTC (rev 12) +++ trunk/src/main/webapp/search.jsp 2010-05-10 12:45:14 UTC (rev 13) @@ -14,7 +14,8 @@ <h1>Search</h1> <form method="POST" action="search?action=addUrl"> - URL <input type="text" name="url" size="20" value="URL|tag1 tag2..."><br /> + 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> @@ -23,6 +24,8 @@ <input type="submit" value="Find"> </form> + <a href="search?action=generateToken">Generate Token</a><br/> + <% if (bookmarkActions != null) { Map<String, Integer> tagsCloud = bookmarkActions.getTagsCloud(); Set<String> tags = tagsCloud.keySet(); @@ -31,7 +34,7 @@ int font = bookmarkActions.getFont(value); %> -<FONT SIZE="<%=font%>" ><a href="search?action=clickontag&tag=<%=tag%>"><%=tag%></a></FONT> +<FONT SIZE="<%=font%>" ><a href="search?action=clickOnTag&tag=<%=tag%>"><%=tag%></a></FONT> <% } @@ -43,7 +46,7 @@ <% for (Bookmark bookmark : bookmarkList) { %> - <%=bookmark.getLink()%><br/> + <a href="<%=bookmark.getLink()%>"><%=bookmark.getDescription()%></a><br/> <% } } else if (!tagsSearch.isEmpty() && bookmarkList.isEmpty()) {
participants (1)
-
bbrossaudï¼ users.chorem.org