Fix style issues

pull/30/head
Richard Körber 2016-12-16 01:42:53 +01:00
parent 232a243e92
commit b3fc9a732c
9 changed files with 62 additions and 48 deletions

View File

@ -118,7 +118,7 @@ public class Authorization extends AcmeResource {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends Challenge> T findChallenge(String type) { public <T extends Challenge> T findChallenge(String type) {
Collection<Challenge> result = findCombination(type); Collection<Challenge> result = findCombination(type);
return (result != null ? (T) result.iterator().next() : null); return result != null ? (T) result.iterator().next() : null;
} }
/** /**
@ -217,6 +217,7 @@ public class Authorization extends AcmeResource {
update(); update();
} catch (AcmeRetryAfterException ex) { } catch (AcmeRetryAfterException ex) {
// ignore... The object was still updated. // ignore... The object was still updated.
LOG.debug("Retry-After", ex);
} catch (AcmeException ex) { } catch (AcmeException ex) {
throw new AcmeProtocolException("Could not load lazily", ex); throw new AcmeProtocolException("Could not load lazily", ex);
} }
@ -233,47 +234,47 @@ public class Authorization extends AcmeResource {
protected void unmarshalAuthorization(Map<String, Object> json) { protected void unmarshalAuthorization(Map<String, Object> json) {
this.status = Status.parse((String) json.get("status"), Status.PENDING); this.status = Status.parse((String) json.get("status"), Status.PENDING);
String expires = (String) json.get("expires"); String jsonExpires = (String) json.get("expires");
if (expires != null) { if (jsonExpires != null) {
this.expires = TimestampParser.parse(expires); expires = TimestampParser.parse(jsonExpires);
} }
Map<String, Object> identifier = (Map<String, Object>) json.get("identifier"); Map<String, Object> jsonIdentifier = (Map<String, Object>) json.get("identifier");
if (identifier != null) { if (jsonIdentifier != null) {
String type = (String) identifier.get("type"); String type = (String) jsonIdentifier.get("type");
if (type != null && !"dns".equals(type)) { if (type != null && !"dns".equals(type)) {
throw new AcmeProtocolException("Unknown authorization type: " + type); throw new AcmeProtocolException("Unknown authorization type: " + type);
} }
this.domain = (String) identifier.get("value"); domain = (String) jsonIdentifier.get("value");
} }
Collection<Map<String, Object>> challenges = Collection<Map<String, Object>> jsonChallenges =
(Collection<Map<String, Object>>) json.get("challenges"); (Collection<Map<String, Object>>) json.get("challenges");
List<Challenge> cr = new ArrayList<>(); List<Challenge> cr = new ArrayList<>();
for (Map<String, Object> c : challenges) { for (Map<String, Object> c : jsonChallenges) {
Challenge ch = getSession().createChallenge(c); Challenge ch = getSession().createChallenge(c);
if (ch != null) { if (ch != null) {
cr.add(ch); cr.add(ch);
} }
} }
this.challenges = cr; challenges = cr;
Collection<List<Number>> combinations = Collection<List<Number>> jsonCombinations =
(Collection<List<Number>>) json.get("combinations"); (Collection<List<Number>>) json.get("combinations");
if (combinations != null) { if (jsonCombinations != null) {
List<List<Challenge>> cmb = new ArrayList<>(combinations.size()); List<List<Challenge>> cmb = new ArrayList<>(jsonCombinations.size());
for (List<Number> c : combinations) { for (List<Number> c : jsonCombinations) {
List<Challenge> clist = new ArrayList<>(c.size()); List<Challenge> clist = new ArrayList<>(c.size());
for (Number n : c) { for (Number n : c) {
clist.add(cr.get(n.intValue())); clist.add(cr.get(n.intValue()));
} }
cmb.add(clist); cmb.add(clist);
} }
this.combinations = cmb; combinations = cmb;
} else { } else {
List<List<Challenge>> cmb = new ArrayList<>(1); List<List<Challenge>> cmb = new ArrayList<>(1);
cmb.add(cr); cmb.add(cr);
this.combinations = cmb; combinations = cmb;
} }
loaded = true; loaded = true;

View File

@ -79,7 +79,7 @@ public class Metadata {
*/ */
public String get(String key) { public String get(String key) {
Object value = meta.get(key); Object value = meta.get(key);
return (value != null ? value.toString() : null); return value != null ? value.toString() : null;
} }
/** /**
@ -95,7 +95,7 @@ public class Metadata {
public URI getUri(String key) { public URI getUri(String key) {
Object uri = meta.get(key); Object uri = meta.get(key);
try { try {
return (uri != null ? new URI(uri.toString()) : null); return uri != null ? new URI(uri.toString()) : null;
} catch (URISyntaxException ex) { } catch (URISyntaxException ex) {
throw new AcmeProtocolException("Bad URI: " + uri, ex); throw new AcmeProtocolException("Bad URI: " + uri, ex);
} }

View File

@ -335,6 +335,7 @@ public class Registration extends AcmeResource {
update(); update();
} catch (AcmeRetryAfterException ex) { } catch (AcmeRetryAfterException ex) {
// ignore... The object was still updated. // ignore... The object was still updated.
LOG.debug("Retry-After", ex);
} catch (AcmeException ex) { } catch (AcmeException ex) {
throw new AcmeProtocolException("Could not load lazily", ex); throw new AcmeProtocolException("Could not load lazily", ex);
} }

View File

@ -29,11 +29,13 @@ public enum Status {
* no match * no match
*/ */
public static Status parse(String str) { public static Status parse(String str) {
try { String check = str.toUpperCase();
return valueOf(str.toUpperCase()); for (Status s : values()) {
} catch (IllegalArgumentException ex) { if (s.name().equals(check)) {
return Status.UNKNOWN; return s;
}
} }
return Status.UNKNOWN;
} }
/** /**
@ -47,7 +49,7 @@ public enum Status {
* no match, or {@code def} if the str was {@code null} * no match, or {@code def} if the str was {@code null}
*/ */
public static Status parse(String str, Status def) { public static Status parse(String str, Status def) {
return (str != null ? parse(str) : def); return str != null ? parse(str) : def;
} }
} }

View File

@ -198,7 +198,8 @@ public class DefaultConnection implements Connection {
String response = ""; String response = "";
try { try {
InputStream in = (conn.getResponseCode() < 400 ? conn.getInputStream() : conn.getErrorStream()); InputStream in =
conn.getResponseCode() < 400 ? conn.getInputStream() : conn.getErrorStream();
if (in != null) { if (in != null) {
response = readStream(in); response = readStream(in);
result = JsonUtil.parseJson(response); result = JsonUtil.parseJson(response);
@ -311,7 +312,7 @@ public class DefaultConnection implements Connection {
} }
} }
return (!result.isEmpty() ? result : null); return !result.isEmpty() ? result : null;
} }
@Override @Override
@ -334,7 +335,7 @@ public class DefaultConnection implements Connection {
// HTTP-date // HTTP-date
long date = conn.getHeaderFieldDate("Retry-After", 0L); long date = conn.getHeaderFieldDate("Retry-After", 0L);
return (date != 0 ? new Date(date) : null); return date != 0 ? new Date(date) : null;
} catch (Exception ex) { } catch (Exception ex) {
throw new AcmeProtocolException("Bad retry-after header value: " + header, ex); throw new AcmeProtocolException("Bad retry-after header value: " + header, ex);
} }
@ -410,12 +411,13 @@ public class DefaultConnection implements Connection {
* Log all HTTP headers in debug mode. * Log all HTTP headers in debug mode.
*/ */
private void logHeaders() { private void logHeaders() {
if (LOG.isDebugEnabled()) { if (!LOG.isDebugEnabled()) {
Map<String, List<String>> headers = conn.getHeaderFields(); return;
for (String key : headers.keySet()) { }
for (String value : headers.get(key)) {
LOG.debug("HEADER {}: {}", key, value); for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
} for (String value : entry.getValue()) {
LOG.debug("HEADER {}: {}", entry.getKey(), value);
} }
} }
} }

View File

@ -14,10 +14,13 @@
package org.shredzone.acme4j.connector; package org.shredzone.acme4j.connector;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.util.Properties; import java.util.Properties;
import org.slf4j.LoggerFactory;
/** /**
* A generic HTTP connector. It connects to the given URI with a 10 seconds connection and * A generic HTTP connector. It connects to the given URI with a 10 seconds connection and
* read timeout. * read timeout.
@ -32,16 +35,17 @@ public class HttpConnector {
static { static {
StringBuilder agent = new StringBuilder("acme4j"); StringBuilder agent = new StringBuilder("acme4j");
try {
try (InputStream in = HttpConnector.class.getResourceAsStream("/org/shredzone/acme4j/version.properties")) {
Properties prop = new Properties(); Properties prop = new Properties();
prop.load(HttpConnector.class.getResourceAsStream("/org/shredzone/acme4j/version.properties")); prop.load(in);
agent.append('/').append(prop.getProperty("version")); agent.append('/').append(prop.getProperty("version"));
} catch (IOException ex) { } catch (IOException ex) {
// Ignore, just don't use a version // Ignore, just don't use a version
LoggerFactory.getLogger(HttpConnector.class).warn("Could not read library version", ex);
} }
agent.append(" Java/").append(System.getProperty("java.version")); agent.append(" Java/").append(System.getProperty("java.version"));
USER_AGENT = agent.toString(); USER_AGENT = agent.toString();
} }

View File

@ -44,7 +44,8 @@ public class AcmeRateLimitExceededException extends AcmeServerException {
public AcmeRateLimitExceededException(String type, String detail, Date retryAfter, Collection<URI> documents) { public AcmeRateLimitExceededException(String type, String detail, Date retryAfter, Collection<URI> documents) {
super(type, detail); super(type, detail);
this.retryAfter = retryAfter; this.retryAfter = retryAfter;
this.documents = (documents != null ? Collections.unmodifiableCollection(documents) : null); this.documents =
documents != null ? Collections.unmodifiableCollection(documents) : null;
} }
/** /**
@ -52,7 +53,7 @@ public class AcmeRateLimitExceededException extends AcmeServerException {
* moment is not known. * moment is not known.
*/ */
public Date getRetryAfter() { public Date getRetryAfter() {
return (retryAfter != null ? new Date(retryAfter.getTime()) : null); return retryAfter != null ? new Date(retryAfter.getTime()) : null;
} }
/** /**

View File

@ -33,7 +33,7 @@ public class AcmeRetryAfterException extends AcmeException {
* Returns the retry-after date returned by the server. * Returns the retry-after date returned by the server.
*/ */
public Date getRetryAfter() { public Date getRetryAfter() {
return (retryAfter != null ? new Date(retryAfter.getTime()) : null); return retryAfter != null ? new Date(retryAfter.getTime()) : null;
} }
} }

View File

@ -25,7 +25,7 @@ import java.util.regex.Pattern;
* *
* @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC 3339</a> * @see <a href="https://www.ietf.org/rfc/rfc3339.txt">RFC 3339</a>
*/ */
public class TimestampParser { public final class TimestampParser {
private static final Pattern DATE_PATTERN = Pattern.compile( private static final Pattern DATE_PATTERN = Pattern.compile(
"^(\\d{4})-(\\d{2})-(\\d{2})T" "^(\\d{4})-(\\d{2})-(\\d{2})T"
@ -36,6 +36,10 @@ public class TimestampParser {
private static final Pattern TZ_PATTERN = Pattern.compile( private static final Pattern TZ_PATTERN = Pattern.compile(
"([+-])(\\d{2}):?(\\d{2})$"); "([+-])(\\d{2}):?(\\d{2})$");
private TimestampParser() {
// Utility class without constructor
}
/** /**
* Parses a RFC 3339 formatted date. * Parses a RFC 3339 formatted date.
* *
@ -58,15 +62,14 @@ public class TimestampParser {
int minute = Integer.parseInt(m.group(5)); int minute = Integer.parseInt(m.group(5));
int second = Integer.parseInt(m.group(6)); int second = Integer.parseInt(m.group(6));
String msStr = m.group(7); StringBuilder msStr = new StringBuilder();
if (msStr == null) { if (m.group(7) != null) {
msStr = "000"; msStr.append(m.group(7));
} else {
while (msStr.length() < 3) {
msStr += '0';
}
} }
int ms = Integer.parseInt(msStr); while (msStr.length() < 3) {
msStr.append('0');
}
int ms = Integer.parseInt(msStr.toString());
String tz = m.group(8); String tz = m.group(8);
if ("Z".equalsIgnoreCase(tz)) { if ("Z".equalsIgnoreCase(tz)) {