Search in sources :

Example 26 with RequestProcessing

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

the class FeedProcessor method tagArticlesRSS.

/**
     * Tag articles RSS output.
     *
     * @param context the specified context
     * @throws IOException io exception
     */
@RequestProcessing(value = { "/tag-articles-rss.do" }, method = { HTTPRequestMethod.GET, HTTPRequestMethod.HEAD })
public void tagArticlesRSS(final HTTPRequestContext context) throws IOException {
    final HttpServletResponse response = context.getResponse();
    final HttpServletRequest request = context.getRequest();
    final RssRenderer renderer = new RssRenderer();
    context.setRenderer(renderer);
    final String queryString = request.getQueryString();
    if (Strings.isEmptyOrNull(queryString)) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    final String oIdMap = queryString.split("&")[0];
    final String tagId = oIdMap.split("=")[1];
    final Channel channel = new Channel();
    try {
        final JSONObject tag = tagRepository.get(tagId);
        if (null == tag) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final String tagTitle = tag.getString(Tag.TAG_TITLE);
        final JSONObject preference = preferenceQueryService.getPreference();
        if (null == preference) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final String blogTitle = preference.getString(Option.ID_C_BLOG_TITLE);
        final String blogSubtitle = preference.getString(Option.ID_C_BLOG_SUBTITLE) + ", " + tagTitle;
        final int outputCnt = preference.getInt(Option.ID_C_FEED_OUTPUT_CNT);
        channel.setTitle(StringEscapeUtils.escapeXml(blogTitle));
        channel.setLastBuildDate(new Date());
        channel.setLink(Latkes.getServePath());
        channel.setAtomLink(Latkes.getServePath() + "/tag-articles-rss.do");
        channel.setGenerator("Solo, ver " + SoloServletListener.VERSION);
        final String localeString = preference.getString(Option.ID_C_LOCALE_STRING);
        final String country = Locales.getCountry(localeString).toLowerCase();
        final String language = Locales.getLanguage(localeString).toLowerCase();
        channel.setLanguage(language + '-' + country);
        channel.setDescription(blogSubtitle);
        final JSONObject tagArticleResult = tagArticleRepository.getByTagId(tagId, 1, outputCnt);
        final JSONArray tagArticleRelations = tagArticleResult.getJSONArray(Keys.RESULTS);
        if (0 == tagArticleRelations.length()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final List<JSONObject> articles = new ArrayList<JSONObject>();
        for (int i = 0; i < tagArticleRelations.length(); i++) {
            final JSONObject tagArticleRelation = tagArticleRelations.getJSONObject(i);
            final String articleId = tagArticleRelation.getString(Article.ARTICLE + "_" + Keys.OBJECT_ID);
            final JSONObject article = articleRepository.get(articleId);
            if (// Skips the unpublished article
            article.getBoolean(Article.ARTICLE_IS_PUBLISHED) && Strings.isEmptyOrNull(article.optString(Article.ARTICLE_VIEW_PWD))) {
                // Skips article with password
                articles.add(article);
            }
        }
        final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
        String authorName = "";
        if (!hasMultipleUsers && !articles.isEmpty()) {
            authorName = articleQueryService.getAuthor(articles.get(0)).getString(User.USER_NAME);
        }
        final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE));
        for (int i = 0; i < articles.size(); i++) {
            Item item = getItemForArticles(articles, hasMultipleUsers, authorName, isFullContent, i);
            channel.addItem(item);
        }
        renderer.setContent(channel.toString());
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Get tag article rss error", e);
        try {
            context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : Channel(org.b3log.solo.model.feed.rss.Channel) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) RssRenderer(org.b3log.latke.servlet.renderer.RssRenderer) Date(java.util.Date) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Item(org.b3log.solo.model.feed.rss.Item) JSONObject(org.json.JSONObject) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 27 with RequestProcessing

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

the class IndexProcessor method register.

/**
     * Show register page.
     *
     * @param context the specified context
     * @param request the specified HTTP servlet request
     * @param response the specified HTTP servlet response
     */
@RequestProcessing(value = "/register", method = HTTPRequestMethod.GET)
public void register(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) {
    final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
    context.setRenderer(renderer);
    renderer.setTemplateName("register.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    try {
        final Map<String, String> langs = langPropsService.getAll(Locales.getLocale(request));
        dataModel.putAll(langs);
        final JSONObject preference = preferenceQueryService.getPreference();
        filler.fillBlogFooter(request, dataModel, preference);
        filler.fillMinified(dataModel);
    } 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());
        }
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONObject(org.json.JSONObject) ConsoleRenderer(org.b3log.solo.processor.renderer.ConsoleRenderer) IOException(java.io.IOException) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 28 with RequestProcessing

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

the class PageConsole method removePage.

/**
     * Removes a page by the specified request.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/page/*", method = HTTPRequestMethod.DELETE)
public void removePage(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    if (!userQueryService.isAdminLoggedIn(request)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();
    renderer.setJSONObject(jsonObject);
    try {
        final String pageId = request.getRequestURI().substring((Latkes.getContextPath() + "/console/page/").length());
        pageMgmtService.removePage(pageId);
        jsonObject.put(Keys.STATUS_CODE, true);
        jsonObject.put(Keys.MSG, langPropsService.get("removeSuccLabel"));
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        jsonObject.put(Keys.STATUS_CODE, false);
        jsonObject.put(Keys.MSG, langPropsService.get("removeFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 29 with RequestProcessing

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

the class PageConsole method getPage.

/**
     * Gets a page by the specified request.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean
     *     "page": {
     *         "oId": "",
     *         "pageTitle": "",
     *         "pageContent": ""
     *         "pageOrder": int,
     *         "pagePermalink": "",
     *         "pageCommentCount": int,
     *     }
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/page/*", method = HTTPRequestMethod.GET)
public void getPage(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    if (!userQueryService.isLoggedIn(request, response)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    try {
        final String requestURI = request.getRequestURI();
        final String pageId = requestURI.substring((Latkes.getContextPath() + "/console/page/").length());
        final JSONObject result = pageQueryService.getPage(pageId);
        if (null == result) {
            renderer.setJSONObject(QueryResults.defaultResult());
            return;
        }
        renderer.setJSONObject(result);
        result.put(Keys.STATUS_CODE, true);
        result.put(Keys.MSG, langPropsService.get("getSuccLabel"));
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 30 with RequestProcessing

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

the class PluginConsole method updateSetting.

/**
     * update the setting of the plugin. 
     * 
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @param renderer the specified {@link ConsoleRenderer}
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/plugin/updateSetting", method = HTTPRequestMethod.POST)
public void updateSetting(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context, final JSONRenderer renderer) throws Exception {
    context.setRenderer(renderer);
    final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
    final String pluginoId = requestJSONObject.getString(Keys.OBJECT_ID);
    final String settings = requestJSONObject.getString(Plugin.PLUGIN_SETTING);
    final JSONObject ret = pluginMgmtService.updatePluginSetting(pluginoId, settings);
    renderer.setJSONObject(ret);
}
Also used : JSONObject(org.json.JSONObject) 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