KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > weblogsping > WeblogsPingPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.weblogsping;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.xmlrpc.XmlRpcClient;
36 import org.apache.xmlrpc.XmlRpcException;
37 import org.blojsom.blog.Blog;
38 import org.blojsom.blog.Entry;
39 import org.blojsom.event.Event;
40 import org.blojsom.event.EventBroadcaster;
41 import org.blojsom.event.Filter;
42 import org.blojsom.event.Listener;
43 import org.blojsom.plugin.Plugin;
44 import org.blojsom.plugin.PluginException;
45 import org.blojsom.plugin.admin.event.EntryAddedEvent;
46 import org.blojsom.plugin.admin.event.EntryDeletedEvent;
47 import org.blojsom.plugin.admin.event.EntryEvent;
48 import org.blojsom.plugin.admin.event.EntryUpdatedEvent;
49 import org.blojsom.util.BlojsomConstants;
50 import org.blojsom.util.BlojsomUtils;
51
52 import javax.servlet.http.HttpServletRequest JavaDoc;
53 import javax.servlet.http.HttpServletResponse JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.net.MalformedURLException JavaDoc;
56 import java.util.Map JavaDoc;
57 import java.util.Vector JavaDoc;
58
59 /**
60  * WeblogsPingPlugin
61  *
62  * @author David Czarnecki
63  * @version $Id: WeblogsPingPlugin.java,v 1.1 2006/03/20 21:31:09 czarneckid Exp $
64  * @since blojsom 3.0
65  */

66 public class WeblogsPingPlugin implements Listener, Plugin {
67
68     private Log _logger = LogFactory.getLog(WeblogsPingPlugin.class);
69
70     private static final String JavaDoc WEBLOGS_PING_METHOD = "weblogUpdates.ping";
71     private static final String JavaDoc WEBLOGS_EXTENDED_PING_METHOD = "weblogUpdates.extendedPing";
72     private static final String JavaDoc DEFAULT_PREFERRED_SYNDICATION_FLAVOR = "rss2";
73
74     public static final String JavaDoc BLOG_PING_URLS_IP = "blog-ping-urls";
75     public static final String JavaDoc NO_PING_WEBLOGS_METADATA = "no-ping-weblogs";
76
77     private EventBroadcaster _eventBroadcaster;
78
79     /**
80      * Default constructor
81      */

82     public WeblogsPingPlugin() {
83     }
84
85     /**
86      * Set the {@link EventBroadcaster} event broadcaster
87      *
88      * @param eventBroadcaster {@link EventBroadcaster}
89      */

90     public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
91         _eventBroadcaster = eventBroadcaster;
92     }
93
94     /**
95      * Initialize this plugin. This method only called when the plugin is instantiated.
96      *
97      * @throws PluginException If there is an error initializing the plugin
98      */

99     public void init() throws PluginException {
100         Filter pingEventFilter = new Filter() {
101             public boolean processEvent(Event event) {
102                 return (event instanceof EntryAddedEvent || event instanceof EntryDeletedEvent || event instanceof EntryUpdatedEvent);
103             }
104         };
105
106         _eventBroadcaster.addListener(this, pingEventFilter);
107     }
108
109     /**
110      * Process the blog entries
111      *
112      * @param httpServletRequest Request
113      * @param httpServletResponse Response
114      * @param blog {@link Blog} instance
115      * @param context Context
116      * @param entries Blog entries retrieved for the particular request
117      * @return Modified set of blog entries
118      * @throws PluginException If there is an error processing the blog entries
119      */

120     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
121         return entries;
122     }
123
124     /**
125      * Handle an event broadcast from another component
126      *
127      * @param event {@link org.blojsom.event.BlojsomEvent} to be handled
128      */

129     public void handleEvent(Event event) {
130         if (event instanceof EntryEvent) {
131             EntryEvent entryEvent = (EntryEvent) event;
132             Blog blog = entryEvent.getBlog();
133             String JavaDoc syndicationURL = blog.getBlogURL();
134
135             // Check for meta-data indicating a ping should not be sent
136
Map JavaDoc metaData = entryEvent.getEntry().getMetaData();
137             if (BlojsomUtils.checkMapForKey(metaData, NO_PING_WEBLOGS_METADATA)) {
138                 return;
139             }
140
141             // Check to see if there is a particular flavor the user wants to send with the extended ping
142
String JavaDoc preferredSyndicationFlavor = blog.getProperty(BlojsomConstants.PREFERRED_SYNDICATION_FLAVOR);
143             if (BlojsomUtils.checkNullOrBlank(preferredSyndicationFlavor)) {
144                 preferredSyndicationFlavor = DEFAULT_PREFERRED_SYNDICATION_FLAVOR;
145             }
146             syndicationURL = syndicationURL + "?flavor=" + preferredSyndicationFlavor;
147
148             // If they are provided, loop through that list of URLs to ping
149
String JavaDoc pingURLsIP = blog.getProperty(BLOG_PING_URLS_IP);
150             String JavaDoc[] pingURLs = BlojsomUtils.parseDelimitedList(pingURLsIP, BlojsomConstants.WHITESPACE);
151             if (pingURLs != null && pingURLs.length > 0) {
152                 Vector JavaDoc params = new Vector JavaDoc();
153                 Vector JavaDoc extendedParams = new Vector JavaDoc();
154                 params.add(blog.getBlogName());
155                 extendedParams.add(blog.getBlogName());
156                 params.add(blog.getBlogURL());
157                 extendedParams.add(blog.getBlogURL());
158                 extendedParams.add(blog.getBlogURL());
159                 extendedParams.add(syndicationURL);
160
161                 for (int i = 0; i < pingURLs.length; i++) {
162                     String JavaDoc pingURL = pingURLs[i];
163                     try {
164                         XmlRpcClient weblogsPingClient = new XmlRpcClient(pingURL);
165                         // Try sending an extended weblogs ping first followed by the normal weblogs ping if failed
166
try {
167                             weblogsPingClient.execute(WEBLOGS_EXTENDED_PING_METHOD, extendedParams);
168                         } catch (XmlRpcException e) {
169                             _logger.error(e);
170                             try {
171                                 weblogsPingClient.execute(WEBLOGS_PING_METHOD, params);
172                             } catch (XmlRpcException e1) {
173                                 if (_logger.isErrorEnabled()) {
174                                     _logger.error(e1);
175                                 }
176                             } catch (IOException JavaDoc e1) {
177                                 if (_logger.isErrorEnabled()) {
178                                     _logger.error(e1);
179                                 }
180                             }
181                         } catch (IOException JavaDoc e) {
182                             if (_logger.isErrorEnabled()) {
183                                 _logger.error(e);
184                             }
185                         }
186                     } catch (MalformedURLException JavaDoc e) {
187                         if (_logger.isErrorEnabled()) {
188                             _logger.error(e);
189                         }
190                     }
191                 }
192
193                 if (_logger.isDebugEnabled()) {
194                     _logger.debug("Pinged notification URLs based on blog entry event");
195                 }
196             } else {
197                 if (_logger.isDebugEnabled()) {
198                     _logger.debug("No ping notification URLs specified");
199                 }
200             }
201         }
202     }
203
204     /**
205      * Process an event from another component
206      *
207      * @param event {@link BlojsomEvent} to be handled
208      */

209     public void processEvent(Event event) {
210     }
211
212     /**
213      * Perform any cleanup for the plugin. Called after {@link #process}.
214      *
215      * @throws PluginException If there is an error performing cleanup for this plugin
216      */

217     public void cleanup() throws PluginException {
218     }
219
220     /**
221      * Called when BlojsomServlet is taken out of service
222      *
223      * @throws PluginException If there is an error in finalizing this plugin
224      */

225     public void destroy() throws PluginException {
226     }
227 }
228
Popular Tags