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

pkiraly / metadata-qa-api / #730

23 Oct 2025 09:26AM UTC coverage: 86.354% (-0.06%) from 86.412%
#730

push

pkiraly
Fix OR, AND and ContentType checkers

15 of 34 new or added lines in 5 files covered. (44.12%)

1 existing line in 1 file now uncovered.

5670 of 6566 relevant lines covered (86.35%)

0.86 hits per line

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

76.47
/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.List;
9
import java.util.logging.Logger;
10

11
public class ContentTypeExtractor {
12
  private static final Logger LOGGER = Logger.getLogger(ContentTypeExtractor.class.getCanonicalName());
1✔
13
  public static final int DEFAULT_TIMEOUT = 5000;
14
  private static final List<String> HEADER_KEYS = List.of("Content-Type", "content-type");
1✔
15

16
  private int timeout;
17
  private boolean debug = false;
1✔
18

19
  public ContentTypeExtractor() {
×
20
    this.timeout = DEFAULT_TIMEOUT;
×
21
  }
×
22

23
  public ContentTypeExtractor(int timeout) {
1✔
24
    this.timeout = timeout;
1✔
25
  }
1✔
26

27
  public String getContentType(String url) throws IOException {
28
    String contentType = null;
1✔
29
    URL urlObj = new URL(url);
1✔
30
    HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
1✔
31

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

56
  private String extractContentType(HttpURLConnection urlConnection) {
57
    String contentType = null;
1✔
58
    for (String key : HEADER_KEYS) {
1✔
59
      contentType = urlConnection.getHeaderField(key);
1✔
60
      if (StringUtils.isNotBlank(contentType))
1✔
61
        break;
1✔
62
    }
×
63
    return contentType;
1✔
64
  }
65

66
  public void setDebug() {
NEW
67
    this.debug = true;
×
NEW
68
  }
×
69
}
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