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/CouchDbClient.java b/src/main/java/org/lightcouch/CouchDbClient.java index 2e1f054..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; @@ -24,6 +31,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; @@ -184,16 +192,24 @@ 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(); + } + + HostnameVerifier hostnameVerifier = props.getHostnameVerifier(); + if(hostnameVerifier == null) { + hostnameVerifier = new NoopHostnameVerifier(); + } - return registry.register("https", new SSLConnectionSocketFactory(sslcontext, - new NoopHostnameVerifier())).build(); + return registry.register("https", new SSLConnectionSocketFactory(sslContext, + 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 10c920f..15e4834 100644 --- a/src/main/java/org/lightcouch/CouchDbProperties.java +++ b/src/main/java/org/lightcouch/CouchDbProperties.java @@ -12,10 +12,19 @@ * 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; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; + /** * Represents configuration properties for connecting to CouchDB. * @@ -40,6 +49,8 @@ public class CouchDbProperties { private int maxConnections; private String proxyHost; private int proxyPort; + private SSLContext sslContext; + private HostnameVerifier hostnameVerifier; public CouchDbProperties() { // default constructor @@ -107,6 +118,14 @@ public String getProxyHost() { public int getProxyPort() { return proxyPort; } + + public SSLContext getSSLContext() { + return sslContext; + } + + public HostnameVerifier getHostnameVerifier() { + return hostnameVerifier; + } public CouchDbProperties setDbName(String dbName) { this.dbName = dbName; @@ -172,6 +191,16 @@ public CouchDbProperties setProxyPort(int proxyPort) { this.proxyPort = proxyPort; return this; } + + public CouchDbProperties setSSLContext(SSLContext sslContext) { + this.sslContext = sslContext; + return this; + } + + public CouchDbProperties setHostnameVerifier(HostnameVerifier hostnameVerifier) { + this.hostnameVerifier = hostnameVerifier; + return this; + } public void clearPassword() { setPassword(""); diff --git a/src/main/java/org/lightcouch/View.java b/src/main/java/org/lightcouch/View.java index b6a0c20..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; @@ -167,7 +174,7 @@ public List query(Class classOfT) { close(instream); } } - + /** * Queries a view. * @param Object type K (key) @@ -178,6 +185,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 +208,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) {