1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro.rss

File DefaultRomeFeedFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

6
19
1
1
88
51
6
0.32
19
1
6

Classes

Class Line # Actions
DefaultRomeFeedFactory 41 19 0% 6 9
0.6538461465.4%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Factory implementation using Rome to return the feed's data.
37    *
38    * @version $Id: 6787f714e3b2ee064bd9adf70fa57a7a391d8cf3 $
39    * @since 1.9
40    */
 
41    public class DefaultRomeFeedFactory implements RomeFeedFactory
42    {
43    /**
44    * The maximum number of milliseconds to wait when inquiring the RSS feed provider.
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   
 
51  7 toggle @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    }