Skip to content

Std Response object can handle input streams that are null (only header is sent) #243

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

Open
wants to merge 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ public String getContentType() {

public InputStream getContent() {
try {
return new ConnectionReleasingInputStream(entity.getContent());
InputStream content = entity.getContent();
if(content == null) {
return null;
}
return new ConnectionReleasingInputStream(content);
} catch (Exception e) {
throw Exceptions.propagate(e);
}
Expand Down
11 changes: 9 additions & 2 deletions org.ektorp/src/main/java/org/ektorp/http/StdResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,17 @@ protected static JsonNode responseBodyAsNode(InputStream inputStream, ObjectMapp
return mapper.readTree(inputStream);
}

@edu.umd.cs.findbugs.annotations.SuppressWarnings(
value="NP_LOAD_OF_KNOWN_NULL_VALUE",
justification="The response should be null only if it is not a valid OK message. "
+ "In that case, it is safe to die. In any case, this method should be owerwritten if the class is extended.")
protected static <T> T checkResponseBodyOkAndReturnDefaultValue(HttpResponse hr, T defaultValue, ObjectMapper mapper) throws IOException {
InputStream content = hr.getContent();
InputStream content = null;
try {
content = hr.getContent();
if(content == null){
return defaultValue;
}
JsonNode body = responseBodyAsNode(content, MAPPER);
JsonNode okNode = body.get("ok");
if (okNode != null) {
Expand All @@ -90,7 +97,7 @@ protected static <T> T checkResponseBodyOkAndReturnDefaultValue(HttpResponse hr,
public T error(HttpResponse hr) {
throw StdResponseHandler.createDbAccessException(hr);
}

public T success(HttpResponse hr) throws Exception {
return checkResponseBodyOkAndReturnDefaultValue(hr, null, MAPPER);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package org.ektorp.impl.changes;

import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;
import org.ektorp.changes.*;
import org.ektorp.changes.ChangesFeed;
import org.ektorp.changes.DocumentChange;
import org.ektorp.http.HttpResponse;
import org.ektorp.util.*;
import org.slf4j.*;
import org.ektorp.util.Exceptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
* @author henrik lundgren
Expand Down Expand Up @@ -117,7 +122,11 @@ public void run() {
String reason = !shouldRun ? "Cancelled" : "EOF";
LOG.info("Changes feed stopped. Reason: " + reason);
} catch (Exception e) {
handleException(e);
if(!shouldRun) {
LOG.info("Changes feed was interrupted");
} else {
handleException(e);
}
} finally {
sendInterruptMarker();
httpResponse.abort();
Expand Down