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