Cache MimeTypes to improve performance

See gh-16507
pull/16502/head
wenqi.huang 6 years ago committed by Stephane Nicoll
parent 0875a07fe5
commit b3f94c47d9

@ -17,7 +17,9 @@
package org.springframework.boot.web.embedded.netty; package org.springframework.boot.web.embedded.netty;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpHeaders;
@ -67,6 +69,10 @@ final class CompressionCustomizer implements NettyServerCustomizer {
if (ObjectUtils.isEmpty(mimeTypes)) { if (ObjectUtils.isEmpty(mimeTypes)) {
return ALWAYS_COMPRESS; return ALWAYS_COMPRESS;
} }
List<MimeType> mimeTypeList = Arrays.stream(mimeTypes)
.map(MimeTypeUtils::parseMimeType).collect(Collectors.toList());
return (request, response) -> { return (request, response) -> {
String contentType = response.responseHeaders() String contentType = response.responseHeaders()
.get(HttpHeaderNames.CONTENT_TYPE); .get(HttpHeaderNames.CONTENT_TYPE);
@ -74,7 +80,7 @@ final class CompressionCustomizer implements NettyServerCustomizer {
return false; return false;
} }
MimeType contentMimeType = MimeTypeUtils.parseMimeType(contentType); MimeType contentMimeType = MimeTypeUtils.parseMimeType(contentType);
return Arrays.stream(mimeTypes).map(MimeTypeUtils::parseMimeType) return mimeTypeList.stream()
.anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType)); .anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType));
}; };
} }

Loading…
Cancel
Save