View Javadoc
1   /**
2    * This file Copyright (c) 2008-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator.importhandler;
35  
36  import org.apache.commons.lang3.StringUtils;
37  import org.apache.commons.lang3.builder.ToStringBuilder;
38  
39  import com.rometools.rome.feed.synd.SyndFeed;
40  
41  /**
42   * A {@link #name named} feed channel that typically belongs to an {@link AggregateFeed}. Knows the {@link #url url} on
43   * which a given RSS or ATOM feed resides and {@link #feed holds} the {@link SyndFeed feed} that was fetched after
44   * successful retrieval by a {@link RSSFeedFetcher}.
45   *
46   * @see AggregateFeed
47   * @see RSSFeedFetcher
48   * @see SyndFeed
49   */
50  public class FeedChannel {
51  
52      private final String name;
53      private final String url;
54      private final String title;
55      private SyndFeed feed;
56  
57      /**
58       * Create a new feed channel from the given <code>name</code>, <code>url</code> and <code>title</code>.
59       *
60       * @param name the name of the feed channel (e.g.: 'channel-0')
61       * @param url the url of the feed channel (e.g.: 'http://robl.jteam.nl/?feed=rss2'
62       * @param title the title of the feed channel (e.g.: 'I'll take the red pill ...')
63       * @throws IllegalArgumentException if the given <code>name</code> or <code>url</code> provided is blank, or
64       * <code>title</code> is <code>null</code>
65       */
66      public FeedChannel(String name, String url, String title) {
67          if (StringUtils.isBlank(name) || StringUtils.isBlank(url)) {
68              throw new IllegalArgumentException("'name' and 'url' must not be blank");
69          }
70          if (title == null) {
71              throw new IllegalArgumentException("'title' must not be null");
72          }
73          this.name = name;
74          this.url = url;
75          this.title = title;
76      }
77  
78      @Override
79      public boolean equals(Object o) {
80          if (this == o) return true;
81          if (o == null || getClass() != o.getClass()) return false;
82  
83          FeedChannel that = (FeedChannel) o;
84  
85          if (feed != null ? !feed.equals(that.feed) : that.feed != null) return false;
86          if (!name.equals(that.name)) return false;
87          if (!title.equals(that.title)) return false;
88          if (!url.equals(that.url)) return false;
89  
90          return true;
91      }
92  
93      @Override
94      public int hashCode() {
95          int result = name.hashCode();
96          result = 31 * result + url.hashCode();
97          result = 31 * result + title.hashCode();
98          result = 31 * result + (feed != null ? feed.hashCode() : 0);
99          return result;
100     }
101 
102     @Override
103     public String toString() {
104         return new ToStringBuilder(this)
105                 .append("name", name)
106                 .append("url", url)
107                 .append("title", title)
108                 .append("feed", feed)
109                 .toString();
110     }
111 
112     //  Helper methods
113 
114     /**
115      * Return <code>true</code> when this feed channel contains a {@link #feed}, <code>false</code> otherwise.
116      *
117      * @return true when this feed channel contains a feed, false otherwise
118      */
119     public boolean hasFeed() {
120         return feed != null;
121     }
122 
123     //  Getters & setters
124 
125     /**
126      * Return the name of the feed channel.
127      *
128      * @return the name of the feed channel
129      */
130     public String getName() {
131         return name;
132     }
133 
134     /**
135      * Return the url which points to the RSS or ATOM feed for this feed channel.
136      *
137      * @return the url which points to the RSS or ATOM feed for this feed channel
138      */
139     public String getUrl() {
140         return url;
141     }
142 
143     /**
144      * Return the title of the feed channel.
145      *
146      * @return the title of the feed channel
147      */
148     public String getTitle() {
149         return title;
150     }
151 
152     /**
153      * Set the feed (fetch result) for this feed channel.
154      *
155      * @param feed the feed (fetch result) for this feed channel (must not be null)
156      * @throws IllegalArgumentException if the given feed is <code>null</code>
157      */
158     public void setFeed(SyndFeed feed) {
159         if (feed == null) {
160             throw new IllegalArgumentException("'feed' must not be null");
161         }
162         this.feed = feed;
163     }
164 
165     /**
166      * Return the feed (fetch result) for this feed channel.
167      *
168      * @return the feed (fetch result) for this feed channel or <code>null</code> if no result was set
169      */
170     public SyndFeed getFeed() {
171         return feed;
172     }
173 
174 }