PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/blogracy-web/src/main/java/net/blogracy/controller/ActivitiesController.java

https://github.com/rik0/blogracy
Java | 212 lines | 165 code | 23 blank | 24 comment | 12 complexity | 92ffddaea3dd8d0f023ce5cca3f1bac5 MD5 | raw file
  1. /*
  2. * Copyright (c) 2011 Enrico Franchi, Michele Tomaiuolo and University of Parma.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. package net.blogracy.controller;
  23. import java.io.File;
  24. import java.io.FileReader;
  25. import java.io.FileWriter;
  26. import java.io.IOException;
  27. import java.security.InvalidParameterException;
  28. import java.security.MessageDigest;
  29. import java.security.NoSuchAlgorithmException;
  30. import java.text.DateFormat;
  31. import java.text.SimpleDateFormat;
  32. import java.util.TimeZone;
  33. import java.util.ArrayList;
  34. import java.util.Date;
  35. import java.util.HashMap;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.Map.Entry;
  40. import javax.jms.Connection;
  41. import javax.jms.ConnectionFactory;
  42. import javax.jms.DeliveryMode;
  43. import javax.jms.Destination;
  44. import javax.jms.Message;
  45. import javax.jms.MessageConsumer;
  46. import javax.jms.MessageListener;
  47. import javax.jms.MessageProducer;
  48. import javax.jms.Session;
  49. import javax.jms.TextMessage;
  50. import net.blogracy.config.Configurations;
  51. import net.blogracy.util.FileUtils;
  52. import org.apache.activemq.ActiveMQConnection;
  53. import org.apache.activemq.ActiveMQConnectionFactory;
  54. import org.apache.commons.codec.binary.Base32;
  55. import org.apache.shindig.protocol.conversion.BeanConverter;
  56. import org.apache.shindig.protocol.conversion.BeanJsonConverter;
  57. import org.apache.shindig.social.core.model.ActivityEntryImpl;
  58. import org.apache.shindig.social.core.model.ActivityObjectImpl;
  59. import org.apache.shindig.social.core.model.AlbumImpl;
  60. import org.apache.shindig.social.core.model.MediaItemImpl;
  61. import org.apache.shindig.social.opensocial.model.ActivityEntry;
  62. import org.apache.shindig.social.opensocial.model.ActivityObject;
  63. import org.apache.shindig.social.opensocial.model.Album;
  64. import org.apache.shindig.social.opensocial.model.MediaItem;
  65. import org.apache.shindig.social.opensocial.model.MediaItem.Type;
  66. import org.json.JSONArray;
  67. import org.json.JSONException;
  68. import org.json.JSONObject;
  69. import org.json.JSONTokener;
  70. import com.google.inject.Binder;
  71. import com.google.inject.Guice;
  72. import com.google.inject.Module;
  73. import com.google.inject.name.Names;
  74. /**
  75. * Generic functions to manipulate feeds are defined in this class.
  76. */
  77. public class ActivitiesController {
  78. static final DateFormat ISO_DATE_FORMAT = new SimpleDateFormat(
  79. "yyyy-MM-dd'T'HH:mm:ss'Z'");
  80. static final String CACHE_FOLDER = Configurations.getPathConfig()
  81. .getCachedFilesDirectoryPath();
  82. private static final FileSharing sharing = FileSharing.getSingleton();
  83. private static final DistributedHashTable dht = DistributedHashTable
  84. .getSingleton();
  85. private static final ActivitiesController theInstance = new ActivitiesController();
  86. private static BeanJsonConverter CONVERTER = new BeanJsonConverter(
  87. Guice.createInjector(new Module() {
  88. @Override
  89. public void configure(Binder b) {
  90. b.bind(BeanConverter.class)
  91. .annotatedWith(
  92. Names.named("shindig.bean.converter.json"))
  93. .to(BeanJsonConverter.class);
  94. }
  95. }));
  96. public static ActivitiesController getSingleton() {
  97. return theInstance;
  98. }
  99. public ActivitiesController() {
  100. ISO_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
  101. }
  102. static public List<ActivityEntry> getFeed(String user) {
  103. List<ActivityEntry> result = new ArrayList<ActivityEntry>();
  104. System.out.println("Getting feed: " + user);
  105. JSONObject record = dht.getRecord(user);
  106. if (record != null) {
  107. try {
  108. String latestHash = FileSharing.getHashFromMagnetURI(record
  109. .getString("uri"));
  110. File dbFile = new File(CACHE_FOLDER + File.separator
  111. + latestHash + ".json");
  112. if (!dbFile.exists() && record.has("prev")) {
  113. latestHash = FileSharing.getHashFromMagnetURI(record
  114. .getString("prev"));
  115. dbFile = new File(CACHE_FOLDER + File.separator
  116. + latestHash + ".json");
  117. }
  118. if (dbFile.exists()) {
  119. System.out.println("Getting feed: "
  120. + dbFile.getAbsolutePath());
  121. JSONObject db = new JSONObject(new JSONTokener(
  122. new FileReader(dbFile)));
  123. JSONArray items = db.getJSONArray("items");
  124. for (int i = 0; i < items.length(); ++i) {
  125. JSONObject item = items.getJSONObject(i);
  126. ActivityEntry entry = (ActivityEntry) CONVERTER
  127. .convertToObject(item, ActivityEntry.class);
  128. result.add(entry);
  129. }
  130. System.out.println("Feed loaded");
  131. } else {
  132. System.out.println("Feed not found");
  133. }
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. return result;
  139. }
  140. public void addFeedEntry(String id, String text, File attachment) {
  141. try {
  142. String hash = sharing.hash(text);
  143. File textFile = new File(CACHE_FOLDER + File.separator + hash
  144. + ".txt");
  145. FileWriter w = new FileWriter(textFile);
  146. w.write(text);
  147. w.close();
  148. String textUri = sharing.seed(textFile);
  149. String attachmentUri = null;
  150. if (attachment != null) {
  151. attachmentUri = sharing.seed(attachment);
  152. }
  153. final List<ActivityEntry> feed = getFeed(id);
  154. final ActivityEntry entry = new ActivityEntryImpl();
  155. entry.setVerb("post");
  156. entry.setUrl(textUri);
  157. entry.setPublished(ISO_DATE_FORMAT.format(new Date()));
  158. entry.setContent(text);
  159. if (attachment != null) {
  160. ActivityObject enclosure = new ActivityObjectImpl();
  161. enclosure.setUrl(attachmentUri);
  162. entry.setObject(enclosure);
  163. }
  164. feed.add(0, entry);
  165. String feedUri = seedActivityStream(id, feed);
  166. DistributedHashTable.getSingleton().store(id, feedUri,
  167. entry.getPublished());
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. public String seedActivityStream(String userId,
  173. final List<ActivityEntry> feed) throws JSONException, IOException {
  174. final File feedFile = new File(CACHE_FOLDER + File.separator + userId
  175. + ".json");
  176. JSONArray items = new JSONArray();
  177. for (int i = 0; i < feed.size(); ++i) {
  178. JSONObject item = new JSONObject(feed.get(i));
  179. items.put(item);
  180. }
  181. JSONObject db = new JSONObject();
  182. db.put("items", items);
  183. FileWriter writer = new FileWriter(feedFile);
  184. db.write(writer);
  185. writer.close();
  186. String feedUri = sharing.seed(feedFile);
  187. return feedUri;
  188. }
  189. }