Search in sources :

Example 46 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.

the class NotificationProcessor method showBroadcastNotifications.

/**
 * Shows [broadcast] notifications.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/notifications/broadcast", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, LoginCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showBroadcastNotifications(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/broadcast.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("broadcastNotificationsCnt");
    final int windowSize = Symphonys.getInt("broadcastNotificationsWindowSize");
    final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
    final JSONObject result = notificationQueryService.getBroadcastNotifications(avatarViewMode, userId, pageNum, pageSize);
    final List<JSONObject> broadcastNotifications = (List<JSONObject>) result.get(Keys.RESULTS);
    dataModel.put(Common.BROADCAST_NOTIFICATIONS, broadcastNotifications);
    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)

Example 47 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.

the class SearchProcessor method search.

/**
 * Searches.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/search", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void search(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("search-articles.ftl");
    if (!Symphonys.getBoolean("es.enabled") && !Symphonys.getBoolean("algolia.enabled")) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    final Map<String, Object> dataModel = renderer.getDataModel();
    String keyword = request.getParameter("key");
    if (StringUtils.isBlank(keyword)) {
        keyword = "";
    }
    dataModel.put(Common.KEY, Escapes.escapeHTML(keyword));
    final String p = request.getParameter("p");
    int pageNum = 1;
    if (StringUtils.isNotBlank(p) && Strings.isNumeric(p)) {
        pageNum = Integer.valueOf(p);
    }
    int pageSize = Symphonys.getInt("indexArticlesCnt");
    final JSONObject user = userQueryService.getCurrentUser(request);
    if (null != user) {
        pageSize = user.optInt(UserExt.USER_LIST_PAGE_SIZE);
    }
    final List<JSONObject> articles = new ArrayList<>();
    int total = 0;
    if (Symphonys.getBoolean("es.enabled")) {
        final JSONObject result = searchQueryService.searchElasticsearch(Article.ARTICLE, keyword, pageNum, pageSize);
        if (null == result || 0 != result.optInt("status")) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final JSONObject hitsResult = result.optJSONObject("hits");
        final JSONArray hits = hitsResult.optJSONArray("hits");
        for (int i = 0; i < hits.length(); i++) {
            final JSONObject article = hits.optJSONObject(i).optJSONObject("_source");
            articles.add(article);
        }
        total = result.optInt("total");
    }
    if (Symphonys.getBoolean("algolia.enabled")) {
        final JSONObject result = searchQueryService.searchAlgolia(keyword, pageNum, pageSize);
        if (null == result) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final JSONArray hits = result.optJSONArray("hits");
        for (int i = 0; i < hits.length(); i++) {
            final JSONObject article = hits.optJSONObject(i);
            articles.add(article);
        }
        total = result.optInt("nbHits");
        if (total > 1000) {
            // Algolia limits the maximum number of search results to 1000
            total = 1000;
        }
    }
    final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
    articleQueryService.organizeArticles(avatarViewMode, articles);
    final Integer participantsCnt = Symphonys.getInt("latestArticleParticipantsCnt");
    articleQueryService.genParticipants(avatarViewMode, articles, participantsCnt);
    dataModel.put(Article.ARTICLES, articles);
    final int pageCount = (int) Math.ceil(total / (double) pageSize);
    final List<Integer> pageNums = Paginator.paginate(pageNum, pageSize, pageCount, Symphonys.getInt("defaultPaginationWindowSize"));
    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);
    dataModelService.fillRandomArticles(avatarViewMode, dataModel);
    dataModelService.fillSideHotArticles(avatarViewMode, dataModel);
    dataModelService.fillSideTags(dataModel);
    dataModelService.fillLatestCmts(dataModel);
    String searchEmptyLabel = langPropsService.get("searchEmptyLabel");
    searchEmptyLabel = searchEmptyLabel.replace("${key}", keyword);
    dataModel.put("searchEmptyLabel", searchEmptyLabel);
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) 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 48 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer 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 49 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer 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)

Example 50 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.

the class DomainProcessor method showDomains.

/**
 * Shows domains.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/domains", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showDomains(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("domains.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final JSONObject statistic = optionQueryService.getStatistic();
    final int tagCnt = statistic.optInt(Option.ID_C_STATISTIC_TAG_COUNT);
    dataModel.put(Tag.TAG_T_COUNT, tagCnt);
    final int domainCnt = statistic.optInt(Option.ID_C_STATISTIC_DOMAIN_COUNT);
    dataModel.put(Domain.DOMAIN_T_COUNT, domainCnt);
    final List<JSONObject> domains = domainQueryService.getAllDomains();
    dataModel.put(Common.ALL_DOMAINS, domains);
    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

AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)120 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)119 JSONObject (org.json.JSONObject)117 After (org.b3log.latke.servlet.annotation.After)105 Before (org.b3log.latke.servlet.annotation.Before)105 ServiceException (org.b3log.latke.service.ServiceException)22 IOException (java.io.IOException)15 Date (java.util.Date)12 ArrayList (java.util.ArrayList)8 JSONArray (org.json.JSONArray)8 Auth (com.qiniu.util.Auth)7 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)7 ConsoleRenderer (org.b3log.solo.processor.renderer.ConsoleRenderer)7 ParseException (java.text.ParseException)6 List (java.util.List)6 EventException (org.b3log.latke.event.EventException)3 JSONException (org.json.JSONException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Locale (java.util.Locale)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2