From c1fd30dea22b3525c7cc2c6e38e74e51a64d63c8 Mon Sep 17 00:00:00 2001 From: shaufe Date: Sun, 5 Jul 2015 09:22:19 +0200 Subject: [PATCH 1/5] Adding functionality to explicitly allow an empty view result --- src/main/java/org/lightcouch/View.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/lightcouch/View.java b/src/main/java/org/lightcouch/View.java index b6a0c20..0a996ba 100644 --- a/src/main/java/org/lightcouch/View.java +++ b/src/main/java/org/lightcouch/View.java @@ -167,7 +167,7 @@ public List query(Class classOfT) { close(instream); } } - + /** * Queries a view. * @param Object type K (key) @@ -178,6 +178,20 @@ public List query(Class classOfT) { * @return The View result entries. */ public ViewResult queryView(Class classOfK, Class classOfV, Class classOfT) { + return queryView(classOfK, classOfV, classOfT, true); + } + + /** + * Queries a view. + * @param Object type K (key) + * @param Object type V (value) + * @param classOfK The class of type K. + * @param classOfV The class of type V. + * @param classOfT The class of type T. + * @param throwOnEmptyResult + * @return The View result entries. + */ + public ViewResult queryView(Class classOfK, Class classOfV, Class classOfT, boolean throwOnEmptyResult) { InputStream instream = null; try { Reader reader = new InputStreamReader(instream = queryForStream(), Charsets.UTF_8); @@ -187,7 +201,7 @@ public ViewResult queryView(Class classOfK, Class class vr.setOffset(getAsInt(json, "offset")); vr.setUpdateSeq(getAsLong(json, "update_seq")); JsonArray jsonArray = json.getAsJsonArray("rows"); - if(jsonArray.size() == 0) { // validate available rows + if(jsonArray.size() == 0 && throwOnEmptyResult) { // validate available rows throw new NoDocumentException("No result was returned by this view query."); } for (JsonElement e : jsonArray) { From e5ca69510a199495f8c235499c75be8a9c10f28d Mon Sep 17 00:00:00 2001 From: shaufe Date: Mon, 5 Oct 2015 10:16:09 +0200 Subject: [PATCH 2/5] * ignore bin folder * state changes in View.java according to Apache License requirements --- .gitignore | 1 + src/main/java/org/lightcouch/View.java | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/.gitignore b/.gitignore index 597cce6..d1b3522 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ target *.log *.log.* +/bin/ diff --git a/src/main/java/org/lightcouch/View.java b/src/main/java/org/lightcouch/View.java index 0a996ba..009e96c 100644 --- a/src/main/java/org/lightcouch/View.java +++ b/src/main/java/org/lightcouch/View.java @@ -12,6 +12,13 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * MODIFICATION OF THIS FILE: According to the requirements of the above-stated + * license, I hereby state modifications to this file: Sebastian Haufe added a + * further method "queryView" with an additional boolean argument + * "throwOnEmptyResult". This method modifies the existing one, the existing + * one now delegates to the new one. For this change, a pull request to the + * lightcouch master branch has been requested but not yet granted. */ package org.lightcouch; From 6f8fcea868ae207b5eba1da7c974233fde7a0fbc Mon Sep 17 00:00:00 2001 From: shaufe Date: Mon, 8 Feb 2016 17:46:35 +0100 Subject: [PATCH 3/5] Extend properties to allow an optional parameter SSLContext which allows to customize the level of trust on https connections (e.g. to reject connections for untrusted certificates) --- .../java/org/lightcouch/CouchDbClient.java | 20 +++++++++++-------- .../org/lightcouch/CouchDbProperties.java | 12 +++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/lightcouch/CouchDbClient.java b/src/main/java/org/lightcouch/CouchDbClient.java index 2e1f054..3614631 100644 --- a/src/main/java/org/lightcouch/CouchDbClient.java +++ b/src/main/java/org/lightcouch/CouchDbClient.java @@ -184,15 +184,19 @@ private Registry createRegistry(CouchDbProperties props . create(); if("https".equals(props.getProtocol())) { - SSLContext sslcontext = SSLContexts.custom() - .loadTrustMaterial(null, new TrustStrategy(){ - public boolean isTrusted(X509Certificate[] chain, String authType) - throws CertificateException { - return true; - } - }).build(); + SSLContext sslContext = props.getSSLContext(); + if (sslContext == null) + { + sslContext = SSLContexts.custom() + .loadTrustMaterial(null, new TrustStrategy(){ + public boolean isTrusted(X509Certificate[] chain, String authType) + throws CertificateException { + return true; + } + }).build(); + } - return registry.register("https", new SSLConnectionSocketFactory(sslcontext, + return registry.register("https", new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier())).build(); } else { return registry.register("http", PlainConnectionSocketFactory.INSTANCE).build(); diff --git a/src/main/java/org/lightcouch/CouchDbProperties.java b/src/main/java/org/lightcouch/CouchDbProperties.java index 10c920f..2821444 100644 --- a/src/main/java/org/lightcouch/CouchDbProperties.java +++ b/src/main/java/org/lightcouch/CouchDbProperties.java @@ -16,6 +16,8 @@ package org.lightcouch; +import javax.net.ssl.SSLContext; + /** * Represents configuration properties for connecting to CouchDB. * @@ -40,6 +42,7 @@ public class CouchDbProperties { private int maxConnections; private String proxyHost; private int proxyPort; + private SSLContext sslContext; public CouchDbProperties() { // default constructor @@ -107,6 +110,10 @@ public String getProxyHost() { public int getProxyPort() { return proxyPort; } + + public SSLContext getSSLContext() { + return sslContext; + } public CouchDbProperties setDbName(String dbName) { this.dbName = dbName; @@ -172,6 +179,11 @@ public CouchDbProperties setProxyPort(int proxyPort) { this.proxyPort = proxyPort; return this; } + + public CouchDbProperties setSSLContext(SSLContext sslContext) { + this.sslContext = sslContext; + return this; + } public void clearPassword() { setPassword(""); From 146de19ace8804d9bb44a4bbbcf8fb89b992b08c Mon Sep 17 00:00:00 2001 From: shaufe Date: Tue, 9 Feb 2016 20:22:56 +0100 Subject: [PATCH 4/5] Extend properties to allow an optional parameter HostnameVerifier which allows to check whether a certificate is issued by the requested hostname --- src/main/java/org/lightcouch/CouchDbClient.java | 11 ++++++++--- src/main/java/org/lightcouch/CouchDbProperties.java | 11 +++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/lightcouch/CouchDbClient.java b/src/main/java/org/lightcouch/CouchDbClient.java index 3614631..c7542b2 100644 --- a/src/main/java/org/lightcouch/CouchDbClient.java +++ b/src/main/java/org/lightcouch/CouchDbClient.java @@ -24,6 +24,7 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import org.apache.http.Consts; @@ -185,8 +186,7 @@ private Registry createRegistry(CouchDbProperties props if("https".equals(props.getProtocol())) { SSLContext sslContext = props.getSSLContext(); - if (sslContext == null) - { + if (sslContext == null) { sslContext = SSLContexts.custom() .loadTrustMaterial(null, new TrustStrategy(){ public boolean isTrusted(X509Certificate[] chain, String authType) @@ -195,9 +195,14 @@ public boolean isTrusted(X509Certificate[] chain, String authType) } }).build(); } + + HostnameVerifier hostnameVerifier = props.getHostnameVerifier(); + if(hostnameVerifier == null) { + hostnameVerifier = new NoopHostnameVerifier(); + } return registry.register("https", new SSLConnectionSocketFactory(sslContext, - new NoopHostnameVerifier())).build(); + hostnameVerifier)).build(); } else { return registry.register("http", PlainConnectionSocketFactory.INSTANCE).build(); } diff --git a/src/main/java/org/lightcouch/CouchDbProperties.java b/src/main/java/org/lightcouch/CouchDbProperties.java index 2821444..e8852da 100644 --- a/src/main/java/org/lightcouch/CouchDbProperties.java +++ b/src/main/java/org/lightcouch/CouchDbProperties.java @@ -16,6 +16,7 @@ package org.lightcouch; +import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; /** @@ -43,6 +44,7 @@ public class CouchDbProperties { private String proxyHost; private int proxyPort; private SSLContext sslContext; + private HostnameVerifier hostnameVerifier; public CouchDbProperties() { // default constructor @@ -114,6 +116,10 @@ public int getProxyPort() { public SSLContext getSSLContext() { return sslContext; } + + public HostnameVerifier getHostnameVerifier() { + return hostnameVerifier; + } public CouchDbProperties setDbName(String dbName) { this.dbName = dbName; @@ -184,6 +190,11 @@ public CouchDbProperties setSSLContext(SSLContext sslContext) { this.sslContext = sslContext; return this; } + + public CouchDbProperties setHostnameVerifier(HostnameVerifier hostnameVerifier) { + this.hostnameVerifier = hostnameVerifier; + return this; + } public void clearPassword() { setPassword(""); From 91de412b2dff5075c2095574e2b950598519dafe Mon Sep 17 00:00:00 2001 From: shaufe Date: Sun, 28 Feb 2016 16:37:43 +0100 Subject: [PATCH 5/5] Add license statements for the last two commits --- src/main/java/org/lightcouch/CouchDbClient.java | 7 +++++++ src/main/java/org/lightcouch/CouchDbProperties.java | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/org/lightcouch/CouchDbClient.java b/src/main/java/org/lightcouch/CouchDbClient.java index c7542b2..b525e64 100644 --- a/src/main/java/org/lightcouch/CouchDbClient.java +++ b/src/main/java/org/lightcouch/CouchDbClient.java @@ -12,6 +12,13 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * MODIFICATION OF THIS FILE: According to the requirements of the above-stated + * license, I hereby state modifications to this file: Sebastian Haufe changed + * method createRegistry to use newly introduced parameters sslContext and + * hostnameVerifier in the CouchDbProperties class in case they are explicitly + * set. Otherwise, the code behaves as before. This note should be removed once + * the pull request to the master has been granted. */ package org.lightcouch; diff --git a/src/main/java/org/lightcouch/CouchDbProperties.java b/src/main/java/org/lightcouch/CouchDbProperties.java index e8852da..15e4834 100644 --- a/src/main/java/org/lightcouch/CouchDbProperties.java +++ b/src/main/java/org/lightcouch/CouchDbProperties.java @@ -12,6 +12,12 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * MODIFICATION OF THIS FILE: According to the requirements of the above-stated + * license, I hereby state modifications to this file: Sebastian Haufe added the + * further parameters sslContext and hostnameVerifier and corresponding access + * methods. This note should be removed once the pull request to the master has + * been granted. */ package org.lightcouch;