Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding functionality to explicitly allow an empty view result #47

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
target
*.log
*.log.*
/bin/
34 changes: 25 additions & 9 deletions src/main/java/org/lightcouch/CouchDbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -184,16 +192,24 @@ private Registry<ConnectionSocketFactory> createRegistry(CouchDbProperties props
.<ConnectionSocketFactory> 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();
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/lightcouch/CouchDbProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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("");
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/org/lightcouch/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,7 +174,7 @@ public <T> List<T> query(Class<T> classOfT) {
close(instream);
}
}

/**
* Queries a view.
* @param <K> Object type K (key)
Expand All @@ -178,6 +185,20 @@ public <T> List<T> query(Class<T> classOfT) {
* @return The View result entries.
*/
public <K, V, T> ViewResult<K, V, T> queryView(Class<K> classOfK, Class<V> classOfV, Class<T> classOfT) {
return queryView(classOfK, classOfV, classOfT, true);
}

/**
* Queries a view.
* @param <K> Object type K (key)
* @param <V> 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 <K, V, T> ViewResult<K, V, T> queryView(Class<K> classOfK, Class<V> classOfV, Class<T> classOfT, boolean throwOnEmptyResult) {
InputStream instream = null;
try {
Reader reader = new InputStreamReader(instream = queryForStream(), Charsets.UTF_8);
Expand All @@ -187,7 +208,7 @@ public <K, V, T> ViewResult<K, V, T> queryView(Class<K> classOfK, Class<V> 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) {
Expand Down