/*
 * Copyright 2012-2018 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.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.web.embedded.netty;

import java.util.Arrays;
import java.util.function.BiPredicate;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import reactor.ipc.netty.http.server.HttpServerOptions;
import reactor.ipc.netty.http.server.HttpServerRequest;
import reactor.ipc.netty.http.server.HttpServerResponse;

import org.springframework.boot.web.server.Compression;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * Configure the HTTP compression on an Reactor Netty request/response handler.
 *
 * @author Stephane Maldini
 * @author Phillip Webb
 */
final class CompressionCustomizer implements NettyServerCustomizer {

	private final Compression compression;

	CompressionCustomizer(Compression compression) {
		this.compression = compression;
	}

	@Override
	public void customize(HttpServerOptions.Builder builder) {
		if (this.compression.getMinResponseSize() >= 0) {
			builder.compression(this.compression.getMinResponseSize());
		}
		CompressionPredicate mimeTypes = getMimeTypesPredicate(
				this.compression.getMimeTypes());
		CompressionPredicate excludedUserAgents = getExcludedUserAgentsPredicate(
				this.compression.getExcludedUserAgents());
		builder.compression(mimeTypes.and(excludedUserAgents));
	}

	private CompressionPredicate getMimeTypesPredicate(String[] mimeTypes) {
		if (ObjectUtils.isEmpty(mimeTypes)) {
			return CompressionPredicate.ALWAYS;
		}
		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)
					.anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType));
		};
	}

	private CompressionPredicate getExcludedUserAgentsPredicate(
			String[] excludedUserAgents) {
		if (ObjectUtils.isEmpty(excludedUserAgents)) {
			return CompressionPredicate.ALWAYS;
		}
		return (request, response) -> {
			HttpHeaders headers = request.requestHeaders();
			return Arrays.stream(excludedUserAgents).noneMatch((candidate) -> headers
					.contains(HttpHeaderNames.USER_AGENT, candidate, true));
		};
	}

	private interface CompressionPredicate
			extends BiPredicate<HttpServerRequest, HttpServerResponse> {

		static final CompressionPredicate ALWAYS = (request, response) -> true;

	}

}

@@ -1453,16 +1453,110 @@
 izer %7B%0A%0A
+%09private static final CompressionPredicate ALWAYS_COMPRESS = (request,%0A%09%09%09response) -%3E true;%0A%0A
 %09private
@@ -1587,16 +1587,16 @@
 ession;%0A
-
 %0A%09Compre
@@ -2275,34 +2275,22 @@
 urn 
-CompressionPredicate.ALWAY
+ALWAYS_COMPRES
 S;%0A%09
@@ -2869,34 +2869,22 @@
 urn 
-CompressionPredicate.ALWAY
+ALWAYS_COMPRES
 S;%0A%09
@@ -3159,16 +3159,16 @@
 edicate%0A
+
 %09%09%09exten
@@ -3228,83 +3228,8 @@
  %7B%0A%0A
-%09%09static final CompressionPredicate ALWAYS = (request, response) -%3E true;%0A%0A
 %09%7D%0A%0A

Fix Checkstyle violations