From 4b6aea18e31e862ea91e1e18c32b63c4ad8df481 Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Fri, 27 Sep 2024 10:44:38 +0200 Subject: [PATCH] Expose the mime mapping class as API --- .../core/http/{impl => }/MimeMapping.java | 21 +++++++++++++------ .../core/http/impl/Http1xServerResponse.java | 8 ++----- .../core/http/impl/Http2ServerResponse.java | 9 ++------ 3 files changed, 19 insertions(+), 19 deletions(-) rename vertx-core/src/main/java/io/vertx/core/http/{impl => }/MimeMapping.java (98%) diff --git a/vertx-core/src/main/java/io/vertx/core/http/impl/MimeMapping.java b/vertx-core/src/main/java/io/vertx/core/http/MimeMapping.java similarity index 98% rename from vertx-core/src/main/java/io/vertx/core/http/impl/MimeMapping.java rename to vertx-core/src/main/java/io/vertx/core/http/MimeMapping.java index 94147774c6f..a345ac69004 100644 --- a/vertx-core/src/main/java/io/vertx/core/http/impl/MimeMapping.java +++ b/vertx-core/src/main/java/io/vertx/core/http/MimeMapping.java @@ -9,7 +9,7 @@ * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 */ -package io.vertx.core.http.impl; +package io.vertx.core.http; import java.util.HashMap; import java.util.Map; @@ -20,7 +20,7 @@ * * @author Tim Fox */ -public class MimeMapping { +public final class MimeMapping { private static final Map m = new HashMap<>(); static { @@ -1013,14 +1013,23 @@ public class MimeMapping { m.put("ice", "x-conference/x-cooltalk"); } - public static String getMimeTypeForExtension(String ext) { + /** + * @param ext the file name extension + * @return the matching mime type for a file extension (e.g. {@code .mkv}) or {@code null} + */ + public static String mimeTypeForExtension(String ext) { return m.get(ext); } - public static String getMimeTypeForFilename(String filename) { + + /** + * @param filename the file name + * @return the matching mime type for a file name or {@code null}, the file extension is used for lookup + */ + public static String mimeTypeForFilename(String filename) { int li = filename.lastIndexOf('.'); if (li != -1 && li != filename.length() - 1) { - String ext = filename.substring(li + 1, filename.length()); - return MimeMapping.getMimeTypeForExtension(ext); + String ext = filename.substring(li + 1); + return mimeTypeForExtension(ext); } return null; } diff --git a/vertx-core/src/main/java/io/vertx/core/http/impl/Http1xServerResponse.java b/vertx-core/src/main/java/io/vertx/core/http/impl/Http1xServerResponse.java index 551df458b7e..81e4031a9a8 100644 --- a/vertx-core/src/main/java/io/vertx/core/http/impl/Http1xServerResponse.java +++ b/vertx-core/src/main/java/io/vertx/core/http/impl/Http1xServerResponse.java @@ -28,12 +28,8 @@ import io.vertx.core.MultiMap; import io.vertx.core.Promise; import io.vertx.core.buffer.Buffer; +import io.vertx.core.http.*; import io.vertx.core.internal.buffer.BufferInternal; -import io.vertx.core.http.Cookie; -import io.vertx.core.http.HttpClosedException; -import io.vertx.core.http.HttpHeaders; -import io.vertx.core.http.HttpMethod; -import io.vertx.core.http.HttpServerResponse; import io.vertx.core.http.impl.headers.HeadersMultiMap; import io.vertx.core.internal.ContextInternal; import io.vertx.core.internal.VertxInternal; @@ -459,7 +455,7 @@ public Future sendFile(String filename, long offset, long length) { } if (!headers.contains(HttpHeaders.CONTENT_TYPE)) { - String contentType = MimeMapping.getMimeTypeForFilename(filename); + String contentType = MimeMapping.mimeTypeForFilename(filename); if (contentType != null) { headers.set(HttpHeaders.CONTENT_TYPE, contentType); } diff --git a/vertx-core/src/main/java/io/vertx/core/http/impl/Http2ServerResponse.java b/vertx-core/src/main/java/io/vertx/core/http/impl/Http2ServerResponse.java index 0ef476e7aac..e1f90d91306 100644 --- a/vertx-core/src/main/java/io/vertx/core/http/impl/Http2ServerResponse.java +++ b/vertx-core/src/main/java/io/vertx/core/http/impl/Http2ServerResponse.java @@ -25,13 +25,8 @@ import io.vertx.core.MultiMap; import io.vertx.core.Promise; import io.vertx.core.buffer.Buffer; +import io.vertx.core.http.*; import io.vertx.core.internal.buffer.BufferInternal; -import io.vertx.core.http.Cookie; -import io.vertx.core.http.HttpHeaders; -import io.vertx.core.http.HttpMethod; -import io.vertx.core.http.HttpServerResponse; -import io.vertx.core.http.StreamPriority; -import io.vertx.core.http.StreamResetException; import io.vertx.core.http.impl.headers.Http2HeadersAdaptor; import io.vertx.core.internal.PromiseInternal; import io.vertx.core.net.HostAndPort; @@ -559,7 +554,7 @@ public Future sendFile(String filename, long offset, long length) { putHeader(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(contentLength)); } if (headers.get(HttpHeaderNames.CONTENT_TYPE) == null) { - String contentType = MimeMapping.getMimeTypeForFilename(filename); + String contentType = MimeMapping.mimeTypeForFilename(filename); if (contentType != null) { putHeader(HttpHeaderNames.CONTENT_TYPE, contentType); }