Search in sources :

Example 11 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class ArticleProcessor method getRandomArticles.

/**
     * Gets random articles with the specified context.
     *
     * @param context the specified context
     * @throws Exception exception
     */
@RequestProcessing(value = "/get-random-articles.do", method = HTTPRequestMethod.POST)
public void getRandomArticles(final HTTPRequestContext context) throws Exception {
    final JSONObject jsonObject = new JSONObject();
    final JSONObject preference = preferenceQueryService.getPreference();
    final int displayCnt = preference.getInt(Option.ID_C_RANDOM_ARTICLES_DISPLAY_CNT);
    if (0 == displayCnt) {
        jsonObject.put(Common.RANDOM_ARTICLES, new ArrayList<JSONObject>());
        final JSONRenderer renderer = new JSONRenderer();
        context.setRenderer(renderer);
        renderer.setJSONObject(jsonObject);
        return;
    }
    Stopwatchs.start("Get Random Articles");
    final List<JSONObject> randomArticles = getRandomArticles(preference);
    jsonObject.put(Common.RANDOM_ARTICLES, randomArticles);
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    renderer.setJSONObject(jsonObject);
    Stopwatchs.end();
}
Also used : JSONObject(org.json.JSONObject) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 12 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class ArticleProcessor method getArticleContent.

/**
     * Gets article content with the specified context.
     *
     * @param context the specified context
     * @param request the specified request
     */
@RequestProcessing(value = "/get-article-content", method = HTTPRequestMethod.GET)
public void getArticleContent(final HTTPRequestContext context, final HttpServletRequest request) {
    final String articleId = request.getParameter("id");
    if (Strings.isEmptyOrNull(articleId)) {
        return;
    }
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    String content;
    try {
        content = articleQueryService.getArticleContent(request, articleId);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, "Can not get article content", e);
        return;
    }
    if (null == content) {
        return;
    }
    renderer.setContent(content);
}
Also used : ServiceException(org.b3log.latke.service.ServiceException) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 13 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class ArticleProcessor method getRelevantArticles.

/**
     * Gets relevant articles with the specified context.
     *
     * @param context the specified context
     * @param request the specified request
     * @param response the specified response
     * @throws Exception exception
     */
@RequestProcessing(value = "/article/id/*/relevant/articles", method = HTTPRequestMethod.GET)
public void getRelevantArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final JSONObject jsonObject = new JSONObject();
    final JSONObject preference = preferenceQueryService.getPreference();
    final int displayCnt = preference.getInt(Option.ID_C_RELEVANT_ARTICLES_DISPLAY_CNT);
    if (0 == displayCnt) {
        jsonObject.put(Common.RANDOM_ARTICLES, new ArrayList<JSONObject>());
        final JSONRenderer renderer = new JSONRenderer();
        context.setRenderer(renderer);
        renderer.setJSONObject(jsonObject);
        return;
    }
    Stopwatchs.start("Get Relevant Articles");
    final String requestURI = request.getRequestURI();
    final String articleId = StringUtils.substringBetween(requestURI, "/article/id/", "/relevant/articles");
    if (Strings.isEmptyOrNull(articleId)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    final JSONObject article = articleQueryService.getArticleById(articleId);
    if (null == article) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    final List<JSONObject> relevantArticles = articleQueryService.getRelevantArticles(article, preference);
    jsonObject.put(Common.RELEVANT_ARTICLES, relevantArticles);
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    renderer.setJSONObject(jsonObject);
    Stopwatchs.end();
}
Also used : JSONObject(org.json.JSONObject) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 14 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class LoginProcessor method showLogin.

/**
     * Shows login page.
     *
     * @param context the specified context
     * @throws Exception exception
     */
@RequestProcessing(value = "/login", method = HTTPRequestMethod.GET)
public void showLogin(final HTTPRequestContext context) throws Exception {
    final HttpServletRequest request = context.getRequest();
    String destinationURL = request.getParameter(Common.GOTO);
    if (Strings.isEmptyOrNull(destinationURL)) {
        destinationURL = Latkes.getServePath() + Common.ADMIN_INDEX_URI;
    }
    final HttpServletResponse response = context.getResponse();
    userMgmtService.tryLogInWithCookie(request, response);
    if (null != userService.getCurrentUser(request)) {
        // User has already logged in
        response.sendRedirect(destinationURL);
        return;
    }
    renderPage(context, "login.ftl", destinationURL, request);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 15 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class LoginProcessor method showForgot.

/**
     * Shows forgotten password page.
     *
     * @param context the specified context
     * @throws Exception exception
     */
@RequestProcessing(value = "/forgot", method = HTTPRequestMethod.GET)
public void showForgot(final HTTPRequestContext context) throws Exception {
    final HttpServletRequest request = context.getRequest();
    String destinationURL = request.getParameter(Common.GOTO);
    if (Strings.isEmptyOrNull(destinationURL)) {
        destinationURL = Latkes.getServePath() + Common.ADMIN_INDEX_URI;
    }
    renderPage(context, "reset-pwd.ftl", destinationURL, request);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)104 JSONObject (org.json.JSONObject)94 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)67 ServiceException (org.b3log.latke.service.ServiceException)51 IOException (java.io.IOException)35 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 JSONException (org.json.JSONException)12 JSONArray (org.json.JSONArray)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 EventException (org.b3log.latke.event.EventException)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 TextHTMLRenderer (org.b3log.latke.servlet.renderer.TextHTMLRenderer)7 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)7 ConsoleRenderer (org.b3log.solo.processor.renderer.ConsoleRenderer)7 Date (java.util.Date)6 ExecutionException (java.util.concurrent.ExecutionException)6 ArrayList (java.util.ArrayList)5 MalformedURLException (java.net.MalformedURLException)4 HttpSession (javax.servlet.http.HttpSession)4