RSSEnclosurePlugin.java [plain text]
package org.blojsom.plugin.common;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.blojsom.blog.BlogEntry;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.plugin.BlojsomPlugin;
import org.blojsom.plugin.BlojsomPluginException;
import org.blojsom.plugin.admin.event.ProcessBlogEntryEvent;
import org.blojsom.util.BlojsomUtils;
import org.blojsom.event.BlojsomListener;
import org.blojsom.event.BlojsomEvent;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.activation.MimetypesFileTypeMap;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.TreeMap;
import java.util.HashMap;
import java.net.URLEncoder;
public class RSSEnclosurePlugin implements BlojsomPlugin, BlojsomListener {
private Log _logger = LogFactory.getLog(RSSEnclosurePlugin.class);
private BlojsomConfiguration _blojsomConfiguration;
private static final String RSS_ENCLOSURE_TEMPLATE = "org/blojsom/plugin/common/templates/admin-rss-enclosure-attachment.vm";
private static final String RSS_ENCLOSURE_ATTACHMENT = "RSS_ENCLOSURE_ATTACHMENT";
public static final String DEFAULT_MIME_TYPE = "application/octet-stream";
public static final String METADATA_RSS_ENCLOSURE = "rss-enclosure";
public static final String METADATA_ESS_ENCLOSURE_OBJECT = "rss-enclosure-object";
protected static final String MIME_TYPE_XMPEGURL = "audio/x-mpegurl m3u";
protected static final String MIME_TYPE_XMPEG = "audio/x-mpeg mp1 mp2 mp3 mpa mpega";
public RSSEnclosurePlugin() {
}
public void init(ServletConfig servletConfig, BlojsomConfiguration
blojsomConfiguration) throws BlojsomPluginException {
_blojsomConfiguration = blojsomConfiguration;
_blojsomConfiguration.getEventBroadcaster().addListener(this);
_logger.debug("Initialized RSS enclosures plugin");
}
protected void addAdditionalMimeTypes(MimetypesFileTypeMap mimeTypes) {
mimeTypes.addMimeTypes(MIME_TYPE_XMPEGURL);
mimeTypes.addMimeTypes(MIME_TYPE_XMPEG);
}
public BlogEntry[] process(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, BlogUser user, Map context,
BlogEntry[] entries) throws BlojsomPluginException {
for (int i = 0; i < entries.length; i++) {
BlogEntry entry = entries[i];
if (BlojsomUtils.checkMapForKey(entry.getMetaData(),
METADATA_RSS_ENCLOSURE)) {
String enclosureName = BlojsomUtils.getFilenameFromPath((String)
entry.getMetaData().get(METADATA_RSS_ENCLOSURE));
File enclosure = new
File(_blojsomConfiguration.getInstallationDirectory()
+ _blojsomConfiguration.getResourceDirectory() +
user.getId() + "/" + enclosureName);
if (enclosure.exists()) {
_logger.debug("Adding enclosure to entry for file: " + enclosureName);
MimetypesFileTypeMap mimetypesFileTypeMap = new
MimetypesFileTypeMap();
addAdditionalMimeTypes(mimetypesFileTypeMap);
String type =
mimetypesFileTypeMap.getContentType(enclosure);
StringBuffer enclosureElement = new StringBuffer();
String urlName = enclosure.getName();
try {
urlName = URLEncoder.encode(urlName, "UTF-8").replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
_logger.error(e);
}
String url = user.getBlog().getBlogBaseURL() + _blojsomConfiguration.getResourceDirectory() +
user.getId() + "/" + urlName;
enclosureElement.append("<enclosure url=\"");
enclosureElement.append(url);
enclosureElement.append("\" length=\"");
enclosureElement.append(enclosure.length());
enclosureElement.append("\" type=\"");
if (BlojsomUtils.checkNullOrBlank(type)) {
type = DEFAULT_MIME_TYPE;
}
enclosureElement.append(type);
enclosureElement.append("\" />");
RSSEnclosure rssEnclosure = new RSSEnclosure(url, enclosure.length(), type);
entry.getMetaData().put(METADATA_RSS_ENCLOSURE,
enclosureElement.toString());
entry.getMetaData().put(METADATA_ESS_ENCLOSURE_OBJECT, rssEnclosure);
_logger.debug("Added enclosure: " + enclosureElement.toString());
}
}
}
return entries;
}
public void cleanup() throws BlojsomPluginException {
}
public void destroy() throws BlojsomPluginException {
}
public void handleEvent(BlojsomEvent event) {
}
public void processEvent(BlojsomEvent event) {
if (event instanceof ProcessBlogEntryEvent) {
_logger.debug("Handling process blog entry event");
ProcessBlogEntryEvent processBlogEntryEvent = (ProcessBlogEntryEvent) event;
String blogID = processBlogEntryEvent.getBlogUser().getId();
Map templateAdditions = (Map) processBlogEntryEvent.getContext().get("BLOJSOM_TEMPLATE_ADDITIONS");
if (templateAdditions == null) {
templateAdditions = new TreeMap();
}
templateAdditions.put(getClass().getName(), "#parse('" + RSS_ENCLOSURE_TEMPLATE + "')");
processBlogEntryEvent.getContext().put("BLOJSOM_TEMPLATE_ADDITIONS", templateAdditions);
File resourceDirectory = new File(_blojsomConfiguration.getInstallationDirectory() + _blojsomConfiguration.getResourceDirectory() + blogID + "/");
_logger.debug("looking in resources directory: " + _blojsomConfiguration.getInstallationDirectory() + _blojsomConfiguration.getResourceDirectory() + blogID + "/");
Map resourceFilesMap = null;
if (resourceDirectory.exists()) {
File[] resourceFiles = resourceDirectory.listFiles();
if (resourceFiles != null) {
resourceFilesMap = new HashMap(resourceFiles.length);
for (int i = 0; i < resourceFiles.length; i++) {
File resourceFile = resourceFiles[i];
resourceFilesMap.put(resourceFile.getName(), resourceFile.getName());
}
}
} else {
resourceFilesMap = new HashMap();
}
if (processBlogEntryEvent.getBlogEntry() != null) {
String currentEnclosure = (String) processBlogEntryEvent.getBlogEntry().getMetaData().get(METADATA_RSS_ENCLOSURE);
_logger.debug("Current enclosure: " + currentEnclosure);
processBlogEntryEvent.getContext().put(RSS_ENCLOSURE_ATTACHMENT, currentEnclosure);
}
String rssEnclosure = BlojsomUtils.getRequestValue(METADATA_RSS_ENCLOSURE, processBlogEntryEvent.getHttpServletRequest());
if (!BlojsomUtils.checkNullOrBlank(rssEnclosure) && processBlogEntryEvent.getBlogEntry() != null) {
processBlogEntryEvent.getBlogEntry().getMetaData().put(METADATA_RSS_ENCLOSURE, rssEnclosure);
processBlogEntryEvent.getContext().put(RSS_ENCLOSURE_ATTACHMENT, rssEnclosure);
_logger.debug("Added/updated RSS enclosure: " + BlojsomUtils.getFilenameFromPath(rssEnclosure));
}
resourceFilesMap = new TreeMap(resourceFilesMap);
processBlogEntryEvent.getContext().put("PLUGIN_RSS_ENCLOSURE_FILES", resourceFilesMap);
}
}
public class RSSEnclosure {
private String url;
private long length;
private String type;
public RSSEnclosure(String url, long length, String type) {
this.url = url;
this.length = length;
this.type = type;
}
public String getUrl() {
return url;
}
public long getLength() {
return length;
}
public String getType() {
return type;
}
public String getFilename() {
return BlojsomUtils.escapeString(BlojsomUtils.urlDecode(BlojsomUtils.getFilenameFromPath(url)));
}
}
}