Expect JSON results to be always present

This saves an unnecessary and annoying null dereference check.
feature/mock
Richard Körber 2019-10-27 14:47:23 +01:00
parent 72ebca46aa
commit 2215bfe83d
No known key found for this signature in database
GPG Key ID: AAB9FD19C78AA3E0
11 changed files with 23 additions and 69 deletions

View File

@ -203,10 +203,7 @@ public class Account extends AcmeJsonResource {
} }
Authorization auth = getLogin().bindAuthorization(authLocation); Authorization auth = getLogin().bindAuthorization(authLocation);
JSON json = conn.readJsonResponse(); auth.setJSON(conn.readJsonResponse());
if (json != null) {
auth.setJSON(json);
}
return auth; return auth;
} }
} }
@ -258,11 +255,7 @@ public class Account extends AcmeJsonResource {
claims.put(KEY_STATUS, "deactivated"); claims.put(KEY_STATUS, "deactivated");
conn.sendSignedRequest(getLocation(), claims, getLogin()); conn.sendSignedRequest(getLocation(), claims, getLogin());
setJSON(conn.readJsonResponse());
JSON json = conn.readJsonResponse();
if (json != null) {
setJSON(json);
}
} }
} }
@ -348,11 +341,7 @@ public class Account extends AcmeJsonResource {
} }
conn.sendSignedRequest(getLocation(), claims, getLogin()); conn.sendSignedRequest(getLocation(), claims, getLogin());
setJSON(conn.readJsonResponse());
JSON json = conn.readJsonResponse();
if (json != null) {
setJSON(json);
}
} }
} }
} }

View File

@ -30,7 +30,6 @@ import org.shredzone.acme4j.connector.Resource;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.toolbox.AcmeUtils; import org.shredzone.acme4j.toolbox.AcmeUtils;
import org.shredzone.acme4j.toolbox.JSON;
import org.shredzone.acme4j.toolbox.JSONBuilder; import org.shredzone.acme4j.toolbox.JSONBuilder;
import org.shredzone.acme4j.toolbox.JoseUtils; import org.shredzone.acme4j.toolbox.JoseUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -219,10 +218,7 @@ public class AccountBuilder {
} }
Login login = new Login(location, keyPair, session); Login login = new Login(location, keyPair, session);
JSON json = conn.readJsonResponse(); login.getAccount().setJSON(conn.readJsonResponse());
if (json != null) {
login.getAccount().setJSON(json);
}
return login; return login;
} }
} }

View File

@ -117,10 +117,7 @@ public abstract class AcmeJsonResource extends AcmeResource {
LOG.debug("update {}", resourceType); LOG.debug("update {}", resourceType);
try (Connection conn = getSession().connect()) { try (Connection conn = getSession().connect()) {
conn.sendSignedPostAsGetRequest(getLocation(), getLogin()); conn.sendSignedPostAsGetRequest(getLocation(), getLogin());
JSON json = conn.readJsonResponse(); setJSON(conn.readJsonResponse());
if (json != null) {
setJSON(json);
}
conn.handleRetryAfter(resourceType + " is not completed yet"); conn.handleRetryAfter(resourceType + " is not completed yet");
} }
} }

View File

@ -28,7 +28,6 @@ import org.shredzone.acme4j.connector.Connection;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.toolbox.AcmeUtils; import org.shredzone.acme4j.toolbox.AcmeUtils;
import org.shredzone.acme4j.toolbox.JSON;
import org.shredzone.acme4j.toolbox.JSON.Value; import org.shredzone.acme4j.toolbox.JSON.Value;
import org.shredzone.acme4j.toolbox.JSONBuilder; import org.shredzone.acme4j.toolbox.JSONBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -156,11 +155,7 @@ public class Authorization extends AcmeJsonResource {
claims.put("status", "deactivated"); claims.put("status", "deactivated");
conn.sendSignedRequest(getLocation(), claims, getLogin()); conn.sendSignedRequest(getLocation(), claims, getLogin());
setJSON(conn.readJsonResponse());
JSON json = conn.readJsonResponse();
if (json != null) {
setJSON(json);
}
} }
} }

View File

@ -135,9 +135,7 @@ public class Login {
public Challenge bindChallenge(URL location) throws AcmeException { public Challenge bindChallenge(URL location) throws AcmeException {
Connection connect = session.connect(); Connection connect = session.connect();
connect.sendSignedPostAsGetRequest(location, this); connect.sendSignedPostAsGetRequest(location, this);
JSON data = connect.readJsonResponse(); return createChallenge(connect.readJsonResponse());
Objects.requireNonNull(data, "data");
return createChallenge(data);
} }
/** /**

View File

@ -26,7 +26,6 @@ import javax.annotation.ParametersAreNonnullByDefault;
import org.shredzone.acme4j.connector.Connection; import org.shredzone.acme4j.connector.Connection;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.toolbox.JSON;
import org.shredzone.acme4j.toolbox.JSON.Value; import org.shredzone.acme4j.toolbox.JSON.Value;
import org.shredzone.acme4j.toolbox.JSONBuilder; import org.shredzone.acme4j.toolbox.JSONBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -266,11 +265,7 @@ public class Order extends AcmeJsonResource {
claims.put("status", "canceled"); claims.put("status", "canceled");
conn.sendSignedRequest(getLocation(), claims, getLogin()); conn.sendSignedRequest(getLocation(), claims, getLogin());
setJSON(conn.readJsonResponse());
JSON json = conn.readJsonResponse();
if (json != null) {
setJSON(json);
}
} }
} }

View File

@ -29,7 +29,6 @@ import org.shredzone.acme4j.connector.Connection;
import org.shredzone.acme4j.connector.Resource; import org.shredzone.acme4j.connector.Resource;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.toolbox.JSON;
import org.shredzone.acme4j.toolbox.JSONBuilder; import org.shredzone.acme4j.toolbox.JSONBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -322,10 +321,7 @@ public class OrderBuilder {
} }
Order order = new Order(login, orderLocation); Order order = new Order(login, orderLocation);
JSON json = conn.readJsonResponse(); order.setJSON(conn.readJsonResponse());
if (json != null) {
order.setJSON(json);
}
return order; return order;
} }
} }

View File

@ -153,11 +153,7 @@ public class Challenge extends AcmeJsonResource {
prepareResponse(claims); prepareResponse(claims);
conn.sendSignedRequest(getLocation(), claims, getLogin()); conn.sendSignedRequest(getLocation(), claims, getLogin());
setJSON(conn.readJsonResponse());
JSON json = conn.readJsonResponse();
if (json != null) {
setJSON(json);
}
} }
} }

View File

@ -26,6 +26,7 @@ import javax.annotation.ParametersAreNonnullByDefault;
import org.shredzone.acme4j.Login; import org.shredzone.acme4j.Login;
import org.shredzone.acme4j.Session; import org.shredzone.acme4j.Session;
import org.shredzone.acme4j.exception.AcmeException; import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.exception.AcmeRetryAfterException; import org.shredzone.acme4j.exception.AcmeRetryAfterException;
import org.shredzone.acme4j.toolbox.JSON; import org.shredzone.acme4j.toolbox.JSON;
import org.shredzone.acme4j.toolbox.JSONBuilder; import org.shredzone.acme4j.toolbox.JSONBuilder;
@ -130,9 +131,10 @@ public interface Connection extends AutoCloseable {
/** /**
* Reads a server response as JSON data. * Reads a server response as JSON data.
* *
* @return The JSON response, or {@code null} if the server did not provide any data. * @return The JSON response.
* @throws AcmeProtocolException
* if the JSON response was empty.
*/ */
@CheckForNull
JSON readJsonResponse() throws AcmeException; JSON readJsonResponse() throws AcmeException;
/** /**

View File

@ -161,12 +161,11 @@ public class DefaultConnection implements Connection {
} }
@Override @Override
@CheckForNull
public JSON readJsonResponse() throws AcmeException { public JSON readJsonResponse() throws AcmeException {
assertConnectionIsOpen(); assertConnectionIsOpen();
if (conn.getContentLength() == 0) { if (conn.getContentLength() == 0) {
return null; throw new AcmeProtocolException("Empty response");
} }
String contentType = AcmeUtils.getContentType(conn.getHeaderField(CONTENT_TYPE_HEADER)); String contentType = AcmeUtils.getContentType(conn.getHeaderField(CONTENT_TYPE_HEADER));
@ -174,20 +173,19 @@ public class DefaultConnection implements Connection {
throw new AcmeProtocolException("Unexpected content type: " + contentType); throw new AcmeProtocolException("Unexpected content type: " + contentType);
} }
JSON result = null;
try { try {
InputStream in = InputStream in =
conn.getResponseCode() < 400 ? conn.getInputStream() : conn.getErrorStream(); conn.getResponseCode() < 400 ? conn.getInputStream() : conn.getErrorStream();
if (in != null) { if (in == null) {
result = JSON.parse(in); throw new AcmeProtocolException("JSON response is empty");
LOG.debug("Result JSON: {}", result.toString());
} }
JSON result = JSON.parse(in);
LOG.debug("Result JSON: {}", result.toString());
return result;
} catch (IOException ex) { } catch (IOException ex) {
throw new AcmeNetworkException(ex); throw new AcmeNetworkException(ex);
} }
return result;
} }
@Override @Override
@ -461,11 +459,7 @@ public class DefaultConnection implements Connection {
throw new AcmeException("HTTP " + conn.getResponseCode() + ": " + conn.getResponseMessage()); throw new AcmeException("HTTP " + conn.getResponseCode() + ": " + conn.getResponseMessage());
} }
JSON problemJson = readJsonResponse(); Problem problem = new Problem(readJsonResponse(), conn.getURL());
if (problemJson == null) {
throw new AcmeProtocolException("Empty problem response");
}
Problem problem = new Problem(problemJson, conn.getURL());
String error = AcmeUtils.stripErrorPrefix(problem.getType().toString()); String error = AcmeUtils.stripErrorPrefix(problem.getType().toString());

View File

@ -146,11 +146,7 @@ public class ResourceIterator<T extends AcmeResource> implements Iterator<T> {
Session session = login.getSession(); Session session = login.getSession();
try (Connection conn = session.connect()) { try (Connection conn = session.connect()) {
conn.sendSignedPostAsGetRequest(nextUrl, login); conn.sendSignedPostAsGetRequest(nextUrl, login);
fillUrlList(conn.readJsonResponse());
JSON json = conn.readJsonResponse();
if (json != null) {
fillUrlList(json);
}
nextUrl = conn.getLinks("next").stream().findFirst().orElse(null); nextUrl = conn.getLinks("next").stream().findFirst().orElse(null);
} }