Search in sources :

Example 56 with AbstractFreeMarkerRenderer

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

the class UserProcessor method showLinkForge.

/**
 * Shows user link forge.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @param userName the specified user name
 * @throws Exception exception
 */
@RequestProcessing(value = "/member/{userName}/forge/link", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, UserBlockCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showLinkForge(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String userName) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("/home/link-forge.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
    final JSONObject user = (JSONObject) request.getAttribute(User.USER);
    user.put(UserExt.USER_T_CREATE_TIME, new Date(user.getLong(Keys.OBJECT_ID)));
    fillHomeUser(dataModel, user, roleQueryService);
    final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
    avatarQueryService.fillUserAvatarURL(avatarViewMode, user);
    final String followingId = user.optString(Keys.OBJECT_ID);
    dataModel.put(Follow.FOLLOWING_ID, followingId);
    final boolean isLoggedIn = (Boolean) dataModel.get(Common.IS_LOGGED_IN);
    if (isLoggedIn) {
        final JSONObject currentUser = (JSONObject) dataModel.get(Common.CURRENT_USER);
        final String followerId = currentUser.optString(Keys.OBJECT_ID);
        final boolean isFollowing = followQueryService.isFollowing(followerId, followingId, Follow.FOLLOWING_TYPE_C_USER);
        dataModel.put(Common.IS_FOLLOWING, isFollowing);
    }
    final List<JSONObject> tags = linkForgeQueryService.getUserForgedLinks(user.optString(Keys.OBJECT_ID));
    dataModel.put(Tag.TAGS, (Object) tags);
    dataModel.put(Common.TYPE, "linkForge");
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Date(java.util.Date) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 57 with AbstractFreeMarkerRenderer

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

the class UserProcessor method showHomeFollowingArticles.

/**
 * Shows user home following articles page.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @param userName the specified user name
 * @throws Exception exception
 */
@RequestProcessing(value = "/member/{userName}/following/articles", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, AnonymousViewCheck.class, UserBlockCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showHomeFollowingArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String userName) throws Exception {
    final JSONObject user = (JSONObject) request.getAttribute(User.USER);
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("/home/following-articles.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
    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("userHomeFollowingArticlesCnt");
    final int windowSize = Symphonys.getInt("userHomeFollowingArticlesWindowSize");
    fillHomeUser(dataModel, user, roleQueryService);
    final String followingId = user.optString(Keys.OBJECT_ID);
    dataModel.put(Follow.FOLLOWING_ID, followingId);
    final int avatarViewMode = (int) request.getAttribute(UserExt.USER_AVATAR_VIEW_MODE);
    avatarQueryService.fillUserAvatarURL(avatarViewMode, user);
    final JSONObject followingArticlesResult = followQueryService.getFollowingArticles(avatarViewMode, followingId, pageNum, pageSize);
    final List<JSONObject> followingArticles = (List<JSONObject>) followingArticlesResult.opt(Keys.RESULTS);
    dataModel.put(Common.USER_HOME_FOLLOWING_ARTICLES, followingArticles);
    final boolean isLoggedIn = (Boolean) dataModel.get(Common.IS_LOGGED_IN);
    if (isLoggedIn) {
        final JSONObject currentUser = (JSONObject) dataModel.get(Common.CURRENT_USER);
        final String followerId = currentUser.optString(Keys.OBJECT_ID);
        final boolean isFollowing = followQueryService.isFollowing(followerId, followingId, Follow.FOLLOWING_TYPE_C_USER);
        dataModel.put(Common.IS_FOLLOWING, isFollowing);
        for (final JSONObject followingArticle : followingArticles) {
            final String homeUserFollowingArticleId = followingArticle.optString(Keys.OBJECT_ID);
            followingArticle.put(Common.IS_FOLLOWING, followQueryService.isFollowing(followerId, homeUserFollowingArticleId, Follow.FOLLOWING_TYPE_C_ARTICLE));
        }
    }
    user.put(UserExt.USER_T_CREATE_TIME, new Date(user.getLong(Keys.OBJECT_ID)));
    final int followingArticleCnt = followingArticlesResult.optInt(Pagination.PAGINATION_RECORD_COUNT);
    final int pageCount = (int) Math.ceil(followingArticleCnt / (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);
    dataModel.put(Pagination.PAGINATION_RECORD_COUNT, followingArticleCnt);
    dataModel.put(Common.TYPE, "followingArticles");
}
Also used : AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Date(java.util.Date) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 58 with AbstractFreeMarkerRenderer

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

the class InitProcessor method showInit.

/**
     * Shows initialization page.
     *
     * @param context the specified http request context
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @throws Exception exception
     */
@RequestProcessing(value = "/init", method = HTTPRequestMethod.GET)
public void showInit(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    if (initService.isInited()) {
        response.sendRedirect("/");
        return;
    }
    final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
    renderer.setTemplateName("init.ftl");
    context.setRenderer(renderer);
    final Map<String, Object> dataModel = renderer.getDataModel();
    final Map<String, String> langs = langPropsService.getAll(Locales.getLocale(request));
    dataModel.putAll(langs);
    dataModel.put(Common.VERSION, SoloServletListener.VERSION);
    dataModel.put(Common.STATIC_RESOURCE_VERSION, Latkes.getStaticResourceVersion());
    dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
    Keys.fillRuntime(dataModel);
    filler.fillMinified(dataModel);
}
Also used : JSONObject(org.json.JSONObject) ConsoleRenderer(org.b3log.solo.processor.renderer.ConsoleRenderer) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 59 with AbstractFreeMarkerRenderer

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

the class LoginProcessor method renderPage.

/**
     * Render a page template with the destination URL.
     *
     * @param context        the context
     * @param pageTemplate   the page template
     * @param destinationURL the destination URL
     * @param request        for reset password page
     * @throws JSONException    the JSONException
     * @throws ServiceException the ServiceException
     */
private void renderPage(final HTTPRequestContext context, final String pageTemplate, final String destinationURL, final HttpServletRequest request) throws JSONException, ServiceException {
    final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
    renderer.setTemplateName(pageTemplate);
    context.setRenderer(renderer);
    final Map<String, Object> dataModel = renderer.getDataModel();
    final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
    final JSONObject preference = preferenceQueryService.getPreference();
    dataModel.putAll(langs);
    dataModel.put(Common.GOTO, destinationURL);
    dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
    dataModel.put(Common.VERSION, SoloServletListener.VERSION);
    dataModel.put(Common.STATIC_RESOURCE_VERSION, Latkes.getStaticResourceVersion());
    dataModel.put(Option.ID_C_BLOG_TITLE, preference.getString(Option.ID_C_BLOG_TITLE));
    final String token = request.getParameter("token");
    final String email = request.getParameter("login");
    final JSONObject tokenObj = optionQueryService.getOptionById(token);
    if (tokenObj == null) {
        dataModel.put("inputType", "email");
    } else {
        // TODO verify the expired time in the tokenObj
        dataModel.put("inputType", "password");
        dataModel.put("userEmailHidden", email);
    }
    final String from = request.getParameter("from");
    if ("forgot".equals(from)) {
        dataModel.put("resetMsg", langPropsService.get("resetPwdSuccessSend"));
    } else if ("reset".equals(from)) {
        dataModel.put("resetMsg", langPropsService.get("resetPwdSuccessMsg"));
    } else {
        dataModel.put("resetMsg", "");
    }
    Keys.fillRuntime(dataModel);
    filler.fillMinified(dataModel);
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ConsoleRenderer(org.b3log.solo.processor.renderer.ConsoleRenderer) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)

Example 60 with AbstractFreeMarkerRenderer

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

the class TagProcessor method showTagArticles.

/**
     * Shows articles related with a tag with the specified context.
     *
     * @param context the specified context
     * @throws IOException io exception
     */
@RequestProcessing(value = "/tags/**", method = HTTPRequestMethod.GET)
public void showTagArticles(final HTTPRequestContext context) throws IOException {
    final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
    context.setRenderer(renderer);
    renderer.setTemplateName("tag-articles.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final HttpServletRequest request = context.getRequest();
    final HttpServletResponse response = context.getResponse();
    try {
        String requestURI = request.getRequestURI();
        if (!requestURI.endsWith("/")) {
            requestURI += "/";
        }
        String tagTitle = getTagTitle(requestURI);
        final int currentPageNum = getCurrentPageNum(requestURI, tagTitle);
        if (-1 == currentPageNum) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        LOGGER.log(Level.DEBUG, "Tag[title={0}, currentPageNum={1}]", new Object[] { tagTitle, currentPageNum });
        tagTitle = URLDecoder.decode(tagTitle, "UTF-8");
        final JSONObject result = tagQueryService.getTagByTitle(tagTitle);
        if (null == result) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final JSONObject tag = result.getJSONObject(Tag.TAG);
        final String tagId = tag.getString(Keys.OBJECT_ID);
        final JSONObject preference = preferenceQueryService.getPreference();
        Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
        final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
        final int windowSize = preference.getInt(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE);
        final List<JSONObject> articles = articleQueryService.getArticlesByTag(tagId, currentPageNum, pageSize);
        if (articles.isEmpty()) {
            try {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            } catch (final IOException ex) {
                LOGGER.error(ex.getMessage());
            }
        }
        final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
        if (hasMultipleUsers) {
            filler.setArticlesExProperties(request, articles, preference);
        } else {
            // All articles composed by the same author
            final JSONObject author = articleQueryService.getAuthor(articles.get(0));
            filler.setArticlesExProperties(request, articles, author, preference);
        }
        final int tagArticleCount = tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
        final int pageCount = (int) Math.ceil((double) tagArticleCount / (double) pageSize);
        LOGGER.log(Level.TRACE, "Paginate tag-articles[currentPageNum={0}, pageSize={1}, pageCount={2}, windowSize={3}]", new Object[] { currentPageNum, pageSize, pageCount, windowSize });
        final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
        LOGGER.log(Level.TRACE, "tag-articles[pageNums={0}]", pageNums);
        Collections.sort(articles, Comparators.ARTICLE_CREATE_DATE_COMPARATOR);
        fillPagination(dataModel, pageCount, currentPageNum, articles, pageNums);
        dataModel.put(Common.PATH, "/tags/" + URLEncoder.encode(tagTitle, "UTF-8"));
        dataModel.put(Keys.OBJECT_ID, tagId);
        dataModel.put(Tag.TAG, tag);
        filler.fillSide(request, dataModel, preference);
        filler.fillBlogHeader(request, response, dataModel, preference);
        filler.fillBlogFooter(request, dataModel, preference);
        statisticMgmtService.incBlogViewCount(request, response);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        try {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } catch (final IOException ex) {
            LOGGER.error(ex.getMessage());
        }
    } catch (final JSONException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        try {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } catch (final IOException ex) {
            LOGGER.error(ex.getMessage());
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) JSONException(org.json.JSONException) FreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) IOException(java.io.IOException) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONObject(org.json.JSONObject) 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