]> source.dussan.org Git - gitblit.git/commitdiff
RSS search parameters. Include refs as categories in RSS entries.
authorJames Moger <james.moger@gitblit.com>
Thu, 3 Nov 2011 21:09:35 +0000 (17:09 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 3 Nov 2011 21:09:35 +0000 (17:09 -0400)
src/com/gitblit/RpcServlet.java
src/com/gitblit/SyndicationServlet.java
src/com/gitblit/utils/SyndicationUtils.java
tests/com/gitblit/tests/SyndicationUtilsTest.java

index c7ff53947fbb9128c38497b0a078fd5256353851..068562e80feb082ee0f75946d948321416516a8a 100644 (file)
@@ -232,6 +232,7 @@ public class RpcServlet extends JsonServlet {
                                List<String> keys = new ArrayList<String>();\r
                                keys.add(Keys.web.siteName);\r
                                keys.add(Keys.web.mountParameters);\r
+                               keys.add(Keys.web.syndicationEntries);\r
                                \r
                                if (allowManagement) {\r
                                        // keys necessary for repository and/or user management\r
index 75101799ab05b39e72c2d179508fbfe555c53d3f..af0fab715bab87e2c327fb475b31ae164914f69f 100644 (file)
@@ -18,18 +18,22 @@ package com.gitblit;
 import java.text.MessageFormat;\r
 import java.util.ArrayList;\r
 import java.util.List;\r
+import java.util.Map;\r
 \r
 import javax.servlet.http.HttpServlet;\r
 \r
+import org.eclipse.jgit.lib.ObjectId;\r
 import org.eclipse.jgit.lib.Repository;\r
 import org.eclipse.jgit.revwalk.RevCommit;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
+import com.gitblit.models.RefModel;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.SyndicatedEntryModel;\r
 import com.gitblit.utils.HttpUtils;\r
 import com.gitblit.utils.JGitUtils;\r
+import com.gitblit.utils.JGitUtils.SearchType;\r
 import com.gitblit.utils.StringUtils;\r
 import com.gitblit.utils.SyndicationUtils;\r
 \r
@@ -126,6 +130,14 @@ public class SyndicationServlet extends HttpServlet {
                String repositoryName = url;\r
                String objectId = request.getParameter("h");\r
                String l = request.getParameter("l");\r
+               String searchString = request.getParameter("s");\r
+               SearchType searchType = SearchType.COMMIT;\r
+               if (!StringUtils.isEmpty(request.getParameter("st"))) {\r
+                       SearchType type = SearchType.forName(request.getParameter("st"));\r
+                       if (type != null) {\r
+                               searchType = type;\r
+                       }\r
+               }\r
                int length = GitBlit.getInteger(Keys.web.syndicationEntries, 25);\r
                if (StringUtils.isEmpty(objectId)) {\r
                        objectId = org.eclipse.jgit.lib.Constants.HEAD;\r
@@ -140,7 +152,16 @@ public class SyndicationServlet extends HttpServlet {
                response.setContentType("application/rss+xml; charset=UTF-8");\r
                Repository repository = GitBlit.self().getRepository(repositoryName);\r
                RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);\r
-               List<RevCommit> commits = JGitUtils.getRevLog(repository, objectId, 0, length);\r
+               List<RevCommit> commits;\r
+               if (StringUtils.isEmpty(searchString)) {\r
+                       // standard log/history lookup\r
+                       commits = JGitUtils.getRevLog(repository, objectId, 0, length);\r
+               } else {\r
+                       // repository search\r
+                       commits = JGitUtils.searchRevlogs(repository, objectId, searchString, searchType, 0,\r
+                                       length);\r
+               }\r
+               Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository);\r
                List<SyndicatedEntryModel> entries = new ArrayList<SyndicatedEntryModel>();\r
 \r
                boolean mountParameters = GitBlit.getBoolean(Keys.web.mountParameters, true);\r
@@ -165,6 +186,14 @@ public class SyndicationServlet extends HttpServlet {
                        entry.content = commit.getFullMessage();\r
                        entry.repository = model.name;\r
                        entry.branch = objectId;\r
+                       List<RefModel> refs = allRefs.get(commit.getId());\r
+                       if (refs != null && refs.size() > 0) {\r
+                               List<String> tags = new ArrayList<String>();\r
+                               for (RefModel ref : refs) {\r
+                                       tags.add(ref.getName());\r
+                               }\r
+                               entry.tags = tags;\r
+                       }\r
                        entries.add(entry);\r
                }\r
                String feedLink;\r
index d9d1d84827161020358645df5acae8f45873a065..85d5f8d2c582d4c893ef188326d0ed5c3239f3b5 100644 (file)
@@ -22,11 +22,15 @@ import java.io.OutputStreamWriter;
 import java.net.URLConnection;\r
 import java.text.MessageFormat;\r
 import java.util.ArrayList;\r
+import java.util.Arrays;\r
 import java.util.List;\r
 \r
 import com.gitblit.Constants;\r
 import com.gitblit.GitBlitException;\r
 import com.gitblit.models.SyndicatedEntryModel;\r
+import com.gitblit.utils.JGitUtils.SearchType;\r
+import com.sun.syndication.feed.synd.SyndCategory;\r
+import com.sun.syndication.feed.synd.SyndCategoryImpl;\r
 import com.sun.syndication.feed.synd.SyndContent;\r
 import com.sun.syndication.feed.synd.SyndContentImpl;\r
 import com.sun.syndication.feed.synd.SyndEntry;\r
@@ -84,10 +88,21 @@ public class SyndicationUtils {
                        entry.setLink(entryModel.link);\r
                        entry.setPublishedDate(entryModel.published);\r
 \r
+                       if (entryModel.tags != null && entryModel.tags.size() > 0) {\r
+                               List<SyndCategory> tags = new ArrayList<SyndCategory>();\r
+                               for (String tag : entryModel.tags) {\r
+                                       SyndCategoryImpl cat = new SyndCategoryImpl();\r
+                                       cat.setName(tag);\r
+                                       tags.add(cat);\r
+                               }\r
+                               entry.setCategories(tags);\r
+                       }\r
+\r
                        SyndContent content = new SyndContentImpl();\r
                        content.setType(entryModel.contentType);\r
                        content.setValue(entryModel.content);\r
                        entry.setDescription(content);\r
+                       \r
                        entries.add(entry);\r
                }\r
                feed.setEntries(entries);\r
@@ -125,6 +140,63 @@ public class SyndicationUtils {
                if (!StringUtils.isEmpty(branch)) {\r
                        parameters.add("h=" + branch);\r
                }\r
+               return readFeed(url, parameters, repository, branch, username, password);\r
+       }\r
+\r
+       /**\r
+        * Reads a Gitblit RSS search feed.\r
+        * \r
+        * @param url\r
+        *            the url of the Gitblit server\r
+        * @param repository\r
+        *            the repository name\r
+        * @param fragment\r
+        *            the search fragment\r
+        * @param searchType\r
+        *            the search type (optional, defaults to COMMIT)\r
+        * @param numberOfEntries\r
+        *            the number of entries to retrieve. if <= 0 the server default\r
+        *            is used.\r
+        * @param username\r
+        * @param password\r
+        * @return a list of SyndicationModel entries\r
+        * @throws {@link IOException}\r
+        */\r
+       public static List<SyndicatedEntryModel> readSearchFeed(String url, String repository,\r
+                       String branch, String fragment, SearchType searchType, int numberOfEntries,\r
+                       String username, char[] password) throws IOException {\r
+               // determine parameters\r
+               List<String> parameters = new ArrayList<String>();\r
+               parameters.add("s=" + StringUtils.encodeURL(fragment));\r
+               if (numberOfEntries > 0) {\r
+                       parameters.add("l=" + numberOfEntries);\r
+               }\r
+               if (!StringUtils.isEmpty(branch)) {\r
+                       parameters.add("h=" + branch);\r
+               }\r
+               if (searchType != null) {\r
+                       parameters.add("st=" + searchType.name());\r
+               }\r
+               return readFeed(url, parameters, repository, branch, username, password);\r
+       }\r
+\r
+       /**\r
+        * Reads a Gitblit RSS feed.\r
+        * \r
+        * @param url\r
+        *            the url of the Gitblit server\r
+        * @param parameters\r
+        *            the list of RSS parameters\r
+        * @param repository\r
+        *            the repository name\r
+        * @param username\r
+        * @param password\r
+        * @return a list of SyndicationModel entries\r
+        * @throws {@link IOException}\r
+        */\r
+       private static List<SyndicatedEntryModel> readFeed(String url, List<String> parameters,\r
+                       String repository, String branch, String username, char[] password) throws IOException {\r
+               // build url\r
                StringBuilder sb = new StringBuilder();\r
                sb.append(MessageFormat.format("{0}" + Constants.SYNDICATION_PATH + "{1}", url, repository));\r
                if (parameters.size() > 0) {\r
@@ -140,7 +212,6 @@ public class SyndicationUtils {
                        }\r
                }\r
                String feedUrl = sb.toString();\r
-\r
                URLConnection conn = ConnectionUtils.openReadConnection(feedUrl, username, password);\r
                InputStream is = conn.getInputStream();\r
                SyndFeedInput input = new SyndFeedInput();\r
@@ -163,6 +234,14 @@ public class SyndicationUtils {
                        model.link = entry.getLink();\r
                        model.content = entry.getDescription().getValue();\r
                        model.contentType = entry.getDescription().getType();\r
+                       if (entry.getCategories() != null && entry.getCategories().size() > 0) {\r
+                               List<String> tags = new ArrayList<String>();\r
+                               for (Object p : entry.getCategories()) {\r
+                                       SyndCategory cat = (SyndCategory) p;\r
+                                       tags.add(cat.getName());\r
+                               }\r
+                               model.tags = tags;\r
+                       }\r
                        entries.add(model);\r
                }\r
                return entries;\r
index 64b99bdfbd245410f657a4d7f80d01bfd9ada5ed..98bdd4bff0b74f2d3a15b783e4a343a44dcdfbcb 100644 (file)
@@ -57,4 +57,12 @@ public class SyndicationUtilsTest extends TestCase {
                assertTrue(feed.size() > 0);\r
                assertEquals(5, feed.size());\r
        }\r
+\r
+       public void testSearchFeedRead() throws Exception {\r
+               List<SyndicatedEntryModel> feed = SyndicationUtils.readSearchFeed("https://localhost:8443",\r
+                               "ticgit.git", null, "documentation", null, 5, "admin", "admin".toCharArray());\r
+               assertTrue(feed != null);\r
+               assertTrue(feed.size() > 0);\r
+               assertEquals(2, feed.size());\r
+       }\r
 }
\ No newline at end of file