All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.plugin.extension.resources.AbstractWebStaticResourceExtension Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.plugin.extension.resources;

import org.springframework.context.ApplicationContext;
import org.springframework.http.CacheControl;
import org.zodiac.commons.util.Colls;
import org.zodiac.plugin.extension.AbstractPluginExtension;
import org.zodiac.plugin.extension.resources.thymeleaf.ThymeleafProcessor;
import org.zodiac.plugin.factory.process.pipe.PluginPipeProcessorExtend;
import org.zodiac.plugin.factory.process.post.PluginPostProcessorExtend;
import org.zodiac.sdk.toolkit.constants.StringPool;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 插件静态资源访问的扩展插件。
 */
public abstract class AbstractWebStaticResourceExtension extends AbstractPluginExtension {

    private final static String KEY = "AbstractWebStaticResourceExtension";

    /**
     * 访问插件静态资源前缀。默认为: static-plugin。
     */
    private static String pluginStaticResourcePathPrefix = "static-plugin";

    /**
     * 访问静态资源的缓存控制。默认最大1小时。主要针对http协议的缓存。
     */
    private static CacheControl pluginStaticResourcesCacheControl =
        CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic();

    private final Set includes = CollUtil.set(1);

    public AbstractWebStaticResourceExtension() {}

    public AbstractWebStaticResourceExtension(Include... includes) {
        if (includes != null) {
            this.includes.addAll(Arrays.asList(includes));
        }
    }

    @Override
    public String key() {
        return KEY;
    }

    @Override
    public List getPluginPipeProcessor(ApplicationContext mainApplicationContext) {
        if (includes.contains(Include.THYMELEAF)) {
            final List pluginPipeProcessorExtends = CollUtil.list(1);
            pluginPipeProcessorExtends.add(new ThymeleafProcessor());
            return pluginPipeProcessorExtends;
        }
        return null;
    }

    @Override
    public List getPluginPostProcessor(ApplicationContext mainApplicationContext) {
        final List pluginPostProcessorExtends = CollUtil.list();
        pluginPostProcessorExtends.add(new PluginResourceResolverProcess());
        return pluginPostProcessorExtends;
    }

    /**
     * 设置访问插件静态资源前缀
     * 
     * @param pluginStaticResourcePathPrefix
     *            静态资源前缀。默认为: static-plugin。
     * @return AbstractWebStaticResourceExtension
     */
    public AbstractWebStaticResourceExtension setPathPrefix(String pluginStaticResourcePathPrefix) {
        if (pluginStaticResourcePathPrefix != null && !StringPool.EMPTY.equals(pluginStaticResourcePathPrefix)) {
            AbstractWebStaticResourceExtension.pluginStaticResourcePathPrefix = pluginStaticResourcePathPrefix;
        }
        return this;
    }

    /**
     * 设置缓存控制
     * 
     * @param pluginStaticResourcesCacheControl
     *            访问静态资源的缓存控制。默认最大1小时。主要针对http协议的缓存。
     * @return AbstractWebStaticResourceExtension
     */
    public AbstractWebStaticResourceExtension setCacheControl(CacheControl pluginStaticResourcesCacheControl) {
        if (pluginStaticResourcesCacheControl == null) {
            AbstractWebStaticResourceExtension.pluginStaticResourcesCacheControl = null;
        } else {
            AbstractWebStaticResourceExtension.pluginStaticResourcesCacheControl = pluginStaticResourcesCacheControl;
        }
        return this;
    }

    public static String getPluginStaticResourcePathPrefix() {
        return pluginStaticResourcePathPrefix;
    }

    public static CacheControl getPluginStaticResourcesCacheControl() {
        return pluginStaticResourcesCacheControl;
    }

    public enum Include {
        THYMELEAF
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy