/** * Determines whether the specified permalink exists. * * @param permalink the specified permalink * @return {@code true} if exists, returns {@code false} otherwise */ public boolean exist(final String permalink) { try { return isReservedLink(permalink) || null != articleRepository.getByPermalink(permalink) || null != pageRepository.getByPermalink(permalink) || permalink.endsWith(".ftl"); } catch (final RepositoryException e) { LOGGER.log( Level.ERROR, "Determines whether the permalink[" + permalink + "] exists failed, returns true", e); return true; } }
/** * Can the current user access an article specified by the given article id? * * @param articleId the given article id * @param request the specified request * @return {@code true} if the current user can access the article, {@code false} otherwise * @throws Exception exception */ public boolean canAccessArticle(final String articleId, final HttpServletRequest request) throws Exception { if (Strings.isEmptyOrNull(articleId)) { return false; } if (isAdminLoggedIn(request)) { return true; } final JSONObject article = articleRepository.get(articleId); final String currentUserEmail = getCurrentUser(request).getString(User.USER_EMAIL); if (!article.getString(Article.ARTICLE_AUTHOR_EMAIL).equals(currentUserEmail)) { return false; } return true; }
/** * Try to write response from cache. * * @param request the specified request * @param response the specified response * @param chain filter chain * @throws IOException io exception * @throws ServletException servlet exception */ @Override public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { final long startTimeMillis = System.currentTimeMillis(); request.setAttribute(Keys.HttpRequest.START_TIME_MILLIS, startTimeMillis); final HttpServletRequest httpServletRequest = (HttpServletRequest) request; final String requestURI = httpServletRequest.getRequestURI(); LOGGER.log(Level.FINER, "Request URI[{0}]", requestURI); if (StaticResources.isStatic(httpServletRequest)) { final String path = httpServletRequest.getServletPath() + httpServletRequest.getPathInfo(); LOGGER.log(Level.FINEST, "Requests a static resource, forwards to servlet[path={0}]", path); request.getRequestDispatcher(path).forward(request, response); return; } if (!Latkes.isPageCacheEnabled()) { LOGGER.log(Level.FINEST, "Page cache is disabled"); chain.doFilter(request, response); return; } final String skinDirName = (String) httpServletRequest.getAttribute(Keys.TEMAPLTE_DIR_NAME); if ("mobile".equals(skinDirName)) { // Mobile request, bypasses page caching chain.doFilter(request, response); return; } String pageCacheKey; final String queryString = httpServletRequest.getQueryString(); pageCacheKey = (String) request.getAttribute(Keys.PAGE_CACHE_KEY); if (Strings.isEmptyOrNull(pageCacheKey)) { pageCacheKey = PageCaches.getPageCacheKey(requestURI, queryString); request.setAttribute(Keys.PAGE_CACHE_KEY, pageCacheKey); } final JSONObject cachedPageContentObject = PageCaches.get(pageCacheKey, httpServletRequest, (HttpServletResponse) response); if (null == cachedPageContentObject) { LOGGER.log(Level.FINER, "Page cache miss for request URI[{0}]", requestURI); chain.doFilter(request, response); return; } final String cachedType = cachedPageContentObject.optString(PageCaches.CACHED_TYPE); try { // If cached an article that has view password, dispatches the password form if (langPropsService.get(PageTypes.ARTICLE.getLangeLabel()).equals(cachedType) && cachedPageContentObject.has(PageCaches.CACHED_PWD)) { JSONObject article = new JSONObject(); final String articleId = cachedPageContentObject.optString(PageCaches.CACHED_OID); article.put(Keys.OBJECT_ID, articleId); article.put( Article.ARTICLE_VIEW_PWD, cachedPageContentObject.optString(PageCaches.CACHED_PWD)); if (articles.needViewPwd(httpServletRequest, article)) { article = articleRepository.get(articleId); // Loads the article entity final HttpServletResponse httpServletResponse = (HttpServletResponse) response; try { httpServletResponse.sendRedirect( Latkes.getServePath() + "/console/article-pwd" + articles.buildArticleViewPwdFormParameters(article)); return; } catch (final Exception e) { httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); return; } } } } catch (final Exception e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); chain.doFilter(request, response); } try { LOGGER.log( Level.FINEST, "Writes resposne for page[pageCacheKey={0}] from cache", pageCacheKey); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); final PrintWriter writer = response.getWriter(); String cachedPageContent = cachedPageContentObject.getString(PageCaches.CACHED_CONTENT); final String topBarHTML = TopBars.getTopBarHTML((HttpServletRequest) request, (HttpServletResponse) response); cachedPageContent = cachedPageContent.replace(Common.TOP_BAR_REPLACEMENT_FLAG, topBarHTML); final String cachedTitle = cachedPageContentObject.getString(PageCaches.CACHED_TITLE); LOGGER.log( Level.FINEST, "Cached value[key={0}, type={1}, title={2}]", new Object[] {pageCacheKey, cachedType, cachedTitle}); statistics.incBlogViewCount((HttpServletRequest) request, (HttpServletResponse) response); final long endimeMillis = System.currentTimeMillis(); final String dateString = DateFormatUtils.format(endimeMillis, "yyyy/MM/dd HH:mm:ss"); final String msg = String.format( "<!-- Cached by B3log Solo(%1$d ms), %2$s -->", endimeMillis - startTimeMillis, dateString); LOGGER.finer(msg); cachedPageContent += Strings.LINE_SEPARATOR + msg; writer.write(cachedPageContent); writer.flush(); writer.close(); } catch (final JSONException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); chain.doFilter(request, response); } catch (final RepositoryException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); chain.doFilter(request, response); } catch (final ServiceException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); chain.doFilter(request, response); } }