|
| 1 | +/* |
| 2 | + * Copyright 2015-2025 the original author or authors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | + * accompanies this distribution and is available at |
| 7 | + * |
| 8 | + * https://www.eclipse.org/legal/epl-v20.html |
| 9 | + */ |
| 10 | + |
| 11 | +package org.junit.jupiter.api; |
| 12 | + |
| 13 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 14 | +import static org.apiguardian.api.API.Status.MAINTAINED; |
| 15 | + |
| 16 | +import java.nio.charset.Charset; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.regex.Matcher; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +import org.apiguardian.api.API; |
| 23 | +import org.jspecify.annotations.Nullable; |
| 24 | +import org.junit.platform.commons.PreconditionViolationException; |
| 25 | +import org.junit.platform.commons.util.Preconditions; |
| 26 | + |
| 27 | +/** |
| 28 | + * Represents a media type as defined by |
| 29 | + * <a href="https://tools.ietf.org/html/rfc2045">RFC 2045</a>. |
| 30 | + * |
| 31 | + * <p><strong>WARNING</strong>: This type should not be extended by third parties. |
| 32 | + * |
| 33 | + * @since 6.0 |
| 34 | + * @see TestReporter#publishFile(Path, MediaType) |
| 35 | + * @see TestReporter#publishFile(String, MediaType, org.junit.jupiter.api.function.ThrowingConsumer) |
| 36 | + * @see org.junit.jupiter.api.extension.ExtensionContext#publishFile(String, MediaType, org.junit.jupiter.api.function.ThrowingConsumer) |
| 37 | + */ |
| 38 | +@API(status = MAINTAINED, since = "6.0") |
| 39 | +public class MediaType { |
| 40 | + |
| 41 | + private static final Pattern PATTERN; |
| 42 | + |
| 43 | + static { |
| 44 | + // https://datatracker.ietf.org/doc/html/rfc2045#section-5.1 |
| 45 | + String whitespace = "[ \t]*"; |
| 46 | + String token = "[0-9A-Za-z!#$%&'*+.^_`|~-]+"; |
| 47 | + String quotedString = "\"(?:[^\"\\\\]|\\.)*\""; |
| 48 | + String parameter = ";" + whitespace + token + "=" + "(?:" + token + "|" + quotedString + ")"; |
| 49 | + PATTERN = Pattern.compile(token + "/" + token + "(?:" + whitespace + parameter + ")*"); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * The {@code text/plain} media type. |
| 54 | + */ |
| 55 | + public static final MediaType TEXT_PLAIN = create("text", "plain"); |
| 56 | + |
| 57 | + /** |
| 58 | + * The {@code text/plain; charset=UTF-8} media type. |
| 59 | + */ |
| 60 | + public static final MediaType TEXT_PLAIN_UTF_8 = create("text", "plain", UTF_8); |
| 61 | + |
| 62 | + /** |
| 63 | + * The {@code application/json} media type. |
| 64 | + */ |
| 65 | + public static final MediaType APPLICATION_JSON = create("application", "json"); |
| 66 | + |
| 67 | + /** |
| 68 | + * The {@code application/octet-stream} media type. |
| 69 | + */ |
| 70 | + public static final MediaType APPLICATION_OCTET_STREAM = create("application", "octet-stream"); |
| 71 | + |
| 72 | + /** |
| 73 | + * The {@code image/jpeg} media type. |
| 74 | + */ |
| 75 | + public static final MediaType IMAGE_JPEG = create("image", "jpeg"); |
| 76 | + |
| 77 | + /** |
| 78 | + * The {@code image/png} media type. |
| 79 | + */ |
| 80 | + public static final MediaType IMAGE_PNG = create("image", "png"); |
| 81 | + |
| 82 | + private final String value; |
| 83 | + |
| 84 | + /** |
| 85 | + * Parse the given media type value. |
| 86 | + * |
| 87 | + * <p>Must be valid according to |
| 88 | + * <a href="https://tools.ietf.org/html/rfc2045">RFC 2045</a>. |
| 89 | + * |
| 90 | + * @param value the media type value to parse; never {@code null} or blank |
| 91 | + * @return the parsed media type |
| 92 | + * @throws PreconditionViolationException if the value is not a valid media type |
| 93 | + */ |
| 94 | + public static MediaType parse(String value) { |
| 95 | + return new MediaType(value); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Create a media type with the given type and subtype. |
| 100 | + * |
| 101 | + * @param type the type; never {@code null} or blank |
| 102 | + * @param subtype the subtype; never {@code null} or blank |
| 103 | + * @return the media type |
| 104 | + */ |
| 105 | + public static MediaType create(String type, String subtype) { |
| 106 | + return new MediaType(type, subtype, null); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Create a media type with the given type, subtype, and charset. |
| 111 | + * |
| 112 | + * @param type the type; never {@code null} or blank |
| 113 | + * @param subtype the subtype; never {@code null} or blank |
| 114 | + * @param charset the charset; never {@code null} |
| 115 | + * @return the media type |
| 116 | + */ |
| 117 | + public static MediaType create(String type, String subtype, Charset charset) { |
| 118 | + Preconditions.notNull(charset, "charset must not be null"); |
| 119 | + return new MediaType(type, subtype, charset); |
| 120 | + } |
| 121 | + |
| 122 | + protected MediaType(String type, String subtype, @Nullable Charset charset) { |
| 123 | + this("%s/%s%s".formatted(// |
| 124 | + Preconditions.notBlank(type, "type must not be null or blank").strip(), |
| 125 | + Preconditions.notBlank(subtype, "subtype must not be null or blank").strip(), |
| 126 | + (charset != null ? ("; charset=" + charset.name()) : ""))); |
| 127 | + } |
| 128 | + |
| 129 | + protected MediaType(String value) { |
| 130 | + // Mimic sealed types, permitting only this class and api.extension.MediaType. |
| 131 | + if (getClass() != MediaType.class |
| 132 | + && !getClass().getName().equals("org.junit.jupiter.api.extension.MediaType")) { |
| 133 | + throw new IllegalStateException( |
| 134 | + "Type '%s' is not permitted to extend MediaType".formatted(getClass().getName())); |
| 135 | + } |
| 136 | + |
| 137 | + String strippedValue = Preconditions.notBlank(value, "value must not be null or blank").strip(); |
| 138 | + Matcher matcher = PATTERN.matcher(strippedValue); |
| 139 | + Preconditions.condition(matcher.matches(), () -> "Invalid media type: '" + strippedValue + "'"); |
| 140 | + this.value = strippedValue; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * {@return a string representation of this media type} |
| 145 | + */ |
| 146 | + @Override |
| 147 | + public final String toString() { |
| 148 | + return this.value; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public final boolean equals(Object obj) { |
| 153 | + return this == obj || (obj instanceof MediaType that && Objects.equals(this.value, that.value)); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public final int hashCode() { |
| 158 | + return Objects.hashCode(this.value); |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments