FetcherHelperPlugin.java [plain text]
package org.blojsom.plugin.fetcher;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.blojsom.blog.BlogCategory;
import org.blojsom.blog.BlogEntry;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.fetcher.BlojsomFetcher;
import org.blojsom.fetcher.BlojsomFetcherException;
import org.blojsom.plugin.BlojsomPlugin;
import org.blojsom.plugin.BlojsomPluginException;
import org.blojsom.util.BlojsomUtils;
import org.blojsom.util.BlojsomConstants;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
public class FetcherHelperPlugin implements BlojsomPlugin {
private Log _logger = LogFactory.getLog(FetcherHelperPlugin.class);
private static final String BLOJSOM_PLUGIN_FETCHER_HELPER = "BLOJSOM_PLUGIN_FETCHER_HELPER";
protected BlojsomFetcher _fetcher;
public FetcherHelperPlugin() {
}
public void init(ServletConfig servletConfig, BlojsomConfiguration blojsomConfiguration) throws BlojsomPluginException {
String fetcherClassName = blojsomConfiguration.getFetcherClass();
try {
Class fetcherClass = Class.forName(fetcherClassName);
_fetcher = (BlojsomFetcher) fetcherClass.newInstance();
_fetcher.init(servletConfig, blojsomConfiguration);
_logger.info("Added blojsom fetcher: " + fetcherClassName);
} catch (ClassNotFoundException e) {
_logger.error(e);
throw new BlojsomPluginException(e);
} catch (InstantiationException e) {
_logger.error(e);
throw new BlojsomPluginException(e);
} catch (IllegalAccessException e) {
_logger.error(e);
throw new BlojsomPluginException(e);
} catch (BlojsomFetcherException e) {
_logger.error(e);
throw new BlojsomPluginException(e);
}
}
public BlogEntry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlogUser user, Map context, BlogEntry[] entries) throws BlojsomPluginException {
context.put(BLOJSOM_PLUGIN_FETCHER_HELPER, new FetcherHelper(_fetcher, user));
return entries;
}
public void cleanup() throws BlojsomPluginException {
}
public void destroy() throws BlojsomPluginException {
}
public class FetcherHelper {
private BlojsomFetcher _fetcher;
private BlogUser _blog;
public FetcherHelper(BlojsomFetcher fetcher, BlogUser blog) {
_fetcher = fetcher;
_blog = blog;
}
public BlogEntry[] fetchEntriesForCategory(String categoryName, int entriesLimit) {
BlogEntry[] entries = new BlogEntry[0];
categoryName = BlojsomUtils.normalize(categoryName);
BlogCategory category;
category = _fetcher.newBlogCategory();
category.setCategory(categoryName);
category.setCategoryURL(_blog.getBlog().getBlogURL() + BlojsomUtils.removeInitialSlash(categoryName));
Map fetchMap = new HashMap();
fetchMap.put(BlojsomFetcher.FETCHER_CATEGORY, category);
fetchMap.put(BlojsomFetcher.FETCHER_NUM_POSTS_INTEGER, new Integer(entriesLimit));
try {
entries = _fetcher.fetchEntries(fetchMap, _blog);
if (entries != null) {
_logger.debug("Retrieved " + entries.length + " entries from category: " + categoryName);
} else {
_logger.debug("No entries found in category: " + categoryName);
}
} catch (BlojsomFetcherException e) {
_logger.error(e);
}
return entries;
}
public BlogEntry[] fetchEntriesForFlavor(String flavor, int entriesLimit) {
BlogEntry[] entries = new BlogEntry[0];
Map fetchMap = new HashMap();
fetchMap.put(BlojsomFetcher.FETCHER_FLAVOR, flavor);
fetchMap.put(BlojsomFetcher.FETCHER_NUM_POSTS_INTEGER, new Integer(entriesLimit));
try {
entries = _fetcher.fetchEntries(fetchMap, _blog);
if (entries != null) {
_logger.debug("Retrieved " + entries.length + " entries for flavor: " + flavor);
} else {
_logger.debug("No entries found for flavor: " + flavor);
}
} catch (BlojsomFetcherException e) {
_logger.error(e);
}
return entries;
}
public BlogEntry[] fetchPermalink(String categoryName, String permalink) {
BlogEntry[] entries = new BlogEntry[0];
try {
permalink = URLDecoder.decode(permalink, BlojsomConstants.UTF8);
} catch (UnsupportedEncodingException e) {
_logger.error(e);
}
categoryName = BlojsomUtils.normalize(categoryName);
BlogCategory category;
category = _fetcher.newBlogCategory();
category.setCategory(categoryName);
category.setCategoryURL(_blog.getBlog().getBlogURL() + BlojsomUtils.removeInitialSlash(categoryName));
Map fetchMap = new HashMap();
fetchMap.put(BlojsomFetcher.FETCHER_CATEGORY, category);
fetchMap.put(BlojsomFetcher.FETCHER_PERMALINK, permalink);
try {
entries = _fetcher.fetchEntries(fetchMap, _blog);
if (entries != null) {
_logger.debug("Retrieved " + entries.length + " entries from category: " + categoryName);
} else {
_logger.debug("No entries found in category: " + categoryName);
}
} catch (BlojsomFetcherException e) {
_logger.error(e);
}
return entries;
}
}
}