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

zalando / logbook / #4471

04 Mar 2024 07:51PM CUT coverage: 100.0%. Remained the same
#4471

push

web-flow
Add a warning about logbook.write.max-body-size not preventing the buffering (#1777)

3539 of 3539 relevant lines covered (100.0%)

1.0 hits per line

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

100.0
/logbook-api/src/main/java/org/zalando/logbook/ContentType.java
1
package org.zalando.logbook;
2

3
import lombok.experimental.UtilityClass;
4

5
import javax.annotation.Nullable;
6
import java.nio.charset.Charset;
7
import java.nio.charset.IllegalCharsetNameException;
8
import java.nio.charset.StandardCharsets;
9
import java.nio.charset.UnsupportedCharsetException;
10

11

12
@UtilityClass
13
public class ContentType {
14

15
    @Nullable
16
    String parseMimeType(@Nullable String contentTypeValue) {
17
        if (contentTypeValue != null) {
1✔
18
            int indexOfSemicolon = contentTypeValue.indexOf(SEMICOLON);
1✔
19
            if (indexOfSemicolon != -1) {
1✔
20
                return contentTypeValue.substring(0, indexOfSemicolon);
1✔
21
            } else {
22
                return contentTypeValue.length() > 0 ? contentTypeValue : null;
1✔
23
            }
24
        }
25
        return null;
1✔
26
    }
27

28
    @Nullable
29
    public Charset parseCharset(@Nullable String contentTypeValue) {
30
        if (contentTypeValue != null) {
1✔
31
            String charsetRaw = null;
1✔
32

33
            int indexOfCharset = contentTypeValue.toLowerCase().indexOf(CHARSET_PREFIX);
1✔
34
            if (indexOfCharset != -1) {
1✔
35
                int indexOfEncoding = indexOfCharset + CHARSET_PREFIX.length();
1✔
36
                if (indexOfEncoding < contentTypeValue.length()) {
1✔
37
                    String charsetCandidate = contentTypeValue.substring(indexOfEncoding);
1✔
38
                    int indexOfSemicolon = charsetCandidate.indexOf(SEMICOLON);
1✔
39
                    charsetRaw = indexOfSemicolon == -1 ? charsetCandidate : charsetCandidate.substring(0, indexOfSemicolon);
1✔
40
                }
41
            }
42

43
            if (charsetRaw != null) {
1✔
44
                if (charsetRaw.length() > 2) {
1✔
45
                    if (charsetRaw.charAt(0) == '"' && charsetRaw.charAt(charsetRaw.length() - 1) == '"') {
1✔
46
                        charsetRaw = charsetRaw.substring(1, charsetRaw.length() - 1);
1✔
47
                    }
48
                }
49
                try {
50
                    return Charset.forName(charsetRaw);
1✔
51
                } catch (IllegalCharsetNameException | UnsupportedCharsetException ignored) {
1✔
52
                    // ignore
53
                }
54
            }
55
        }
56

57
        if (isJsonMediaType(contentTypeValue)) {
1✔
58
            /*
59
             * RFC 8259
60
             * JSON text exchanged between systems that are not part of a closed
61
             * ecosystem MUST be encoded using UTF-8.
62
             */
63
            return StandardCharsets.UTF_8;
1✔
64
        }
65
        return null;
1✔
66
    }
67

68
    public static boolean isJsonMediaType(@Nullable final String contentType) {
69
        if (contentType == null) {
1✔
70
            return false;
1✔
71
        }
72
        final String lowerCasedContentType = contentType.toLowerCase();
1✔
73
        if (lowerCasedContentType.startsWith("application/")) {
1✔
74
            String mediaType = parseMimeType(lowerCasedContentType);
1✔
75
            return mediaType.endsWith("/json") || mediaType.endsWith("+json");
1✔
76
        }
77
        return false;
1✔
78
    }
79

80
    public static final String CONTENT_TYPE_HEADER = "Content-Type";
81
    private static final String SEMICOLON = ";";
82
    private static final String CHARSET_PREFIX = "charset=";
83
}
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

© 2025 Coveralls, Inc