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

org.cattleframework.webmvc.WebMvcUrlRewriteAutoConfiguration Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 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.cattleframework.webmvc;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.cattleframework.webmvc.filter.UrlRewriteEnhanceFilter;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * Web Mvc Url Rewrite自动配置
 * 
 * @author orange
 *
 */
@ConditionalOnClass(name = "org.tuckey.web.filters.urlrewrite.UrlRewriteFilter")
@ConditionalOnProperty(prefix = "cattle.web.urlrewrite", name = "enabled", matchIfMissing = false)
@AutoConfiguration(before = WebMvcAutoConfiguration.class)
@EnableConfigurationProperties(WebMvcUrlRewriteProperties.class)
public class WebMvcUrlRewriteAutoConfiguration {

    @Bean
    public FilterRegistrationBean urlRewriteFilterRegistrationBean(
	    WebMvcUrlRewriteProperties rewriteProperties) {
	FilterRegistrationBean registrationBean = new FilterRegistrationBean();
	registrationBean.setFilter(new UrlRewriteEnhanceFilter());
	registrationBean.addUrlPatterns("/*");
	registrationBean.addInitParameter("confPath",
		rewriteProperties.getConfPath() != null ? rewriteProperties.getConfPath() : "");
	registrationBean.addInitParameter("statusPath",
		rewriteProperties.getStatusPath() != null ? rewriteProperties.getStatusPath() : "");
	registrationBean.addInitParameter("statusEnabled", String.valueOf(rewriteProperties.isStatusEnabled()));
	registrationBean.addInitParameter("statusEnabledOnHosts",
		rewriteProperties.getStatusEnabledOnHosts() != null ? rewriteProperties.getStatusEnabledOnHosts() : "");
	registrationBean.addInitParameter("allowConfSwapViaHttp",
		String.valueOf(rewriteProperties.isAllowConfSwapViaHttp()));
	StringBuilder stringBuffer = new StringBuilder();
	if (CollectionUtils.isNotEmpty(rewriteProperties.getModRewriteConf())) {
	    for (String modRewriteConf : rewriteProperties.getModRewriteConf()) {
		if (StringUtils.isBlank(modRewriteConf)) {
		    continue;
		}
		if (!stringBuffer.isEmpty()) {
		    stringBuffer.append('\n');
		}
		stringBuffer.append(StringUtils.trim(modRewriteConf));
	    }
	}
	registrationBean.addInitParameter("modRewriteConfText", stringBuffer.toString());
	return registrationBean;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy