Skip to content

Commit

Permalink
Merge pull request #16507 from aftersss
Browse files Browse the repository at this point in the history
* pr/16507:
  Polish "Cache MimeTypes to improve performance"
  Cache MimeTypes to improve performance
  • Loading branch information
snicoll committed Apr 19, 2019
2 parents 0875a07 + 2448efc commit 6774cc5
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,9 @@
package org.springframework.boot.web.embedded.netty;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
Expand Down Expand Up @@ -63,18 +65,20 @@ public HttpServer apply(HttpServer server) {
return server;
}

private CompressionPredicate getMimeTypesPredicate(String[] mimeTypes) {
if (ObjectUtils.isEmpty(mimeTypes)) {
private CompressionPredicate getMimeTypesPredicate(String[] mimeTypeIds) {
if (ObjectUtils.isEmpty(mimeTypeIds)) {
return ALWAYS_COMPRESS;
}
List<MimeType> mimeTypes = Arrays.stream(mimeTypeIds)
.map(MimeTypeUtils::parseMimeType).collect(Collectors.toList());
return (request, response) -> {
String contentType = response.responseHeaders()
.get(HttpHeaderNames.CONTENT_TYPE);
if (StringUtils.isEmpty(contentType)) {
return false;
}
MimeType contentMimeType = MimeTypeUtils.parseMimeType(contentType);
return Arrays.stream(mimeTypes).map(MimeTypeUtils::parseMimeType)
return mimeTypes.stream()
.anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType));
};
}
Expand Down

0 comments on commit 6774cc5

Please sign in to comment.