Merge pull request #16507 from aftersss

* pr/16507:
  Polish "Cache MimeTypes to improve performance"
  Cache MimeTypes to improve performance
pull/16502/head
Stephane Nicoll 6 years ago
commit 6774cc5119

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -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;
@ -63,10 +65,12 @@ final class CompressionCustomizer implements NettyServerCustomizer {
return server; return server;
} }
private CompressionPredicate getMimeTypesPredicate(String[] mimeTypes) { private CompressionPredicate getMimeTypesPredicate(String[] mimeTypeIds) {
if (ObjectUtils.isEmpty(mimeTypes)) { if (ObjectUtils.isEmpty(mimeTypeIds)) {
return ALWAYS_COMPRESS; return ALWAYS_COMPRESS;
} }
List<MimeType> mimeTypes = Arrays.stream(mimeTypeIds)
.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 +78,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 mimeTypes.stream()
.anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType)); .anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType));
}; };
} }

Loading…
Cancel
Save