• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkiraly / metadata-qa-api / #699

08 Jul 2025 01:41PM UTC coverage: 86.612% (-0.01%) from 86.623%
#699

push

pkiraly
Improve link validation

4 of 4 new or added lines in 2 files covered. (100.0%)

5 existing lines in 2 files now uncovered.

5609 of 6476 relevant lines covered (86.61%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.19
/src/main/java/de/gwdg/metadataqa/api/util/LinkValidator.java
1
package de.gwdg.metadataqa.api.util;
2

3
import java.io.IOException;
4
import java.net.HttpURLConnection;
5
import java.net.URL;
6
import java.util.logging.Logger;
7

8
public class LinkValidator {
9
  private static final Logger LOGGER = Logger.getLogger(LinkValidator.class.getCanonicalName());
1✔
10
  public static final int DEFAULT_TIMEOUT = 5000;
11
  private int timeout;
12

13
  public LinkValidator() {
14
    this(DEFAULT_TIMEOUT);
×
15
  }
×
16

17
  public LinkValidator(int timeout) {
1✔
18
    this.timeout = timeout;
1✔
19
  }
1✔
20

21
  public boolean isValid(String url) throws IOException {
22
    URL urlObj = new URL(url);
1✔
23
    HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
1✔
24

25
    urlConnection.setConnectTimeout(timeout);
1✔
26
    urlConnection.setReadTimeout(timeout);
1✔
27
    urlConnection.connect();
1✔
28
    int responseCode = urlConnection.getResponseCode();
1✔
29
    if (responseCode == 200) {
1✔
UNCOV
30
      return true;
×
31
    } else if (responseCode == 301 // Moved Permanently
1✔
32
            || responseCode == 302 // Found
33
            || responseCode == 303 // See Other
34
            || responseCode == 304 // Not Modified
35
            || responseCode == 307 // Temporary Redirect
36
            || responseCode == 308 // Permanent Redirect
37
    ) {
UNCOV
38
      String location = urlConnection.getHeaderField("Location");
×
UNCOV
39
      return isValid(location);
×
40
    } else if (responseCode == 401 // Unauthorized
1✔
41
            || responseCode == 402 // Payment Required
42
            || responseCode == 403 // Forbidden
43
            || responseCode == 407 // Proxy Authentication Required
44
    ) {
45
      return true;
1✔
46
    } else {
47
      LOGGER.warning(String.format("URL %s returns unhandled status code: %d.\n", url, responseCode));
1✔
48
    }
49
    return false;
1✔
50
  }
51

52
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc