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 FeedFetcherCache cache = getFeedInfoCache();
112 if (cache != null) {
113 SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl);
114 setRequestHeaders(connection, syndFeedInfo);
115 httpConnection.connect();
116 try {
117 fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, connection);
118
119 if (syndFeedInfo == null) {
120
121 syndFeedInfo = new SyndFeedInfo();
122 retrieveAndCacheFeed(feedUrl, syndFeedInfo, httpConnection);
123 } else {
124
125 int responseCode = httpConnection.getResponseCode();
126 if (responseCode != HttpURLConnection.HTTP_NOT_MODIFIED) {
127
128
129
130
131 retrieveAndCacheFeed(feedUrl, syndFeedInfo, httpConnection);
132 } else {
133
134 fireEvent(FetcherEvent.EVENT_TYPE_FEED_UNCHANGED, connection);
135 }
136 }
137
138 return syndFeedInfo.getSyndFeed();
139 } finally {
140 httpConnection.disconnect();
141 }
142 } else {
143 fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, connection);
144 InputStream inputStream = null;
145 setRequestHeaders(connection, null);
146 httpConnection.connect();
147 try {
148 inputStream = httpConnection.getInputStream();
149 return getSyndFeedFromStream(inputStream, connection);
150 } catch (java.io.IOException e) {
151 handleErrorCodes(((HttpURLConnection) connection).getResponseCode());
152 } finally {
153 if (inputStream != null) {
154 inputStream.close();
155 }
156 httpConnection.disconnect();
157 }
158
159 return null;
160 }
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 XmlReader reader = null;
173 if (connection.getHeaderField("Content-Type") != null) {
174 reader = new XmlReader(is, connection.getHeaderField("Content-Type"), true);
175 } else {
176 reader = new XmlReader(is, true);
177 }
178
179 SyndFeedInput syndFeedInput = new SyndFeedInput();
180 syndFeedInput.setPreserveWireFeed(isPreserveWireFeed());
181
182 return syndFeedInput.build(reader);
183
184 }
185
186 private SyndFeed getSyndFeedFromStream(InputStream inputStream, URLConnection connection) throws IOException, IllegalArgumentException, FeedException {
187 SyndFeed feed = readSyndFeedFromStream(inputStream, connection);
188 fireEvent(FetcherEvent.EVENT_TYPE_FEED_RETRIEVED, connection, feed);
189 return feed;
190 }
191
192 }