Search in sources :

Example 51 with RequestProcessing

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

the class RepairProcessor method restoreSigns.

/**
     * Restores the signs of preference to default.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/fix/restore-signs.do", method = HTTPRequestMethod.GET)
public void restoreSigns(final HTTPRequestContext context) {
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    try {
        final JSONObject preference = preferenceQueryService.getPreference();
        final String originalSigns = preference.getString(Option.ID_C_SIGNS);
        preference.put(Option.ID_C_SIGNS, Option.DefaultPreference.DEFAULT_SIGNS);
        preferenceMgmtService.updatePreference(preference);
        // Sends the sample signs to developer
        final Message msg = new MailService.Message();
        msg.setFrom(preference.getString(Option.ID_C_ADMIN_EMAIL));
        msg.addRecipient("DL88250@gmail.com");
        msg.setSubject("Restore signs");
        msg.setHtmlBody(originalSigns + "<p>Admin email: " + preference.getString(Option.ID_C_ADMIN_EMAIL) + "</p>");
        MAIL_SVC.send(msg);
        renderer.setContent("Restores signs succeeded.");
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        renderer.setContent("Restores signs failed, error msg[" + e.getMessage() + "]");
    }
}
Also used : JSONObject(org.json.JSONObject) Message(org.b3log.latke.mail.MailService.Message) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 52 with RequestProcessing

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

the class RepairProcessor method removeAllDataGET.

/**
     * Shows remove all data page.
     *
     * @param context the specified context
     * @param request the specified HTTP servlet request
     */
@RequestProcessing(value = "/rm-all-data.do", method = HTTPRequestMethod.GET)
public void removeAllDataGET(final HTTPRequestContext context, final HttpServletRequest request) {
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    try {
        final StringBuilder htmlBuilder = new StringBuilder();
        htmlBuilder.append("<html><head><title>WARNING!</title>");
        htmlBuilder.append("<script type='text/javascript'");
        htmlBuilder.append("src='").append(Latkes.getStaticServer()).append("/js/lib/jquery/jquery.min.js'");
        htmlBuilder.append("></script></head><body>");
        htmlBuilder.append("<button id='ok' onclick='removeData()'>");
        htmlBuilder.append("Continue to delete ALL DATA</button></body>");
        htmlBuilder.append("<script type='text/javascript'>");
        htmlBuilder.append("function removeData() {");
        htmlBuilder.append("$.ajax({type: 'POST',url:'").append(Latkes.getContextPath()).append("/rm-all-data.do',");
        htmlBuilder.append("dataType: 'text/html',success: function(result){");
        htmlBuilder.append("$('html').html(result);}});}</script></html>");
        renderer.setContent(htmlBuilder.toString());
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        try {
            context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : IOException(java.io.IOException) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 53 with RequestProcessing

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

the class RepairProcessor method restoreStat.

/**
     * Restores the statistics.
     *
     * <p>
     * <ul>
     * <li>Uses the value of {@link Statistic#STATISTIC_PUBLISHED_BLOG_COMMENT_COUNT} for
     * {@link Statistic#STATISTIC_BLOG_COMMENT_COUNT}</li>
     * <li>Uses the value of {@link Statistic#STATISTIC_PUBLISHED_ARTICLE_COUNT} for
     * {@link Statistic#STATISTIC_BLOG_ARTICLE_COUNT}</li>
     * </ul>
     * </p>
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/fix/restore-stat.do", method = HTTPRequestMethod.GET)
public void restoreStat(final HTTPRequestContext context) {
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    try {
        final JSONObject statistic = statisticQueryService.getStatistic();
        if (statistic.has(Statistic.STATISTIC_BLOG_COMMENT_COUNT) && statistic.has(Statistic.STATISTIC_BLOG_ARTICLE_COUNT)) {
            LOGGER.info("No need for repairing statistic");
            renderer.setContent("No need for repairing statistic.");
            return;
        }
        if (!statistic.has(Statistic.STATISTIC_BLOG_COMMENT_COUNT)) {
            statistic.put(Statistic.STATISTIC_BLOG_COMMENT_COUNT, statistic.getInt(Statistic.STATISTIC_PUBLISHED_BLOG_COMMENT_COUNT));
        }
        if (!statistic.has(Statistic.STATISTIC_BLOG_ARTICLE_COUNT)) {
            statistic.put(Statistic.STATISTIC_BLOG_ARTICLE_COUNT, statistic.getInt(Statistic.STATISTIC_PUBLISHED_ARTICLE_COUNT));
        }
        statisticMgmtService.updateStatistic(statistic);
        renderer.setContent("Restores statistic succeeded.");
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        renderer.setContent("Restores statistics failed, error msg[" + e.getMessage() + "]");
    }
}
Also used : JSONObject(org.json.JSONObject) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 54 with RequestProcessing

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

the class TagConsole method getUnusedTags.

/**
     * Gets all unused tags.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "unusedTags": [
     *         {"tagTitle": "", tagReferenceCount": 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 IOException io exception
     */
@RequestProcessing(value = "/console/tag/unused", method = HTTPRequestMethod.GET)
public void getUnusedTags(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws IOException {
    if (!userQueryService.isLoggedIn(request, response)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();
    renderer.setJSONObject(jsonObject);
    final List<JSONObject> unusedTags = new ArrayList<JSONObject>();
    try {
        jsonObject.put(Common.UNUSED_TAGS, unusedTags);
        final List<JSONObject> tags = tagQueryService.getTags();
        for (int i = 0; i < tags.size(); i++) {
            final JSONObject tag = tags.get(i);
            final int tagRefCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
            if (0 == tagRefCnt) {
                unusedTags.add(tag);
            }
        }
        jsonObject.put(Keys.STATUS_CODE, true);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets unused tags failed", e);
        jsonObject.put(Keys.STATUS_CODE, false);
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 55 with RequestProcessing

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

the class TagConsole method getTags.

/**
     * Gets all tags.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "tags": [
     *         {"tagTitle": "", tagReferenceCount": 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 IOException io exception
     */
@RequestProcessing(value = "/console/tags", method = HTTPRequestMethod.GET)
public void getTags(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws IOException {
    if (!userQueryService.isLoggedIn(request, response)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();
    renderer.setJSONObject(jsonObject);
    try {
        jsonObject.put(Tag.TAGS, tagQueryService.getTags());
        jsonObject.put(Keys.STATUS_CODE, true);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets tags failed", e);
        jsonObject.put(Keys.STATUS_CODE, false);
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) IOException(java.io.IOException) 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