Search in sources :

Example 1 with TextXMLRenderer

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

the class SitemapProcessor method sitemap.

/**
     * Returns the sitemap.
     * 
     * @param context the specified context
     */
@RequestProcessing(value = "/sitemap.xml", method = HTTPRequestMethod.GET)
public void sitemap(final HTTPRequestContext context) {
    final TextXMLRenderer renderer = new TextXMLRenderer();
    context.setRenderer(renderer);
    final Sitemap sitemap = new Sitemap();
    try {
        addArticles(sitemap);
        addNavigations(sitemap);
        addTags(sitemap);
        addArchives(sitemap);
        LOGGER.log(Level.INFO, "Generating sitemap....");
        final String content = sitemap.toString();
        LOGGER.log(Level.INFO, "Generated sitemap");
        renderer.setContent(content);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Get blog article feed error", e);
        try {
            context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : Sitemap(org.b3log.solo.model.sitemap.Sitemap) TextXMLRenderer(org.b3log.latke.servlet.renderer.TextXMLRenderer) IOException(java.io.IOException) IOException(java.io.IOException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 2 with TextXMLRenderer

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

the class MetaWeblogAPI method metaWeblog.

/**
     * MetaWeblog requests processing.
     *
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     */
@RequestProcessing(value = "/apis/metaweblog", method = HTTPRequestMethod.POST)
public void metaWeblog(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) {
    final TextXMLRenderer renderer = new TextXMLRenderer();
    context.setRenderer(renderer);
    String responseContent = null;
    try {
        final ServletInputStream inputStream = request.getInputStream();
        final String xml = IOUtils.toString(inputStream, "UTF-8");
        final JSONObject requestJSONObject = XML.toJSONObject(xml);
        final JSONObject methodCall = requestJSONObject.getJSONObject(METHOD_CALL);
        final String methodName = methodCall.getString(METHOD_NAME);
        LOGGER.log(Level.INFO, "MetaWeblog[methodName={0}]", methodName);
        final JSONArray params = methodCall.getJSONObject("params").getJSONArray("param");
        if (METHOD_DELETE_POST.equals(methodName)) {
            // Removes the first argument "appkey"
            params.remove(0);
        }
        final String userEmail = params.getJSONObject(INDEX_USER_EMAIL).getJSONObject("value").getString("string");
        final JSONObject user = userQueryService.getUserByEmail(userEmail);
        if (null == user) {
            throw new Exception("No user[email=" + userEmail + "]");
        }
        final String userPwd = params.getJSONObject(INDEX_USER_PWD).getJSONObject("value").getString("string");
        if (!user.getString(User.USER_PASSWORD).equals(MD5.hash(userPwd))) {
            throw new Exception("Wrong password");
        }
        if (METHOD_GET_USERS_BLOGS.equals(methodName)) {
            responseContent = getUsersBlogs();
        } else if (METHOD_GET_CATEGORIES.equals(methodName)) {
            responseContent = getCategories();
        } else if (METHOD_GET_RECENT_POSTS.equals(methodName)) {
            final int numOfPosts = params.getJSONObject(INDEX_NUM_OF_POSTS).getJSONObject("value").getInt("int");
            responseContent = getRecentPosts(numOfPosts);
        } else if (METHOD_NEW_POST.equals(methodName)) {
            final JSONObject article = parsetPost(methodCall);
            article.put(Article.ARTICLE_AUTHOR_EMAIL, userEmail);
            addArticle(article);
            final StringBuilder stringBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse>").append("<params><param><value><string>").append(article.getString(Keys.OBJECT_ID)).append("</string></value></param></params></methodResponse>");
            responseContent = stringBuilder.toString();
        } else if (METHOD_GET_POST.equals(methodName)) {
            final String postId = params.getJSONObject(INDEX_POST_ID).getJSONObject("value").getString("string");
            responseContent = getPost(postId);
        } else if (METHOD_EDIT_POST.equals(methodName)) {
            final JSONObject article = parsetPost(methodCall);
            final String postId = params.getJSONObject(INDEX_POST_ID).getJSONObject("value").getString("string");
            article.put(Keys.OBJECT_ID, postId);
            article.put(Article.ARTICLE_AUTHOR_EMAIL, userEmail);
            final JSONObject updateArticleRequest = new JSONObject();
            updateArticleRequest.put(Article.ARTICLE, article);
            articleMgmtService.updateArticle(updateArticleRequest);
            final StringBuilder stringBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse>").append("<params><param><value><string>").append(postId).append("</string></value></param></params></methodResponse>");
            responseContent = stringBuilder.toString();
        } else if (METHOD_DELETE_POST.equals(methodName)) {
            final String postId = params.getJSONObject(INDEX_POST_ID).getJSONObject("value").getString("string");
            articleMgmtService.removeArticle(postId);
            final StringBuilder stringBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse>").append("<params><param><value><boolean>").append(true).append("</boolean></value></param></params></methodResponse>");
            responseContent = stringBuilder.toString();
        } else {
            throw new UnsupportedOperationException("Unsupported method[name=" + methodName + "]");
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final StringBuilder stringBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse>").append("<fault><value><struct>").append("<member><name>faultCode</name><value><int>500</int></value></member>").append("<member><name>faultString</name><value><string>").append(e.getMessage()).append("</string></value></member></struct></value></fault></methodResponse>");
        responseContent = stringBuilder.toString();
    }
    renderer.setContent(responseContent);
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) JSONObject(org.json.JSONObject) TextXMLRenderer(org.b3log.latke.servlet.renderer.TextXMLRenderer) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) ParseException(java.text.ParseException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)2 TextXMLRenderer (org.b3log.latke.servlet.renderer.TextXMLRenderer)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ServletInputStream (javax.servlet.ServletInputStream)1 ServiceException (org.b3log.latke.service.ServiceException)1 Sitemap (org.b3log.solo.model.sitemap.Sitemap)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1