Skip to content

Commit

Permalink
* SetLocaleRequestContext中增加功能:可根据request uri,自动设置与默认值不同的input/output…
Browse files Browse the repository at this point in the history
… charset

  <set-locale>
    <override uri="*.json" inputCharset="UTF-8" outputCharset="UTF-8" />
  </set-locale>
  • Loading branch information
Michael Zhou committed Jun 14, 2012
1 parent f1eef6c commit 269aff5
Show file tree
Hide file tree
Showing 11 changed files with 575 additions and 137 deletions.
14 changes: 10 additions & 4 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@

* 改进:让webx同时支持spring2/3。
(修改了一处代码,使resource loading机制可在spring3下运行;修改了若干单元测试,使它们在spring3下可以通过测试。)

虽然webx仍以spring 2.5.6.SEC02编译,但只需要将项目的pom.xml中的spring依赖改成3.0.6.RELEASE就可以工作。
如希望以spring3来编译webx,只需要指定参数:mvn ... -Pspring3 即可。这样编译所生成的citrus-webx-all包将依赖于spring 3。

注意:spring3和spring2的代码是不完全兼容的。在spring2上编译的代码,未必能直接在spring3上运行,反之亦然。
下面的命令确保webx同时兼容于spring的两个版本。
mvn clean install -- 在spring2中编译并测试
Expand All @@ -157,7 +157,7 @@
-DdevelopmentMode.allowedHosts=192.168.1.*

* 改进:增加MDC: %X{productionMode},在日志中自动显示当前的生产模式或开发模式。

* 新增功能:将webx当作filter使用,filter chain的下游可以使用webx所提供的request contexts功能。配置方法如下(WEB-INF/web.xml):

<filter>
Expand Down Expand Up @@ -232,5 +232,11 @@
* 改进form/velocity service测试,使之可以在非(locale=CHINA, charset=GBK)的情况下测试通过。
* bugfix: number-validator设置限定范围以后,当用户输入非法数字时,validator抛出异常。
* 改进form,对于field.getKey(),同时支持压缩格式(_fm.g._0.f)和非压缩格式(_fm.group._0.field),大小写不敏感。这样可以方便测试。
如果<form fieldKeyFormat="uncompressed">参数被明确指定,那么所有输出的字段名全为非压缩的,并且不支持压缩格式。
如果<form fieldKeyFormat="uncompressed">参数被明确指定,那么所有输出的字段名全为非压缩的,并且不支持压缩格式。
* 删除暂时没用的asm/codegen子模块,并将少量依赖到的类移到generictype子模块中。
* SetLocaleRequestContext中增加功能:可根据request uri,自动设置与默认值不同的input/output charset

<set-locale>
<override uri="*.json" inputCharset="UTF-8" outputCharset="UTF-8" />
</set-locale>

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2002-2012 Alibaba Group Holding Limited.
* All rights reserved.
*
* 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.alibaba.citrus.service.requestcontext.locale.impl;

import static com.alibaba.citrus.util.StringUtil.*;
import static com.alibaba.citrus.util.regex.PathNameWildcardCompiler.*;

import java.util.regex.Pattern;

/**
* 根据request uri来设置输入、输出charset。
*
* @author Michael Zhou
*/
public class SetLocaleOverrider {
private Pattern requestUriPattern;
private String requestUriPatternName;
private String inputCharset;
private String outputCharset;

public Pattern getRequestUriPattern() {
return requestUriPattern;
}

public void setUri(String requestUriPatternName) {
this.requestUriPattern = compilePathName(requestUriPatternName);
this.requestUriPatternName = requestUriPatternName;
}

public String getInputCharset() {
return inputCharset;
}

public void setInputCharset(String inputCharset) {
this.inputCharset = trimToNull(inputCharset);
}

public String getOutputCharset() {
return outputCharset;
}

public void setOutputCharset(String outputCharset) {
this.outputCharset = trimToNull(outputCharset);
}

@Override
public String toString() {
return String.format("Override[uri=%s, inputCharset=%s, outputCharset=%s]", requestUriPatternName, inputCharset, outputCharset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

package com.alibaba.citrus.service.requestcontext.locale.impl;

import static com.alibaba.citrus.springext.util.DomUtil.*;
import static com.alibaba.citrus.springext.util.SpringExtUtil.*;

import java.util.List;

import com.alibaba.citrus.springext.support.parser.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
Expand All @@ -29,5 +32,22 @@ public class SetLocaleRequestContextFactoryDefinitionParser extends
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
attributesToProperties(element, builder);

List<Object> overriders = createManagedList(element, parserContext);
ElementSelector overriderSelector = and(sameNs(element), name("override"));

for (Element subElement : subElements(element, overriderSelector)) {
overriders.add(parseOverrider(subElement, parserContext));
}

builder.addPropertyValue("overriders", overriders);
}

private Object parseOverrider(Element element, ParserContext parserContext) {
BeanDefinitionBuilder overriderBuilder = BeanDefinitionBuilder.genericBeanDefinition(SetLocaleOverrider.class);

attributesToProperties(element, overriderBuilder, "uri", "inputCharset", "outputCharset");

return overriderBuilder.getBeanDefinition();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
* @author Michael Zhou
*/
public class SetLocaleRequestContextFactoryImpl extends AbstractRequestContextFactory<SetLocaleRequestContext> {
private String inputCharsetParam;
private Pattern inputCharsetPattern;
private String outputCharsetParam;
private Pattern outputCharsetPattern;
private String defaultLocaleName;
private Locale defaultLocale;
private String defaultCharset;
private String sessionKey;
private String paramKey;
private String inputCharsetParam;
private Pattern inputCharsetPattern;
private String outputCharsetParam;
private Pattern outputCharsetPattern;
private SetLocaleOverrider[] overriders;
private String defaultLocaleName;
private Locale defaultLocale;
private String defaultCharset;
private String sessionKey;
private String paramKey;

public void setInputCharsetParam(String inputCharsetParam) {
this.inputCharsetParam = trimToNull(inputCharsetParam);
Expand All @@ -55,6 +56,10 @@ public void setOutputCharsetParam(String outputCharsetParam) {
this.outputCharsetParam = trimToNull(outputCharsetParam);
}

public void setOverriders(SetLocaleOverrider[] overriders) {
this.overriders = overriders;
}

public void setDefaultLocale(String defaultLocaleName) {
this.defaultLocaleName = trimToNull(defaultLocaleName);
}
Expand Down Expand Up @@ -103,6 +108,7 @@ public SetLocaleRequestContext getRequestContextWrapper(RequestContext wrappedCo

requestContext.setInputCharsetPattern(inputCharsetPattern);
requestContext.setOutputCharsetPattern(outputCharsetPattern);
requestContext.setOverriders(overriders);
requestContext.setDefaultLocale(defaultLocale);
requestContext.setDefaultCharset(defaultCharset);
requestContext.setSessionKey(sessionKey);
Expand Down
Loading

0 comments on commit 269aff5

Please sign in to comment.