Skip to content
Merged
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spotbugs-maven-plugin.version>4.4.2.2</spotbugs-maven-plugin.version>
<spotbugs.version>4.4.2</spotbugs.version>
<spotbugs-maven-plugin.version>4.5.0.0</spotbugs-maven-plugin.version>
<spotbugs.version>4.5.0</spotbugs.version>
<spotbugs-maven-plugin.failOnError>true</spotbugs-maven-plugin.failOnError>
<hamcrest.version>2.2</hamcrest.version>
<okhttp3.version>4.9.2</okhttp3.version>
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/org/kohsuke/github/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -578,20 +578,12 @@ private void detectInvalidCached404Response(GitHubConnectorResponse connectorRes
private void noteRateLimit(@Nonnull RateLimitTarget rateLimitTarget,
@Nonnull GitHubConnectorResponse connectorResponse) {
try {
String limitString = Objects.requireNonNull(connectorResponse.header("X-RateLimit-Limit"),
"Missing X-RateLimit-Limit");
String remainingString = Objects.requireNonNull(connectorResponse.header("X-RateLimit-Remaining"),
"Missing X-RateLimit-Remaining");
String resetString = Objects.requireNonNull(connectorResponse.header("X-RateLimit-Reset"),
"Missing X-RateLimit-Reset");
int limit, remaining;
long reset;
limit = Integer.parseInt(limitString);
remaining = Integer.parseInt(remainingString);
reset = Long.parseLong(resetString);
int limit = connectorResponse.parseInt("X-RateLimit-Limit");
int remaining = connectorResponse.parseInt("X-RateLimit-Remaining");
int reset = connectorResponse.parseInt("X-RateLimit-Reset");
GHRateLimit.Record observed = new GHRateLimit.Record(limit, remaining, reset, connectorResponse);
updateRateLimit(GHRateLimit.fromRecord(observed, rateLimitTarget));
} catch (NumberFormatException | NullPointerException e) {
} catch (NumberFormatException e) {
LOGGER.log(FINEST, "Missing or malformed X-RateLimit header: ", e);
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/kohsuke/github/GitHubResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ static <T> T parseBody(GitHubConnectorResponse connectorResponse, T instance) th
/**
* Gets the body of the response as a {@link String}.
*
* @param connectorResponse
* the response to read
* @return the body of the response as a {@link String}.
* @throws IOException
* if an I/O Exception occurs.
* @param connectorResponse
*/
@Nonnull
static String getBodyAsString(GitHubConnectorResponse connectorResponse) throws IOException {
Expand All @@ -138,15 +139,16 @@ static String getBodyAsString(GitHubConnectorResponse connectorResponse) throws
/**
* Gets the body of the response as a {@link String}.
*
* @param connectorResponse
* the response to read
* @return the body of the response as a {@link String}.
* @throws IOException
* if an I/O Exception occurs.
* @param connectorResponse
*/
static String getBodyAsStringOrNull(GitHubConnectorResponse connectorResponse) {
try {
return getBodyAsString(connectorResponse);
} catch (NullPointerException | IOException e) {
} catch (IOException e) {
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public String header(String name) {
*
* @return the response body
* @throws IOException
* if an I/O Exception occurs.
* if response stream is null or an I/O Exception occurs.
*/
@Nonnull
public abstract InputStream bodyStream() throws IOException;

/**
Expand Down Expand Up @@ -121,7 +122,7 @@ public Map<String, List<String>> allHeaders() {
}

/**
* Handles the "Content-Encoding" header.
* Handles wrapping the body stream if indicated by the "Content-Encoding" header.
*
* @param stream
* the stream to possibly wrap
Expand All @@ -139,6 +140,24 @@ protected InputStream wrapStream(InputStream stream) throws IOException {
throw new UnsupportedOperationException("Unexpected Content-Encoding: " + encoding);
}

/**
* Parse a header value as a signed decimal integer.
*
* @param name
* the header field to parse
* @return integer value of the header field
* @throws NumberFormatException
* if the header is missing or does not contain a parsable integer.
*/
public final int parseInt(String name) throws NumberFormatException {
try {
String headerValue = header(name);
return Integer.parseInt(headerValue);
} catch (NumberFormatException e) {
throw new NumberFormatException(name + ": " + e.getMessage());
}
}

public abstract static class ByteArrayResponse extends GitHubConnectorResponse {

private boolean inputStreamRead = false;
Expand All @@ -155,6 +174,7 @@ protected ByteArrayResponse(@Nonnull GitHubConnectorRequest request,
* {@inheritDoc}
*/
@Override
@Nonnull
public InputStream bodyStream() throws IOException {
if (isClosed) {
throw new IOException("Response is closed");
Expand All @@ -171,7 +191,11 @@ public InputStream bodyStream() throws IOException {
}
}

return inputBytes == null ? null : new ByteArrayInputStream(inputBytes);
if (inputBytes == null) {
throw new IOException("Response body missing, stream null");
}

return new ByteArrayInputStream(inputBytes);
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/kohsuke/github/internal/EnumUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ public static <E extends Enum<E>> E getNullableEnumOrDefault(Class<E> enumClass,
*/
public static <E extends Enum<E>> E getEnumOrDefault(Class<E> enumClass, String value, E defaultEnum) {
try {
return Enum.valueOf(enumClass, value.toUpperCase(Locale.ROOT));
} catch (NullPointerException | IllegalArgumentException e) {
LOGGER.warning("Unknown value " + value + " for enum class " + enumClass.getName() + ", defaulting to "
+ defaultEnum.name());
return defaultEnum;
if (value != null) {
return Enum.valueOf(enumClass, value.toUpperCase(Locale.ROOT));
}
} catch (IllegalArgumentException e) {
}

LOGGER.warning("Unknown value " + value + " for enum class " + enumClass.getName() + ", defaulting to "
+ defaultEnum.name());
return defaultEnum;
}

private EnumUtils() {
Expand Down