001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.rss.util;
016    
017    import com.liferay.portal.kernel.util.Time;
018    import com.liferay.portal.kernel.webcache.WebCacheException;
019    import com.liferay.portal.kernel.webcache.WebCacheItem;
020    
021    import com.sun.syndication.feed.synd.SyndFeed;
022    import com.sun.syndication.io.SyndFeedInput;
023    import com.sun.syndication.io.XmlReader;
024    
025    import java.net.URL;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class RSSWebCacheItem implements WebCacheItem {
031    
032            public RSSWebCacheItem(String url) {
033                    _url = url;
034            }
035    
036            public Object convert(String key) throws WebCacheException {
037                    SyndFeed feed = null;
038    
039                    try {
040    
041                            // com.liferay.portal.kernel.util.HttpUtil will break the connection
042                            // if it spends more than 5 seconds looking up a location. However,
043                            // German umlauts do not get encoded correctly. This may be a bug
044                            // with commons-httpclient or with how FeedParser uses
045                            // java.io.Reader.
046    
047                            // Use http://xml.newsisfree.com/feeds/29/629.xml and
048                            // http://test.domosoft.com/up/RSS to test if German umlauts show
049                            // up correctly.
050    
051                            /*Reader reader = new StringReader(
052                                    new String(HttpUtil.URLtoByteArray(_url)));
053    
054                            channel = FeedParser.parse(builder, reader);*/
055    
056                            SyndFeedInput input = new SyndFeedInput();
057    
058                            feed = input.build(new XmlReader(new URL(_url)));
059                    }
060                    catch (Exception e) {
061                            throw new WebCacheException(_url + " " + e.toString());
062                    }
063    
064                    return feed;
065            }
066    
067            public long getRefreshTime() {
068                    return _REFRESH_TIME;
069            }
070    
071            private static final long _REFRESH_TIME = Time.MINUTE * 20;
072    
073            private String _url;
074    
075    }