001
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
037 public class RSSWebCacheItem implements WebCacheItem {
038
039 public RSSWebCacheItem(String url) {
040 _url = url;
041 }
042
043 public Object convert(String key) throws WebCacheException {
044 SyndFeed feed = null;
045
046 try {
047
048
049
050
051
052
053
054
055
056
057
058
062
063 HttpImpl httpImpl = null;
064
065 Object httpObject = HttpUtil.getHttp();
066
067 if (httpObject instanceof DoPrivilegedBean) {
068 DoPrivilegedBean doPrivilegedBean =
069 (DoPrivilegedBean)httpObject;
070
071 httpImpl = (HttpImpl)doPrivilegedBean.getActualBean();
072 }
073 else {
074 httpImpl = (HttpImpl)httpObject;
075 }
076
077 HostConfiguration hostConfiguration = httpImpl.getHostConfiguration(
078 _url);
079
080 HttpClient httpClient = httpImpl.getClient(hostConfiguration);
081
082 httpImpl.proxifyState(httpClient.getState(), hostConfiguration);
083
084 HttpClientParams httpClientParams = httpClient.getParams();
085
086 httpClientParams.setConnectionManagerTimeout(
087 PropsValues.RSS_CONNECTION_TIMEOUT);
088 httpClientParams.setSoTimeout(PropsValues.RSS_CONNECTION_TIMEOUT);
089
090 GetMethod getMethod = new GetMethod(_url);
091
092 httpClient.executeMethod(hostConfiguration, getMethod);
093
094 SyndFeedInput input = new SyndFeedInput();
095
096 feed = input.build(
097 new XmlReader(getMethod.getResponseBodyAsStream()));
098 }
099 catch (Exception e) {
100 throw new WebCacheException(_url + " " + e.toString());
101 }
102
103 return feed;
104 }
105
106 public long getRefreshTime() {
107 return _REFRESH_TIME;
108 }
109
110 private static final long _REFRESH_TIME = Time.MINUTE * 20;
111
112 private String _url;
113
114 }