Skip to content

Commit

Permalink
More HttpURLConnection cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Sep 14, 2024
1 parent b523dba commit 71984a1
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 164 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/kohsuke/github/GitHubAbuseLimitHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import javax.annotation.Nonnull;

import static java.net.HttpURLConnection.HTTP_FORBIDDEN;

// TODO: Auto-generated Javadoc
/**
* Pluggable strategy to determine what to do when the API rate limit is reached.
Expand Down Expand Up @@ -56,7 +57,7 @@ private boolean isTooManyRequests(GitHubConnectorResponse connectorResponse) {
* @return true if the status code is HTTP_FORBIDDEN
*/
private boolean isForbidden(GitHubConnectorResponse connectorResponse) {
return connectorResponse.statusCode() == HttpURLConnection.HTTP_FORBIDDEN;
return connectorResponse.statusCode() == HTTP_FORBIDDEN;
}

/**
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/org/kohsuke/github/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.net.ssl.SSLHandshakeException;

import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
import static java.net.HttpURLConnection.*;
import static java.net.HttpURLConnection.HTTP_ACCEPTED;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
import static java.net.HttpURLConnection.HTTP_NOT_MODIFIED;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
import static java.util.logging.Level.*;
import static org.apache.commons.lang3.StringUtils.defaultString;

Expand Down Expand Up @@ -440,13 +444,6 @@ public <T> GitHubResponse<T> sendRequest(GitHubRequest request, @CheckForNull Bo
if (retries > 0 && e.connectorRequest != null) {
connectorRequest = e.connectorRequest;
}
} catch (SocketException | SocketTimeoutException | SSLHandshakeException e) {
// These transient errors thrown by HttpURLConnection
if (retries > 0) {
logRetryConnectionError(e, connectorRequest.url(), retries);
continue;
}
throw interpretApiError(e, connectorRequest, connectorResponse);
} catch (IOException e) {
throw interpretApiError(e, connectorRequest, connectorResponse);
} finally {
Expand Down Expand Up @@ -656,10 +653,10 @@ private static <T> GitHubResponse<T> createResponse(@Nonnull GitHubConnectorResp
}

private static boolean shouldIgnoreBody(@Nonnull GitHubConnectorResponse connectorResponse) {
if (connectorResponse.statusCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
if (connectorResponse.statusCode() == HTTP_NOT_MODIFIED) {
// special case handling for 304 unmodified, as the content will be ""
return true;
} else if (connectorResponse.statusCode() == HttpURLConnection.HTTP_ACCEPTED) {
} else if (connectorResponse.statusCode() == HTTP_ACCEPTED) {

// Response code 202 means data is being generated or an action that can require some time is triggered.
// This happens in specific cases:
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/kohsuke/github/GitHubRateLimitHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;

import javax.annotation.Nonnull;

import static java.net.HttpURLConnection.HTTP_FORBIDDEN;

// TODO: Auto-generated Javadoc
/**
* Pluggable strategy to determine what to do when the API rate limit is reached.
Expand All @@ -31,7 +32,7 @@ public abstract class GitHubRateLimitHandler extends GitHubConnectorResponseErro
*/
@Override
boolean isError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
return connectorResponse.statusCode() == HttpURLConnection.HTTP_FORBIDDEN
return connectorResponse.statusCode() == HTTP_FORBIDDEN
&& "0".equals(connectorResponse.header("X-RateLimit-Remaining"));
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/kohsuke/github/GitHubResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.logging.Level;
Expand All @@ -19,6 +18,9 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import static java.net.HttpURLConnection.HTTP_NO_CONTENT;


// TODO: Auto-generated Javadoc
/**
* A GitHubResponse
Expand Down Expand Up @@ -86,7 +88,7 @@ class GitHubResponse<T> {
@CheckForNull
static <T> T parseBody(GitHubConnectorResponse connectorResponse, Class<T> type) throws IOException {

if (connectorResponse.statusCode() == HttpURLConnection.HTTP_NO_CONTENT) {
if (connectorResponse.statusCode() == HTTP_NO_CONTENT) {
if (type != null && type.isArray()) {
// no content for array should be empty array
return type.cast(Array.newInstance(type.getComponentType(), 0));
Expand Down
4 changes: 0 additions & 4 deletions src/test/java/org/kohsuke/github/AbuseLimitHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Map;

Expand Down Expand Up @@ -67,14 +66,11 @@ protected WireMockConfiguration getWireMockOptions() {
public void testHandler_Fail() throws Exception {
// Customized response that templates the date to keep things working
snapshotNotAllowed();
final HttpURLConnection[] savedConnection = new HttpURLConnection[1];

gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl())
.withAbuseLimitHandler(new GitHubAbuseLimitHandler() {
@Override
public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException {
savedConnection[0] = null;
HttpURLConnection uc = null;
// Verify
// assertThat(GitHubClient.parseInstant(connectorResponse.header("Date")).toEpochMilli(),
// Matchers.greaterThanOrEqualTo(new Date().getTime() - 10000));
Expand Down
Loading

0 comments on commit 71984a1

Please sign in to comment.