1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.rss; |
21 |
|
|
22 |
|
import javax.net.ssl.HttpsURLConnection; |
23 |
|
import java.net.SocketTimeoutException; |
24 |
|
import java.net.URLConnection; |
25 |
|
import java.text.MessageFormat; |
26 |
|
|
27 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
28 |
|
import org.xwiki.rendering.macro.rss.RssMacroParameters; |
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
|
31 |
|
import com.sun.syndication.feed.synd.SyndFeed; |
32 |
|
import com.sun.syndication.io.SyndFeedInput; |
33 |
|
import com.sun.syndication.io.XmlReader; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@version |
39 |
|
@since |
40 |
|
|
|
|
| 65.4% |
Uncovered Elements: 9 (26) |
Complexity: 6 |
Complexity Density: 0.32 |
|
41 |
|
public class DefaultRomeFeedFactory implements RomeFeedFactory |
42 |
|
{ |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
private static final int TIMEOUT_MILLISECONDS = 5000; |
47 |
|
private static final String USER_AGENT_HEADER = "User-Agent"; |
48 |
|
private static final String VERSION = DefaultRomeFeedFactory.class.getPackage().getImplementationVersion(); |
49 |
|
private static final String USER_AGENT = "XWiki/" + VERSION; |
50 |
|
|
|
|
| 64% |
Uncovered Elements: 9 (25) |
Complexity: 6 |
Complexity Density: 0.32 |
|
51 |
7 |
@Override... |
52 |
|
public SyndFeed createFeed(RssMacroParameters parameters) throws MacroExecutionException |
53 |
|
{ |
54 |
7 |
if (StringUtils.isEmpty(parameters.getFeed())) { |
55 |
1 |
throw new MacroExecutionException("The required 'feed' parameter is missing"); |
56 |
|
} |
57 |
|
|
58 |
6 |
SyndFeedInput syndFeedInput = new SyndFeedInput(); |
59 |
|
|
60 |
6 |
SyndFeed feed; |
61 |
6 |
try { |
62 |
6 |
if (StringUtils.startsWith(parameters.getFeed().toLowerCase(), "https")) { |
63 |
0 |
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) parameters.getFeedURL().openConnection(); |
64 |
0 |
httpsURLConnection.setConnectTimeout(TIMEOUT_MILLISECONDS); |
65 |
0 |
httpsURLConnection.setRequestProperty(USER_AGENT_HEADER, USER_AGENT); |
66 |
0 |
feed = syndFeedInput.build(new XmlReader(httpsURLConnection)); |
67 |
|
|
68 |
|
} else { |
69 |
6 |
URLConnection httpURLConnection = parameters.getFeedURL().openConnection(); |
70 |
6 |
httpURLConnection.setConnectTimeout(TIMEOUT_MILLISECONDS); |
71 |
6 |
httpURLConnection.setRequestProperty(USER_AGENT_HEADER, USER_AGENT); |
72 |
6 |
feed = syndFeedInput.build(new XmlReader(httpURLConnection)); |
73 |
|
} |
74 |
|
} catch (SocketTimeoutException ex) { |
75 |
0 |
throw new MacroExecutionException(MessageFormat.format("Connection timeout when trying to reach [{0}]", |
76 |
|
parameters.getFeedURL())); |
77 |
|
} catch (Exception ex) { |
78 |
0 |
throw new MacroExecutionException(MessageFormat.format("Error processing [{0}] : {1}", parameters |
79 |
|
.getFeedURL(), ex.getMessage()), ex); |
80 |
|
} |
81 |
6 |
if (feed == null) { |
82 |
0 |
throw new MacroExecutionException(MessageFormat.format("No feed found at [{0}]", |
83 |
|
parameters.getFeedURL())); |
84 |
|
} |
85 |
|
|
86 |
6 |
return feed; |
87 |
|
} |
88 |
|
} |