* @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)) {
params.remove(0); // Removes the first argument "appkey"
}
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(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><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><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><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.SEVERE, e.getMessage(), e);
responseContent = "";
final StringBuilder stringBuilder =
new StringBuilder(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodResponse><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);
}