Search in sources :

Example 56 with Before

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

the class SettingsProcessor method showSettings.

/**
 * Shows settings pages.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = { "/settings", "/settings/*" }, method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = { CSRFToken.class, PermissionGrant.class, StopwatchEndAdvice.class })
public void showSettings(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    final String requestURI = request.getRequestURI();
    String page = StringUtils.substringAfter(requestURI, "/settings/");
    if (StringUtils.isBlank(page)) {
        page = "profile";
    }
    page += ".ftl";
    renderer.setTemplateName("/home/settings/" + page);
    final Map<String, Object> dataModel = renderer.getDataModel();
    final JSONObject user = (JSONObject) request.getAttribute(User.USER);
    user.put(UserExt.USER_T_CREATE_TIME, new Date(user.getLong(Keys.OBJECT_ID)));
    UserProcessor.fillHomeUser(dataModel, user, roleQueryService);
    final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
    avatarQueryService.fillUserAvatarURL(avatarViewMode, user);
    final String userId = user.optString(Keys.OBJECT_ID);
    final int invitedUserCount = userQueryService.getInvitedUserCount(userId);
    dataModel.put(Common.INVITED_USER_COUNT, invitedUserCount);
    // Qiniu file upload authenticate
    final Auth auth = Auth.create(Symphonys.get("qiniu.accessKey"), Symphonys.get("qiniu.secretKey"));
    final String uploadToken = auth.uploadToken(Symphonys.get("qiniu.bucket"));
    dataModel.put("qiniuUploadToken", uploadToken);
    dataModel.put("qiniuDomain", Symphonys.get("qiniu.domain"));
    if (!Symphonys.getBoolean("qiniu.enabled")) {
        dataModel.put("qiniuUploadToken", "");
    }
    final long imgMaxSize = Symphonys.getLong("upload.img.maxSize");
    dataModel.put("imgMaxSize", imgMaxSize);
    final long fileMaxSize = Symphonys.getLong("upload.file.maxSize");
    dataModel.put("fileMaxSize", fileMaxSize);
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
    String inviteTipLabel = (String) dataModel.get("inviteTipLabel");
    inviteTipLabel = inviteTipLabel.replace("{point}", String.valueOf(Pointtransfer.TRANSFER_SUM_C_INVITE_REGISTER));
    dataModel.put("inviteTipLabel", inviteTipLabel);
    String pointTransferTipLabel = (String) dataModel.get("pointTransferTipLabel");
    pointTransferTipLabel = pointTransferTipLabel.replace("{point}", Symphonys.get("pointTransferMin"));
    dataModel.put("pointTransferTipLabel", pointTransferTipLabel);
    String dataExportTipLabel = (String) dataModel.get("dataExportTipLabel");
    dataExportTipLabel = dataExportTipLabel.replace("{point}", String.valueOf(Pointtransfer.TRANSFER_SUM_C_DATA_EXPORT));
    dataModel.put("dataExportTipLabel", dataExportTipLabel);
    final String allowRegister = optionQueryService.getAllowRegister();
    dataModel.put("allowRegister", allowRegister);
    String buyInvitecodeLabel = langPropsService.get("buyInvitecodeLabel");
    buyInvitecodeLabel = buyInvitecodeLabel.replace("${point}", String.valueOf(Pointtransfer.TRANSFER_SUM_C_BUY_INVITECODE));
    buyInvitecodeLabel = buyInvitecodeLabel.replace("${point2}", String.valueOf(Pointtransfer.TRANSFER_SUM_C_INVITECODE_USED));
    dataModel.put("buyInvitecodeLabel", buyInvitecodeLabel);
    final List<JSONObject> invitecodes = invitecodeQueryService.getValidInvitecodes(userId);
    for (final JSONObject invitecode : invitecodes) {
        String msg = langPropsService.get("expireTipLabel");
        msg = msg.replace("${time}", DateFormatUtils.format(invitecode.optLong(Keys.OBJECT_ID) + Symphonys.getLong("invitecode.expired"), "yyyy-MM-dd HH:mm"));
        invitecode.put(Common.MEMO, msg);
    }
    dataModel.put(Invitecode.INVITECODES, (Object) invitecodes);
    if (requestURI.contains("function")) {
        dataModel.put(Emotion.EMOTIONS, emotionQueryService.getEmojis(userId));
        dataModel.put(Emotion.SHORT_T_LIST, emojiLists);
    }
    if (requestURI.contains("i18n")) {
        dataModel.put(Common.LANGUAGES, Languages.getAvailableLanguages());
        final List<JSONObject> timezones = new ArrayList<>();
        final List<TimeZones.TimeZoneWithDisplayNames> timeZones = TimeZones.getInstance().getTimeZones();
        for (final TimeZones.TimeZoneWithDisplayNames timeZone : timeZones) {
            final JSONObject timezone = new JSONObject();
            timezone.put(Common.ID, timeZone.getTimeZone().getID());
            timezone.put(Common.NAME, timeZone.getDisplayName());
            timezones.add(timezone);
        }
        dataModel.put(Common.TIMEZONES, timezones);
    }
    dataModel.put(Common.TYPE, "settings");
}
Also used : TimeZones(org.b3log.symphony.util.TimeZones) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) JSONObject(org.json.JSONObject) Auth(com.qiniu.util.Auth) 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 57 with Before

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

the class SettingsProcessor method updateI18n.

/**
 * Updates user i18n.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 */
@RequestProcessing(value = "/settings/i18n", method = HTTPRequestMethod.POST)
@Before(adviceClass = { LoginCheck.class, CSRFCheck.class })
public void updateI18n(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) {
    context.renderJSON();
    JSONObject requestJSONObject;
    try {
        requestJSONObject = Requests.parseRequestJSONObject(request, response);
        request.setAttribute(Keys.REQUEST, requestJSONObject);
    } catch (final Exception e) {
        LOGGER.warn(e.getMessage());
        requestJSONObject = new JSONObject();
    }
    String userLanguage = requestJSONObject.optString(UserExt.USER_LANGUAGE, Locale.SIMPLIFIED_CHINESE.toString());
    if (!Languages.getAvailableLanguages().contains(userLanguage)) {
        userLanguage = Locale.US.toString();
    }
    String userTimezone = requestJSONObject.optString(UserExt.USER_TIMEZONE, TimeZone.getDefault().getID());
    if (!Arrays.asList(TimeZone.getAvailableIDs()).contains(userTimezone)) {
        userTimezone = TimeZone.getDefault().getID();
    }
    try {
        final JSONObject user = userQueryService.getCurrentUser(request);
        user.put(UserExt.USER_LANGUAGE, userLanguage);
        user.put(UserExt.USER_TIMEZONE, userTimezone);
        userMgmtService.updateUser(user.optString(Keys.OBJECT_ID), user);
        context.renderTrueResult();
    } catch (final ServiceException e) {
        context.renderMsg(e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 58 with Before

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

the class SettingsProcessor method updateAvatar.

/**
 * Updates user avatar.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/settings/avatar", method = HTTPRequestMethod.POST)
@Before(adviceClass = { LoginCheck.class, CSRFCheck.class, UpdateProfilesValidation.class })
public void updateAvatar(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    context.renderJSON();
    final JSONObject requestJSONObject = (JSONObject) request.getAttribute(Keys.REQUEST);
    final String userAvatarURL = requestJSONObject.optString(UserExt.USER_AVATAR_URL);
    final JSONObject user = userQueryService.getCurrentUser(request);
    user.put(UserExt.USER_AVATAR_TYPE, UserExt.USER_AVATAR_TYPE_C_UPLOAD);
    user.put(UserExt.USER_UPDATE_TIME, System.currentTimeMillis());
    if (Strings.contains(userAvatarURL, new String[] { "<", ">", "\"", "'" })) {
        user.put(UserExt.USER_AVATAR_URL, Symphonys.get("defaultThumbnailURL"));
    } else {
        if (Symphonys.getBoolean("qiniu.enabled")) {
            final String qiniuDomain = Symphonys.get("qiniu.domain");
            if (!StringUtils.startsWith(userAvatarURL, qiniuDomain)) {
                user.put(UserExt.USER_AVATAR_URL, Symphonys.get("defaultThumbnailURL"));
            } else {
                user.put(UserExt.USER_AVATAR_URL, userAvatarURL);
            }
        } else {
            user.put(UserExt.USER_AVATAR_URL, userAvatarURL);
        }
    }
    try {
        userMgmtService.updateUser(user.optString(Keys.OBJECT_ID), user);
        context.renderTrueResult();
    } catch (final ServiceException e) {
        context.renderMsg(e.getMessage());
    }
}
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 59 with Before

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

the class SettingsProcessor method updateGeoStatus.

/**
 * Updates user geo status.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 */
@RequestProcessing(value = "/settings/geo/status", method = HTTPRequestMethod.POST)
@Before(adviceClass = { LoginCheck.class, CSRFCheck.class })
public void updateGeoStatus(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) {
    context.renderJSON();
    JSONObject requestJSONObject;
    try {
        requestJSONObject = Requests.parseRequestJSONObject(request, response);
        request.setAttribute(Keys.REQUEST, requestJSONObject);
    } catch (final Exception e) {
        LOGGER.warn(e.getMessage());
        requestJSONObject = new JSONObject();
    }
    int geoStatus = requestJSONObject.optInt(UserExt.USER_GEO_STATUS);
    if (UserExt.USER_GEO_STATUS_C_PRIVATE != geoStatus && UserExt.USER_GEO_STATUS_C_PUBLIC != geoStatus) {
        geoStatus = UserExt.USER_GEO_STATUS_C_PUBLIC;
    }
    try {
        final JSONObject user = userQueryService.getCurrentUser(request);
        user.put(UserExt.USER_GEO_STATUS, geoStatus);
        userMgmtService.updateUser(user.optString(Keys.OBJECT_ID), user);
        context.renderTrueResult();
    } catch (final ServiceException e) {
        context.renderMsg(e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException) Before(org.b3log.latke.servlet.annotation.Before) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 60 with Before

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

the class ChatRoomProcessor method showChatRoom.

/**
 * Shows chat room.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = { "/cr", "/chat-room", "/community" }, method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showChatRoom(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("chat-room.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    dataModel.put(Common.MESSAGES, messages);
    dataModel.put("chatRoomMsgCnt", Symphonys.getInt("chatRoom.msgCnt"));
    // Qiniu file upload authenticate
    final Auth auth = Auth.create(Symphonys.get("qiniu.accessKey"), Symphonys.get("qiniu.secretKey"));
    dataModel.put("qiniuUploadToken", auth.uploadToken(Symphonys.get("qiniu.bucket")));
    dataModel.put("qiniuDomain", Symphonys.get("qiniu.domain"));
    final long imgMaxSize = Symphonys.getLong("upload.img.maxSize");
    dataModel.put("imgMaxSize", imgMaxSize);
    final long fileMaxSize = Symphonys.getLong("upload.file.maxSize");
    dataModel.put("fileMaxSize", fileMaxSize);
    dataModel.put(Common.ONLINE_CHAT_CNT, SESSIONS.size());
    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);
}
Also used : Auth(com.qiniu.util.Auth) 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