*/\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
--- /dev/null
+/*\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
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
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
*/\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
*/\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
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
}\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
}\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
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
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
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
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
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