PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/roller-5.0.1/weblogger-web/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/BloggerAPIHandler.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 501 lines | 296 code | 77 blank | 128 comment | 26 complexity | 09a12e4386e68a609bc61b718b368118 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. The ASF licenses this file to You
  4. * under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License. For additional information regarding
  15. * copyright in this work, please see the NOTICE file in the top level
  16. * directory of this distribution.
  17. */
  18. package org.apache.roller.weblogger.webservices.xmlrpc;
  19. import java.sql.Timestamp;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.Hashtable;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.StringTokenizer;
  27. import java.util.Vector;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. import org.apache.roller.weblogger.WebloggerException;
  32. import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
  33. import org.apache.roller.weblogger.business.Weblogger;
  34. import org.apache.roller.weblogger.business.WebloggerFactory;
  35. import org.apache.roller.weblogger.business.UserManager;
  36. import org.apache.roller.weblogger.business.WeblogEntryManager;
  37. import org.apache.roller.weblogger.pojos.User;
  38. import org.apache.roller.weblogger.pojos.WeblogEntry;
  39. import org.apache.roller.weblogger.pojos.WeblogTemplate;
  40. import org.apache.roller.weblogger.pojos.Weblog;
  41. import org.apache.roller.weblogger.util.Utilities;
  42. import org.apache.xmlrpc.XmlRpcException;
  43. /**
  44. * Weblogger XML-RPC Handler for the Blogger v1 API.
  45. *
  46. * Blogger API spec can be found at http://plant.blogger.com/api/index.html
  47. * See also http://xmlrpc.free-conversant.com/docs/bloggerAPI
  48. *
  49. * @author David M Johnson
  50. */
  51. public class BloggerAPIHandler extends BaseAPIHandler {
  52. static final long serialVersionUID = 2398898776655115019L;
  53. private static Log mLogger = LogFactory.getLog(BloggerAPIHandler.class);
  54. public BloggerAPIHandler() {
  55. super();
  56. }
  57. /**
  58. * Delete a Post
  59. *
  60. * @param appkey Unique identifier/passcode of the application sending the post
  61. * @param postid Unique identifier of the post to be changed
  62. * @param userid Login for a Blogger user who has permission to post to the blog
  63. * @param password Password for said username
  64. * @param publish Ignored
  65. * @throws XmlRpcException
  66. * @return
  67. */
  68. public boolean deletePost(String appkey, String postid, String userid,
  69. String password, boolean publish) throws Exception {
  70. mLogger.debug("deletePost() Called =====[ SUPPORTED ]=====");
  71. mLogger.debug(" Appkey: " + appkey);
  72. mLogger.debug(" PostId: " + postid);
  73. mLogger.debug(" UserId: " + userid);
  74. Weblogger roller = WebloggerFactory.getWeblogger();
  75. WeblogEntryManager weblogMgr = roller.getWeblogEntryManager();
  76. WeblogEntry entry = weblogMgr.getWeblogEntry(postid);
  77. // Return false if entry not found
  78. if (entry == null) return false;
  79. validate(entry.getWebsite().getHandle(), userid, password);
  80. try {
  81. // notify cache
  82. flushPageCache(entry.getWebsite());
  83. // delete the entry
  84. weblogMgr.removeWeblogEntry(entry);
  85. roller.flush();
  86. } catch (Exception e) {
  87. String msg = "ERROR in blogger.deletePost: "+e.getClass().getName();
  88. mLogger.error(msg,e);
  89. throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
  90. }
  91. return true;
  92. }
  93. /**
  94. * Edits the main index template of a given blog. Weblogger only support
  95. * updating the main template, the default template of your weblog.
  96. *
  97. * @param appkey Unique identifier/passcode of the application sending the post
  98. * @param blogid Unique identifier of the blog the post will be added to
  99. * @param userid Login for a Blogger user who has permission to post to the blog
  100. * @param password Password for said username
  101. * @param template The text for the new template (usually mostly HTML).
  102. * @param templateType Determines which of the blog's templates is to be set.
  103. * @return
  104. * @throws XmlRpcException
  105. */
  106. public boolean setTemplate(String appkey, String blogid, String userid,
  107. String password, String templateData,
  108. String templateType) throws Exception {
  109. mLogger.debug("setTemplate() Called =====[ SUPPORTED ]=====");
  110. mLogger.debug(" Appkey: " + appkey);
  111. mLogger.debug(" BlogId: " + blogid);
  112. mLogger.debug(" UserId: " + userid);
  113. mLogger.debug(" Template: " + templateData);
  114. mLogger.debug(" Type: " + templateType);
  115. validate(blogid, userid, password);
  116. if (! templateType.equals("main")) {
  117. throw new XmlRpcException(
  118. UNKNOWN_EXCEPTION, "Roller only supports main template");
  119. }
  120. try {
  121. WeblogTemplate page = WebloggerFactory.getWeblogger().getWeblogManager().getPage(templateType);
  122. page.setContents(templateData);
  123. WebloggerFactory.getWeblogger().getWeblogManager().savePage(page);
  124. flushPageCache(page.getWebsite());
  125. return true;
  126. } catch (WebloggerException e) {
  127. String msg = "ERROR in BlooggerAPIHander.setTemplate";
  128. mLogger.error(msg,e);
  129. throw new XmlRpcException(UNKNOWN_EXCEPTION,msg);
  130. }
  131. }
  132. /**
  133. * Returns the main or archive index template of a given blog
  134. *
  135. * @param appkey Unique identifier/passcode of the application sending the post
  136. * @param blogid Unique identifier of the blog the post will be added to
  137. * @param userid Login for a Blogger user who has permission to post to the blog
  138. * @param password Password for said username
  139. * @param templateType Determines which of the blog's templates will be returned. Currently, either "main" or "archiveIndex"
  140. * @throws XmlRpcException
  141. * @return
  142. */
  143. public String getTemplate(String appkey, String blogid, String userid,
  144. String password, String templateType)
  145. throws Exception {
  146. mLogger.debug("getTemplate() Called =====[ SUPPORTED ]=====");
  147. mLogger.debug(" Appkey: " + appkey);
  148. mLogger.debug(" BlogId: " + blogid);
  149. mLogger.debug(" UserId: " + userid);
  150. mLogger.debug(" Type: " + templateType);
  151. validate(blogid, userid,password);
  152. try {
  153. WeblogTemplate page = WebloggerFactory.getWeblogger().getWeblogManager().getPage(templateType);
  154. if ( null == page ) {
  155. throw new XmlRpcException(UNKNOWN_EXCEPTION,"Template not found");
  156. } else {
  157. return page.getContents();
  158. }
  159. } catch (Exception e) {
  160. String msg = "ERROR in BlooggerAPIHander.getTemplate";
  161. mLogger.error(msg,e);
  162. throw new XmlRpcException(UNKNOWN_EXCEPTION,msg);
  163. }
  164. }
  165. /**
  166. * Authenticates a user and returns basic user info (name, email, userid, etc.)
  167. *
  168. * @param appkey Unique identifier/passcode of the application sending the post
  169. * @param userid Login for a Blogger user who has permission to post to the blog
  170. * @param password Password for said username
  171. * @throws XmlRpcException
  172. * @return
  173. */
  174. public Object getUserInfo(String appkey, String userid, String password)
  175. throws Exception {
  176. mLogger.debug("getUserInfo() Called =====[ SUPPORTED ]=====");
  177. mLogger.debug(" Appkey: " + appkey);
  178. mLogger.debug(" UserId: " + userid);
  179. validateUser(userid, password);
  180. try {
  181. Weblogger roller = WebloggerFactory.getWeblogger();
  182. UserManager userMgr = roller.getUserManager();
  183. User user = userMgr.getUserByUserName(userid);
  184. // parses full name into two strings, firstname and lastname
  185. String firstname = "", lastname = "";
  186. StringTokenizer toker = new StringTokenizer(user.getFullName());
  187. if (toker.hasMoreTokens()) {
  188. firstname = toker.nextToken();
  189. }
  190. while (toker.hasMoreTokens()) {
  191. if ( !lastname.equals("") ) {
  192. lastname += " ";
  193. }
  194. lastname += toker.nextToken();
  195. }
  196. // TODO: Should screen name be renamed nickname and used here?
  197. // populates user information to return as a result
  198. Hashtable result = new Hashtable();
  199. result.put("nickname", user.getUserName());
  200. result.put("userid", user.getUserName());
  201. result.put("email", "");
  202. result.put("lastname", lastname);
  203. result.put("firstname", firstname);
  204. return result;
  205. } catch (WebloggerException e) {
  206. String msg = "ERROR in BlooggerAPIHander.getInfo";
  207. mLogger.error(msg,e);
  208. throw new XmlRpcException(UNKNOWN_EXCEPTION,msg);
  209. }
  210. }
  211. /**
  212. * Returns information on all the blogs a given user is a member of
  213. *
  214. * @param appkey Unique identifier/passcode of the application sending the post
  215. * @param userid Login for a Blogger user who has permission to post to the blog
  216. * @param password Password for said username
  217. * @throws XmlRpcException
  218. * @return
  219. */
  220. public Object getUsersBlogs(String appkey, String userid, String password)
  221. throws Exception {
  222. mLogger.debug("getUsersBlogs() Called ===[ SUPPORTED ]=======");
  223. mLogger.debug(" Appkey: " + appkey);
  224. mLogger.debug(" UserId: " + userid);
  225. Vector result = new Vector();
  226. if (validateUser(userid, password)) {
  227. try {
  228. String contextUrl = WebloggerRuntimeConfig.getAbsoluteContextURL();
  229. UserManager umgr = WebloggerFactory.getWeblogger().getUserManager();
  230. User user = umgr.getUserByUserName(userid);
  231. // get list of user's enabled websites
  232. List websites = WebloggerFactory.getWeblogger().getWeblogManager().getUserWeblogs(user, true);
  233. Iterator iter = websites.iterator();
  234. while (iter.hasNext()) {
  235. Weblog website = (Weblog)iter.next();
  236. // only include weblog's that have client API support enabled
  237. if (Boolean.TRUE.equals(website.getEnableBloggerApi())) {
  238. Hashtable blog = new Hashtable(3);
  239. blog.put("url", website.getURL());
  240. blog.put("blogid", website.getHandle());
  241. blog.put("blogName", website.getName());
  242. result.add(blog);
  243. }
  244. }
  245. } catch (Exception e) {
  246. String msg = "ERROR in BlooggerAPIHander.getUsersBlogs";
  247. mLogger.error(msg,e);
  248. throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
  249. }
  250. }
  251. return result;
  252. }
  253. /**
  254. * Edits a given post. Optionally, will publish the blog after making the edit
  255. *
  256. * @param appkey Unique identifier/passcode of the application sending the post
  257. * @param postid Unique identifier of the post to be changed
  258. * @param userid Login for a Blogger user who has permission to post to the blog
  259. * @param password Password for said username
  260. * @param content Contents of the post
  261. * @param publish If true, the blog will be published immediately after the post is made
  262. * @throws XmlRpcException
  263. * @return
  264. */
  265. public boolean editPost(String appkey, String postid, String userid,
  266. String password, String content, boolean publish)
  267. throws Exception {
  268. mLogger.debug("editPost() Called ========[ SUPPORTED ]=====");
  269. mLogger.debug(" Appkey: " + appkey);
  270. mLogger.debug(" PostId: " + postid);
  271. mLogger.debug(" UserId: " + userid);
  272. mLogger.debug(" Publish: " + publish);
  273. mLogger.debug(" Content:\n " + content);
  274. if (validateUser(userid, password)) {
  275. try {
  276. Timestamp current = new Timestamp(System.currentTimeMillis());
  277. Weblogger roller = WebloggerFactory.getWeblogger();
  278. WeblogEntryManager weblogMgr = roller.getWeblogEntryManager();
  279. WeblogEntry entry = weblogMgr.getWeblogEntry(postid);
  280. entry.setText(content);
  281. entry.setUpdateTime(current);
  282. if (Boolean.valueOf(publish).booleanValue()) {
  283. entry.setStatus(WeblogEntry.PUBLISHED);
  284. } else {
  285. entry.setStatus(WeblogEntry.DRAFT);
  286. }
  287. // save the entry
  288. weblogMgr.saveWeblogEntry(entry);
  289. roller.flush();
  290. // notify cache
  291. flushPageCache(entry.getWebsite());
  292. return true;
  293. } catch (Exception e) {
  294. String msg = "ERROR in BlooggerAPIHander.editPost";
  295. mLogger.error(msg,e);
  296. throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
  297. }
  298. }
  299. return false;
  300. }
  301. /**
  302. * Makes a new post to a designated blog. Optionally, will publish the blog after making the post
  303. *
  304. * @param appkey Unique identifier/passcode of the application sending the post
  305. * @param blogid Unique identifier of the blog the post will be added to
  306. * @param userid Login for a Blogger user who has permission to post to the blog
  307. * @param password Password for said username
  308. * @param content Contents of the post
  309. * @param publish If true, the blog will be published immediately after the post is made
  310. * @throws XmlRpcException
  311. * @return
  312. */
  313. public String newPost(String appkey, String blogid, String userid,
  314. String password, String content, boolean publish)
  315. throws Exception {
  316. mLogger.debug("newPost() Called ===========[ SUPPORTED ]=====");
  317. mLogger.debug(" Appkey: " + appkey);
  318. mLogger.debug(" BlogId: " + blogid);
  319. mLogger.debug(" UserId: " + userid);
  320. mLogger.debug(" Publish: " + publish);
  321. mLogger.debug(" Content:\n " + content);
  322. Weblog website = validate(blogid, userid, password);
  323. // extract the title from the content
  324. String title = "";
  325. if (content.indexOf("<title>") != -1) {
  326. title =
  327. content.substring(content.indexOf("<title>") + 7,
  328. content.indexOf("</title>"));
  329. content = StringUtils.replace(content, "<title>"+title+"</title>", "");
  330. }
  331. if (StringUtils.isEmpty(title)) {
  332. title = Utilities.truncateNicely(content, 15, 15, "...");
  333. }
  334. try {
  335. Weblogger roller = WebloggerFactory.getWeblogger();
  336. WeblogEntryManager weblogMgr = roller.getWeblogEntryManager();
  337. Timestamp current = new Timestamp(System.currentTimeMillis());
  338. WeblogEntry entry = new WeblogEntry();
  339. entry.setTitle(title);
  340. entry.setText(content);
  341. entry.setLocale(website.getLocale());
  342. entry.setPubTime(current);
  343. entry.setUpdateTime(current);
  344. User user = roller.getUserManager().getUserByUserName(userid);
  345. entry.setCreatorUserName(user.getUserName());
  346. entry.setWebsite(website);
  347. entry.setCategory(website.getBloggerCategory());
  348. entry.setCommentDays(new Integer(website.getDefaultCommentDays()));
  349. if (Boolean.valueOf(publish).booleanValue()) {
  350. entry.setStatus(WeblogEntry.PUBLISHED);
  351. } else {
  352. entry.setStatus(WeblogEntry.DRAFT);
  353. }
  354. // save the entry
  355. weblogMgr.saveWeblogEntry(entry);
  356. roller.flush();
  357. // notify cache
  358. flushPageCache(entry.getWebsite());
  359. return entry.getId();
  360. } catch (Exception e) {
  361. String msg = "ERROR in BlooggerAPIHander.newPost";
  362. mLogger.error(msg,e);
  363. throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
  364. }
  365. }
  366. /**
  367. * This method was added to the Blogger 1.0 API via an Email from Evan
  368. * Williams to the Yahoo Group bloggerDev, see the email message for details -
  369. * http://groups.yahoo.com/group/bloggerDev/message/225
  370. *
  371. * @param appkey Unique identifier/passcode of the application sending the post
  372. * @param blogid Unique identifier of the blog the post will be added to
  373. * @param userid Login for a Blogger user who has permission to post to the blog
  374. * @param password Password for said username
  375. * @param numposts Number of Posts to Retrieve
  376. * @throws XmlRpcException
  377. * @return Vector of Hashtables, each containing dateCreated, userid, postid, content
  378. */
  379. public Object getRecentPosts(String appkey, String blogid, String userid,
  380. String password, int numposts)
  381. throws Exception {
  382. mLogger.debug("getRecentPosts() Called ===========[ SUPPORTED ]=====");
  383. mLogger.debug(" Appkey: " + appkey);
  384. mLogger.debug(" BlogId: " + blogid);
  385. mLogger.debug(" UserId: " + userid);
  386. mLogger.debug(" Number: " + numposts);
  387. Weblog website = validate(blogid, userid,password);
  388. try {
  389. Vector results = new Vector();
  390. Weblogger roller = WebloggerFactory.getWeblogger();
  391. WeblogEntryManager weblogMgr = roller.getWeblogEntryManager();
  392. if (website != null) {
  393. Map entries = weblogMgr.getWeblogEntryObjectMap(
  394. website, // website
  395. null, // startDate
  396. new Date(), // endDate
  397. null, // catName
  398. null, // tags
  399. null, null, 0, -1);
  400. Iterator iter = entries.values().iterator();
  401. while (iter.hasNext()) {
  402. ArrayList list = (ArrayList) iter.next();
  403. Iterator i = list.iterator();
  404. while (i.hasNext()) {
  405. WeblogEntry entry = (WeblogEntry) i.next();
  406. Hashtable result = new Hashtable();
  407. if (entry.getPubTime() != null) {
  408. result.put("dateCreated", entry.getPubTime());
  409. }
  410. result.put("userid", userid);
  411. result.put("postid", entry.getId());
  412. result.put("content", entry.getText());
  413. results.add(result);
  414. }
  415. }
  416. }
  417. return results;
  418. } catch (Exception e) {
  419. String msg = "ERROR in BlooggerAPIHander.getRecentPosts";
  420. mLogger.error(msg,e);
  421. throw new XmlRpcException(UNKNOWN_EXCEPTION, msg);
  422. }
  423. }
  424. }