Search in sources :

Example 91 with Before

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

the class AdminProcessor method addDomainTag.

/**
 * Adds a tag into 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}/add-tag", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void addDomainTag(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String domainId) throws Exception {
    String tagTitle = request.getParameter(Tag.TAG_TITLE);
    final JSONObject tag = tagQueryService.getTagByTitle(tagTitle);
    String tagId;
    if (tag != null) {
        tagId = tag.optString(Keys.OBJECT_ID);
    } else {
        try {
            if (Strings.isEmptyOrNull(tagTitle)) {
                throw new Exception(langPropsService.get("tagsErrorLabel"));
            }
            tagTitle = Tag.formatTags(tagTitle);
            if (!Tag.containsWhiteListTags(tagTitle)) {
                if (!Tag.TAG_TITLE_PATTERN.matcher(tagTitle).matches()) {
                    throw new Exception(langPropsService.get("tagsErrorLabel"));
                }
                if (tagTitle.length() > Tag.MAX_TAG_TITLE_LENGTH) {
                    throw new Exception(langPropsService.get("tagsErrorLabel"));
                }
            }
        } 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;
        }
        final JSONObject admin = (JSONObject) request.getAttribute(User.USER);
        final String userId = admin.optString(Keys.OBJECT_ID);
        try {
            tagId = tagMgmtService.addTag(userId, tagTitle);
        } 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;
        }
    }
    if (domainQueryService.containTag(tagTitle, domainId)) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        String msg = langPropsService.get("domainContainTagLabel");
        msg = msg.replace("{tag}", tagTitle);
        dataModel.put(Keys.MSG, msg);
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    final JSONObject domainTag = new JSONObject();
    domainTag.put(Domain.DOMAIN + "_" + Keys.OBJECT_ID, domainId);
    domainTag.put(Tag.TAG + "_" + Keys.OBJECT_ID, tagId);
    domainMgmtService.addDomainTag(domainTag);
    response.sendRedirect(Latkes.getServePath() + "/admin/domain/" + domainId);
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) 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 92 with Before

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

the class AdminProcessor method removeDomain.

/**
 * Removes a tag from 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}/remove-tag", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void removeDomain(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String domainId) throws Exception {
    final String tagTitle = request.getParameter(Tag.TAG_TITLE);
    final JSONObject tag = tagQueryService.getTagByTitle(tagTitle);
    if (null == tag) {
        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("invalidTagLabel"));
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    final JSONObject domainTag = new JSONObject();
    domainTag.put(Domain.DOMAIN + "_" + Keys.OBJECT_ID, domainId);
    domainTag.put(Tag.TAG + "_" + Keys.OBJECT_ID, tag.optString(Keys.OBJECT_ID));
    domainMgmtService.removeDomainTag(domainId, tag.optString(Keys.OBJECT_ID));
    response.sendRedirect(Latkes.getServePath() + "/admin/domain/" + domainId);
}
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 93 with Before

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

the class AdminProcessor method initPoint.

/**
 * Compensates a user's initial point.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @param userId   the specified user id
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/user/{userId}/init-point", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void initPoint(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String userId) throws Exception {
    try {
        final JSONObject user = userQueryService.getUser(userId);
        if (null == user || UserExt.USER_STATUS_C_VALID != user.optInt(UserExt.USER_STATUS) || UserExt.NULL_USER_NAME.equals(user.optString(User.USER_NAME))) {
            response.sendRedirect(Latkes.getServePath() + "/admin/user/" + userId);
            return;
        }
        final List<JSONObject> records = pointtransferQueryService.getLatestPointtransfers(userId, Pointtransfer.TRANSFER_TYPE_C_INIT, 1);
        if (records.isEmpty()) {
            pointtransferMgmtService.transfer(Pointtransfer.ID_C_SYS, userId, Pointtransfer.TRANSFER_TYPE_C_INIT, Pointtransfer.TRANSFER_SUM_C_INIT, userId, Long.valueOf(userId));
        }
    } catch (final IOException | NumberFormatException | 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) IOException(java.io.IOException) 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 94 with Before

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

the class AdminProcessor method updateBanner.

/**
 * Updates banner.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/ad/banner", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void updateBanner(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String headerBanner = request.getParameter("headerBanner");
    JSONObject adOption = optionQueryService.getOption(Option.ID_C_HEADER_BANNER);
    if (null == adOption) {
        adOption = new JSONObject();
        adOption.put(Keys.OBJECT_ID, Option.ID_C_HEADER_BANNER);
        adOption.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_AD);
        adOption.put(Option.OPTION_VALUE, headerBanner);
        optionMgmtService.addOption(adOption);
    } else {
        adOption.put(Option.OPTION_VALUE, headerBanner);
        optionMgmtService.updateOption(Option.ID_C_HEADER_BANNER, adOption);
    }
    response.sendRedirect(Latkes.getServePath() + "/admin/ad");
}
Also used : JSONObject(org.json.JSONObject) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 95 with Before

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

the class AdminProcessor method updateUser.

/**
 * Updates a user.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @param userId   the specified user id
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/user/{userId}", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void updateUser(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String userId) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("admin/user.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final JSONObject user = userQueryService.getUser(userId);
    dataModel.put(User.USER, user);
    final String oldRole = user.optString(User.USER_ROLE);
    final JSONObject result = roleQueryService.getRoles(1, Integer.MAX_VALUE, 10);
    final List<JSONObject> roles = (List<JSONObject>) result.opt(Role.ROLES);
    dataModel.put(Role.ROLES, roles);
    final Enumeration<String> parameterNames = request.getParameterNames();
    while (parameterNames.hasMoreElements()) {
        final String name = parameterNames.nextElement();
        final String value = request.getParameter(name);
        switch(name) {
            case UserExt.USER_POINT:
            case UserExt.USER_APP_ROLE:
            case UserExt.USER_STATUS:
            case UserExt.USER_COMMENT_VIEW_MODE:
            case UserExt.USER_AVATAR_VIEW_MODE:
            case UserExt.USER_LIST_PAGE_SIZE:
            case UserExt.USER_NOTIFY_STATUS:
            case UserExt.USER_SUB_MAIL_STATUS:
            case UserExt.USER_KEYBOARD_SHORTCUTS_STATUS:
            case UserExt.USER_GEO_STATUS:
            case UserExt.USER_ARTICLE_STATUS:
            case UserExt.USER_COMMENT_STATUS:
            case UserExt.USER_FOLLOWING_USER_STATUS:
            case UserExt.USER_FOLLOWING_TAG_STATUS:
            case UserExt.USER_FOLLOWING_ARTICLE_STATUS:
            case UserExt.USER_WATCHING_ARTICLE_STATUS:
            case UserExt.USER_FOLLOWER_STATUS:
            case UserExt.USER_POINT_STATUS:
            case UserExt.USER_ONLINE_STATUS:
            case UserExt.USER_UA_STATUS:
            case UserExt.USER_TIMELINE_STATUS:
            case UserExt.USER_FORGE_LINK_STATUS:
            case UserExt.USER_JOIN_POINT_RANK:
            case UserExt.USER_JOIN_USED_POINT_RANK:
                user.put(name, Integer.valueOf(value));
                break;
            case User.USER_PASSWORD:
                final String oldPwd = user.getString(name);
                if (!oldPwd.equals(value) && !Strings.isEmptyOrNull(value)) {
                    user.put(name, MD5.hash(value));
                }
                break;
            case UserExt.SYNC_TO_CLIENT:
                user.put(UserExt.SYNC_TO_CLIENT, Boolean.valueOf(value));
                break;
            default:
                user.put(name, value);
                break;
        }
    }
    final JSONObject currentUser = (JSONObject) request.getAttribute(User.USER);
    if (!Role.ROLE_ID_C_ADMIN.equals(currentUser.optString(User.USER_ROLE))) {
        user.put(User.USER_ROLE, oldRole);
    }
    userMgmtService.updateUser(userId, user);
    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