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);
}
}
}
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);
}
Aggregations