/**
   * Init.
   *
   * @throws Exception exception
   */
  @Test
  public void init() throws Exception {
    final InitService initService = getInitService();

    final JSONObject requestJSONObject = new JSONObject();
    requestJSONObject.put(User.USER_EMAIL, "*****@*****.**");
    requestJSONObject.put(User.USER_NAME, "Admin");
    requestJSONObject.put(User.USER_PASSWORD, "pass");

    initService.init(requestJSONObject);

    final UserQueryService userQueryService = getUserQueryService();
    Assert.assertNotNull(userQueryService.getUserByEmail("*****@*****.**"));
  }
Beispiel #2
0
  /**
   * Resets forgotten password.
   *
   * <p>Renders the response with a json object, for example,
   *
   * <pre>
   * {
   *     "isLoggedIn": boolean,
   *     "msg": "" // optional, exists if isLoggedIn equals to false
   * }
   * </pre>
   *
   * @param context the specified context
   */
  @RequestProcessing(value = "/reset", method = HTTPRequestMethod.POST)
  public void reset(final HTTPRequestContext context) {
    final HttpServletRequest request = context.getRequest();
    final JSONRenderer renderer = new JSONRenderer();

    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();

    renderer.setJSONObject(jsonObject);

    try {
      final JSONObject requestJSONObject;

      requestJSONObject = Requests.parseRequestJSONObject(request, context.getResponse());
      final String userEmail = requestJSONObject.getString(User.USER_EMAIL);
      final String newPwd = requestJSONObject.getString("newPwd");
      final JSONObject user = userQueryService.getUserByEmail(userEmail);

      user.put(User.USER_PASSWORD, newPwd);
      userMgmtService.updateUser(user);
      LOGGER.log(Level.DEBUG, "[{0}]'s password updated successfully.", new Object[] {userEmail});

      jsonObject.put("succeed", true);
      jsonObject.put("to", Latkes.getServePath() + "/login?from=reset");
      jsonObject.put(Keys.MSG, langPropsService.get("resetPwdSuccessMsg"));
    } catch (final Exception e) {
      LOGGER.log(Level.ERROR, e.getMessage(), e);
    }
  }
Beispiel #3
0
  /**
   * Logins.
   *
   * <p>Renders the response with a json object, for example,
   *
   * <pre>
   * {
   *     "isLoggedIn": boolean,
   *     "msg": "" // optional, exists if isLoggedIn equals to false
   * }
   * </pre>
   *
   * @param context the specified context
   */
  @RequestProcessing(value = "/login", method = HTTPRequestMethod.POST)
  public void login(final HTTPRequestContext context) {
    final HttpServletRequest request = context.getRequest();

    final JSONRenderer renderer = new JSONRenderer();

    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();

    renderer.setJSONObject(jsonObject);

    try {
      jsonObject.put(Common.IS_LOGGED_IN, false);
      final String loginFailLabel = langPropsService.get("loginFailLabel");

      jsonObject.put(Keys.MSG, loginFailLabel);

      final JSONObject requestJSONObject =
          Requests.parseRequestJSONObject(request, context.getResponse());
      final String userEmail = requestJSONObject.getString(User.USER_EMAIL);
      final String userPwd = requestJSONObject.getString(User.USER_PASSWORD);

      if (Strings.isEmptyOrNull(userEmail) || Strings.isEmptyOrNull(userPwd)) {
        return;
      }

      LOGGER.log(Level.INFO, "Login[email={0}]", userEmail);

      final JSONObject user = userQueryService.getUserByEmail(userEmail);

      if (null == user) {
        LOGGER.log(Level.WARN, "Not found user[email={0}]", userEmail);
        return;
      }

      if (MD5.hash(userPwd).equals(user.getString(User.USER_PASSWORD))) {
        Sessions.login(request, context.getResponse(), user);

        LOGGER.log(Level.INFO, "Logged in[email={0}]", userEmail);

        jsonObject.put(Common.IS_LOGGED_IN, true);

        if (Role.VISITOR_ROLE.equals(user.optString(User.USER_ROLE))) {
          jsonObject.put("to", Latkes.getServePath());
        } else {
          jsonObject.put("to", Latkes.getServePath() + Common.ADMIN_INDEX_URI);
        }

        jsonObject.remove(Keys.MSG);

        return;
      }

      LOGGER.log(Level.WARN, "Wrong password[{0}]", userPwd);
    } catch (final Exception e) {
      LOGGER.log(Level.ERROR, e.getMessage(), e);
    }
  }
Beispiel #4
0
  /**
   * Resets forgotten password.
   *
   * <p>Renders the response with a json object, for example,
   *
   * <pre>
   * {
   *     "isLoggedIn": boolean,
   *     "msg": "" // optional, exists if isLoggedIn equals to false
   * }
   * </pre>
   *
   * @param context the specified context
   */
  @RequestProcessing(value = "/forgot", method = HTTPRequestMethod.POST)
  public void forgot(final HTTPRequestContext context) {
    final HttpServletRequest request = context.getRequest();

    final JSONRenderer renderer = new JSONRenderer();

    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();

    renderer.setJSONObject(jsonObject);

    try {
      jsonObject.put("succeed", false);
      jsonObject.put(Keys.MSG, langPropsService.get("resetPwdSuccessMsg"));

      final JSONObject requestJSONObject =
          Requests.parseRequestJSONObject(request, context.getResponse());
      final String userEmail = requestJSONObject.getString(User.USER_EMAIL);

      if (Strings.isEmptyOrNull(userEmail)) {
        LOGGER.log(Level.WARN, "Why user's email is empty");
        return;
      }

      LOGGER.log(Level.INFO, "Login[email={0}]", userEmail);

      final JSONObject user = userQueryService.getUserByEmail(userEmail);

      if (null == user) {
        LOGGER.log(Level.WARN, "Not found user[email={0}]", userEmail);
        jsonObject.put(Keys.MSG, langPropsService.get("userEmailNotFoundMsg"));
        return;
      }

      sendResetUrl(userEmail, jsonObject);
    } catch (final Exception e) {
      LOGGER.log(Level.ERROR, e.getMessage(), e);
    }
  }
Beispiel #5
0
  /**
   * Shows articles related with a tag with the specified context.
   *
   * @param context the specified context
   * @throws IOException io exception
   */
  @RequestProcessing(value = "/tags/**", method = HTTPRequestMethod.GET)
  public void showTagArticles(final HTTPRequestContext context) throws IOException {
    final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();

    context.setRenderer(renderer);

    renderer.setTemplateName("tag-articles.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();

    final HttpServletRequest request = context.getRequest();
    final HttpServletResponse response = context.getResponse();

    try {
      String requestURI = request.getRequestURI();

      if (!requestURI.endsWith("/")) {
        requestURI += "/";
      }

      String tagTitle = getTagTitle(requestURI);
      final int currentPageNum = getCurrentPageNum(requestURI, tagTitle);

      if (-1 == currentPageNum) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
      }

      LOGGER.log(
          Level.DEBUG,
          "Tag[title={0}, currentPageNum={1}]",
          new Object[] {tagTitle, currentPageNum});

      tagTitle = URLDecoder.decode(tagTitle, "UTF-8");
      final JSONObject result = tagQueryService.getTagByTitle(tagTitle);

      if (null == result) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
      }

      final JSONObject tag = result.getJSONObject(Tag.TAG);
      final String tagId = tag.getString(Keys.OBJECT_ID);

      final JSONObject preference = preferenceQueryService.getPreference();

      Skins.fillLangs(
          preference.optString(Option.ID_C_LOCALE_STRING),
          (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME),
          dataModel);

      final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
      final int windowSize = preference.getInt(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE);

      final List<JSONObject> articles =
          articleQueryService.getArticlesByTag(tagId, currentPageNum, pageSize);

      if (articles.isEmpty()) {
        try {
          response.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
        } catch (final IOException ex) {
          LOGGER.error(ex.getMessage());
        }
      }

      final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();

      if (hasMultipleUsers) {
        filler.setArticlesExProperties(request, articles, preference);
      } else {
        // All articles composed by the same author
        final JSONObject author = articleQueryService.getAuthor(articles.get(0));

        filler.setArticlesExProperties(request, articles, author, preference);
      }

      final int tagArticleCount = tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
      final int pageCount = (int) Math.ceil((double) tagArticleCount / (double) pageSize);

      LOGGER.log(
          Level.TRACE,
          "Paginate tag-articles[currentPageNum={0}, pageSize={1}, pageCount={2}, windowSize={3}]",
          new Object[] {currentPageNum, pageSize, pageCount, windowSize});
      final List<Integer> pageNums =
          Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);

      LOGGER.log(Level.TRACE, "tag-articles[pageNums={0}]", pageNums);

      Collections.sort(articles, Comparators.ARTICLE_CREATE_DATE_COMPARATOR);

      fillPagination(dataModel, pageCount, currentPageNum, articles, pageNums);
      dataModel.put(Common.PATH, "/tags/" + URLEncoder.encode(tagTitle, "UTF-8"));
      dataModel.put(Keys.OBJECT_ID, tagId);
      dataModel.put(Tag.TAG, tag);

      filler.fillSide(request, dataModel, preference);
      filler.fillBlogHeader(request, response, dataModel, preference);
      filler.fillBlogFooter(request, dataModel, preference);

      statisticMgmtService.incBlogViewCount(request, response);
    } catch (final ServiceException e) {
      LOGGER.log(Level.ERROR, e.getMessage(), e);

      try {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
      } catch (final IOException ex) {
        LOGGER.error(ex.getMessage());
      }
    } catch (final JSONException e) {
      LOGGER.log(Level.ERROR, e.getMessage(), e);

      try {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
      } catch (final IOException ex) {
        LOGGER.error(ex.getMessage());
      }
    }
  }