Skip to content
Browse files

Merge git://github.com/tomamic/blogracy

  • Loading branch information...
2 parents b8c4957 + 156c307 commit 89b81cd19563c2176929154dffee468d7f65d6df @rik0 rik0 committed Apr 2, 2012
View
92 src/main/java/it/unipr/aotlab/blogracy/Blogracy.java
@@ -27,6 +27,7 @@
import it.unipr.aotlab.blogracy.logging.Logger;
import it.unipr.aotlab.blogracy.model.hashes.Hashes;
import it.unipr.aotlab.blogracy.model.users.User;
+import it.unipr.aotlab.blogracy.util.FileUtils;
import it.unipr.aotlab.blogracy.web.misc.HttpResponseCode;
import it.unipr.aotlab.blogracy.web.resolvers.ErrorPageResolver;
import it.unipr.aotlab.blogracy.web.resolvers.RequestResolver;
@@ -45,6 +46,7 @@
import org.gudy.azureus2.plugins.ddb.DistributedDatabaseListener;
import org.gudy.azureus2.plugins.ddb.DistributedDatabaseValue;
import org.gudy.azureus2.plugins.download.Download;
+import org.gudy.azureus2.plugins.download.DownloadCompletionListener;
import org.gudy.azureus2.plugins.download.DownloadException;
import org.gudy.azureus2.plugins.torrent.Torrent;
import org.gudy.azureus2.plugins.torrent.TorrentException;
@@ -58,12 +60,16 @@
import org.gudy.azureus2.ui.webplugin.WebPlugin;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
+import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
@@ -81,6 +87,7 @@
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.feed.synd.SyndLink;
import com.sun.syndication.feed.synd.SyndLinkImpl;
+import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.SyndFeedOutput;
import com.sun.syndication.io.XmlReader;
@@ -437,22 +444,25 @@ private void initializeURLMapper() throws PluginException {
}
// TODO: probably to move to Network and implementing classes
- public void addDownload(final String fileHash,
+ public Download addDownload(final String fileHash,
final String downloadDirectory,
final String fileName) {
+ Download download = null;
try {
URL magnetURI = new URL("magnet:?xt=urn:btih:" + fileHash);
- addDownload(magnetURI, downloadDirectory, fileName);
+ download = addDownload(magnetURI, downloadDirectory, fileName);
} catch (MalformedURLException e1) {
- Logger.error("Error generating MagnetURI for: " + fileName);
+ Logger.error("Error generating MagnetURI for: " + fileHash);
}
+ return download;
}
// TODO: probably to move to Network and implementing classes
- public void addDownload(final URL magnetURI,
+ public Download addDownload(final URL magnetURI,
final String downloadDirectory,
final String fileName) {
// add magnet-uri to download manager
+ Download download = null;
try {
ResourceDownloader rdl =
plugin.getUtilities()
@@ -461,20 +471,21 @@ public void addDownload(final URL magnetURI,
Torrent torrent =
plugin.getTorrentManager()
.createFromBEncodedInputStream(is);
- Download download = plugin.getDownloadManager().addDownload(
+ download = plugin.getDownloadManager().addDownload(
torrent,
null,
new File(downloadDirectory)
);
- download.renameDownload(fileName);
- Logger.info(fileName + " added to download list!");
+ if (fileName != null) download.renameDownload(fileName);
+ Logger.info(magnetURI + " added to download list");
} catch (ResourceDownloaderException e1) {
- Logger.error("Resource download exception for: " + fileName);
+ Logger.error("Resource download exception for: " + magnetURI);
} catch (TorrentException e1) {
- Logger.error("Torrent exception for: " + fileName);
+ Logger.error("Torrent exception for: " + magnetURI);
} catch (DownloadException e1) {
- Logger.error("Download exception for: " + fileName);
+ Logger.error("Download exception for: " + magnetURI);
}
+ return download;
}
// TODO: unused, remove?
@@ -489,6 +500,40 @@ static String getHashFromMagnetURI(String uri) {
return hash;
}
+ public static boolean checkNewFeed(File newFile, File oldFile) {
+ boolean newer = true;
+ if (oldFile.exists() && oldFile.getName().endsWith(".rss")) {
+ SyndFeed oldFeed = null;
+ SyndFeed newFeed = null;
+ try {
+ oldFeed = new SyndFeedInput().build(new XmlReader(oldFile));
+ newFeed = new SyndFeedInput().build(new XmlReader(newFile));
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (FeedException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ if (newFeed != null || newFeed.getEntries().size() > 0) {
+ if (oldFeed != null && oldFeed.getEntries().size() > 0) {
+ SyndEntry newEntry = (SyndEntry) newFeed.getEntries().get(0);
+ SyndEntry oldEntry = (SyndEntry) oldFeed.getEntries().get(0);
+ if (oldEntry.getPublishedDate().getTime() > newEntry.getPublishedDate().getTime()) {
+ newer = false;
+ }
+ }
+ if (newer) {
+ // verifySignature(newFeed);
+ }
+ } else {
+ newer = false;
+ }
+ }
+ return newer;
+ }
+
// TODO: probably to move to Network and implementing classes
public void addIndirectDownload(final String key,
final String downloadDirectory,
@@ -508,7 +553,17 @@ public void event(DistributedDatabaseEvent event) {
try {
String value = (String) event.getValue().getValue(String.class);
URL magnetURI = new URL(value);
- addDownload(magnetURI, downloadDirectory, fileName);
+ Download download = addDownload(magnetURI, downloadDirectory, null);
+ download.addCompletionListener(new DownloadCompletionListener() {
+ @Override
+ public void onCompletion(Download download) {
+ File newFile = new File(download.getSavePath() + File.separator + download.getName());
+ File oldFile = new File(downloadDirectory + File.separator + fileName);
+ if (checkNewFeed(newFile, oldFile)) {
+ FileUtils.copyFile(newFile, oldFile);
+ }
+ }
+ });
} catch (MalformedURLException e1) {
Logger.error("Error retrieving a value " +
"from the DDB: " + key);
@@ -519,11 +574,10 @@ public void event(DistributedDatabaseEvent event) {
}
}
},
- ddb.createKey(
- key.getBytes()),
- TIMEOUT,
- DistributedDatabase.OP_EXHAUSTIVE_READ
- );
+ ddb.createKey(key),
+ TIMEOUT,
+ DistributedDatabase.OP_EXHAUSTIVE_READ
+ );
} catch (DistributedDatabaseException e) {
Logger.error("Problem reading from the DDB");
}
@@ -613,7 +667,7 @@ public SyndFeed getFeed(User user) {
return feed;
}
- public void updateFeed(User user, URL uri, String text, URL attachment) {
+ public void addFeedEntry(User user, URL uri, String text, URL attachment) {
try {
SyndFeed feed = getFeed(user);
@@ -646,8 +700,11 @@ public void updateFeed(User user, URL uri, String text, URL attachment) {
String folder = Configurations.getPathConfig().getCachedFilesDirectoryPath();
File feedFile = new File(folder + File.separator + user.getHash().getPrintableValue() + ".rss");
new SyndFeedOutput().output(feed, new PrintWriter(feedFile));
+ // TODO: sign feedFile
URL feedUri = shareFile(feedFile);
+
+ // create a copy of latest feed, named after its author
new SyndFeedOutput().output(feed, new PrintWriter(feedFile));
DistributedDatabase ddb = plugin.getDistributedDatabase();
@@ -657,7 +714,6 @@ public void updateFeed(User user, URL uri, String text, URL attachment) {
@Override
public void event(DistributedDatabaseEvent arg0) { }
}, key, new DistributedDatabaseValue[] {value});
- // ddb.put(user, feedUri.toString());
} catch (Exception e) { e.printStackTrace(); }
}
View
41 src/main/java/it/unipr/aotlab/blogracy/util/FileUtils.java
@@ -22,9 +22,13 @@
package it.unipr.aotlab.blogracy.util;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
+import java.nio.channels.FileChannel;
public class FileUtils {
/*
@@ -65,4 +69,41 @@ public static void copyCompletely(Reader input, Writer output)
}
}
+ public static void copyFile(File srcFile, File dstFile) {
+ if (!dstFile.exists()) {
+ try {
+ dstFile.createNewFile();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ FileChannel source = null;
+ FileChannel destination = null;
+ try {
+ source = new FileInputStream(srcFile).getChannel();
+ destination = new FileOutputStream(dstFile).getChannel();
+ destination.transferFrom(source, 0, source.size());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ finally {
+ if (source != null) {
+ try {
+ source.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (destination != null) {
+ try {
+ destination.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
}
View
2 src/main/java/it/unipr/aotlab/blogracy/web/resolvers/MessagesResolver.java
@@ -143,7 +143,7 @@ protected void post(final TrackerWebPageRequest request,
}
if (messageURI != null || attachmentURI != null) {
- Blogracy.getSingleton().updateFeed(user, messageURI, message, attachmentURI);
+ Blogracy.getSingleton().addFeedEntry(user, messageURI, message, attachmentURI);
}
} catch (MultipartStream.MalformedStreamException e) {
View
9 src/main/java/it/unipr/aotlab/blogracy/web/resolvers/UserResolver.java
@@ -41,8 +41,9 @@
private String userName;
private User user;
+ private User localUser;
private SyndFeed feed;
- List<User> friends;
+ List<User> friends;
public UserResolver(String userName) {
this.userName = userName;
@@ -51,8 +52,13 @@ public UserResolver(String userName) {
} else {
user = Users.newUser(Hashes.newHash(userName)); // TODO: remove
}
+ Blogracy.getSingleton().addIndirectDownload(
+ user.getHash().getPrintableValue(),
+ Configurations.getPathConfig().getCachedFilesDirectoryPath(),
+ user.getHash().getPrintableValue() + ".rss");
feed = Blogracy.getSingleton().getFeed(user);
friends = Configurations.getUserConfig().getFriends();
+ localUser = Configurations.getUserConfig().getUser();
}
@Override
@@ -68,6 +74,7 @@ protected void setupContext() {
velocityContext.internalPut("user", user);
velocityContext.internalPut("feed", feed);
velocityContext.internalPut("friends", friends);
+ velocityContext.internalPut("localUser", localUser);
}
@Override
View
17 src/main/resources/templates/GET/user.vm
@@ -180,16 +180,19 @@
</div>
</div>
<div class="span4">
+ <h3>Local user</h3>
+ <ul>
+ <li><a href="http://127.0.0.1:32674/user/$localUser.hash.printableValue">$localUser.localNick</a></li>
+ </ul>
+
<h3>Followers</h3>
<h3>Followees</h3>
- <div class="span4" id="user-friends">
- <ul>
- #foreach( $friend in $friends )
- <li><a href="http://127.0.0.1:32674/user/$friend.hash.printableValue">$friend.localNick</a></li>
- #end
- </ul>
- </div>
+ <ul id="user-friends">
+ #foreach( $friend in $friends )
+ <li><a href="http://127.0.0.1:32674/user/$friend.hash.printableValue">$friend.localNick</a></li>
+ #end
+ </ul>
<h3>Tags</h3>
</div>
</div>

0 comments on commit 89b81cd

Please sign in to comment.
Something went wrong with that request. Please try again.