|
| 1 | +/* |
| 2 | + * Copyright © 2015 The Gravitee team (http://gravitee.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.gravitee.gateway.core.logging; |
| 17 | + |
| 18 | +import io.gravitee.gateway.reactive.api.ExecutionWarn; |
| 19 | +import io.gravitee.gateway.reactive.api.context.base.BaseExecutionContext; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | +import java.util.function.Predicate; |
| 23 | +import java.util.function.Supplier; |
| 24 | +import java.util.regex.Pattern; |
| 25 | +import java.util.regex.PatternSyntaxException; |
| 26 | +import java.util.stream.Stream; |
| 27 | +import lombok.Getter; |
| 28 | +import lombok.RequiredArgsConstructor; |
| 29 | +import lombok.Setter; |
| 30 | +import org.jspecify.annotations.Nullable; |
| 31 | + |
| 32 | +@RequiredArgsConstructor |
| 33 | +public class LoggableContentType { |
| 34 | + |
| 35 | + private static final String DEFAULT_EXCLUDED_CONTENT_TYPES = |
| 36 | + "video.*|audio.*|image.*|application/octet-stream|application/pdf|text/event-stream"; |
| 37 | + private Predicate<String> excludedContentTypesPattern; |
| 38 | + |
| 39 | + @Setter |
| 40 | + @Getter |
| 41 | + private String excludedResponseTypes; |
| 42 | + |
| 43 | + private final Supplier<String> customExcludedResponseTypes; |
| 44 | + |
| 45 | + public LoggableContentType() { |
| 46 | + this(() -> null); |
| 47 | + } |
| 48 | + |
| 49 | + private Predicate<String> matcher(Collection<Supplier<String>> regex, @Nullable BaseExecutionContext ctx) { |
| 50 | + return excludedContentTypesPattern != null |
| 51 | + ? excludedContentTypesPattern |
| 52 | + : (excludedContentTypesPattern = regex |
| 53 | + .stream() |
| 54 | + .map(Supplier::get) |
| 55 | + .filter(pattern -> pattern != null && !pattern.isEmpty()) |
| 56 | + .flatMap(pattern -> { |
| 57 | + try { |
| 58 | + return Stream.of(Pattern.compile(pattern)); |
| 59 | + } catch (PatternSyntaxException e) { |
| 60 | + if (ctx != null) { |
| 61 | + ctx.warnWith( |
| 62 | + new ExecutionWarn("BAD_REGEX_CONTENT_TYPE") |
| 63 | + .cause(e) |
| 64 | + .message( |
| 65 | + "Invalid Content-Type filter regex provided ('%s'). Default one will be used.".formatted( |
| 66 | + pattern |
| 67 | + ) |
| 68 | + ) |
| 69 | + ); |
| 70 | + } |
| 71 | + return Stream.empty(); |
| 72 | + } |
| 73 | + }) |
| 74 | + .findFirst() |
| 75 | + .map(Pattern::asPredicate) |
| 76 | + .orElse(contentType -> true)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Determines if body can be logged for APIv4 |
| 81 | + */ |
| 82 | + public boolean isContentTypeLoggable(@Nullable final String contentType, BaseExecutionContext ctx) { |
| 83 | + return ( |
| 84 | + contentType == null || |
| 85 | + !matcher(List.of(customExcludedResponseTypes, () -> excludedResponseTypes, () -> DEFAULT_EXCLUDED_CONTENT_TYPES), ctx).test( |
| 86 | + contentType |
| 87 | + ) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Determines if body can be logged for APIv2 |
| 93 | + */ |
| 94 | + public boolean isContentTypeLoggable(@Nullable final String contentType, final LoggingContext loggingContext) { |
| 95 | + return ( |
| 96 | + contentType == null || |
| 97 | + !matcher(List.of(loggingContext::getExcludedResponseTypes, () -> DEFAULT_EXCLUDED_CONTENT_TYPES), null).test(contentType) |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
0 commit comments