Coverage Report - net.sourceforge.pebble.webservice.UpdateNotificationPingsClient
 
Classes in this File Line Coverage Branch Coverage Complexity
UpdateNotificationPingsClient
47%
8/17
50%
1/2
1.833
UpdateNotificationPingsClient$UpdateNotificationPingsAsyncCallback
0%
0/12
0%
0/4
1.833
 
 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.webservice;
 33  
 
 34  
 import net.sourceforge.pebble.domain.Blog;
 35  
 import net.sourceforge.pebble.util.StringUtils;
 36  
 
 37  
 import org.apache.commons.logging.Log;
 38  
 import org.apache.commons.logging.LogFactory;
 39  
 import org.apache.xmlrpc.AsyncCallback;
 40  
 import org.apache.xmlrpc.XmlRpcClient;
 41  
 
 42  
 import java.io.IOException;
 43  
 import java.net.URL;
 44  
 import java.util.Hashtable;
 45  
 import java.util.Vector;
 46  
 
 47  
 /**
 48  
  * A simple client to ping (notify) sites like weblogs.com when this blog has
 49  
  * been updated.
 50  
  *
 51  
  * @author    Simon Brown
 52  
  */
 53  184
 public class UpdateNotificationPingsClient {
 54  
 
 55  
   /** the log used by this class */
 56  4
   private static Log log = LogFactory.getLog(UpdateNotificationPingsClient.class);
 57  
 
 58  
   /** the name of the method to call via XML-RPC */
 59  
   private static final String WEBLOGS_METHOD_NAME = "weblogUpdates.ping";
 60  
 
 61  
   /**
 62  
    * Sends a weblogUpdates.ping indicating this the specified blog has
 63  
    * recently been updated. This version sends the blog's home URL.
 64  
    *
 65  
    * @param blog    the Blog representing the updated blog
 66  
    * @param sites   the list of sites (URLs) to ping
 67  
    */
 68  
   public void sendUpdateNotificationPing(Blog blog, String[] sites) {
 69  184
     sendUpdateNotificationPing(blog, blog.getUrl(), sites);
 70  184
   }
 71  
 
 72  
   /**
 73  
    * Sends a weblogUpdates.ping indicating this the specified blog has
 74  
    * recently been updated. This version sends an arbitrary URL.
 75  
    *
 76  
    * @param blog    the Blog representing the updated blog
 77  
    * @param url     the URL to send the ping for
 78  
    * @param sites   the list of sites (URLs) to ping
 79  
    */
 80  
   public void sendUpdateNotificationPing(Blog blog, String url, String[] sites) {
 81  
     try {
 82  184
       for (String site : sites) {
 83  0
         log.info("Sending XML-RPC ping to " + site);
 84  0
         blog.info("Sending XML-RPC ping to " + StringUtils.transformHTML(site));
 85  0
         XmlRpcClient xmlrpc = new XmlRpcClient(site);
 86  0
         Vector params = new Vector();
 87  0
         params.addElement(blog.getName());
 88  0
         params.addElement(url);
 89  0
         xmlrpc.executeAsync(WEBLOGS_METHOD_NAME, params, new UpdateNotificationPingsAsyncCallback(blog));
 90  
       }
 91  0
     } catch (IOException ioe) {
 92  0
       log.error(ioe.getMessage(), ioe);
 93  184
     }
 94  184
   }
 95  
 
 96  
   /**
 97  
    * A callback class used to log the result/error message.
 98  
    */
 99  184
   class UpdateNotificationPingsAsyncCallback implements AsyncCallback {
 100  
 
 101  
     private Blog blog;
 102  
 
 103  0
     public UpdateNotificationPingsAsyncCallback(Blog blog) {
 104  0
       this.blog = blog;
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Called if the XML-RPC was successful.
 109  
      *
 110  
      * @param o         the resulting Object
 111  
      * @param url       the original URL
 112  
      * @param method    the original method name
 113  
      */
 114  
     public void handleResult(Object o, URL url, String method) {
 115  0
       Hashtable result = (Hashtable)o;
 116  0
       if (result != null) {
 117  0
         log.info("Result of XML-RPC ping to " + method + " at " + url + " was " + result.get("flerror") + ", " + result.get("message"));
 118  0
         blog.info("Result of XML-RPC ping to " + t(method) + " at " + t(url) + " was " + t(result.get("flerror")) + ", " + t(result.get("message")));
 119  
       }
 120  0
     }
 121  
 
 122  
     /**
 123  
      * Called if the XML-RPC was not successful.
 124  
      *
 125  
      * @param e         the resulting Exception
 126  
      * @param url       the original URL
 127  
      * @param method    the original method name
 128  
      */
 129  
     public void handleError(Exception e, URL url, String method) {
 130  0
       log.error("Exception when calling " + method + " at " + url, e);
 131  0
     }
 132  
 
 133  
     private String t(Object object) {
 134  0
             if(object == null) return null;
 135  0
             return StringUtils.transformHTML(object.toString());
 136  
     }
 137  
   }
 138  
 
 139  
 }