|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.io.InputStream; |
5 | 5 | import java.io.InputStreamReader; |
| 6 | +import java.net.HttpURLConnection; |
6 | 7 | import java.net.MalformedURLException; |
7 | 8 | import java.net.URL; |
8 | 9 | import java.net.URLConnection; |
|
24 | 25 |
|
25 | 26 | import com.sap.sse.common.Duration; |
26 | 27 | import com.sap.sse.common.TimePoint; |
| 28 | +import com.sap.sse.common.Util; |
27 | 29 | import com.sap.sse.common.Util.Pair; |
28 | 30 | import com.sap.sse.landscape.Release; |
29 | 31 | import com.sap.sse.landscape.ReleaseRepository; |
@@ -153,8 +155,8 @@ private ReleaseIterator() throws MalformedURLException, IOException, ParseExcept |
153 | 155 | } else { |
154 | 156 | logger.fine(()->"Need to fetch page with newest releases because last request was at "+ |
155 | 157 | (lastFetchOfNewestReleases==null?"<never>":lastFetchOfNewestReleases)); |
156 | | - lastFetchOfNewestReleases = now; |
157 | 158 | fillCacheWithNewestReleases(); |
| 159 | + lastFetchOfNewestReleases = now; |
158 | 160 | } |
159 | 161 | cachedReleasesIterator = releasesByPublishingTimePoint.descendingMap().values().iterator(); |
160 | 162 | } |
@@ -215,14 +217,36 @@ public GithubReleasesRepository(String owner, String repositoryName, String defa |
215 | 217 | * cache when this method is invoked. |
216 | 218 | * <p> |
217 | 219 | * |
| 220 | + * If the {@code GITHUB_TOKEN} environment variable is set, it is used as a bearer token forthe Github requests, |
| 221 | + * resulting in a higher rate limit. If no token is set, or if using the token results in a 401 response code |
| 222 | + * (authentication failed), an unauthenticated request is tried instead. |
| 223 | + * <p> |
| 224 | + * |
218 | 225 | * The method makes no changes to the cache or any other state of this instance. |
219 | 226 | * |
220 | 227 | * @return the link to the next page in the returned pair's {@link Pair#getA() A component}, and the sequence of |
221 | 228 | * releases loaded from that page with their publishing time points, ordered from newest to oldest. |
222 | 229 | */ |
223 | 230 | private synchronized Pair<String, Iterable<Pair<TimePoint, GithubRelease>>> getReleasesFromPage(String pageURL) throws IOException, ParseException { |
224 | 231 | logger.info("Requesting releases page "+pageURL); |
225 | | - final URLConnection connection = HttpUrlConnectionHelper.redirectConnection(new URL(pageURL)); |
| 232 | + final URLConnection connection; |
| 233 | + final String githubToken = System.getenv("GITHUB_TOKEN"); |
| 234 | + if (Util.hasLength(githubToken)) { |
| 235 | + final HttpURLConnection authenticatedConnectionAttempt = (HttpURLConnection) HttpUrlConnectionHelper |
| 236 | + .redirectConnectionWithBearerToken(new URL(pageURL), githubToken); |
| 237 | + if (authenticatedConnectionAttempt.getResponseCode() == 401) { |
| 238 | + try { |
| 239 | + authenticatedConnectionAttempt.disconnect(); |
| 240 | + } catch (Exception e) { |
| 241 | + logger.warning("Couldn't disconnect from Github: "+e.getMessage()); |
| 242 | + } |
| 243 | + connection = HttpUrlConnectionHelper.redirectConnection(new URL(pageURL)); |
| 244 | + } else { |
| 245 | + connection = authenticatedConnectionAttempt; |
| 246 | + } |
| 247 | + } else { |
| 248 | + connection = HttpUrlConnectionHelper.redirectConnection(new URL(pageURL)); |
| 249 | + } |
226 | 250 | final InputStream index = (InputStream) connection.getContent(); |
227 | 251 | final String xRatelimitRemaining = connection.getHeaderField("x-ratelimit-remaining"); |
228 | 252 | logger.fine(()->""+xRatelimitRemaining+" requests left in this hour"); |
|
0 commit comments