| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.event.blogentry; |
| 33 | |
|
| 34 | |
import net.sourceforge.pebble.api.event.blogentry.BlogEntryEvent; |
| 35 | |
import net.sourceforge.pebble.audit.AuditTrail; |
| 36 | |
import net.sourceforge.pebble.decorator.PubSubHubBubFeedDecorator; |
| 37 | |
import net.sourceforge.pebble.domain.Blog; |
| 38 | |
import net.sourceforge.pebble.domain.BlogEntry; |
| 39 | |
import net.sourceforge.pebble.domain.Category; |
| 40 | |
import net.sourceforge.pebble.domain.Tag; |
| 41 | |
import org.apache.commons.httpclient.HttpClient; |
| 42 | |
import org.apache.commons.httpclient.methods.PostMethod; |
| 43 | |
import org.apache.commons.logging.Log; |
| 44 | |
import org.apache.commons.logging.LogFactory; |
| 45 | |
|
| 46 | |
import javax.servlet.http.HttpServletResponse; |
| 47 | |
import java.io.IOException; |
| 48 | |
import java.util.Arrays; |
| 49 | |
import java.util.Collection; |
| 50 | |
import java.util.Collections; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 0 | public class PubSubHubBubBlogEntryListener extends BlogEntryListenerSupport { |
| 58 | |
private final static String HUB_MODE_PARAM = "hub.mode"; |
| 59 | |
private final static String HUB_URL_PARAM = "hub.url"; |
| 60 | |
private final static String HUB_MODE = "publish"; |
| 61 | |
private final static String ATOM_XML = "atom.xml"; |
| 62 | 0 | private static final Log log = LogFactory.getLog(PubSubHubBubBlogEntryListener.class); |
| 63 | |
|
| 64 | |
@Override |
| 65 | |
public void blogEntryAdded(BlogEntryEvent event) { |
| 66 | 0 | if (event.getBlogEntry().isPublished()) { |
| 67 | 0 | postToHubs(event); |
| 68 | |
} |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
@Override |
| 72 | |
public void blogEntryRemoved(BlogEntryEvent event) { |
| 73 | 0 | if (event.getBlogEntry().isPublished()) { |
| 74 | 0 | postToHubs(event); |
| 75 | |
} |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
@Override |
| 79 | |
public void blogEntryChanged(BlogEntryEvent event) { |
| 80 | 0 | if (event.getBlogEntry().isPublished()) { |
| 81 | 0 | postToHubs(event); |
| 82 | |
} |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
@Override |
| 86 | |
public void blogEntryPublished(BlogEntryEvent event) { |
| 87 | 0 | postToHubs(event); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public void blogEntryUnpublished(BlogEntryEvent event) { |
| 92 | 0 | postToHubs(event); |
| 93 | 0 | } |
| 94 | |
|
| 95 | |
private void postToHubs(BlogEntryEvent event) { |
| 96 | 0 | BlogEntry entry = event.getBlogEntry(); |
| 97 | 0 | Blog blog = entry.getBlog(); |
| 98 | 0 | String blogUrl = blog.getUrl(); |
| 99 | 0 | for (String hub : getHubs(blog)) { |
| 100 | 0 | HttpClient httpClient = new HttpClient(); |
| 101 | 0 | PostMethod method = new PostMethod(hub); |
| 102 | 0 | method.addParameter(HUB_MODE_PARAM, HUB_MODE); |
| 103 | |
|
| 104 | 0 | method.addParameter(HUB_URL_PARAM, blogUrl + ATOM_XML); |
| 105 | 0 | for (Category category : entry.getCategories()) { |
| 106 | 0 | method.addParameter(HUB_URL_PARAM, category.getPermalink() + ATOM_XML); |
| 107 | |
} |
| 108 | 0 | for (Tag tag : entry.getAllTags()) { |
| 109 | 0 | method.addParameter(HUB_URL_PARAM, tag.getPermalink() + ATOM_XML); |
| 110 | |
} |
| 111 | 0 | method.addParameter(HUB_URL_PARAM, blogUrl + "authors/" + entry.getAuthor() + "/" + ATOM_XML); |
| 112 | |
try { |
| 113 | 0 | int rc = httpClient.executeMethod(method); |
| 114 | 0 | if (rc != HttpServletResponse.SC_NO_CONTENT) { |
| 115 | 0 | String errorMessage = "Unexpected response code received from hub: " + hub + " - " + rc + " " + |
| 116 | |
method.getStatusText(); |
| 117 | 0 | log.warn(errorMessage); |
| 118 | 0 | blog.warn(errorMessage); |
| 119 | |
} |
| 120 | 0 | } catch (IOException e) { |
| 121 | 0 | log.error("Error posting to hub: " + hub, e); |
| 122 | 0 | blog.warn("Error publishing to hub: " + hub + ". Message: " + e.getMessage()); |
| 123 | 0 | } |
| 124 | 0 | } |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
private Collection<String> getHubs(Blog blog) { |
| 128 | 0 | String hubs = blog.getPluginProperties().getProperty(PubSubHubBubFeedDecorator.HUBS_PROPERTY); |
| 129 | 0 | if (hubs == null || hubs.length() == 0) { |
| 130 | 0 | return Collections.emptyList(); |
| 131 | |
} |
| 132 | 0 | return Arrays.asList(hubs.split(",")); |
| 133 | |
} |
| 134 | |
|
| 135 | |
} |