• 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

78.26
/src/main/java/de/gwdg/metadataqa/api/util/ContentTypeExtractor.java
1
package de.gwdg.metadataqa.api.util;
2

3
import org.apache.commons.lang3.StringUtils;
4

5
import java.io.IOException;
6
import java.net.HttpURLConnection;
7
import java.net.URL;
8
import java.util.logging.Logger;
9

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

15
  public ContentTypeExtractor() {
×
16
    this.timeout = DEFAULT_TIMEOUT;
×
17
  }
×
18

19
  public ContentTypeExtractor(int timeout) {
1✔
20
    this.timeout = timeout;
1✔
21
  }
1✔
22

23
  public String getContentType(String url) throws IOException {
24
    String contentType = null;
1✔
25
    URL urlObj = new URL(url);
1✔
26
    HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
1✔
27

28
    urlConnection.setConnectTimeout(timeout);
1✔
29
    urlConnection.setReadTimeout(timeout);
1✔
30
    urlConnection.connect();
1✔
31
    int responseCode = urlConnection.getResponseCode();
1✔
32
    if (responseCode == 200) {
1✔
33
      String rawContentType = urlConnection.getHeaderField("Content-Type");
1✔
34
      if (rawContentType != null && StringUtils.isNotBlank(rawContentType))
1✔
35
        contentType = rawContentType.replaceAll("; ?charset.*$", "");
1✔
36
    } else if (responseCode == 301 // Moved Permanently
1✔
37
            || responseCode == 302 // Found
38
            || responseCode == 303 // See Other
39
            || responseCode == 304 // Not Modified
40
            || responseCode == 307 // Temporary Redirect
41
            || responseCode == 308 // Permanent Redirect
42
    ) {
UNCOV
43
      String location = urlConnection.getHeaderField("Location");
×
UNCOV
44
      return getContentType(location);
×
45
    } else {
46
      LOGGER.warning(String.format("URL %s returns unhandled status code: %d.\n", url, responseCode));
1✔
47
    }
48
    return contentType;
1✔
49
  }
50

51
}
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