<commit_before>/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2016, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   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.thymeleaf.spring5.view.reactive;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.reactive.result.view.RedirectView;
import org.springframework.web.reactive.result.view.View;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.reactive.result.view.ViewResolverSupport;
import org.thymeleaf.spring5.ISpringWebFluxTemplateEngine;
import org.thymeleaf.util.Validate;
import reactor.core.publisher.Mono;


/**
 * <p>
 *   Implementation of the Spring WebFlux {@link ViewResolver}
 *   interface.
 * </p>
 * <p>
 *   View resolvers execute after the controller ends its execution. They receive the name
 *   of the view to be processed and are in charge of creating (and configuring) the
 *   corresponding {@link View} object for it.
 * </p>
 * <p>
 *   The {@link View} implementations managed by this class are of type {@link ThymeleafReactiveView}.
 * </p>
 *
 * @see ThymeleafReactiveView
 * @see ISpringWebFluxTemplateEngine
 *
 * @author <NAME>
 *
 * @since 3.0.3
 *
 */
public class ThymeleafReactiveViewResolver extends ViewResolverSupport implements ViewResolver {

    
    private static final Logger vrlogger = LoggerFactory.getLogger(ThymeleafReactiveViewResolver.class);


    /**
     * <p>
     *   Prefix to be used in view names (returned by controllers) for specifying an
     *   HTTP redirect.
     * </p>
     * <p>
     *   Value: <tt>redirect:</tt>
     * </p>
     */
    public static final String REDIRECT_URL_PREFIX = "redirect:";

    // TODO * Will this exist in future versions of Spring WebFlux? See https://jira.spring.io/browse/SPR-14537
    public static final String FORWARD_URL_PREFIX = "forward:";

    // This provider function for redirect mirrors what is done at the reactive version of UrlBasedViewResolver
    private Function<String, RedirectView> redirectViewProvider = url -> new RedirectView(url);

    private boolean alwaysProcessRedirectAndForward = true;

    private Class<? extends ThymeleafReactiveView> viewClass = ThymeleafReactiveView.class;
    private String[] viewNames = null;
    private String[] excludedViewNames = null;
    private int order = Integer.MAX_VALUE;

    private final Map<String, Object> staticVariables = new LinkedHashMap<String, Object>(10);


    // This will determine whether we will be throttling or not, and if so the size of the chunks that will be produced
    // by the throttled engine each time the back-pressure mechanism asks for a new "unit" (a new DataBuffer)
    //
    // The value established here will be a default value, which can be overridden by specific views at the
    // ThymeleafReactiveView class
    private int responseMaxChunkSizeBytes = ThymeleafReactiveView.DEFAULT_RESPONSE_CHUNK_SIZE_BYTES;


    private ISpringWebFluxTemplateEngine templateEngine;






    /**
     * <p>
     *   Create an instance of <tt>ThymeleafReactiveViewResolver</tt>.
     * </p>
     */
    public ThymeleafReactiveViewResolver() {
        super();
    }




    /**
     * <p>
     *   Set the view class that should be used to create views. This must be a subclass
     *   of {@link ThymeleafReactiveView}.
     * </p>
     *<commit_msg>Added SSE media type (text/event-stream) as supported by default<commit_after>/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2016, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   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.thymeleaf.spring5.view.reactive;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.http.MediaType;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.reactive.result.view.RedirectView;
import org.springframework.web.reactive.result.view.View;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.reactive.result.view.ViewResolverSupport;
import org.thymeleaf.spring5.ISpringWebFluxTemplateEngine;
import org.thymeleaf.util.Validate;
import reactor.core.publisher.Mono;


/**
 * <p>
 *   Implementation of the Spring WebFlux {@link ViewResolver}
 *   interface.
 * </p>
 * <p>
 *   View resolvers execute after the controller ends its execution. They receive the name
 *   of the view to be processed and are in charge of creating (and configuring) the
 *   corresponding {@link View} object for it.
 * </p>
 * <p>
 *   The {@link View} implementations managed by this class are of type {@link ThymeleafReactiveView}.
 * </p>
 *
 * @see ThymeleafReactiveView
 * @see ISpringWebFluxTemplateEngine
 *
 * @author <NAME>
 *
 * @since 3.0.3
 *
 */
public class ThymeleafReactiveViewResolver extends ViewResolverSupport implements ViewResolver {

    
    private static final Logger vrlogger = LoggerFactory.getLogger(ThymeleafReactiveViewResolver.class);


    /**
     * <p>
     *   Prefix to be used in view names (returned by controllers) for specifying an
     *   HTTP redirect.
     * </p>
     * <p>
     *   Value: <tt>redirect:</tt>
     * </p>
     */
    public static final String REDIRECT_URL_PREFIX = "redirect:";

    // TODO * Will this exist in future versions of Spring WebFlux? See https://jira.spring.io/browse/SPR-14537
    public static final String FORWARD_URL_PREFIX = "forward:";

    // Supported media types are:
    //     * The default HTML one: text/html;charset=UTF-8
    //     * SSE (Sever-Sent Events): text/event-stream
    private static final List<MediaType> SUPPORTED_MEDIA_TYPES =
            Arrays.asList(new MediaType[] { ViewResolverSupport.DEFAULT_CONTENT_TYPE, MediaType.TEXT_EVENT_STREAM });

    // This provider function for redirect mirrors what is done at the reactive version of UrlBasedViewResolver
    private Function<String, RedirectView> redirectViewProvider = url -> new RedirectView(url);

    private boolean alwaysProcessRedirectAndForward = true;

    private Class<? extends ThymeleafReactiveView> viewClass = ThymeleafReactiveView.class;
    private String[] viewNames = null;
    private String[] excludedViewNames = null;
    private int order = Integer.MAX_VALUE;

    private final Map<String, Object> staticVariables = new LinkedHashMap<String, Object>(10);


    // This will determine whether we will be throttling or not, and if so the size of the chunks that will be produced
    // by the throttled engine each time the back-pressure mechanism asks for a new "unit" (a new DataBuffer)
    //
    // The value established here will be a default value, which can be overridden by specific views at the
    // ThymeleafReactiveView class
    private int responseMaxChunkSizeBytes = ThymeleafReactiveView.DEFAULT_RESPONSE_CHUNK_SIZE_BYTES;


    private ISpringWebFluxTemplateEngine templateEngine;






    /**
     * <p>
     *   Create an instance of <tt>ThymeleafReactiveViewResolver</tt>.
     * </p>
     */
    public ThymeleafReactiveViewResolver() {
        super();
        setSupportedMediaTypes(SUPPORTED_MEDIA_TYPES);
    }




    /**
     * <p>
     *   Set the view class that should be used to create views. This must be a subclass
     *   of {@link ThymeleafReactiveView}.
     * </p>
     *
{'fraction_non_alphanumeric': 0.11734693877551021, 'fraction_numerical': 0.005518533944189921, 'mean_word_length': 3.7977022977022976, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 52, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2376548', 'n_tokens_mistral': 2756, 'n_tokens_neox': 2633, 'n_words': 841}