File Source: WebloggerRomeFeedFetcher.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.planet.business;
    20  
    21  import java.util.Date;
    22  import java.util.List;
    23  import java.util.Map;
    24  import org.apache.commons.lang.StringUtils;
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27  import org.apache.roller.planet.business.fetcher.FetcherException;
    28  import org.apache.roller.planet.business.fetcher.RomeFeedFetcher;
    29  import org.apache.roller.planet.pojos.Subscription;
    30  import org.apache.roller.planet.pojos.SubscriptionEntry;
    31  import org.apache.roller.weblogger.WebloggerException;
    32  import org.apache.roller.weblogger.business.plugins.PluginManager;
    33  import org.apache.roller.weblogger.business.WebloggerFactory;
    34  import org.apache.roller.weblogger.business.UserManager;
    35  import org.apache.roller.weblogger.business.WeblogManager;
    36  import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
    37  import org.apache.roller.weblogger.pojos.WeblogEntry;
    38  import org.apache.roller.weblogger.pojos.Weblog;
    39  
    40  
    41  /**
    42   * Extends Roller Planet's feed fetcher to fetch local feeds directly from 
    43   * Weblogger so we don't waste time with lots of feed processing.
    44   *
    45   * We expect local feeds to have urls of the style ... weblogger:<blog handle>
    46   */
    47  public class WebloggerRomeFeedFetcher extends RomeFeedFetcher {
    48      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.planet.business.WebloggerRomeFeedFetcher__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    49      private static Log log = LogFactory.getLog(WebloggerRomeFeedFetcher.class); 
    50      
    51      
    52      /**
    53       * Creates a new instance of WebloggerRomeFeedFetcher
    54       */
    55      public WebloggerRomeFeedFetcher() {
                 /* 
    P/P           *  Method: void org.apache.roller.weblogger.planet.business.WebloggerRomeFeedFetcher()
                  */
    56          super();
    57      }
    58      
    59      
    60      @Override
    61      public Subscription fetchSubscription(String feedURL, Date lastModified)
    62              throws FetcherException {
    63          
                 /* 
    P/P           *  Method: Subscription fetchSubscription(String, Date)
                  * 
                  *  Preconditions:
                  *    feedURL != null
                  *    log != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@138 != null
                  *    org.apache.roller.weblogger.business.UserManager:getWebsiteByHandle(...)@87 != null
                  *    org.apache.roller.weblogger.business.WeblogManager:getWeblogEntries(...)@121 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getPluginManager(...)@136 != null
                  *    org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@105 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  *    new Subscription(fetchSubscription#7) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    lastModified: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:startsWith(...)@70: {1}, {0}
                  *    java.util.Date:after(...)@97: {1}, {0}
                  *    java.util.Iterator:hasNext(...)@138: {0}, {1}
                  *    org.apache.commons.lang.StringUtils:isEmpty(...)@141: {1}, {0}
                  *    org.apache.roller.planet.pojos.Subscription:getLastUpdated(...)@111: Inverse{null}, Addr_Set{null}
                  */
    64          if(feedURL == null) {
    65              throw new IllegalArgumentException("feed url cannot be null");
    66          }
    67          
    68          // we handle special weblogger planet integrated subscriptions which have
    69          // feedURLs defined as ... weblogger:<blog handle>
    70          if(!feedURL.startsWith("weblogger:")) {
    71              log.debug("Feed is remote, letting parent handle it - "+feedURL);            
    72              return super.fetchSubscription(feedURL, lastModified);
    73          }
    74          
    75          // extract blog handle from our special feed url
    76          String weblogHandle = null;
    77          String[] items = feedURL.split(":", 2);
+   78          if(items != null && items.length > 1) {
+   79              weblogHandle = items[1];
    80          }
    81          
    82          log.debug("Handling LOCAL feed - "+feedURL);
    83          
    84          Weblog localWeblog;
    85          try {
    86              UserManager usermgr = WebloggerFactory.getWeblogger().getUserManager();
    87              localWeblog = usermgr.getWebsiteByHandle(weblogHandle);
    88              if (localWeblog == null) {
    89                  throw new FetcherException("Local feed - "+feedURL+" no longer exists in weblogger");
    90              }
    91              
    92          } catch (WebloggerException ex) {
    93              throw new FetcherException("Problem looking up local weblog - "+weblogHandle, ex);
    94          }
    95          
    96          // if weblog hasn't changed since last fetch then bail
    97          if(lastModified != null && !localWeblog.getLastModified().after(lastModified)) {
    98              log.debug("Skipping unmodified LOCAL weblog");
    99              return null;
   100          }
   101          
   102          // build planet subscription from weblog
   103          Subscription newSub = new Subscription();
   104          newSub.setFeedURL(feedURL);
   105          newSub.setSiteURL(WebloggerFactory.getWeblogger().getUrlStrategy().getWeblogURL(localWeblog, null, true));
   106          newSub.setTitle(localWeblog.getName());
   107          newSub.setAuthor(localWeblog.getName());
   108          newSub.setLastUpdated(localWeblog.getLastModified());
   109          
   110          // must have a last updated time
   111          if(newSub.getLastUpdated() == null) {
   112              newSub.setLastUpdated(new Date());
   113          }
   114          
   115          // lookup recent entries from weblog and add them to the subscription
   116          try {
   117              int entryCount = WebloggerRuntimeConfig.getIntProperty("site.newsfeeds.defaultEntries");
   118              
   119              // grab recent entries for this weblog
   120              WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
   121              List<WeblogEntry> entries = wmgr.getWeblogEntries(
   122                      localWeblog,
   123                      null,
   124                      null,                        // startDate
   125                      null,                        // endDate
   126                      null,                        // catName
   127                      null,WeblogEntry.PUBLISHED,   // status
   128                      null,                        // text
   129                      null,                        // sortby (null means pubTime)
   130                      null,
   131                      null,                        // locale
   132                      0,                           // offset
   133                      entryCount);
   134              
   135              // Populate subscription object with new entries
   136              PluginManager ppmgr = WebloggerFactory.getWeblogger().getPluginManager();
   137              Map pagePlugins = ppmgr.getWeblogEntryPlugins(localWeblog);
   138              for ( WeblogEntry rollerEntry : entries ) {
   139                  SubscriptionEntry entry = new SubscriptionEntry();
+  140                  String content = "";
   141                  if (!StringUtils.isEmpty(rollerEntry.getText())) {
   142                      content = rollerEntry.getText();
   143                  } else {
   144                      content = rollerEntry.getSummary();
   145                  }
   146                  content = ppmgr.applyWeblogEntryPlugins(pagePlugins, rollerEntry, content);
   147                  
   148                  entry.setAuthor(rollerEntry.getCreator().getScreenName());
   149                  entry.setTitle(rollerEntry.getTitle());
   150                  entry.setPubTime(rollerEntry.getPubTime());
   151                  entry.setText(content);
   152                  entry.setPermalink(rollerEntry.getPermalink());
   153                  entry.setCategoriesString(rollerEntry.getCategory().getPath());
   154                  
   155                  newSub.addEntry(entry);
   156              }
   157              
   158          } catch (WebloggerException ex) {
   159              throw new FetcherException("Error processing entries for local weblog - "+weblogHandle, ex);
   160          }
   161          
   162          // all done
   163          return newSub;
   164      }
   165          
   166  }








SofCheck Inspector Build Version : 2.18479
WebloggerRomeFeedFetcher.java 2009-Jan-02 14:25:34
WebloggerRomeFeedFetcher.class 2009-Sep-04 03:12:43