Coverage Report - net.sourceforge.pebble.event.blogentry.PubSubHubBubBlogEntryListener
 
Classes in this File Line Coverage Branch Coverage Complexity
PubSubHubBubBlogEntryListener
0%
0/43
0%
0/18
2.571
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 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
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 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  
  * Listener that posts to a PubSubHubBub Hub when a blog entry is added/published/changed.
 54  
  *
 55  
  * @author James Roper
 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  
       // Add every feed that might have this
 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  
 }