001    /**
002     * Copyright (c) 2000-2012 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.HttpUtil;
018    import com.liferay.portal.kernel.util.Time;
019    import com.liferay.portal.kernel.webcache.WebCacheException;
020    import com.liferay.portal.kernel.webcache.WebCacheItem;
021    import com.liferay.portal.util.HttpImpl;
022    import com.liferay.portal.util.PropsValues;
023    
024    import com.sun.syndication.feed.synd.SyndFeed;
025    import com.sun.syndication.io.SyndFeedInput;
026    import com.sun.syndication.io.XmlReader;
027    
028    import org.apache.commons.httpclient.HostConfiguration;
029    import org.apache.commons.httpclient.HttpClient;
030    import org.apache.commons.httpclient.methods.GetMethod;
031    import org.apache.commons.httpclient.params.HttpClientParams;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class RSSWebCacheItem implements WebCacheItem {
037    
038            public RSSWebCacheItem(String url) {
039                    _url = url;
040            }
041    
042            public Object convert(String key) throws WebCacheException {
043                    SyndFeed feed = null;
044    
045                    try {
046    
047                            // com.liferay.portal.kernel.util.HttpUtil will break the connection
048                            // if it spends more than 5 seconds looking up a location. However,
049                            // German umlauts do not get encoded correctly. This may be a bug
050                            // with commons-httpclient or with how FeedParser uses
051                            // java.io.Reader.
052    
053                            // Use http://xml.newsisfree.com/feeds/29/629.xml and
054                            // http://test.domosoft.com/up/RSS to test if German umlauts show
055                            // up correctly.
056    
057                            /*Reader reader = new StringReader(
058                                    new String(HttpUtil.URLtoByteArray(_url)));
059    
060                            channel = FeedParser.parse(builder, reader);*/
061    
062                            HttpImpl httpImpl = (HttpImpl)HttpUtil.getHttp();
063    
064                            HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(
065                                    _url);
066    
067                            HttpClient httpClient = httpImpl.getClient(hostConfiguration);
068    
069                            httpImpl.proxifyState(httpClient.getState(), hostConfiguration);
070    
071                            HttpClientParams httpClientParams = httpClient.getParams();
072    
073                            httpClientParams.setConnectionManagerTimeout(
074                                    PropsValues.RSS_CONNECTION_TIMEOUT);
075                            httpClientParams.setSoTimeout(PropsValues.RSS_CONNECTION_TIMEOUT);
076    
077                            GetMethod getMethod = new GetMethod(_url);
078    
079                            httpClient.executeMethod(hostConfiguration, getMethod);
080    
081                            SyndFeedInput input = new SyndFeedInput();
082    
083                            feed = input.build(
084                                    new XmlReader(getMethod.getResponseBodyAsStream()));
085                    }
086                    catch (Exception e) {
087                            throw new WebCacheException(_url + " " + e.toString());
088                    }
089    
090                    return feed;
091            }
092    
093            public long getRefreshTime() {
094                    return _REFRESH_TIME;
095            }
096    
097            private static final long _REFRESH_TIME = Time.MINUTE * 20;
098    
099            private String _url;
100    
101    }