1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.feed

File FeedPluginApi.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

44
201
73
1
958
516
104
0.52
2.75
73
1.42

Classes

Class Line # Actions
FeedPluginApi 38 201 0% 104 318
0.00%
 

Contributing tests

This file is covered by 1 test. .

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 com.xpn.xwiki.plugin.feed;
21   
22    import java.io.IOException;
23    import java.util.Collection;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import com.sun.syndication.feed.synd.SyndEntry;
30    import com.sun.syndication.feed.synd.SyndFeed;
31    import com.sun.syndication.feed.synd.SyndImage;
32    import com.xpn.xwiki.XWiki;
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.XWikiException;
35    import com.xpn.xwiki.plugin.PluginApi;
36    import com.xpn.xwiki.web.XWikiRequest;
37   
 
38    public class FeedPluginApi extends PluginApi<FeedPlugin>
39    {
40    private static final String BLOG_POST_CLASS_NAME = "Blog.BlogPostClass";
41   
42    private static final String BLOG_POST_TEMPLATE_NAME = "Blog.BlogPostTemplate";
43   
44    private static final Map<String, Object> BLOG_FIELDS_MAPPING;
45   
46    public static final String FEED_PLUGIN_EXCEPTION = "FeedPluginException";
47   
 
48  0 toggle static {
49  0 BLOG_FIELDS_MAPPING = new HashMap<String, Object>();
50  0 BLOG_FIELDS_MAPPING.put(SyndEntryDocumentSource.FIELD_TITLE, "Blog.BlogPostClass_title");
51  0 BLOG_FIELDS_MAPPING.put(SyndEntryDocumentSource.FIELD_DESCRIPTION, "Blog.BlogPostClass_content");
52  0 BLOG_FIELDS_MAPPING.put(SyndEntryDocumentSource.FIELD_CATEGORIES, "Blog.BlogPostClass_category");
53  0 BLOG_FIELDS_MAPPING.put(SyndEntryDocumentSource.FIELD_PUBLISHED_DATE, "Blog.BlogPostClass_publishDate");
54  0 BLOG_FIELDS_MAPPING.put(SyndEntryDocumentSource.CONTENT_LENGTH, 400);
55    }
56   
 
57  0 toggle public FeedPluginApi(FeedPlugin plugin, XWikiContext context)
58    {
59  0 super(plugin, context);
60    }
61   
62    /**
63    * Return the inner plugin object, if the user has the required programming rights.
64    *
65    * @return The wrapped plugin object.
66    * @deprecated Use {@link PluginApi#getInternalPlugin()}
67    */
 
68  0 toggle @Deprecated
69    public FeedPlugin getPlugin()
70    {
71  0 return super.getInternalPlugin();
72    }
73   
 
74  0 toggle public SyndFeed getFeeds(String sfeeds) throws IOException
75    {
76  0 return getProtectedPlugin().getFeeds(sfeeds, getXWikiContext());
77    }
78   
 
79  0 toggle public SyndFeed getFeeds(String sfeeds, boolean force) throws IOException
80    {
81  0 return getProtectedPlugin().getFeeds(sfeeds, force, getXWikiContext());
82    }
83   
 
84  0 toggle public SyndFeed getFeeds(String sfeeds, boolean ignoreInvalidFeeds, boolean force) throws IOException
85    {
86  0 return getProtectedPlugin().getFeeds(sfeeds, ignoreInvalidFeeds, force, getXWikiContext());
87    }
88   
 
89  0 toggle public SyndFeed getFeed(String sfeed) throws IOException
90    {
91  0 return getProtectedPlugin().getFeed(sfeed, false, getXWikiContext());
92    }
93   
 
94  0 toggle public SyndFeed getFeed(String sfeed, boolean force) throws IOException
95    {
96  0 return getProtectedPlugin().getFeed(sfeed, force, getXWikiContext());
97    }
98   
 
99  0 toggle public SyndFeed getFeed(String sfeed, boolean ignoreInvalidFeeds, boolean force) throws IOException
100    {
101  0 return getProtectedPlugin().getFeed(sfeed, ignoreInvalidFeeds, force, getXWikiContext());
102    }
103   
 
104  0 toggle public int updateFeeds() throws XWikiException
105    {
106  0 return updateFeeds("XWiki.FeedList");
107    }
108   
 
109  0 toggle public int updateFeeds(String feedDoc) throws XWikiException
110    {
111  0 return updateFeeds(feedDoc, true);
112    }
113   
 
114  0 toggle public int updateFeeds(String feedDoc, boolean fullContent) throws XWikiException
115    {
116  0 return updateFeeds(feedDoc, fullContent, true);
117    }
118   
 
119  0 toggle public int updateFeeds(String feedDoc, boolean fullContent, boolean oneDocPerEntry) throws XWikiException
120    {
121  0 if (hasProgrammingRights()) {
122  0 return getProtectedPlugin().updateFeeds(feedDoc, fullContent, oneDocPerEntry, getXWikiContext());
123    } else {
124  0 return -1;
125    }
126    }
127   
 
128  0 toggle public int updateFeeds(String feedDoc, boolean fullContent, boolean oneDocPerEntry, boolean force)
129    throws XWikiException
130    {
131  0 if (hasProgrammingRights()) {
132  0 return getProtectedPlugin().updateFeeds(feedDoc, fullContent, oneDocPerEntry, force, getXWikiContext());
133    } else {
134  0 return -1;
135    }
136    }
137   
 
138  0 toggle public int updateFeeds(String feedDoc, boolean fullContent, boolean oneDocPerEntry, boolean force, String space)
139    throws XWikiException
140    {
141  0 if (hasProgrammingRights()) {
142  0 return getProtectedPlugin().updateFeeds(feedDoc, fullContent, oneDocPerEntry, force, space,
143    getXWikiContext());
144    } else {
145  0 return -1;
146    }
147    }
148   
 
149  0 toggle public int updateFeedsInSpace(String spaceReference, boolean fullContent, boolean force) throws XWikiException
150    {
151  0 if (hasProgrammingRights()) {
152  0 return getProtectedPlugin().updateFeedsInSpace(spaceReference, fullContent, true, force, getXWikiContext());
153    } else {
154  0 return -1;
155    }
156    }
157   
 
158  0 toggle public int updateFeed(String feedname, String feedurl)
159    {
160  0 if (hasProgrammingRights()) {
161  0 return getProtectedPlugin().updateFeed(feedname, feedurl, false, true, getXWikiContext());
162    } else {
163  0 return -1;
164    }
165    }
166   
 
167  0 toggle public int updateFeed(String feedname, String feedurl, boolean fullContent)
168    {
169  0 if (hasProgrammingRights()) {
170  0 return getProtectedPlugin().updateFeed(feedname, feedurl, fullContent, true, getXWikiContext());
171    } else {
172  0 return -1;
173    }
174    }
175   
 
176  0 toggle public int updateFeed(String feedname, String feedurl, boolean fullContent, boolean oneDocPerEntry)
177    {
178  0 if (hasProgrammingRights()) {
179  0 return getProtectedPlugin().updateFeed(feedname, feedurl, fullContent, oneDocPerEntry, getXWikiContext());
180    } else {
181  0 return -1;
182    }
183    }
184   
 
185  0 toggle public int updateFeed(String feedname, String feedurl, boolean fullContent, boolean oneDocPerEntry, boolean force)
186    {
187  0 if (hasProgrammingRights()) {
188  0 return getProtectedPlugin().updateFeed(feedname, feedurl, fullContent, oneDocPerEntry, force,
189    getXWikiContext());
190    } else {
191  0 return -1;
192    }
193    }
194   
 
195  0 toggle public int updateFeed(String feedname, String feedurl, boolean fullContent, boolean oneDocPerEntry, boolean force,
196    String space)
197    {
198  0 if (hasProgrammingRights()) {
199  0 return getProtectedPlugin().updateFeed(feedname, feedurl, fullContent, oneDocPerEntry, force, space,
200    getXWikiContext());
201    } else {
202  0 return -1;
203    }
204    }
205   
 
206  0 toggle public boolean startUpdateFeedsInSpace(String space, int scheduleTimer) throws XWikiException
207    {
208  0 return getProtectedPlugin().startUpdateFeedsInSpace(space, false, scheduleTimer, this.context);
209    }
210   
 
211  0 toggle public boolean startUpdateFeedsInSpace(String space, boolean fullContent, int scheduleTimer) throws XWikiException
212    {
213  0 return getProtectedPlugin().startUpdateFeedsInSpace(space, fullContent, scheduleTimer, this.context);
214    }
215   
 
216  0 toggle public void stopUpdateFeedsInSpace(String space) throws XWikiException
217    {
218  0 getProtectedPlugin().stopUpdateFeedsInSpace(space, this.context);
219    }
220   
 
221  0 toggle public UpdateThread getUpdateThread(String space)
222    {
223  0 return getProtectedPlugin().getUpdateThread(space, this.context);
224    }
225   
 
226  0 toggle public Collection<String> getActiveUpdateThreads()
227    {
228  0 return getProtectedPlugin().getActiveUpdateThreads();
229    }
230   
231    /**
232    * Tries to instantiate a class implementing the {@link SyndEntrySource} interface using the given parameters
233    *
234    * @param className the name of a class implementing {@link SyndEntrySource} interface
235    * @param params constructor parameters
236    * @return a new SyndEntrySource instance
237    */
 
238  0 toggle public SyndEntrySourceApi getSyndEntrySource(String className, Map<String, Object> params)
239    {
240  0 getXWikiContext().remove(FEED_PLUGIN_EXCEPTION);
241  0 try {
242  0 SyndEntrySource source = getProtectedPlugin().getSyndEntrySource(className, params, getXWikiContext());
243  0 return new SyndEntrySourceApi(source, getXWikiContext());
244    } catch (XWikiException e) {
245  0 getXWikiContext().put(FEED_PLUGIN_EXCEPTION, e);
246  0 return null;
247    }
248    }
249   
250    /**
251    * @see #getSyndEntrySource(String, Map)
252    */
 
253  0 toggle public SyndEntrySourceApi getSyndEntrySource(String className)
254    {
255  0 return this.getSyndEntrySource(className, null);
256    }
257   
258    /**
259    * Instantiates the default strategy for converting documents in feed entries.
260    *
261    * @param params strategy parameters
262    * @return a new {@link SyndEntrySourceApi} wrapping a {@link SyndEntryDocumentSource} object
263    */
 
264  0 toggle public SyndEntrySourceApi getSyndEntryDocumentSource(Map<String, Object> params)
265    {
266  0 return this.getSyndEntrySource(SyndEntryDocumentSource.class.getName());
267    }
268   
269    /**
270    * @see #getSyndEntryDocumentSource(Map)
271    */
 
272  0 toggle public SyndEntrySourceApi getSyndEntryDocumentSource()
273    {
274  0 Map<String, Object> params = Collections.emptyMap();
275  0 return getSyndEntryDocumentSource(params);
276    }
277   
278    /**
279    * Instantiates the default strategy for converting articles in feed entries.
280    *
281    * @return a new {@link SyndEntrySourceApi}
282    */
 
283  0 toggle public SyndEntrySourceApi getSyndEntryArticleSource()
284    {
285  0 Map<String, Object> params = Collections.emptyMap();
286  0 return getSyndEntryArticleSource(params);
287    }
288   
289    /**
290    * Instantiates the default strategy for converting articles in feed entries, allowing you to customize it through
291    * parameters.
292    *
293    * @return a new {@link SyndEntrySourceApi}
294    */
 
295  0 toggle public SyndEntrySourceApi getSyndEntryArticleSource(Map<String, Object> params)
296    {
297  0 Map<String, Object> defParams = new HashMap<String, Object>();
298  0 defParams.put(SyndEntryDocumentSource.FIELD_TITLE, "XWiki.ArticleClass_title");
299  0 defParams.put(SyndEntryDocumentSource.FIELD_DESCRIPTION, "XWiki.ArticleClass_content");
300  0 defParams.put(SyndEntryDocumentSource.FIELD_CATEGORIES, "XWiki.ArticleClass_category");
301  0 defParams.put(SyndEntryDocumentSource.CONTENT_LENGTH, 400);
302  0 defParams.putAll(params);
303  0 return this.getSyndEntrySource(SyndEntryDocumentSource.class.getName(), defParams);
304    }
305   
306    /**
307    * Creates an empty feed entry
308    *
309    * @return a new feed entry
310    */
 
311  0 toggle public SyndEntry getFeedEntry()
312    {
313  0 return getProtectedPlugin().getFeedEntry(getXWikiContext());
314    }
315   
316    /**
317    * Creates an empty feed image
318    *
319    * @return a new feed image
320    */
 
321  0 toggle public SyndImage getFeedImage()
322    {
323  0 return getProtectedPlugin().getFeedImage(this.context);
324    }
325   
326    /**
327    * Creates a new feed image having the given properties.
328    *
329    * @param url image URL
330    * @param title image title
331    * @param description image description
332    * @return a new feed image
333    */
 
334  0 toggle public SyndImage getFeedImage(String url, String link, String title, String description)
335    {
336  0 SyndImage image = getFeedImage();
337  0 image.setUrl(url);
338  0 image.setLink(link);
339  0 image.setTitle(title);
340  0 image.setDescription(description);
341  0 return image;
342    }
343   
344    /**
345    * Creates a new instance of the default feed image. The default image file name is taken from the <i>logo</i> skin
346    * preference. If this preference is missing, <i>logo.png</i> is used instead.
347    *
348    * @return a new feed image
349    */
 
350  0 toggle public SyndImage getDefaultFeedImage()
351    {
352    // Currently, getSkinFile method returns relative (internal) URLs. I couldn't find a way to
353    // get the external URL for a skin file. Is this something forbidden? I've noticed that we
354    // actually compute the full URL but we strip it with urlFactory.getURL(url, context). So
355    // what do you think of overloading the getSkinFile method by adding a absoluteURL flag?
356  0 XWiki xwiki = getXWikiContext().getWiki();
357  0 String fileName = xwiki.getSkinPreference("logo", "logo.png", getXWikiContext());
358  0 String url = xwiki.getSkinFile(fileName, getXWikiContext());
359  0 String port = "";
360  0 XWikiRequest request = getXWikiContext().getRequest();
361  0 if (("http".equals(request.getScheme()) && request.getServerPort() != 80)
362    || ("https".equals(request.getScheme()) && request.getServerPort() != 443)) {
363  0 port = ":" + request.getServerPort();
364    }
365  0 url = request.getScheme() + "://" + request.getServerName() + port + url;
366  0 String link = "http://" + request.getServerName();
367  0 return getFeedImage(url, link, "XWiki Logo", "XWiki Logo");
368    }
369   
370    /**
371    * Creates an empty feed
372    *
373    * @return a new feed
374    */
 
375  0 toggle public SyndFeed getFeed()
376    {
377  0 return getProtectedPlugin().getFeed(getXWikiContext());
378    }
379   
380    /**
381    * Computes a new feed from a list of source items and a corresponding strategy for converting them in feed entries
382    *
383    * @param list the list of source items
384    * @param sourceApi the strategy to use for computing feed entries from source items
385    * @param sourceParams strategy parameters
386    * @return a new feed
387    */
 
388  0 toggle public SyndFeed getFeed(List<Object> list, SyndEntrySourceApi sourceApi, Map<String, Object> sourceParams)
389    {
390  0 Map<String, Object> metadata = Collections.emptyMap();
391  0 return getFeed(list, sourceApi, sourceParams, metadata);
392    }
393   
394    /**
395    * Creates a new feed from a list of documents, using the default strategy for converting documents in feed entries.
396    * You can customize this strategy using strategy parameters.
397    *
398    * @param list a list of {@link com.xpn.xwiki.api.Document} objects, {@link com.xpn.xwiki.doc.XWikiDocument} objects or document names
399    * @param params strategy parameters
400    * @return a new feed
401    * @see com.xpn.xwiki.api.Document
402    * @see #getFeed(List, SyndEntrySourceApi, Map)
403    * @see SyndEntryDocumentSource
404    */
 
405  0 toggle public SyndFeed getDocumentFeed(List<Object> list, Map<String, Object> params)
406    {
407  0 Map<String, Object> metadata = Collections.emptyMap();
408  0 return getDocumentFeed(list, params, metadata);
409    }
410   
411    /**
412    * Creates a new feed from a list of articles, using the default strategy for converting articles in feed entries.
413    * By articles we mean any document containing an <code>XWiki.ArticleClass</code> object.
414    *
415    * @param list a list of articles
416    * @return a new feed
417    * @see com.xpn.xwiki.api.Document
418    * @see #getFeed(List, SyndEntrySourceApi, Map)
419    * @see SyndEntrySourceApi
420    */
 
421  0 toggle public SyndFeed getArticleFeed(List<Object> list)
422    {
423  0 Map<String, Object> params = Collections.emptyMap();
424  0 return getArticleFeed(list, params);
425    }
426   
427    /**
428    * Instantiates the default document feed.
429    *
430    * @param list a list of {@link com.xpn.xwiki.api.Document} objects, {@link com.xpn.xwiki.doc.XWikiDocument} objects or document names
431    * @return a new feed
432    * @see #getDocumentFeed(List, Map)
433    */
 
434  0 toggle public SyndFeed getWebFeed(List<Object> list)
435    {
436  0 Map<String, Object> metadata = new HashMap<String, Object>();
437  0 return getWebFeed(list, metadata);
438    }
439   
440    /**
441    * Instantiates the default article feed.
442    *
443    * @param list a list of articles (as document instances or document names)
444    * @return a new feed
445    * @see #getArticleFeed(List)
446    */
 
447  0 toggle public SyndFeed getBlogFeed(List<Object> list)
448    {
449  0 Map<String, Object> params = Collections.emptyMap();
450  0 return getBlogFeed(list, params);
451    }
452   
453    /**
454    * Creates a new feed from the result of an HQL query and a corresponding strategy for converting the retrieved
455    * documents in feed entries.
456    *
457    * @param query the HQL query used for retrieving the documents
458    * @param count the maximum number of documents to retrieve
459    * @param start the start index
460    * @param sourceApi the strategy to use for computing feed entries from source items
461    * @param sourceParams strategy parameters
462    * @return a new feed
463    */
 
464  0 toggle public SyndFeed getFeed(String query, int count, int start, SyndEntrySourceApi sourceApi,
465    Map<String, Object> sourceParams)
466    {
467  0 Map<String, Object> metadata = Collections.emptyMap();
468  0 return getFeed(query, count, start, sourceApi, sourceParams, metadata);
469    }
470   
471    /**
472    * Creates a new feed from the result of an HQL query, using the default strategy for converting documents in feed
473    * entries. You can customize this strategy using strategy parameters.
474    *
475    * @param query the HQL query used for retrieving the documents
476    * @param count the maximum number of documents to retrieve
477    * @param start the start index
478    * @param params strategy parameters
479    * @return a new feed
480    * @see com.xpn.xwiki.api.Document
481    * @see #getFeed(String, int, int, SyndEntrySourceApi, Map)
482    * @see SyndEntryDocumentSource
483    */
 
484  0 toggle public SyndFeed getDocumentFeed(String query, int count, int start, Map<String, Object> params)
485    {
486  0 Map<String, Object> metadata = Collections.emptyMap();
487  0 return getDocumentFeed(query, count, start, params, metadata);
488    }
489   
490    /**
491    * Creates a new feed from the result of an HQL query, using the default strategy for converting articles in feed
492    * entries. By articles we mean any document containing a <code>XWiki.ArticleClass</code> object.
493    *
494    * @param query the HQL query used for retrieving the articles
495    * @param count the maximum number of articles to retrieve
496    * @param start the start index
497    * @return a new feed
498    * @see com.xpn.xwiki.api.Document
499    * @see #getFeed(String, int, int, SyndEntrySourceApi, Map)
500    * @see SyndEntrySourceApi
501    */
 
502  0 toggle public SyndFeed getArticleFeed(String query, int count, int start)
503    {
504  0 Map<String, Object> params = Collections.emptyMap();
505  0 return getArticleFeed(query, count, start, params);
506    }
507   
508    /**
509    * Instantiates the default document feed.
510    *
511    * @param query the HQL query used for retrieving the documents
512    * @param count the maximum number of documents to retrieve
513    * @param start the start index
514    * @return a new feed
515    * @see #getDocumentFeed(String, int, int, Map)
516    */
 
517  0 toggle public SyndFeed getWebFeed(String query, int count, int start)
518    {
519  0 Map<String, Object> metadata = new HashMap<String, Object>();
520  0 return getWebFeed(query, count, start, metadata);
521    }
522   
523    /**
524    * Instantiates the default article feed.
525    *
526    * @param query the HQL query used for retrieving the articles
527    * @param count the maximum number of articles to retrieve
528    * @param start the start index
529    * @return a new feed
530    * @see #getArticleFeed(String, int, int)
531    */
 
532  0 toggle public SyndFeed getBlogFeed(String query, int count, int start)
533    {
534  0 return getBlogFeed(query, count, start, Collections.<String, Object> emptyMap());
535    }
536   
537    /**
538    * Computes a new feed from a list of source items and a corresponding strategy for converting them in feed entries,
539    * filling in the feed meta data.
540    *
541    * @param list the list of source items
542    * @param sourceApi the strategy to use for computing feed entries from source items
543    * @param sourceParams strategy parameters
544    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
545    * @return a new feed
546    */
 
547  0 toggle public SyndFeed getFeed(List<Object> list, SyndEntrySourceApi sourceApi, Map<String, Object> sourceParams,
548    Map<String, Object> metadata)
549    {
550  0 getXWikiContext().remove(FEED_PLUGIN_EXCEPTION);
551  0 try {
552  0 return getProtectedPlugin().getFeed(list, sourceApi.getSyndEntrySource(), sourceParams, metadata,
553    getXWikiContext());
554    } catch (XWikiException e) {
555  0 getXWikiContext().put(FEED_PLUGIN_EXCEPTION, e);
556  0 return null;
557    }
558    }
559   
560    /**
561    * Creates a new feed from a list of documents, using the default strategy for converting documents in feed entries,
562    * filling in the feed meta data. You can customize the default strategy by using strategy parameters.
563    *
564    * @param list a list of {@link com.xpn.xwiki.api.Document} objects, {@link com.xpn.xwiki.doc.XWikiDocument} objects or document names
565    * @param params strategy parameters
566    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
567    * @return a new feed
568    * @see com.xpn.xwiki.api.Document
569    * @see #getFeed(List, SyndEntrySourceApi, Map, Map)
570    * @see SyndEntryDocumentSource
571    */
 
572  0 toggle public SyndFeed getDocumentFeed(List<Object> list, Map<String, Object> params, Map<String, Object> metadata)
573    {
574  0 return getFeed(list, getSyndEntryDocumentSource(), params, metadata);
575    }
576   
577    /**
578    * Creates a new feed from a list of articles, using the default strategy for converting articles in feed entries,
579    * filling in the feed meta data. By articles we mean any document containing an <code>XWiki.ArticleClass</code>
580    * object.
581    *
582    * @param list a list of articles
583    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
584    * @return a new feed
585    * @see com.xpn.xwiki.api.Document
586    * @see #getFeed(List, SyndEntrySourceApi, Map, Map)
587    * @see SyndEntrySourceApi
588    */
 
589  0 toggle public SyndFeed getArticleFeed(List<Object> list, Map<String, Object> metadata)
590    {
591  0 Map<String, Object> params = Collections.emptyMap();
592  0 return getFeed(list, getSyndEntryArticleSource(), params, metadata);
593    }
594   
 
595  0 toggle private static boolean keyHasValue(Map<String, Object> map, String key, Object defaultValue)
596    {
597  0 Object value = map.get(key);
598  0 return value != null && !value.equals(defaultValue);
599    }
600   
 
601  0 toggle private Map<String, Object> fillWebFeedMetadata(Map<String, Object> metadata)
602    {
603    // these strings should be taken from a resource bundle
604  0 String title = "Feed for document changes";
605  0 String description = title;
606  0 if (!keyHasValue(metadata, "title", "")) {
607  0 metadata.put("title", title);
608    }
609  0 if (!keyHasValue(metadata, "description", "")) {
610  0 metadata.put("description", description);
611    }
612  0 return metadata;
613    }
614   
 
615  0 toggle private Map<String, Object> fillBlogFeedMetadata(Map<String, Object> metadata)
616    {
617    // Make sure that we don't have an immutable Map
618  0 Map<String, Object> result = new HashMap<String, Object>(metadata);
619   
620    // these strings should be taken from a resource bundle
621  0 String title = "Personal Wiki Blog";
622  0 String description = title;
623  0 if (!keyHasValue(result, "title", "")) {
624  0 result.put("title", title);
625    }
626  0 if (!keyHasValue(result, "description", "")) {
627  0 result.put("description", description);
628    }
629  0 return result;
630    }
631   
632    /**
633    * Instantiates the default document feed.
634    *
635    * @param list a list of {@link com.xpn.xwiki.api.Document} objects, {@link com.xpn.xwiki.doc.XWikiDocument} objects or document names
636    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
637    * @return a new feed
638    * @see #getDocumentFeed(List, Map)
639    */
 
640  0 toggle public SyndFeed getWebFeed(List<Object> list, Map<String, Object> metadata)
641    {
642  0 Map<String, Object> params = Collections.emptyMap();
643  0 SyndFeed webFeed = getDocumentFeed(list, params, fillWebFeedMetadata(metadata));
644  0 if (webFeed != null) {
645  0 webFeed.setImage(getDefaultFeedImage());
646    }
647  0 return webFeed;
648    }
649   
650    /**
651    * Instantiates the default article feed.
652    *
653    * @param list a list of articles (as document instances or document names)
654    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
655    * @return a new feed
656    * @see #getArticleFeed(List, Map)
657    */
 
658  0 toggle public SyndFeed getBlogFeed(List<Object> list, Map<String, Object> metadata)
659    {
660  0 Map<String, Object> params = Collections.emptyMap();
661  0 SyndFeed blogFeed =
662    getFeed(list, getSyndEntrySource(SyndEntryDocumentSource.class.getName(), BLOG_FIELDS_MAPPING), params,
663    fillBlogFeedMetadata(metadata));
664  0 if (blogFeed != null) {
665  0 blogFeed.setImage(getDefaultFeedImage());
666    }
667  0 return blogFeed;
668    }
669   
670    /**
671    * Creates a new feed from the result of an HQL query and a corresponding strategy for converting the retrieved
672    * documents in feed entries, filling in the feed meta data.
673    *
674    * @param query the HQL query used for retrieving the documents
675    * @param count the maximum number of documents to retrieve
676    * @param start the start index
677    * @param sourceApi the strategy to use for computing feed entries from source items
678    * @param sourceParams strategy parameters
679    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
680    * @return a new feed
681    */
 
682  0 toggle public SyndFeed getFeed(String query, int count, int start, SyndEntrySourceApi sourceApi,
683    Map<String, Object> sourceParams, Map<String, Object> metadata)
684    {
685  0 getXWikiContext().remove(FEED_PLUGIN_EXCEPTION);
686  0 try {
687  0 return getProtectedPlugin().getFeed(query, count, start, sourceApi.getSyndEntrySource(), sourceParams,
688    metadata, getXWikiContext());
689    } catch (XWikiException e) {
690  0 getXWikiContext().put(FEED_PLUGIN_EXCEPTION, e);
691  0 return null;
692    }
693    }
694   
695    /**
696    * Creates a new feed from the result of an HQL query, using the default strategy for converting documents in feed
697    * entries, filling in the feed meta data. You can customize the default strategy by using strategy parameters.
698    *
699    * @param query the HQL query used for retrieving the documents
700    * @param count the maximum number of documents to retrieve
701    * @param start the start index
702    * @param params strategy parameters
703    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
704    * @return a new feed
705    * @see com.xpn.xwiki.api.Document
706    * @see #getFeed(String, int, int, SyndEntrySourceApi, Map, Map)
707    * @see SyndEntryDocumentSource
708    */
 
709  0 toggle public SyndFeed getDocumentFeed(String query, int count, int start, Map<String, Object> params,
710    Map<String, Object> metadata)
711    {
712  0 return getFeed(query, count, start, getSyndEntryDocumentSource(), params, metadata);
713    }
714   
715    /**
716    * Creates a new feed from the result of an HQL query, using the default strategy for converting articles in feed
717    * entries, filling in the feed meta data. By articles we mean any document containing a
718    * <code>XWiki.ArticleClass</code> object.
719    *
720    * @param query the HQL query used for retrieving the articles
721    * @param count the maximum number of articles to retrieve
722    * @param start the start index
723    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
724    * @return a new feed
725    * @see com.xpn.xwiki.api.Document
726    * @see #getFeed(String, int, int, SyndEntrySourceApi, Map, Map)
727    * @see SyndEntrySourceApi
728    */
 
729  0 toggle public SyndFeed getArticleFeed(String query, int count, int start, Map<String, Object> metadata)
730    {
731  0 Map<String, Object> params = Collections.emptyMap();
732  0 return getFeed(query, count, start, getSyndEntryArticleSource(), params, metadata);
733    }
734   
735    /**
736    * Instantiates the default document feed.
737    *
738    * @param query the HQL query used for retrieving the documents
739    * @param count the maximum number of documents to retrieve
740    * @param start the start index
741    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
742    * @return a new feed
743    * @see #getDocumentFeed(String, int, int, Map)
744    */
 
745  0 toggle public SyndFeed getWebFeed(String query, int count, int start, Map<String, Object> metadata)
746    {
747  0 if (query == null) {
748  0 query = "where 1=1 order by doc.date desc";
749    }
750  0 Map<String, Object> params = new HashMap<String, Object>();
751  0 SyndFeed webFeed = getDocumentFeed(query, count, start, params, fillWebFeedMetadata(metadata));
752  0 if (webFeed != null) {
753  0 webFeed.setImage(getDefaultFeedImage());
754    }
755  0 return webFeed;
756    }
757   
758    /**
759    * Instantiates the default article feed.
760    *
761    * @param query the HQL query used for retrieving the articles
762    * @param count the maximum number of articles to retrieve
763    * @param start the start index
764    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
765    * @return a new feed
766    * @see #getArticleFeed(String, int, int, Map)
767    */
 
768  0 toggle public SyndFeed getBlogFeed(String query, int count, int start, Map<String, Object> metadata)
769    {
770  0 return getBlogFeed(query, count, start, null, metadata);
771    }
772   
773    /**
774    * Instantiates the default article feed.
775    *
776    * @param query the HQL query used for retrieving the articles
777    * @param count the maximum number of articles to retrieve
778    * @param start the start index
779    * @param blogPostClassName The name of the Blog Class the data are retrieved from. If null the Default Blog
780    * Application is used.
781    * @param metadata feed meta data (includes the author, description, copyright, encoding, url, title)
782    * @return a new feed
783    * @see #getArticleFeed(String, int, int, Map)
784    */
 
785  0 toggle public SyndFeed getBlogFeed(String query, int count, int start, String blogPostClassName,
786    Map<String, Object> metadata)
787    {
788  0 if (query == null) {
789  0 XWikiRequest request = getXWikiContext().getRequest();
790  0 String category = request.getParameter("category");
791  0 if (category == null || category.equals("")) {
792  0 query =
793    ", BaseObject as obj where obj.name=doc.fullName and obj.className='" + BLOG_POST_CLASS_NAME
794    + "' and obj.name<>'" + BLOG_POST_TEMPLATE_NAME + "' order by doc.creationDate desc";
795    } else {
796  0 query =
797    ", BaseObject as obj, DBStringListProperty as prop join prop.list list where obj.name=doc.fullName and obj.className='"
798    + BLOG_POST_CLASS_NAME + "' and obj.name<>'" + BLOG_POST_TEMPLATE_NAME
799    + "' and obj.id=prop.id.id and prop.id.name='category' and list = '" + category
800    + "' order by doc.creationDate desc";
801    }
802    }
803  0 Map<String, Object> params = Collections.emptyMap();
804  0 Map<String, Object> blogMappings = null;
805  0 if (blogPostClassName == null) {
806  0 blogMappings = BLOG_FIELDS_MAPPING;
807    } else {
808  0 blogMappings = new HashMap<String, Object>();
809  0 blogMappings.put(SyndEntryDocumentSource.FIELD_TITLE, blogPostClassName + "_title");
810  0 blogMappings.put(SyndEntryDocumentSource.FIELD_DESCRIPTION, blogPostClassName + "_content");
811  0 blogMappings.put(SyndEntryDocumentSource.FIELD_CATEGORIES, blogPostClassName + "_category");
812  0 blogMappings.put(SyndEntryDocumentSource.FIELD_PUBLISHED_DATE, blogPostClassName + "_publishDate");
813  0 blogMappings.put(SyndEntryDocumentSource.CONTENT_LENGTH, new Integer(400));
814    }
815  0 SyndFeed blogFeed =
816    getFeed(query, count, start, getSyndEntrySource(SyndEntryDocumentSource.class.getName(), blogMappings),
817    params, fillBlogFeedMetadata(metadata));
818  0 if (blogFeed != null) {
819  0 blogFeed.setImage(getDefaultFeedImage());
820    }
821  0 return blogFeed;
822    }
823   
824    /**
825    * Converts a feed into its string representation using the specified syntax
826    *
827    * @param feed any type of feed, implementing the {@link SyndFeed} interface
828    * @param type the feed type (syntax) to use, <b>null</b> if none. It can be any version of RSS or Atom. Some
829    * possible values are "rss_1.0", "rss_2.0" and "atom_1.0"
830    * @return the string representation of the given feed using the syntax associated with the specified feed type
831    */
 
832  0 toggle public String getFeedOutput(SyndFeed feed, String type)
833    {
834  0 return getProtectedPlugin().getFeedOutput(feed, type, getXWikiContext());
835    }
836   
837    /**
838    * @see #getFeedOutput(SyndFeed, String)
839    * @see #getFeed(List, SyndEntrySourceApi, Map, Map)
840    */
 
841  0 toggle public String getFeedOutput(List<Object> list, SyndEntrySourceApi sourceApi, Map<String, Object> sourceParams,
842    Map<String, Object> metadata, String type)
843    {
844  0 getXWikiContext().remove(FEED_PLUGIN_EXCEPTION);
845  0 try {
846  0 return getProtectedPlugin().getFeedOutput(list, sourceApi.getSyndEntrySource(), sourceParams, metadata,
847    type, getXWikiContext());
848    } catch (XWikiException e) {
849  0 getXWikiContext().put(FEED_PLUGIN_EXCEPTION, e);
850  0 return null;
851    }
852    }
853   
854    /**
855    * @see #getFeedOutput(List, SyndEntrySourceApi, Map, Map, String)
856    * @see SyndEntryDocumentSource
857    */
 
858  0 toggle public String getDocumentFeedOutput(List<Object> list, Map<String, Object> params, Map<String, Object> metadata,
859    String type)
860    {
861  0 return getFeedOutput(list, getSyndEntryDocumentSource(), params, metadata, type);
862    }
863   
864    /**
865    * @see #getFeedOutput(List, SyndEntrySourceApi, Map, Map, String)
866    * @see SyndEntrySourceApi
867    */
 
868  0 toggle public String getArticleFeedOutput(List<Object> list, Map<String, Object> metadata, String type)
869    {
870  0 Map<String, Object> params = Collections.emptyMap();
871  0 return getFeedOutput(list, getSyndEntryArticleSource(), params, metadata, type);
872    }
873   
874    /**
875    * @see #getWebFeed(List, Map)
876    * @see #getFeedOutput(SyndFeed, String)
877    */
 
878  0 toggle public String getWebFeedOutput(List<Object> list, Map<String, Object> metadata, String type)
879    {
880  0 return getFeedOutput(getWebFeed(list, metadata), type);
881    }
882   
883    /**
884    * @see #getBlogFeed(List, Map)
885    * @see #getFeedOutput(SyndFeed, String)
886    */
 
887  0 toggle public String getBlogFeedOutput(List<Object> list, Map<String, Object> metadata, String type)
888    {
889  0 return getFeedOutput(getBlogFeed(list, metadata), type);
890    }
891   
892    /**
893    * @see #getFeedOutput(SyndFeed, String)
894    * @see #getFeed(String, int, int, SyndEntrySourceApi, Map, Map)
895    */
 
896  0 toggle public String getFeedOutput(String query, int count, int start, SyndEntrySourceApi sourceApi,
897    Map<String, Object> sourceParams, Map<String, Object> metadata, String type)
898    {
899  0 getXWikiContext().remove(FEED_PLUGIN_EXCEPTION);
900  0 try {
901  0 return getProtectedPlugin().getFeedOutput(query, count, start, sourceApi.getSyndEntrySource(),
902    sourceParams, metadata, type, getXWikiContext());
903    } catch (XWikiException e) {
904  0 getXWikiContext().put(FEED_PLUGIN_EXCEPTION, e);
905  0 return null;
906    }
907    }
908   
909    /**
910    * @see #getFeedOutput(String, int, int, SyndEntrySourceApi, Map, Map, String)
911    * @see SyndEntryDocumentSource
912    */
 
913  0 toggle public String getDocumentFeedOutput(String query, int count, int start, Map<String, Object> params,
914    Map<String, Object> metadata, String type)
915    {
916  0 return getFeedOutput(query, count, start, getSyndEntryDocumentSource(), params, metadata, type);
917    }
918   
919    /**
920    * @see #getFeedOutput(String, int, int, SyndEntrySourceApi, Map, Map, String)
921    * @see SyndEntrySourceApi
922    */
 
923  0 toggle public String getArticleFeedOutput(String query, int count, int start, Map<String, Object> metadata, String type)
924    {
925  0 Map<String, Object> params = Collections.emptyMap();
926  0 return getFeedOutput(query, count, start, getSyndEntryArticleSource(), params, metadata, type);
927    }
928   
929    /**
930    * @see #getWebFeed(String, int, int, Map)
931    * @see #getFeedOutput(SyndFeed, String)
932    */
 
933  0 toggle public String getWebFeedOutput(String query, int count, int start, Map<String, Object> metadata, String type)
934    {
935  0 return getFeedOutput(getWebFeed(query, count, start, metadata), type);
936    }
937   
938    /**
939    * @see #getBlogFeed(String, int, int, Map)
940    * @see #getFeedOutput(SyndFeed, String)
941    */
 
942  0 toggle public String getBlogFeedOutput(String query, int count, int start, Map<String, Object> metadata, String type)
943    {
944  0 return getFeedOutput(getBlogFeed(query, count, start, metadata), type);
945    }
946   
947    /**
948    * @see #getBlogFeed(String, int, int, Map)
949    * @see #getFeedOutput(SyndFeed, String)
950    */
 
951  0 toggle public String getBlogFeedOutput(String query, int count, int start, String blogPostClassName,
952    Map<String, Object> metadata, String type)
953    {
954  0 SyndFeed feed = getBlogFeed(query, count, start, blogPostClassName, metadata);
955  0 String ret = getFeedOutput(feed, type);
956  0 return ret;
957    }
958    }