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

pkiraly / metadata-qa-api / #713

12 Aug 2025 06:23PM UTC coverage: 86.646% (-0.01%) from 86.658%
#713

push

pkiraly
Merge branch 'main' of github.com:pkiraly/metadata-qa-api

5619 of 6485 relevant lines covered (86.65%)

0.87 hits per line

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

80.65
/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

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

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

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

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

55
  private String extractContentType(HttpURLConnection urlConnection) {
56
    String contentType = null;
1✔
57
    for (String key : HEADER_KEYS) {
1✔
58
      contentType = urlConnection.getHeaderField(key);
1✔
59
      if (StringUtils.isNotBlank(contentType))
1✔
60
        break;
1✔
61
    }
×
62
    return contentType;
1✔
63
  }
64
}
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