]> source.dussan.org Git - gitblit.git/commitdiff
Bug fix to rss feed. Unit test for rss feed. Refactor ssl connection.
authorJames Moger <james.moger@gitblit.com>
Fri, 28 Oct 2011 11:48:17 +0000 (07:48 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 28 Oct 2011 11:48:17 +0000 (07:48 -0400)
src/com/gitblit/SyndicationFilter.java
src/com/gitblit/utils/ConnectionUtils.java [new file with mode: 0644]
src/com/gitblit/utils/JsonUtils.java
src/com/gitblit/utils/SyndicationUtils.java
tests/com/gitblit/tests/SyndicationUtilsTest.java

index e0424605c4a34cf313a013c3271d2c6a6282ad46..9c7a86300ed115e0fc8e218cafe387241d551818 100644 (file)
@@ -37,6 +37,9 @@ public class SyndicationFilter extends AccessRestrictionFilter {
         */\r
        @Override\r
        protected String extractRepositoryName(String url) {\r
+               if (url.indexOf('?') > -1) {\r
+                       return url.substring(0, url.indexOf('?'));\r
+               }\r
                return url;\r
        }\r
 \r
diff --git a/src/com/gitblit/utils/ConnectionUtils.java b/src/com/gitblit/utils/ConnectionUtils.java
new file mode 100644 (file)
index 0000000..93693ab
--- /dev/null
@@ -0,0 +1,126 @@
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.utils;\r
+\r
+import java.io.IOException;\r
+import java.net.URL;\r
+import java.net.URLConnection;\r
+import java.security.SecureRandom;\r
+import java.security.cert.CertificateException;\r
+import java.security.cert.X509Certificate;\r
+\r
+import javax.net.ssl.HostnameVerifier;\r
+import javax.net.ssl.HttpsURLConnection;\r
+import javax.net.ssl.SSLContext;\r
+import javax.net.ssl.SSLSession;\r
+import javax.net.ssl.TrustManager;\r
+import javax.net.ssl.X509TrustManager;\r
+\r
+import org.eclipse.jgit.util.Base64;\r
+\r
+/**\r
+ * Utility class for establishing HTTP/HTTPS connections.\r
+ * \r
+ * @author James Moger\r
+ * \r
+ */\r
+public class ConnectionUtils {\r
+\r
+       static final String CHARSET;\r
+\r
+       private static final SSLContext SSL_CONTEXT;\r
+\r
+       private static final DummyHostnameVerifier HOSTNAME_VERIFIER;\r
+\r
+       static {\r
+               SSLContext context = null;\r
+               try {\r
+                       context = SSLContext.getInstance("SSL");\r
+                       context.init(null, new TrustManager[] { new DummyTrustManager() }, new SecureRandom());\r
+               } catch (Throwable t) {\r
+                       t.printStackTrace();\r
+               }\r
+               SSL_CONTEXT = context;\r
+               HOSTNAME_VERIFIER = new DummyHostnameVerifier();\r
+               CHARSET = "UTF-8";\r
+       }\r
+\r
+       public static void setAuthorization(URLConnection conn, String username, char[] password) {\r
+               if (!StringUtils.isEmpty(username) && (password != null && password.length > 0)) {\r
+                       conn.setRequestProperty(\r
+                                       "Authorization",\r
+                                       "Basic "\r
+                                                       + Base64.encodeBytes((username + ":" + new String(password)).getBytes()));\r
+               }\r
+       }\r
+\r
+       public static URLConnection openReadConnection(String url, String username, char[] password)\r
+                       throws IOException {\r
+               URLConnection conn = openConnection(url, username, password);\r
+               conn.setRequestProperty("Accept-Charset", ConnectionUtils.CHARSET);\r
+               return conn;\r
+       }\r
+\r
+       public static URLConnection openConnection(String url, String username, char[] password)\r
+                       throws IOException {\r
+               URL urlObject = new URL(url);\r
+               URLConnection conn = urlObject.openConnection();\r
+               setAuthorization(conn, username, password);\r
+               conn.setUseCaches(false);\r
+               conn.setDoOutput(true);\r
+               if (conn instanceof HttpsURLConnection) {\r
+                       HttpsURLConnection secureConn = (HttpsURLConnection) conn;\r
+                       secureConn.setSSLSocketFactory(SSL_CONTEXT.getSocketFactory());\r
+                       secureConn.setHostnameVerifier(HOSTNAME_VERIFIER);\r
+               }\r
+               return conn;\r
+       }\r
+\r
+       /**\r
+        * DummyTrustManager trusts all certificates.\r
+        * \r
+        * @author James Moger\r
+        */\r
+       private static class DummyTrustManager implements X509TrustManager {\r
+\r
+               @Override\r
+               public void checkClientTrusted(X509Certificate[] certs, String authType)\r
+                               throws CertificateException {\r
+               }\r
+\r
+               @Override\r
+               public void checkServerTrusted(X509Certificate[] certs, String authType)\r
+                               throws CertificateException {\r
+               }\r
+\r
+               @Override\r
+               public X509Certificate[] getAcceptedIssuers() {\r
+                       return null;\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Trusts all hostnames from a certificate, including self-signed certs.\r
+        * \r
+        * @author James Moger\r
+        */\r
+       private static class DummyHostnameVerifier implements HostnameVerifier {\r
+               @Override\r
+               public boolean verify(String hostname, SSLSession session) {\r
+                       return true;\r
+               }\r
+       }\r
+}\r
index 5b53bf46d146a3c24624e2689f8873d9181b7fc6..3cb43eb1bf973deb6bc24684934fd94ac3d87a3a 100644 (file)
@@ -22,11 +22,7 @@ import java.io.InputStreamReader;
 import java.io.OutputStream;\r
 import java.lang.reflect.Type;\r
 import java.net.HttpURLConnection;\r
-import java.net.URL;\r
 import java.net.URLConnection;\r
-import java.security.SecureRandom;\r
-import java.security.cert.CertificateException;\r
-import java.security.cert.X509Certificate;\r
 import java.text.DateFormat;\r
 import java.text.ParseException;\r
 import java.text.SimpleDateFormat;\r
@@ -36,15 +32,6 @@ import java.util.Locale;
 import java.util.Map;\r
 import java.util.TimeZone;\r
 \r
-import javax.net.ssl.HostnameVerifier;\r
-import javax.net.ssl.HttpsURLConnection;\r
-import javax.net.ssl.SSLContext;\r
-import javax.net.ssl.SSLSession;\r
-import javax.net.ssl.TrustManager;\r
-import javax.net.ssl.X509TrustManager;\r
-\r
-import org.eclipse.jgit.util.Base64;\r
-\r
 import com.gitblit.GitBlitException.ForbiddenException;\r
 import com.gitblit.GitBlitException.NotAllowedException;\r
 import com.gitblit.GitBlitException.UnauthorizedException;\r
@@ -70,31 +57,12 @@ import com.google.gson.reflect.TypeToken;
  */\r
 public class JsonUtils {\r
 \r
-       public static final String CHARSET;\r
-\r
        public static final Type REPOSITORIES_TYPE = new TypeToken<Map<String, RepositoryModel>>() {\r
        }.getType();\r
 \r
        public static final Type USERS_TYPE = new TypeToken<Collection<UserModel>>() {\r
        }.getType();\r
 \r
-       private static final SSLContext SSL_CONTEXT;\r
-\r
-       private static final DummyHostnameVerifier HOSTNAME_VERIFIER;\r
-\r
-       static {\r
-               SSLContext context = null;\r
-               try {\r
-                       context = SSLContext.getInstance("SSL");\r
-                       context.init(null, new TrustManager[] { new DummyTrustManager() }, new SecureRandom());\r
-               } catch (Throwable t) {\r
-                       t.printStackTrace();\r
-               }\r
-               SSL_CONTEXT = context;\r
-               HOSTNAME_VERIFIER = new DummyHostnameVerifier();\r
-               CHARSET = "UTF-8";\r
-       }\r
-\r
        /**\r
         * Creates JSON from the specified object.\r
         * \r
@@ -188,20 +156,10 @@ public class JsonUtils {
         */\r
        public static String retrieveJsonString(String url, String username, char[] password)\r
                        throws IOException {\r
-               try {\r
-                       URL urlObject = new URL(url);\r
-                       URLConnection conn = urlObject.openConnection();\r
-                       conn.setRequestProperty("Accept-Charset", CHARSET);\r
-                       setAuthorization(conn, username, password);\r
-                       conn.setUseCaches(false);\r
-                       conn.setDoInput(true);\r
-                       if (conn instanceof HttpsURLConnection) {\r
-                               HttpsURLConnection secureConn = (HttpsURLConnection) conn;\r
-                               secureConn.setSSLSocketFactory(SSL_CONTEXT.getSocketFactory());\r
-                               secureConn.setHostnameVerifier(HOSTNAME_VERIFIER);\r
-                       }\r
+               try {                   \r
+                       URLConnection conn = ConnectionUtils.openReadConnection(url, username, password);\r
                        InputStream is = conn.getInputStream();\r
-                       BufferedReader reader = new BufferedReader(new InputStreamReader(is, CHARSET));\r
+                       BufferedReader reader = new BufferedReader(new InputStreamReader(is, ConnectionUtils.CHARSET));\r
                        StringBuilder json = new StringBuilder();\r
                        char[] buffer = new char[4096];\r
                        int len = 0;\r
@@ -257,19 +215,10 @@ public class JsonUtils {
        public static int sendJsonString(String url, String json, String username, char[] password)\r
                        throws IOException {\r
                try {\r
-                       byte[] jsonBytes = json.getBytes(CHARSET);\r
-                       URL urlObject = new URL(url);\r
-                       URLConnection conn = urlObject.openConnection();\r
-                       conn.setRequestProperty("Content-Type", "text/plain;charset=" + CHARSET);\r
+                       byte[] jsonBytes = json.getBytes(ConnectionUtils.CHARSET);\r
+                       URLConnection conn = ConnectionUtils.openConnection(url, username, password);\r
+                       conn.setRequestProperty("Content-Type", "text/plain;charset=" + ConnectionUtils.CHARSET);\r
                        conn.setRequestProperty("Content-Length", "" + jsonBytes.length);\r
-                       setAuthorization(conn, username, password);\r
-                       conn.setUseCaches(false);\r
-                       conn.setDoOutput(true);\r
-                       if (conn instanceof HttpsURLConnection) {\r
-                               HttpsURLConnection secureConn = (HttpsURLConnection) conn;\r
-                               secureConn.setSSLSocketFactory(SSL_CONTEXT.getSocketFactory());\r
-                               secureConn.setHostnameVerifier(HOSTNAME_VERIFIER);\r
-                       }\r
 \r
                        // write json body\r
                        OutputStream os = conn.getOutputStream();\r
@@ -296,15 +245,6 @@ public class JsonUtils {
                }\r
        }\r
 \r
-       private static void setAuthorization(URLConnection conn, String username, char[] password) {\r
-               if (!StringUtils.isEmpty(username) && (password != null && password.length > 0)) {\r
-                       conn.setRequestProperty(\r
-                                       "Authorization",\r
-                                       "Basic "\r
-                                                       + Base64.encodeBytes((username + ":" + new String(password)).getBytes()));\r
-               }\r
-       }\r
-\r
        // build custom gson instance with GMT date serializer/deserializer\r
        // http://code.google.com/p/google-gson/issues/detail?id=281\r
        private static Gson gson() {\r
@@ -343,35 +283,4 @@ public class JsonUtils {
                        }\r
                }\r
        }\r
-\r
-       /**\r
-        * DummyTrustManager trusts all certificates.\r
-        */\r
-       private static class DummyTrustManager implements X509TrustManager {\r
-\r
-               @Override\r
-               public void checkClientTrusted(X509Certificate[] certs, String authType)\r
-                               throws CertificateException {\r
-               }\r
-\r
-               @Override\r
-               public void checkServerTrusted(X509Certificate[] certs, String authType)\r
-                               throws CertificateException {\r
-               }\r
-\r
-               @Override\r
-               public X509Certificate[] getAcceptedIssuers() {\r
-                       return null;\r
-               }\r
-       }\r
-\r
-       /**\r
-        * Trusts all hostnames from a certificate, including self-signed certs.\r
-        */\r
-       private static class DummyHostnameVerifier implements HostnameVerifier {\r
-               @Override\r
-               public boolean verify(String hostname, SSLSession session) {\r
-                       return true;\r
-               }\r
-       }\r
 }\r
index fb40dd566c82a0469013392532bbd0d9e775d34c..3664a1816d684acec460c8cf2a8bb56c5a78b9a7 100644 (file)
 package com.gitblit.utils;\r
 \r
 import java.io.IOException;\r
+import java.io.InputStream;\r
 import java.io.OutputStream;\r
 import java.io.OutputStreamWriter;\r
+import java.net.URLConnection;\r
 import java.text.MessageFormat;\r
 import java.util.ArrayList;\r
 import java.util.List;\r
@@ -33,7 +35,9 @@ import com.sun.syndication.feed.synd.SyndFeed;
 import com.sun.syndication.feed.synd.SyndFeedImpl;\r
 import com.sun.syndication.feed.synd.SyndImageImpl;\r
 import com.sun.syndication.io.FeedException;\r
+import com.sun.syndication.io.SyndFeedInput;\r
 import com.sun.syndication.io.SyndFeedOutput;\r
+import com.sun.syndication.io.XmlReader;\r
 \r
 /**\r
  * Utility class for RSS feeds.\r
@@ -93,4 +97,53 @@ public class SyndicationUtils {
                output.output(feed, writer);\r
                writer.close();\r
        }\r
+\r
+       /**\r
+        * Reads a Gitblit RSS feed.\r
+        * \r
+        * @param url\r
+        *            the url of the Gitblit server\r
+        * @param repository\r
+        *            the repository name\r
+        * @param branch\r
+        *            the branch name (optional)\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 the JSON message as a string\r
+        * @throws {@link IOException}\r
+        */\r
+       public static SyndFeed readFeed(String url, String repository, String branch,\r
+                       int numberOfEntries, String username, char[] password) throws IOException,\r
+                       FeedException {\r
+               String feedUrl;\r
+               if (StringUtils.isEmpty(branch)) {\r
+                       // no branch specified\r
+                       if (numberOfEntries > 0) {\r
+                               // fixed number of entries\r
+                               feedUrl = MessageFormat.format("{0}/feed/{1}?l={2,number,0}", url, repository);\r
+                       } else {\r
+                               // server default number of entries\r
+                               feedUrl = MessageFormat.format("{0}/feed/{1}", url, repository);\r
+                       }\r
+               } else {\r
+                       // branch specified\r
+                       if (numberOfEntries > 0) {\r
+                               // fixed number of entries\r
+                               feedUrl = MessageFormat.format("{0}/feed/{1}?h={2}&l={3,number,0}", url,\r
+                                               repository, branch, numberOfEntries);\r
+                       } else {\r
+                               // server default number of entries\r
+                               feedUrl = MessageFormat.format("{0}/feed/{1}?h={2}", url, repository, branch);\r
+                       }\r
+               }\r
+               URLConnection conn = ConnectionUtils.openReadConnection(feedUrl, username, password);\r
+               InputStream is = conn.getInputStream();\r
+               SyndFeedInput input = new SyndFeedInput();\r
+               SyndFeed feed = input.build(new XmlReader(is));\r
+               is.close();\r
+               return feed;\r
+       }\r
 }\r
index 1fa21fc38c46fee7c1d9685ea35f7322b3109e44..33d7dbd2027b00d258367cb10c1980d29199f3f0 100644 (file)
@@ -23,8 +23,10 @@ import junit.framework.TestCase;
 import org.eclipse.jgit.lib.Repository;\r
 import org.eclipse.jgit.revwalk.RevCommit;\r
 \r
+import com.gitblit.client.GitblitFeed;\r
 import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.SyndicationUtils;\r
+import com.sun.syndication.feed.synd.SyndFeed;\r
 \r
 public class SyndicationUtilsTest extends TestCase {\r
 \r
@@ -40,4 +42,12 @@ public class SyndicationUtilsTest extends TestCase {
                assertTrue(feed.indexOf("<title>Title</title>") > -1);\r
                assertTrue(feed.indexOf("<description>Description</description>") > -1);\r
        }\r
+\r
+       public void testFeedRead() throws Exception {\r
+               GitblitFeed reader = new GitblitFeed("https://localhost:8443", "ticgit.git", "master");\r
+               SyndFeed feed = reader.update(5, "admin", "admin".toCharArray());\r
+               assertTrue(feed != null);\r
+               assertTrue(feed.getEntries().size() > 0);\r
+               assertEquals(5, feed.getEntries().size());\r
+       }\r
 }
\ No newline at end of file