lemval
2012-01-31 1c30dad2115fc513791d8a5b292ad0f7d7b85749
commit | author | age
c22722 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.utils;
17
18 import java.io.IOException;
565ee0 19 import java.io.InputStream;
c22722 20 import java.io.OutputStream;
JM 21 import java.io.OutputStreamWriter;
565ee0 22 import java.net.URLConnection;
c22722 23 import java.text.MessageFormat;
JM 24 import java.util.ArrayList;
25 import java.util.List;
26
8c9a20 27 import com.gitblit.Constants;
4cac0d 28 import com.gitblit.GitBlitException;
ee458f 29 import com.gitblit.models.FeedEntryModel;
9bdb91 30 import com.sun.syndication.feed.synd.SyndCategory;
JM 31 import com.sun.syndication.feed.synd.SyndCategoryImpl;
c22722 32 import com.sun.syndication.feed.synd.SyndContent;
JM 33 import com.sun.syndication.feed.synd.SyndContentImpl;
34 import com.sun.syndication.feed.synd.SyndEntry;
35 import com.sun.syndication.feed.synd.SyndEntryImpl;
36 import com.sun.syndication.feed.synd.SyndFeed;
37 import com.sun.syndication.feed.synd.SyndFeedImpl;
8c9a20 38 import com.sun.syndication.feed.synd.SyndImageImpl;
c22722 39 import com.sun.syndication.io.FeedException;
565ee0 40 import com.sun.syndication.io.SyndFeedInput;
c22722 41 import com.sun.syndication.io.SyndFeedOutput;
565ee0 42 import com.sun.syndication.io.XmlReader;
c22722 43
d9f687 44 /**
JM 45  * Utility class for RSS feeds.
46  * 
47  * @author James Moger
48  * 
49  */
c22722 50 public class SyndicationUtils {
JM 51
d9f687 52     /**
38688b 53      * Outputs an RSS feed of the list of entries to the outputstream.
d9f687 54      * 
JM 55      * @param hostUrl
ec5a88 56      * @param feedLink
d9f687 57      * @param title
JM 58      * @param description
59      * @param repository
38688b 60      * @param entryModels
d9f687 61      * @param os
JM 62      * @throws IOException
63      * @throws FeedException
64      */
ec5a88 65     public static void toRSS(String hostUrl, String feedLink, String title, String description,
ee458f 66             String repository, List<FeedEntryModel> entryModels, OutputStream os)
ec5a88 67             throws IOException, FeedException {
c22722 68
JM 69         SyndFeed feed = new SyndFeedImpl();
8c9a20 70         feed.setFeedType("rss_2.0");
4cac0d 71         feed.setEncoding("UTF-8");
c22722 72         feed.setTitle(title);
ec5a88 73         feed.setLink(feedLink);
c22722 74         feed.setDescription(description);
8c9a20 75         SyndImageImpl image = new SyndImageImpl();
JM 76         image.setTitle(Constants.NAME);
5450d0 77         image.setUrl(hostUrl + "/gitblt_25.png");
8c9a20 78         image.setLink(hostUrl);
JM 79         feed.setImage(image);
c22722 80
JM 81         List<SyndEntry> entries = new ArrayList<SyndEntry>();
ee458f 82         for (FeedEntryModel entryModel : entryModels) {
c22722 83             SyndEntry entry = new SyndEntryImpl();
38688b 84             entry.setTitle(entryModel.title);
JM 85             entry.setAuthor(entryModel.author);
86             entry.setLink(entryModel.link);
87             entry.setPublishedDate(entryModel.published);
c22722 88
9bdb91 89             if (entryModel.tags != null && entryModel.tags.size() > 0) {
JM 90                 List<SyndCategory> tags = new ArrayList<SyndCategory>();
91                 for (String tag : entryModel.tags) {
92                     SyndCategoryImpl cat = new SyndCategoryImpl();
93                     cat.setName(tag);
94                     tags.add(cat);
95                 }
96                 entry.setCategories(tags);
97             }
98
c22722 99             SyndContent content = new SyndContentImpl();
e493cf 100             if (StringUtils.isEmpty(entryModel.contentType)
JM 101                     || entryModel.contentType.equalsIgnoreCase("text/plain")) {
102                 content.setType("text/html");
103                 content.setValue(StringUtils.breakLinesForHtml(entryModel.content));
104             } else {
105                 content.setType(entryModel.contentType);
106                 content.setValue(entryModel.content);
107             }
c22722 108             entry.setDescription(content);
e33b91 109
c22722 110             entries.add(entry);
JM 111         }
112         feed.setEntries(entries);
113
4cac0d 114         OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8");
c22722 115         SyndFeedOutput output = new SyndFeedOutput();
JM 116         output.output(feed, writer);
117         writer.close();
118     }
565ee0 119
JM 120     /**
121      * Reads a Gitblit RSS feed.
122      * 
123      * @param url
124      *            the url of the Gitblit server
125      * @param repository
126      *            the repository name
127      * @param branch
128      *            the branch name (optional)
129      * @param numberOfEntries
130      *            the number of entries to retrieve. if <= 0 the server default
131      *            is used.
e33b91 132      * @param page
JM 133      *            0-indexed. used to paginate the results.
565ee0 134      * @param username
JM 135      * @param password
4cac0d 136      * @return a list of SyndicationModel entries
565ee0 137      * @throws {@link IOException}
JM 138      */
ee458f 139     public static List<FeedEntryModel> readFeed(String url, String repository, String branch,
e33b91 140             int numberOfEntries, int page, String username, char[] password) throws IOException {
c25a1d 141         // build feed url
JM 142         List<String> parameters = new ArrayList<String>();
143         if (numberOfEntries > 0) {
144             parameters.add("l=" + numberOfEntries);
e33b91 145         }
JM 146         if (page > 0) {
147             parameters.add("pg=" + page);
c25a1d 148         }
JM 149         if (!StringUtils.isEmpty(branch)) {
150             parameters.add("h=" + branch);
151         }
9bdb91 152         return readFeed(url, parameters, repository, branch, username, password);
JM 153     }
154
155     /**
156      * Reads a Gitblit RSS search feed.
157      * 
158      * @param url
159      *            the url of the Gitblit server
160      * @param repository
161      *            the repository name
162      * @param fragment
163      *            the search fragment
164      * @param searchType
165      *            the search type (optional, defaults to COMMIT)
166      * @param numberOfEntries
167      *            the number of entries to retrieve. if <= 0 the server default
168      *            is used.
e33b91 169      * @param page
JM 170      *            0-indexed. used to paginate the results.
9bdb91 171      * @param username
JM 172      * @param password
173      * @return a list of SyndicationModel entries
174      * @throws {@link IOException}
175      */
e493cf 176     public static List<FeedEntryModel> readSearchFeed(String url, String repository, String branch,
JM 177             String fragment, Constants.SearchType searchType, int numberOfEntries, int page,
178             String username, char[] password) throws IOException {
9bdb91 179         // determine parameters
JM 180         List<String> parameters = new ArrayList<String>();
181         parameters.add("s=" + StringUtils.encodeURL(fragment));
182         if (numberOfEntries > 0) {
183             parameters.add("l=" + numberOfEntries);
184         }
e33b91 185         if (page > 0) {
JM 186             parameters.add("pg=" + page);
187         }
9bdb91 188         if (!StringUtils.isEmpty(branch)) {
JM 189             parameters.add("h=" + branch);
190         }
191         if (searchType != null) {
192             parameters.add("st=" + searchType.name());
193         }
194         return readFeed(url, parameters, repository, branch, username, password);
195     }
196
197     /**
198      * Reads a Gitblit RSS feed.
199      * 
200      * @param url
201      *            the url of the Gitblit server
202      * @param parameters
203      *            the list of RSS parameters
204      * @param repository
205      *            the repository name
206      * @param username
207      * @param password
208      * @return a list of SyndicationModel entries
209      * @throws {@link IOException}
210      */
ee458f 211     private static List<FeedEntryModel> readFeed(String url, List<String> parameters,
9bdb91 212             String repository, String branch, String username, char[] password) throws IOException {
JM 213         // build url
c25a1d 214         StringBuilder sb = new StringBuilder();
92e2df 215         sb.append(MessageFormat.format("{0}" + Constants.SYNDICATION_PATH + "{1}", url, repository));
c25a1d 216         if (parameters.size() > 0) {
JM 217             boolean first = true;
218             for (String parameter : parameters) {
219                 if (first) {
220                     sb.append('?');
221                     first = false;
222                 } else {
223                     sb.append('&');
224                 }
225                 sb.append(parameter);
565ee0 226             }
JM 227         }
c25a1d 228         String feedUrl = sb.toString();
565ee0 229         URLConnection conn = ConnectionUtils.openReadConnection(feedUrl, username, password);
JM 230         InputStream is = conn.getInputStream();
231         SyndFeedInput input = new SyndFeedInput();
4cac0d 232         SyndFeed feed = null;
JM 233         try {
234             feed = input.build(new XmlReader(is));
235         } catch (FeedException f) {
236             throw new GitBlitException(f);
237         }
565ee0 238         is.close();
ee458f 239         List<FeedEntryModel> entries = new ArrayList<FeedEntryModel>();
4cac0d 240         for (Object o : feed.getEntries()) {
JM 241             SyndEntryImpl entry = (SyndEntryImpl) o;
ee458f 242             FeedEntryModel model = new FeedEntryModel();
4cac0d 243             model.repository = repository;
JM 244             model.branch = branch;
245             model.title = entry.getTitle();
246             model.author = entry.getAuthor();
247             model.published = entry.getPublishedDate();
248             model.link = entry.getLink();
249             model.content = entry.getDescription().getValue();
250             model.contentType = entry.getDescription().getType();
9bdb91 251             if (entry.getCategories() != null && entry.getCategories().size() > 0) {
JM 252                 List<String> tags = new ArrayList<String>();
253                 for (Object p : entry.getCategories()) {
254                     SyndCategory cat = (SyndCategory) p;
255                     tags.add(cat.getName());
256                 }
257                 model.tags = tags;
258             }
4cac0d 259             entries.add(model);
JM 260         }
261         return entries;
565ee0 262     }
c22722 263 }