001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.weblogsping;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.xmlrpc.XmlRpcClient;
035: import org.apache.xmlrpc.XmlRpcException;
036: import org.blojsom.blog.Blog;
037: import org.blojsom.blog.Entry;
038: import org.blojsom.event.Event;
039: import org.blojsom.event.EventBroadcaster;
040: import org.blojsom.event.Filter;
041: import org.blojsom.event.Listener;
042: import org.blojsom.plugin.Plugin;
043: import org.blojsom.plugin.PluginException;
044: import org.blojsom.plugin.admin.event.EntryAddedEvent;
045: import org.blojsom.plugin.admin.event.EntryDeletedEvent;
046: import org.blojsom.plugin.admin.event.EntryEvent;
047: import org.blojsom.plugin.admin.event.EntryUpdatedEvent;
048: import org.blojsom.util.BlojsomConstants;
049: import org.blojsom.util.BlojsomUtils;
050:
051: import javax.servlet.http.HttpServletRequest;
052: import javax.servlet.http.HttpServletResponse;
053: import java.io.IOException;
054: import java.net.MalformedURLException;
055: import java.util.Map;
056: import java.util.Vector;
057:
058: /**
059: * WeblogsPingPlugin
060: *
061: * @author David Czarnecki
062: * @version $Id: WeblogsPingPlugin.java,v 1.6 2007/01/17 02:35:15 czarneckid Exp $
063: * @since blojsom 3.0
064: */
065: public class WeblogsPingPlugin implements Listener, Plugin {
066:
067: private Log _logger = LogFactory.getLog(WeblogsPingPlugin.class);
068:
069: private static final String WEBLOGS_PING_METHOD = "weblogUpdates.ping";
070: private static final String WEBLOGS_EXTENDED_PING_METHOD = "weblogUpdates.extendedPing";
071: private static final String DEFAULT_PREFERRED_SYNDICATION_FLAVOR = "rss2";
072:
073: public static final String BLOG_PING_URLS_IP = "blog-ping-urls";
074: public static final String NO_PING_WEBLOGS_METADATA = "no-ping-weblogs";
075: public static final String PLUGIN_WEBLOGS_PING_FEED_URL_IP = "plugin-weblogs-ping-feed-url";
076:
077: private EventBroadcaster _eventBroadcaster;
078:
079: /**
080: * Default constructor
081: */
082: public WeblogsPingPlugin() {
083: }
084:
085: /**
086: * Set the {@link EventBroadcaster} event broadcaster
087: *
088: * @param eventBroadcaster {@link EventBroadcaster}
089: */
090: public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
091: _eventBroadcaster = eventBroadcaster;
092: }
093:
094: /**
095: * Initialize this plugin. This method only called when the plugin is instantiated.
096: *
097: * @throws PluginException If there is an error initializing the plugin
098: */
099: public void init() throws PluginException {
100: Filter pingEventFilter = new Filter() {
101: public boolean processEvent(Event event) {
102: return (event instanceof EntryAddedEvent
103: || event instanceof EntryDeletedEvent || event instanceof EntryUpdatedEvent);
104: }
105: };
106:
107: _eventBroadcaster.addListener(this , pingEventFilter);
108: }
109:
110: /**
111: * Process the blog entries
112: *
113: * @param httpServletRequest Request
114: * @param httpServletResponse Response
115: * @param blog {@link Blog} instance
116: * @param context Context
117: * @param entries Blog entries retrieved for the particular request
118: * @return Modified set of blog entries
119: * @throws PluginException If there is an error processing the blog entries
120: */
121: public Entry[] process(HttpServletRequest httpServletRequest,
122: HttpServletResponse httpServletResponse, Blog blog,
123: Map context, Entry[] entries) throws PluginException {
124: return entries;
125: }
126:
127: /**
128: * Handle an event broadcast from another component
129: *
130: * @param event {@link Event} to be handled
131: */
132: public void handleEvent(Event event) {
133: if (event instanceof EntryEvent) {
134: EntryEvent entryEvent = (EntryEvent) event;
135: Blog blog = entryEvent.getBlog();
136: String syndicationURL = blog.getBlogURL();
137:
138: // Check for meta-data indicating a ping should not be sent
139: Map metaData = entryEvent.getEntry().getMetaData();
140: if (BlojsomUtils.checkMapForKey(metaData,
141: NO_PING_WEBLOGS_METADATA)) {
142: return;
143: }
144:
145: // Check to see if there is a particular feed or flavor the user wants to send with the extended ping
146: if (!BlojsomUtils.checkNullOrBlank(blog
147: .getProperty(PLUGIN_WEBLOGS_PING_FEED_URL_IP))) {
148: syndicationURL = blog
149: .getProperty(PLUGIN_WEBLOGS_PING_FEED_URL_IP);
150: } else {
151: String preferredSyndicationFlavor = blog
152: .getProperty(BlojsomConstants.PREFERRED_SYNDICATION_FLAVOR);
153: if (BlojsomUtils
154: .checkNullOrBlank(preferredSyndicationFlavor)) {
155: preferredSyndicationFlavor = DEFAULT_PREFERRED_SYNDICATION_FLAVOR;
156: }
157:
158: syndicationURL = syndicationURL + "/feed/"
159: + preferredSyndicationFlavor + "/";
160: }
161:
162: // If they are provided, loop through that list of URLs to ping
163: String pingURLsIP = blog.getProperty(BLOG_PING_URLS_IP);
164: String[] pingURLs = BlojsomUtils.parseDelimitedList(
165: pingURLsIP, BlojsomConstants.WHITESPACE);
166: if (pingURLs != null && pingURLs.length > 0) {
167: Vector params = new Vector();
168: Vector extendedParams = new Vector();
169: params.add(blog.getBlogName());
170: extendedParams.add(blog.getBlogName());
171: params.add(blog.getBlogURL());
172: extendedParams.add(blog.getBlogURL());
173: extendedParams.add(blog.getBlogURL());
174: extendedParams.add(syndicationURL);
175:
176: for (int i = 0; i < pingURLs.length; i++) {
177: String pingURL = pingURLs[i].trim();
178: try {
179: XmlRpcClient weblogsPingClient = new XmlRpcClient(
180: pingURL);
181: // Try sending an extended weblogs ping first followed by the normal weblogs ping if failed
182: try {
183: weblogsPingClient.execute(
184: WEBLOGS_EXTENDED_PING_METHOD,
185: extendedParams);
186: } catch (XmlRpcException e) {
187: _logger.error(e);
188: try {
189: weblogsPingClient.execute(
190: WEBLOGS_PING_METHOD, params);
191: } catch (XmlRpcException e1) {
192: if (_logger.isErrorEnabled()) {
193: _logger.error(e1);
194: }
195: } catch (IOException e1) {
196: if (_logger.isErrorEnabled()) {
197: _logger.error(e1);
198: }
199: }
200: } catch (IOException e) {
201: if (_logger.isErrorEnabled()) {
202: _logger.error(e);
203: }
204: }
205: } catch (MalformedURLException e) {
206: if (_logger.isErrorEnabled()) {
207: _logger.error(e);
208: }
209: }
210: }
211:
212: if (_logger.isDebugEnabled()) {
213: _logger
214: .debug("Pinged notification URLs based on blog entry event");
215: }
216: } else {
217: if (_logger.isDebugEnabled()) {
218: _logger
219: .debug("No ping notification URLs specified");
220: }
221: }
222: }
223: }
224:
225: /**
226: * Process an event from another component
227: *
228: * @param event {@link Event} to be handled
229: */
230: public void processEvent(Event event) {
231: }
232:
233: /**
234: * Perform any cleanup for the plugin. Called after {@link #process}.
235: *
236: * @throws PluginException If there is an error performing cleanup for this plugin
237: */
238: public void cleanup() throws PluginException {
239: }
240:
241: /**
242: * Called when BlojsomServlet is taken out of service
243: *
244: * @throws PluginException If there is an error in finalizing this plugin
245: */
246: public void destroy() throws PluginException {
247: }
248: }
|