Search in sources :

Example 46 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class LoginProcessor method forgetPwd.

/**
 * Forget password.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/forget-pwd", method = HTTPRequestMethod.POST)
@Before(adviceClass = UserForgetPwdValidation.class)
public void forgetPwd(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    context.renderJSON();
    final JSONObject requestJSONObject = (JSONObject) request.getAttribute(Keys.REQUEST);
    final String email = requestJSONObject.optString(User.USER_EMAIL);
    try {
        final JSONObject user = userQueryService.getUserByEmail(email);
        if (null == user) {
            context.renderFalseResult().renderMsg(langPropsService.get("notFoundUserLabel"));
            return;
        }
        final String userId = user.optString(Keys.OBJECT_ID);
        final JSONObject verifycode = new JSONObject();
        verifycode.put(Verifycode.BIZ_TYPE, Verifycode.BIZ_TYPE_C_RESET_PWD);
        final String code = RandomStringUtils.randomAlphanumeric(6);
        verifycode.put(Verifycode.CODE, code);
        verifycode.put(Verifycode.EXPIRED, DateUtils.addDays(new Date(), 1).getTime());
        verifycode.put(Verifycode.RECEIVER, email);
        verifycode.put(Verifycode.STATUS, Verifycode.STATUS_C_UNSENT);
        verifycode.put(Verifycode.TYPE, Verifycode.TYPE_C_EMAIL);
        verifycode.put(Verifycode.USER_ID, userId);
        verifycodeMgmtService.addVerifycode(verifycode);
        context.renderTrueResult().renderMsg(langPropsService.get("verifycodeSentLabel"));
    } catch (final ServiceException e) {
        final String msg = langPropsService.get("resetPwdLabel") + " - " + e.getMessage();
        LOGGER.log(Level.ERROR, msg + "[name={0}, email={1}]", email);
        context.renderMsg(msg);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 47 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class LoginProcessor method showLogin.

/**
 * Shows login page.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/login", method = HTTPRequestMethod.GET)
@Before(adviceClass = StopwatchStartAdvice.class)
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showLogin(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    if (null != userQueryService.getCurrentUser(request) || userMgmtService.tryLogInWithCookie(request, response)) {
        response.sendRedirect(Latkes.getServePath());
        return;
    }
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    String referer = request.getParameter(Common.GOTO);
    if (!StringUtils.startsWith(referer, Latkes.getServePath())) {
        referer = request.getHeader("referer");
    }
    if (StringUtils.isBlank(referer)) {
        referer = Latkes.getServePath();
    }
    renderer.setTemplateName("/verify/login.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    dataModel.put(Common.GOTO, referer);
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
Also used : JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 48 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class LoginProcessor method nextGuideStep.

/**
 * Next guide step.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 */
@RequestProcessing(value = "/guide/next", method = HTTPRequestMethod.POST)
@Before(adviceClass = { LoginCheck.class })
public void nextGuideStep(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) {
    context.renderJSON();
    JSONObject requestJSONObject;
    try {
        requestJSONObject = Requests.parseRequestJSONObject(request, response);
    } catch (final Exception e) {
        LOGGER.warn(e.getMessage());
        return;
    }
    JSONObject user = (JSONObject) request.getAttribute(User.USER);
    final String userId = user.optString(Keys.OBJECT_ID);
    int step = requestJSONObject.optInt(UserExt.USER_GUIDE_STEP);
    if (UserExt.USER_GUIDE_STEP_STAR_PROJECT < step || UserExt.USER_GUIDE_STEP_FIN >= step) {
        step = UserExt.USER_GUIDE_STEP_FIN;
    }
    try {
        user = userQueryService.getUser(userId);
        user.put(UserExt.USER_GUIDE_STEP, step);
        userMgmtService.updateUser(userId, user);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Guide next step [" + step + "] failed", e);
        return;
    }
    context.renderJSON(true);
}
Also used : JSONObject(org.json.JSONObject) ServletException(javax.servlet.ServletException) ServiceException(org.b3log.latke.service.ServiceException) IOException(java.io.IOException) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 49 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class ManProcessor method showMan.

/**
 * Shows man.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/man", method = HTTPRequestMethod.GET)
@Before(adviceClass = StopwatchStartAdvice.class)
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showMan(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    if (!ManQueryService.TLDR_ENABLED) {
        response.sendRedirect("https://hacpai.com/man");
        return;
    }
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("other/man.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
    final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
    dataModelService.fillRandomArticles(avatarViewMode, dataModel);
    dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
    dataModelService.fillSideTags(dataModel);
    dataModelService.fillLatestCmts(dataModel);
    String cmd = request.getParameter(Common.CMD);
    if (StringUtils.isBlank(cmd)) {
        cmd = "man";
    }
    List<JSONObject> mans = manQueryService.getMansByCmdPrefix(cmd);
    if (mans.isEmpty()) {
        mans = manQueryService.getMansByCmdPrefix("man");
    }
    dataModel.put(Common.MANS, mans);
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 50 with Before

use of org.b3log.latke.servlet.annotation.Before in project symphony by b3log.

the class NotificationProcessor method showFollowingNotifications.

/**
 * Shows [following] notifications.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/notifications/following", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showFollowingNotifications(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final JSONObject currentUser = userQueryService.getCurrentUser(request);
    if (null == currentUser) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("/home/notifications/following.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final String userId = currentUser.optString(Keys.OBJECT_ID);
    String pageNumStr = request.getParameter("p");
    if (Strings.isEmptyOrNull(pageNumStr) || !Strings.isNumeric(pageNumStr)) {
        pageNumStr = "1";
    }
    final int pageNum = Integer.valueOf(pageNumStr);
    final int pageSize = Symphonys.getInt("followingNotificationsCnt");
    final int windowSize = Symphonys.getInt("followingNotificationsWindowSize");
    final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
    final JSONObject result = notificationQueryService.getFollowingNotifications(avatarViewMode, userId, pageNum, pageSize);
    final List<JSONObject> followingNotifications = (List<JSONObject>) result.get(Keys.RESULTS);
    dataModel.put(Common.FOLLOWING_NOTIFICATIONS, followingNotifications);
    fillNotificationCount(userId, dataModel);
    final int recordCnt = result.getInt(Pagination.PAGINATION_RECORD_COUNT);
    final int pageCount = (int) Math.ceil((double) recordCnt / (double) pageSize);
    final List<Integer> pageNums = Paginator.paginate(pageNum, pageSize, pageCount, windowSize);
    if (!pageNums.isEmpty()) {
        dataModel.put(Pagination.PAGINATION_FIRST_PAGE_NUM, pageNums.get(0));
        dataModel.put(Pagination.PAGINATION_LAST_PAGE_NUM, pageNums.get(pageNums.size() - 1));
    }
    dataModel.put(Pagination.PAGINATION_CURRENT_PAGE_NUM, pageNum);
    dataModel.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
    dataModel.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

Before (org.b3log.latke.servlet.annotation.Before)169 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)169 JSONObject (org.json.JSONObject)166 After (org.b3log.latke.servlet.annotation.After)135 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)105 ServiceException (org.b3log.latke.service.ServiceException)37 Date (java.util.Date)13 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 JSONArray (org.json.JSONArray)8 Auth (com.qiniu.util.Auth)7 ParseException (java.text.ParseException)6 List (java.util.List)6 ServletException (javax.servlet.ServletException)3 Configuration (com.qiniu.storage.Configuration)1 UploadManager (com.qiniu.storage.UploadManager)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1