Skip to content

Commit

Permalink
Include IOException in the retry mechanism (#1052) (#1053)
Browse files Browse the repository at this point in the history
* Include IOException in the retry mechanism

Signed-off-by: mrizzi <[email protected]>

* Added 'User-Agent' property to links test

Signed-off-by: mrizzi <[email protected]>

---------

Signed-off-by: mrizzi <[email protected]>
(cherry picked from commit f1d379b)

Co-authored-by: Marco Rizzi <[email protected]>
  • Loading branch information
github-actions[bot] and mrizzi authored Jan 16, 2024
1 parent 3c8faf6 commit 98587bc
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
* This tests all the href attribute in ALL the rules every time the tests are executed (PR and nightly builds)
* Helpful to ensure we have always links that points to something so that links are really helpful for the user
* Optimized with a very basic cache for not checking more than once the same URL
*
* <p>
* The runTestsMatching is available in this test
*
* <p>
* To change the connection timeout use the standard "-Dsun.net.client.defaultConnectTimeout=<value>" property
* from https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html
*
* <p>
* To quickly see the debug log, execute the test with the property "-Dorg.slf4j.simpleLogger.log.org.jboss.windup=debug"
*/
@RunWith(Parameterized.class)
Expand Down Expand Up @@ -158,30 +158,31 @@ private boolean isValidLink(final String link)
if (CERT_FAILURE_LINKS.contains(link))
return true;

try {
final long starTime = System.currentTimeMillis();
int returnCode = 0;
if (!CACHE_ANALYZED_LINKS.containsKey(link))
{
for (int retry = 0; !ACCEPTED_RESPONSE_CODE.contains(returnCode) && retry < RETRIES; retry++) {
if (retry > 0) LOG.warn(String.format("Tentative #%d to connect to %s", retry + 1, link));
final long starTime = System.currentTimeMillis();
int returnCode = 0;
if (!CACHE_ANALYZED_LINKS.containsKey(link)) {
for (int retry = 0; !ACCEPTED_RESPONSE_CODE.contains(returnCode) && retry < RETRIES; retry++) {
if (retry > 0) LOG.warn(String.format("Tentative #%d to connect to %s", retry + 1, link));
try {
final URL url = new URL(link);
final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// property name from https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html
urlConn.setConnectTimeout(Integer.getInteger("sun.net.client.defaultConnectTimeout", 5000));
urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0");
urlConn.connect();
returnCode = urlConn.getResponseCode();
} catch (IOException e) {
LOG.error(String.format("'%s' exception connecting to %s", e.getMessage(), link), e);
}
CACHE_ANALYZED_LINKS.put(link, returnCode);
}
final boolean validLink = ACCEPTED_RESPONSE_CODE.contains(CACHE_ANALYZED_LINKS.get(link));
if (validLink) LOG.debug(String.format("Response code %d for %s [%dms]", CACHE_ANALYZED_LINKS.get(link), link, System.currentTimeMillis() - starTime));
else LOG.error(String.format("Response code %d for %s [%dms]", CACHE_ANALYZED_LINKS.get(link), link, System.currentTimeMillis() - starTime));
return validLink;
} catch (IOException e) {
LOG.error(String.format("'%s' exception connecting to %s", e.getMessage(), link), e);
return false;
CACHE_ANALYZED_LINKS.put(link, returnCode);
}
final boolean validLink = ACCEPTED_RESPONSE_CODE.contains(CACHE_ANALYZED_LINKS.get(link));
if (validLink)
LOG.debug(String.format("Response code %d for %s [%dms]", CACHE_ANALYZED_LINKS.get(link), link, System.currentTimeMillis() - starTime));
else
LOG.error(String.format("Response code %d for %s [%dms]", CACHE_ANALYZED_LINKS.get(link), link, System.currentTimeMillis() - starTime));
return validLink;
}

private String buildListOfFailedRulesLinks(final Map<String, List<String>> failedRulesWithLinks)
Expand Down

0 comments on commit 98587bc

Please sign in to comment.