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.technorati;
031:
032: import org.blojsom.blog.Blog;
033: import org.blojsom.blog.Entry;
034: import org.blojsom.event.Event;
035: import org.blojsom.event.EventBroadcaster;
036: import org.blojsom.event.Listener;
037: import org.blojsom.plugin.PluginException;
038: import org.blojsom.plugin.admin.event.ProcessEntryEvent;
039: import org.blojsom.plugin.velocity.StandaloneVelocityPlugin;
040: import org.blojsom.util.BlojsomUtils;
041:
042: import javax.servlet.http.HttpServletRequest;
043: import javax.servlet.http.HttpServletResponse;
044: import java.text.MessageFormat;
045: import java.util.ArrayList;
046: import java.util.HashMap;
047: import java.util.Map;
048: import java.util.TreeMap;
049:
050: /**
051: * Technorati tags plugin
052: *
053: * @author David Czarnecki
054: * @version $Id: TechnoratiTagsPlugin.java,v 1.4 2007/01/17 02:35:14 czarneckid Exp $
055: * @since blojsom 3.0
056: */
057: public class TechnoratiTagsPlugin extends StandaloneVelocityPlugin
058: implements Listener {
059:
060: private static final String TECHNORATI_TAGS_TEMPLATE = "org/blojsom/plugin/technorati/templates/admin-technorati-tags.vm";
061: private static final String TECHNORATI_TAG_LINK_TEMPLATE = "org/blojsom/plugin/technorati/templates/technorati-tag-link.vm";
062: private static final String TECHNORATI_TAGS = "TECHNORATI_TAGS";
063: private static final String TECHNORATI_TAG_LINKS = "TECHNORATI_TAG_LINKS";
064:
065: private EventBroadcaster _eventBroadcaster;
066:
067: public static final String METADATA_TECHNORATI_TAGS = "technorati-tags";
068:
069: /**
070: * Create a new instance of the Technorati tag plugin
071: */
072: public TechnoratiTagsPlugin() {
073: }
074:
075: /**
076: * Set the {@link org.blojsom.event.EventBroadcaster} event broadcaster
077: *
078: * @param eventBroadcaster {@link org.blojsom.event.EventBroadcaster}
079: */
080: public void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
081: _eventBroadcaster = eventBroadcaster;
082: }
083:
084: /**
085: * Initialize this plugin. This method only called when the plugin is instantiated.
086: *
087: * @throws org.blojsom.plugin.PluginException
088: * If there is an error initializing the plugin
089: */
090: public void init() throws PluginException {
091: super .init();
092:
093: _eventBroadcaster.addListener(this );
094: }
095:
096: /**
097: * Process the blog entries
098: *
099: * @param httpServletRequest Request
100: * @param httpServletResponse Response
101: * @param blog {@link Blog} instance
102: * @param context Context
103: * @param entries Blog entries retrieved for the particular request
104: * @return Modified set of blog entries
105: * @throws PluginException If there is an error processing the blog entries
106: */
107: public Entry[] process(HttpServletRequest httpServletRequest,
108: HttpServletResponse httpServletResponse, Blog blog,
109: Map context, Entry[] entries) throws PluginException {
110: for (int i = 0; i < entries.length; i++) {
111: Entry entry = entries[i];
112: Map entryMetaData = entry.getMetaData();
113:
114: if (BlojsomUtils.checkMapForKey(entryMetaData,
115: METADATA_TECHNORATI_TAGS)) {
116: String[] tags = BlojsomUtils
117: .parseOnlyCommaList((String) entryMetaData
118: .get(METADATA_TECHNORATI_TAGS));
119: if (tags != null && tags.length > 0) {
120: ArrayList tagLinks = new ArrayList(tags.length);
121: String tagLinkTemplate = mergeTemplate(
122: TECHNORATI_TAG_LINK_TEMPLATE, blog,
123: new HashMap());
124: for (int j = 0; j < tags.length; j++) {
125: String tag = tags[j].trim();
126: tag = BlojsomUtils.escapeStringSimple(tag);
127:
128: tagLinks.add(MessageFormat.format(
129: tagLinkTemplate, new String[] { tag }));
130: }
131:
132: entryMetaData.put(TECHNORATI_TAG_LINKS, tagLinks
133: .toArray(new String[tagLinks.size()]));
134: }
135: }
136: }
137:
138: return entries;
139: }
140:
141: /**
142: * Perform any cleanup for the plugin. Called after {@link #process}.
143: *
144: * @throws org.blojsom.plugin.PluginException
145: * If there is an error performing cleanup for this plugin
146: */
147: public void cleanup() throws PluginException {
148: }
149:
150: /**
151: * Called when BlojsomServlet is taken out of service
152: *
153: * @throws org.blojsom.plugin.PluginException
154: * If there is an error in finalizing this plugin
155: */
156: public void destroy() throws PluginException {
157: }
158:
159: /**
160: * Handle an event broadcast from another component
161: *
162: * @param event {@link org.blojsom.event.Event} to be handled
163: */
164: public void handleEvent(Event event) {
165: }
166:
167: /**
168: * Process an event from another component
169: *
170: * @param event {@link org.blojsom.event.Event} to be handled
171: */
172: public void processEvent(Event event) {
173: if (event instanceof ProcessEntryEvent) {
174: ProcessEntryEvent processBlogEntryEvent = (ProcessEntryEvent) event;
175:
176: String technoratiTags = BlojsomUtils.getRequestValue(
177: METADATA_TECHNORATI_TAGS, processBlogEntryEvent
178: .getHttpServletRequest());
179: if (processBlogEntryEvent.getEntry() != null) {
180: String savedTechnoratiTags = (String) processBlogEntryEvent
181: .getEntry().getMetaData().get(
182: METADATA_TECHNORATI_TAGS);
183: if (savedTechnoratiTags != null) {
184: if (technoratiTags == null) {
185: // Request parameter not available, save old set of tags
186: technoratiTags = savedTechnoratiTags;
187: // Request parameter blank, so throw away set of tags
188: } else if ("".equals(technoratiTags.trim())) {
189: technoratiTags = "";
190: }
191: }
192:
193: processBlogEntryEvent.getEntry().getMetaData().put(
194: METADATA_TECHNORATI_TAGS, technoratiTags);
195: }
196:
197: Map context = processBlogEntryEvent.getContext();
198:
199: Map templateAdditions = (Map) processBlogEntryEvent
200: .getContext().get("BLOJSOM_TEMPLATE_ADDITIONS");
201: if (templateAdditions == null) {
202: templateAdditions = new TreeMap();
203: }
204:
205: templateAdditions.put(getClass().getName(), "#parse('"
206: + TECHNORATI_TAGS_TEMPLATE + "')");
207: processBlogEntryEvent.getContext().put(
208: "BLOJSOM_TEMPLATE_ADDITIONS", templateAdditions);
209:
210: context.put(TECHNORATI_TAGS, technoratiTags);
211:
212: if (processBlogEntryEvent.getEntry() != null) {
213: processBlogEntryEvent.getEntry().getMetaData().put(
214: METADATA_TECHNORATI_TAGS, technoratiTags);
215: }
216:
217: processBlogEntryEvent.getContext().put(TECHNORATI_TAGS,
218: technoratiTags);
219: }
220: }
221: }
|