public Object handlePostLike(Request request, Response response) {

    String permalink = request.queryParams("permalink");
    String commentOrdinalStr = request.queryParams("comment_ordinal");

    // look up the post in question

    int ordinal = Integer.parseInt(commentOrdinalStr);

    // TODO: check return or have checkSession throw
    String username = sessionDAO.findUserNameBySessionId(request.cookie("session"));
    Document post = blogPostDAO.findByPermalink(permalink);

    //  if post not found, redirect to post not found error
    if (post == null) {
      response.redirect("/post_not_found");
    } else {
      blogPostDAO.likePost(permalink, ordinal);

      response.redirect("/post/" + permalink);
    }

    SimpleHash root = new SimpleHash();

    return templateEngine.render(new ModelAndView(root, "entry_template.ftl"));
  }
  /**
   * @param request The request object providing information about the HTTP request
   * @param response The response object providing functionality for modifying the response
   * @return The content to be set in the response
   * @throws Exception implementation can choose to throw exception
   */
  public String handlePostNewPost(Request request, Response response) throws Exception {

    String title = StringEscapeUtils.escapeHtml4(request.queryParams("subject"));
    String post = StringEscapeUtils.escapeHtml4(request.queryParams("body"));
    String tags = StringEscapeUtils.escapeHtml4(request.queryParams("tags"));

    String username = sessionDAO.findUserNameBySessionId(request.cookie("session"));

    if (username == null) {
      response.redirect("/login"); // only logged in users can post to blog
      return templateEngine.render(new ModelAndView(null, "redirect.ftl"));
    } else if (title.equals("") || post.equals("")) {
      SimpleHash root = new SimpleHash();

      // redisplay page with errors
      root.put("errors", "post must contain a title and blog entry.");
      root.put("subject", title);
      root.put("username", username);
      root.put("tags", tags);
      root.put("body", post);
      return templateEngine.render(new ModelAndView(root, "newpost_template.ftl"));
    } else {
      // extract tags
      List<String> tagsArray = extractTags(tags);

      // substitute some <p> for the paragraph breaks
      post = post.replaceAll("\\r?\\n", "<p>");

      String permalink = blogPostDAO.addPost(title, post, tagsArray, username);

      // now redirect to the blog permalink
      response.redirect("/post/" + permalink);
      return templateEngine.render(new ModelAndView(null, "redirect.ftl"));
    }
  }
  /**
   * @param request The request object providing information about the HTTP request
   * @param response The response object providing functionality for modifying the response
   * @return The content to be set in the response
   * @throws Exception implementation can choose to throw exception
   */
  public String handlePostNewComment(Request request, Response response) throws Exception {

    String name = StringEscapeUtils.escapeHtml4(request.queryParams("commentName"));
    String email = StringEscapeUtils.escapeHtml4(request.queryParams("commentEmail"));
    String body = StringEscapeUtils.escapeHtml4(request.queryParams("commentBody"));
    String permalink = request.queryParams("permalink");

    Document post = blogPostDAO.findByPermalink(permalink);
    if (post == null) {
      response.redirect("/post_not_found");
      return templateEngine.render(new ModelAndView(null, "redirect.ftl"));
    }

    // check that comment is good
    else if (name.equals("") || body.equals("")) {
      // bounce this back to the user for correction
      SimpleHash comment = new SimpleHash();
      comment.put("name", name);
      comment.put("email", email);
      comment.put("body", body);

      SimpleHash root = new SimpleHash();
      root.put("comment", comment);
      root.put("post", post);
      root.put("errors", "Post must contain your name and an actual comment");
      return templateEngine.render(new ModelAndView(root, "entry_template.ftl"));

    } else {
      blogPostDAO.addPostComment(name, email, body, permalink);
      response.redirect("/post/" + permalink);
      return templateEngine.render(new ModelAndView(null, "redirect.ftl"));
    }
  }
  /**
   * Metodo para inicial la sesion del usuario.
   *
   * @param request
   * @param response
   * @return
   */
  public Object doLogin(Request request, Response response) {
    String nickname = request.queryParams("nickname").toLowerCase();
    String password = request.queryParams("password");

    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();

    User user =
        (User)
            session
                .createCriteria(User.class)
                .add(Restrictions.eq("nickname", nickname))
                .add(Restrictions.eq("password", password))
                .uniqueResult();

    UserValidator validator = new UserValidator(request.session());
    validator.validateUser(user);

    if (null != user) {
      request.session(true);
      request.session().attribute("user", user);
      response.redirect("/orders");
    } else {
      response.redirect("/");
    }

    return null;
  }
  /**
   * @param request The request object providing information about the HTTP request
   * @param response The response object providing functionality for modifying the response
   * @return The content to be set in the response
   * @throws Exception implementation can choose to throw exception
   */
  public String handleGetPermaLinkPost(Request request, Response response) throws Exception {

    String permalink = request.params(":permalink");

    System.out.println("/post: get " + permalink);

    Document post = blogPostDAO.findByPermalink(permalink);

    if (post == null) {
      response.redirect("/post_not_found");
      return templateEngine.render(new ModelAndView(null, "redirect.ftl"));

    } else {
      // empty comment to hold new comment in form at bottom of blog entry detail page
      SimpleHash newComment = new SimpleHash();
      newComment.put("name", "");
      newComment.put("email", "");
      newComment.put("body", "");

      SimpleHash root = new SimpleHash();
      root.put("post", post);
      root.put("comments", newComment);
      return templateEngine.render(new ModelAndView(root, "entry_template.ftl"));
    }
  }
Beispiel #6
0
  public Object create(Request request, Response response) throws Exception {
    final Map<String, String[]> project = request.queryMap("project").toMap();

    Project p = Project.fromMap(project);

    repository.save(p);

    response.redirect("/#/index/dashboard");
    return null;
  }
  /**
   * @param request The request object providing information about the HTTP request
   * @param response The response object providing functionality for modifying the response
   * @return The content to be set in the response
   * @throws Exception implementation can choose to throw exception
   */
  public String handleGetNewPost(Request request, Response response) throws Exception {

    // get cookie
    String username = sessionDAO.findUserNameBySessionId(request.cookie("session"));

    if (username == null) {
      // looks like a bad request. user is not logged in
      response.redirect("/login");
      return templateEngine.render(new ModelAndView(null, "redirect.ftl"));
    } else {
      SimpleHash root = new SimpleHash();
      root.put("username", username);
      return templateEngine.render(new ModelAndView(root, "newpost_template.ftl"));
    }
  }
  public ModelAndView handle(Request request, Response response) throws Exception {
    // On regarde si l'utilisateur a accès
    ModelAndView modelAndView = Authentification.checkEnseignant(request, response);
    if (modelAndView != null) return modelAndView;

    Map<String, Object> attributes = new HashMap<String, Object>();
    attributes.put("title", "Inscription au créneau");
    attributes.put("connected", (request.session().attribute("email") != null));
    attributes.put("labo", (request.session().attribute("labo") != null));
    attributes.put("enseignant", (request.session().attribute("enseignant") != null));

    Integer idEnseignant = request.session().attribute("enseignant");

    if (idEnseignant == null) response.redirect("/enseignant");

    return new ModelAndView(attributes, "inscription-creneau.ftl");
  }
  /**
   * Metodo para registrar a un usuario.
   *
   * @param request
   * @param response
   * @return
   */
  public Object doSignup(Request request, Response response) {
    String name = request.queryParams("name");
    String lastName = request.queryParams("last_name");
    String nickname = request.queryParams("nickname").toLowerCase();
    String password = request.queryParams("password");
    String confirmPassword = request.queryParams("confirm_password");

    UserValidator validator = new UserValidator(request.session());

    validator.validateUserName(name);
    validator.validateUserLastName(lastName);
    validator.validateNickname(nickname);
    validator.validatePassword(password, confirmPassword);

    if (!validator.error()) {
      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
      Session session = sessionFactory.openSession();

      Transaction transaction = session.beginTransaction();

      User user = new User();
      user.setName(name);
      user.setLastName(lastName);
      user.setNickname(nickname);
      user.setPassword(password);
      user.setIsAdmin(false);

      session.save(user);

      transaction.commit();
      session.close();

      doLogin(request, response);
    } else {
      response.redirect("/");
    }

    return null;
  }
Beispiel #10
0
 @Override
 public void redirect(String location, int httpStatusCode) {
   delegate.redirect(location, httpStatusCode);
 }
Beispiel #11
0
 @Override
 public void redirect(String location) {
   delegate.redirect(location);
 }
 /**
  * Metodo para cerrar la sesion del usuarios.
  *
  * @param request
  * @param response
  * @return
  */
 public Object doLogout(Request request, Response response) {
   request.session().removeAttribute("user");
   response.redirect("/");
   return null;
 }