1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.rssaggregator.importhandler;
35
36 import java.io.BufferedInputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.net.HttpURLConnection;
40 import java.net.URL;
41 import java.net.URLConnection;
42 import java.util.zip.GZIPInputStream;
43
44 import com.sun.syndication.feed.synd.SyndFeed;
45 import com.sun.syndication.fetcher.FetcherEvent;
46 import com.sun.syndication.fetcher.FetcherException;
47 import com.sun.syndication.fetcher.impl.FeedFetcherCache;
48 import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;
49 import com.sun.syndication.fetcher.impl.SyndFeedInfo;
50 import com.sun.syndication.io.FeedException;
51 import com.sun.syndication.io.SyndFeedInput;
52 import com.sun.syndication.io.XmlReader;
53
54
55
56
57
58
59 public class MgnlHttpURLFeedFetcher extends HttpURLFeedFetcher{
60
61
62
63
64 private int readTimeout = 600000;
65
66
67
68
69 private int connectTimeout = 10000;
70
71 public MgnlHttpURLFeedFetcher(FeedFetcherCache feedInfoCache) {
72 super(feedInfoCache);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 @Override
96 public SyndFeed retrieveFeed(URL feedUrl) throws IllegalArgumentException, IOException, FeedException, FetcherException {
97 if (feedUrl == null) {
98 throw new IllegalArgumentException("null is not a valid URL");
99 }
100
101 URLConnection connection = feedUrl.openConnection();
102
103 connection.setConnectTimeout(connectTimeout);
104 connection.setReadTimeout(readTimeout);
105
106 if (!(connection instanceof HttpURLConnection)) {
107 throw new IllegalArgumentException(feedUrl.toExternalForm() + " is not a valid HTTP Url");
108 }
109 HttpURLConnection httpConnection = (HttpURLConnection)connection;
110
111
112 FeedFetcherCache cache = getFeedInfoCache();
113 if (cache != null) {
114 SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl);
115 setRequestHeaders(connection, syndFeedInfo);
116 httpConnection.connect();
117 try {
118 fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, connection);
119
120 if (syndFeedInfo == null) {
121
122 syndFeedInfo = new SyndFeedInfo();
123 retrieveAndCacheFeed(feedUrl, syndFeedInfo, httpConnection);
124 } else {
125
126 int responseCode = httpConnection.getResponseCode();
127 if (responseCode != HttpURLConnection.HTTP_NOT_MODIFIED) {
128
129
130
131
132 retrieveAndCacheFeed(feedUrl, syndFeedInfo, httpConnection);
133 } else {
134
135 fireEvent(FetcherEvent.EVENT_TYPE_FEED_UNCHANGED, connection);
136 }
137 }
138
139 return syndFeedInfo.getSyndFeed();
140 } finally {
141 httpConnection.disconnect();
142 }
143 }
144 fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, connection);
145 InputStream inputStream = null;
146 setRequestHeaders(connection, null);
147 httpConnection.connect();
148 try {
149 inputStream = httpConnection.getInputStream();
150 return getSyndFeedFromStream(inputStream, connection);
151 } catch (java.io.IOException e) {
152 handleErrorCodes(((HttpURLConnection)connection).getResponseCode());
153 } finally {
154 if (inputStream != null) {
155 inputStream.close();
156 }
157 httpConnection.disconnect();
158 }
159
160 return null;
161 }
162
163 private SyndFeed readSyndFeedFromStream(InputStream inputStream, URLConnection connection) throws IOException, IllegalArgumentException, FeedException {
164 BufferedInputStream is;
165 if ("gzip".equalsIgnoreCase(connection.getContentEncoding())) {
166
167 is = new BufferedInputStream(new GZIPInputStream(inputStream));
168 } else {
169 is = new BufferedInputStream(inputStream);
170 }
171
172
173
174
175
176 XmlReader reader = null;
177 if (connection.getHeaderField("Content-Type") != null) {
178 reader = new XmlReader(is, connection.getHeaderField("Content-Type"), true);
179 } else {
180 reader = new XmlReader(is, true);
181 }
182
183 SyndFeedInput syndFeedInput = new SyndFeedInput();
184 syndFeedInput.setPreserveWireFeed(isPreserveWireFeed());
185
186 return syndFeedInput.build(reader);
187
188 }
189
190 private SyndFeed getSyndFeedFromStream(InputStream inputStream, URLConnection connection) throws IOException, IllegalArgumentException, FeedException {
191 SyndFeed feed = readSyndFeedFromStream(inputStream, connection);
192 fireEvent(FetcherEvent.EVENT_TYPE_FEED_RETRIEVED, connection, feed);
193 return feed;
194 }
195
196 }