Search in sources :

Example 11 with Before

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

the class AdminProcessor method showUsers.

/**
 * Shows admin users.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/users", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showUsers(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("admin/users.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    String pageNumStr = request.getParameter("p");
    if (Strings.isEmptyOrNull(pageNumStr) || !Strings.isNumeric(pageNumStr)) {
        pageNumStr = "1";
    }
    final int pageNum = Integer.valueOf(pageNumStr);
    final int pageSize = PAGE_SIZE;
    final int windowSize = WINDOW_SIZE;
    final JSONObject requestJSONObject = new JSONObject();
    requestJSONObject.put(Pagination.PAGINATION_CURRENT_PAGE_NUM, pageNum);
    requestJSONObject.put(Pagination.PAGINATION_PAGE_SIZE, pageSize);
    requestJSONObject.put(Pagination.PAGINATION_WINDOW_SIZE, windowSize);
    final String query = request.getParameter(Common.QUERY);
    if (!Strings.isEmptyOrNull(query)) {
        requestJSONObject.put(Common.QUERY, query);
    }
    final JSONObject result = userQueryService.getUsers(requestJSONObject);
    dataModel.put(User.USERS, CollectionUtils.jsonArrayToList(result.optJSONArray(User.USERS)));
    final JSONObject pagination = result.optJSONObject(Pagination.PAGINATION);
    final int pageCount = pagination.optInt(Pagination.PAGINATION_PAGE_COUNT);
    final JSONArray pageNums = pagination.optJSONArray(Pagination.PAGINATION_PAGE_NUMS);
    dataModel.put(Pagination.PAGINATION_FIRST_PAGE_NUM, pageNums.opt(0));
    dataModel.put(Pagination.PAGINATION_LAST_PAGE_NUM, pageNums.opt(pageNums.length() - 1));
    dataModel.put(Pagination.PAGINATION_CURRENT_PAGE_NUM, pageNum);
    dataModel.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
    dataModel.put(Pagination.PAGINATION_PAGE_NUMS, CollectionUtils.jsonArrayToList(pageNums));
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) 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 12 with Before

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

the class AdminProcessor method addReservedWord.

/**
 * Adds a reserved word.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/add-reserved-word", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = StopwatchEndAdvice.class)
public void addReservedWord(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    String word = request.getParameter(Common.WORD);
    word = StringUtils.trim(word);
    if (StringUtils.isBlank(word)) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        dataModel.put(Keys.MSG, langPropsService.get("invalidReservedWordLabel"));
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    if (optionQueryService.existReservedWord(word)) {
        response.sendRedirect(Latkes.getServePath() + "/admin/reserved-words");
        return;
    }
    try {
        final JSONObject reservedWord = new JSONObject();
        reservedWord.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_RESERVED_WORDS);
        reservedWord.put(Option.OPTION_VALUE, word);
        optionMgmtService.addOption(reservedWord);
    } catch (final Exception e) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        dataModel.put(Keys.MSG, e.getMessage());
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    response.sendRedirect(Latkes.getServePath() + "/admin/reserved-words");
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) ServiceException(org.b3log.latke.service.ServiceException) ParseException(java.text.ParseException) IOException(java.io.IOException) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 13 with Before

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

the class AdminProcessor method addUser.

/**
 * Adds a user.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/add-user", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void addUser(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String userName = request.getParameter(User.USER_NAME);
    final String email = request.getParameter(User.USER_EMAIL);
    final String password = request.getParameter(User.USER_PASSWORD);
    final String appRole = request.getParameter(UserExt.USER_APP_ROLE);
    final boolean nameInvalid = UserRegisterValidation.invalidUserName(userName);
    final boolean emailInvalid = !Strings.isEmail(email);
    final boolean passwordInvalid = UserRegister2Validation.invalidUserPassword(password);
    if (nameInvalid || emailInvalid || passwordInvalid) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        if (nameInvalid) {
            dataModel.put(Keys.MSG, langPropsService.get("invalidUserNameLabel"));
        } else if (emailInvalid) {
            dataModel.put(Keys.MSG, langPropsService.get("invalidEmailLabel"));
        } else if (passwordInvalid) {
            dataModel.put(Keys.MSG, langPropsService.get("invalidPasswordLabel"));
        }
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    String userId;
    try {
        final JSONObject user = new JSONObject();
        user.put(User.USER_NAME, userName);
        user.put(User.USER_EMAIL, email);
        user.put(User.USER_PASSWORD, MD5.hash(password));
        user.put(UserExt.USER_APP_ROLE, appRole);
        user.put(UserExt.USER_STATUS, UserExt.USER_STATUS_C_VALID);
        final JSONObject admin = (JSONObject) request.getAttribute(User.USER);
        user.put(UserExt.USER_LANGUAGE, admin.optString(UserExt.USER_LANGUAGE));
        userId = userMgmtService.addUser(user);
    } catch (final ServiceException e) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        dataModel.put(Keys.MSG, e.getMessage());
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    response.sendRedirect(Latkes.getServePath() + "/admin/user/" + userId);
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) 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 14 with Before

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

the class AdminProcessor method showDomain.

/**
 * Shows a domain.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @param domainId the specified domain id
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/domain/{domainId}", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showDomain(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String domainId) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("admin/domain.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final JSONObject domain = domainQueryService.getDomain(domainId);
    dataModel.put(Domain.DOMAIN, domain);
    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)

Example 15 with Before

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

the class AdminProcessor method showArticles.

/**
 * Shows admin articles.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/articles", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("admin/articles.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    String pageNumStr = request.getParameter("p");
    if (Strings.isEmptyOrNull(pageNumStr) || !Strings.isNumeric(pageNumStr)) {
        pageNumStr = "1";
    }
    final int pageNum = Integer.valueOf(pageNumStr);
    final int pageSize = PAGE_SIZE;
    final int windowSize = WINDOW_SIZE;
    final JSONObject requestJSONObject = new JSONObject();
    requestJSONObject.put(Pagination.PAGINATION_CURRENT_PAGE_NUM, pageNum);
    requestJSONObject.put(Pagination.PAGINATION_PAGE_SIZE, pageSize);
    requestJSONObject.put(Pagination.PAGINATION_WINDOW_SIZE, windowSize);
    final String articleId = request.getParameter("id");
    if (!Strings.isEmptyOrNull(articleId)) {
        requestJSONObject.put(Keys.OBJECT_ID, articleId);
    }
    final Map<String, Class<?>> articleFields = new HashMap<>();
    articleFields.put(Keys.OBJECT_ID, String.class);
    articleFields.put(Article.ARTICLE_TITLE, String.class);
    articleFields.put(Article.ARTICLE_PERMALINK, String.class);
    articleFields.put(Article.ARTICLE_CREATE_TIME, Long.class);
    articleFields.put(Article.ARTICLE_VIEW_CNT, Integer.class);
    articleFields.put(Article.ARTICLE_COMMENT_CNT, Integer.class);
    articleFields.put(Article.ARTICLE_AUTHOR_ID, String.class);
    articleFields.put(Article.ARTICLE_TAGS, String.class);
    articleFields.put(Article.ARTICLE_STATUS, Integer.class);
    articleFields.put(Article.ARTICLE_STICK, Long.class);
    final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
    final JSONObject result = articleQueryService.getArticles(avatarViewMode, requestJSONObject, articleFields);
    dataModel.put(Article.ARTICLES, CollectionUtils.jsonArrayToList(result.optJSONArray(Article.ARTICLES)));
    final JSONObject pagination = result.optJSONObject(Pagination.PAGINATION);
    final int pageCount = pagination.optInt(Pagination.PAGINATION_PAGE_COUNT);
    final JSONArray pageNums = pagination.optJSONArray(Pagination.PAGINATION_PAGE_NUMS);
    dataModel.put(Pagination.PAGINATION_FIRST_PAGE_NUM, pageNums.opt(0));
    dataModel.put(Pagination.PAGINATION_LAST_PAGE_NUM, pageNums.opt(pageNums.length() - 1));
    dataModel.put(Pagination.PAGINATION_CURRENT_PAGE_NUM, pageNum);
    dataModel.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
    dataModel.put(Pagination.PAGINATION_PAGE_NUMS, CollectionUtils.jsonArrayToList(pageNums));
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) 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