Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
优化 HttpConcatUtil 代码 enhancement fix #39
Browse files Browse the repository at this point in the history
feilong concat version 支持 md5 或者sha fix #37
  • Loading branch information
venusdrogon committed May 26, 2018
1 parent d8c5ed8 commit dd8cbdf
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 30 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<artifactId>feilong-servlet</artifactId>
</dependency>

<dependency>
<groupId>com.feilong.platform</groupId>
<artifactId>feilong-security</artifactId>
<optional>true</optional>
</dependency>

<!-- slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,10 @@ public final class HttpConcatGlobalConfigBuilder{

/** 配置文件 <code>{@value}</code>. */
//XXX support different environment
private static final String CONFIG_FILE = "config/httpconcat";
private static final String CONFIG_FILE = "config/httpconcat";

/** The Constant HTTPCONCAT_RESOURCEBUNDLE. */
private static final ResourceBundle HTTPCONCAT_RESOURCEBUNDLE = ResourceBundleUtil.getResourceBundle(CONFIG_FILE);

/** <code>{@value}</code>. */
private static final String KEY_HTTPCONCAT_SUPPORT = "httpconcat.support";

//---------------------------------------------------------------

/** <code>{@value}</code>. */
private static final String KEY_TEMPLATE_CSS = "httpconcat.template.css";

/** <code>{@value}</code>. */
private static final String KEY_TEMPLATE_JS = "httpconcat.template.js";

/** <code>{@value}</code>. */
private static final String KEY_DEFAULT_CACHE_ENABLE = "httpconcat.defaultCacheEnable";

/** <code>{@value}</code>. */
private static final String KEY_DEFAULT_CACHE_SIZE_LIMIT = "httpconcat.defaultCacheSizeLimit";

//---------------------------------------------------------------
private static final ResourceBundle HTTPCONCAT_RESOURCEBUNDLE = ResourceBundleUtil.getResourceBundle(CONFIG_FILE);

/** Don't let anyone instantiate this class. */
private HttpConcatGlobalConfigBuilder(){
Expand All @@ -77,12 +58,16 @@ private HttpConcatGlobalConfigBuilder(){
public static HttpConcatGlobalConfig build(){
HttpConcatGlobalConfig httpConcatGlobalConfig = new HttpConcatGlobalConfig();

httpConcatGlobalConfig.setHttpConcatSupport(getRequiredValue(KEY_HTTPCONCAT_SUPPORT, Boolean.class));
httpConcatGlobalConfig.setTemplateCss(getRequiredValue(KEY_TEMPLATE_CSS, String.class));
httpConcatGlobalConfig.setTemplateJs(getRequiredValue(KEY_TEMPLATE_JS, String.class));
httpConcatGlobalConfig.setHttpConcatSupport(getRequiredValue("httpconcat.support", Boolean.class));
httpConcatGlobalConfig.setTemplateCss(getRequiredValue("httpconcat.template.css", String.class));
httpConcatGlobalConfig.setTemplateJs(getRequiredValue("httpconcat.template.js", String.class));

httpConcatGlobalConfig.setDefaultCacheEnable(getRequiredValue("httpconcat.defaultCacheEnable", Boolean.class));
httpConcatGlobalConfig.setDefaultCacheSizeLimit(getRequiredValue("httpconcat.defaultCacheSizeLimit", Integer.class));

httpConcatGlobalConfig.setDefaultCacheEnable(getRequiredValue(KEY_DEFAULT_CACHE_ENABLE, Boolean.class));
httpConcatGlobalConfig.setDefaultCacheSizeLimit(getRequiredValue(KEY_DEFAULT_CACHE_SIZE_LIMIT, Integer.class));
httpConcatGlobalConfig.setVersionEncode(getRequiredValue("httpconcat.version.encode", String.class));
httpConcatGlobalConfig.setVersionNameInScope(getRequiredValue("httpconcat.version.nameInScope", String.class));
httpConcatGlobalConfig.setVersionSearchScope(getRequiredValue("httpconcat.version.search.scope", String.class));

return httpConcatGlobalConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import java.util.List;

import com.feilong.taglib.display.httpconcat.command.HttpConcatGlobalConfig;
import com.feilong.taglib.display.httpconcat.command.HttpConcatParam;
import com.feilong.taglib.display.httpconcat.handler.DomainFormatter;
import com.feilong.taglib.display.httpconcat.handler.RootFormatter;
import com.feilong.taglib.display.httpconcat.handler.TypeFormatter;
import com.feilong.taglib.display.httpconcat.handler.VersionFormatter;

/**
* {@link HttpConcatParam} 构造器.
Expand All @@ -46,15 +48,19 @@ private HttpConcatParamBuilder(){
* the item src list
* @param httpConcatParam
* the http concat param
* @param httpConcatGlobalConfig
* @return the http concat param
*/
public static HttpConcatParam standardHttpConcatParam(List<String> itemSrcList,HttpConcatParam httpConcatParam){
public static HttpConcatParam standardHttpConcatParam(
List<String> itemSrcList,
HttpConcatParam httpConcatParam,
HttpConcatGlobalConfig httpConcatGlobalConfig){
HttpConcatParam standardHttpConcatParam = new HttpConcatParam();

standardHttpConcatParam.setDomain(DomainFormatter.format(httpConcatParam.getDomain()));
standardHttpConcatParam.setRoot(RootFormatter.format(httpConcatParam.getRoot()));
standardHttpConcatParam.setType(TypeFormatter.format(httpConcatParam.getType(), itemSrcList));
standardHttpConcatParam.setVersion(httpConcatParam.getVersion());
standardHttpConcatParam.setVersion(VersionFormatter.format(httpConcatParam.getVersion(), httpConcatGlobalConfig));

standardHttpConcatParam.setContent(httpConcatParam.getContent());
standardHttpConcatParam.setHttpConcatSupport(httpConcatParam.getHttpConcatSupport());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ private ResultBuilder(){
*/
public static String build(List<String> itemSrcList,HttpConcatParam httpConcatParam,HttpConcatGlobalConfig httpConcatGlobalConfig){
// 下面的解析均基于standardHttpConcatParam来操作,httpConcatParam只做入参判断,数据转换,以及cache存取
HttpConcatParam standardHttpConcatParam = HttpConcatParamBuilder.standardHttpConcatParam(itemSrcList, httpConcatParam);
HttpConcatParam standardHttpConcatParam = HttpConcatParamBuilder
.standardHttpConcatParam(itemSrcList, httpConcatParam, httpConcatGlobalConfig);

//---------------------------------------------------------------
boolean concatSupport = concatSupport(httpConcatParam, httpConcatGlobalConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,46 @@ public class HttpConcatGlobalConfig{

//---------------------------------------------------------------

/**
*
* httpconcat version 作用域里面变量名字.
*
* <p>
*
* // # 如果没有配置或者是空,那么不会去作用域中查找<br>
* // httpconcat.version.nameInScope=httpconcatVersion
* </p>
*
* @since 1.11.1
*/
private String versionNameInScope;

/**
* version 名字在哪个作用域.
*
* <p>
* // # 如果没有值那么会依次从系列作用域中去找值 page request session application<br>
* // httpconcat.version.search.scope=
* </p>
*
* @since 1.11.1
*/
private String versionSearchScope;

/**
* version值的加密格式,值可以是 md5 或者sha1 (忽视大小写).
*
* <p>
* # 如果没有配置, 那么显示原样内容<br>
* # 如果需要这个功能, 需要依赖 feilong-security jar<br>
* httpconcat.version.encode=
* </p>
*
* @since 1.11.1
*/
private String versionEncode;
//---------------------------------------------------------------

/**
* 获得 the template css.
*
Expand Down Expand Up @@ -168,4 +208,103 @@ public int getDefaultCacheSizeLimit(){
public void setDefaultCacheSizeLimit(int defaultCacheSizeLimit){
this.defaultCacheSizeLimit = defaultCacheSizeLimit;
}

/**
*
* httpconcat version 作用域里面变量名字.
*
* <p>
*
* // # 如果没有配置或者是空,那么不会去作用域中查找<br>
* // httpconcat.version.nameInScope=httpconcatVersion
* </p>
*
* @return the versionNameInScope
* @since 1.11.1
*/
public String getVersionNameInScope(){
return versionNameInScope;
}

/**
*
* httpconcat version 作用域里面变量名字.
*
* <p>
*
* // # 如果没有配置或者是空,那么不会去作用域中查找<br>
* // httpconcat.version.nameInScope=httpconcatVersion
* </p>
*
* @param versionNameInScope
* the versionNameInScope to set
* @since 1.11.1
*/
public void setVersionNameInScope(String versionNameInScope){
this.versionNameInScope = versionNameInScope;
}

/**
* version 名字在哪个作用域.
*
* <p>
* // # 如果没有值那么会依次从系列作用域中去找值 page request session application<br>
* // httpconcat.version.search.scope=
* </p>
*
* @return the versionSearchScope
* @since 1.11.1
*/
public String getVersionSearchScope(){
return versionSearchScope;
}

/**
* version 名字在哪个作用域.
*
* <p>
* // # 如果没有值那么会依次从系列作用域中去找值 page request session application<br>
* // httpconcat.version.search.scope=
* </p>
*
* @param versionSearchScope
* the versionSearchScope to set
* @since 1.11.1
*/
public void setVersionSearchScope(String versionSearchScope){
this.versionSearchScope = versionSearchScope;
}

/**
* version值的加密格式,值可以是 md5 或者sha1 (忽视大小写).
*
* <p>
* # 如果没有配置, 那么显示原样内容<br>
* # 如果需要这个功能, 需要依赖 feilong-security jar<br>
* httpconcat.version.encode=
* </p>
*
* @return the versionEncode
* @since 1.11.1
*/
public String getVersionEncode(){
return versionEncode;
}

/**
* version值的加密格式,值可以是 md5 或者sha1 (忽视大小写).
*
* <p>
* # 如果没有配置, 那么显示原样内容<br>
* # 如果需要这个功能, 需要依赖 feilong-security jar<br>
* httpconcat.version.encode=
* </p>
*
* @param versionEncode
* the versionEncode to set
* @since 1.11.1
*/
public void setVersionEncode(String versionEncode){
this.versionEncode = versionEncode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2008 feilong
*
* 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 com.feilong.taglib.display.httpconcat.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.security.oneway.MD5Util;
import com.feilong.security.oneway.SHA1Util;

/**
* 独立成一个类,避免没有依赖 security 报错.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
* @since 1.11.1
*/
public class VersionEncodeUtil{

/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory.getLogger(VersionEncodeUtil.class);

/**
* Encode.
*
* @param version
* the version
* @param versionEncode
* the version encode
* @return the string
*/
static String encode(String version,String versionEncode){
if ("md5".equalsIgnoreCase(versionEncode)){
return MD5Util.encode(version + VersionFormatter.class.getName());
}

if ("sha1".equalsIgnoreCase(versionEncode)){
return SHA1Util.encode(version + VersionFormatter.class.getName());
}

LOGGER.warn("versionEncode:[{}] ,not md5 and not sha1", versionEncode);
return version;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2008 feilong
*
* 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 com.feilong.taglib.display.httpconcat.handler;

import static com.feilong.core.Validator.isNullOrEmpty;

import com.feilong.taglib.display.httpconcat.command.HttpConcatGlobalConfig;

/**
* version 值格式化.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
* @since 1.11.1
*/
public final class VersionFormatter{

//---------------------------------------------------------------
/** Don't let anyone instantiate this class. */
private VersionFormatter(){
//AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
//see 《Effective Java》 2nd
throw new AssertionError("No " + getClass().getName() + " instances for you!");
}

//---------------------------------------------------------------

/**
* Format.
*
* @param version
* the version
* @param httpConcatGlobalConfig
* the http concat global config
* @return 如果 version 没有值,那么直接返回<br>
* 如果 version 有值,那么:<br>
*/
public static String format(String version,HttpConcatGlobalConfig httpConcatGlobalConfig){
if (isNullOrEmpty(version)){
return version;
}

//---------------------------------------------------------------
String versionEncode = httpConcatGlobalConfig.getVersionEncode();
if (isNullOrEmpty(versionEncode)){
return version;
}

return VersionEncodeUtil.encode(version, versionEncode);
}

}

0 comments on commit dd8cbdf

Please sign in to comment.