Skip to content

Commit

Permalink
添加<setBuffering enabled="true|false" /> valve,可以在pipeline中开关buffering…
Browse files Browse the repository at this point in the history
…,以简化screen代码。
  • Loading branch information
Michael Zhou committed Nov 1, 2012
1 parent 1e38748 commit f19d8a4
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES_SINCE_3.1.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@
有些测试代码需要实现ServletContext接口,但是由于servlet3和2版本对于该接口的API不同,导致无法用一套代码同时兼容servlet3和2。用动态生成类工具就可以解决这个问题。
* 改进request context服务,使之在async模式被启动时,不会提交response。当async线程重新取得控制时,可以重新取得原来的request context。
* 改进<renderTemplate> valve,当buffering关闭时,只不过不显示layout,而不会报错。
* 添加<setBuffering enabled="true|false" /> valve,可以在pipeline中开关buffering,以简化screen代码。
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.turbine.pipeline.valve;

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

import com.alibaba.citrus.service.pipeline.PipelineContext;
import com.alibaba.citrus.service.pipeline.support.AbstractValve;
import com.alibaba.citrus.service.pipeline.support.AbstractValveDefinitionParser;
import com.alibaba.citrus.service.requestcontext.buffered.BufferedRequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
* 开关buffering。
*
* @author Michael Zhou
*/
public class SetBufferingValve extends AbstractValve {
private Logger log = LoggerFactory.getLogger(getClass());
private boolean enabled;

@Autowired
private BufferedRequestContext brc;

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

@Override
public void invoke(PipelineContext pipelineContext) throws Exception {
try {
brc.setBuffering(enabled);
} catch (IllegalStateException e) {
log.warn("Unable to setBuffering({}): {}", enabled, e.getMessage());
}
}

public static class DefinitionParser extends AbstractValveDefinitionParser<SetBufferingValve> {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
attributesToProperties(element, builder, "enabled");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ analyzeURL=com.alibaba.citrus.turbine.pipeline.valve.AnalyzeURLValve$DefinitionP
checkCsrfToken=com.alibaba.citrus.turbine.pipeline.valve.CheckCsrfTokenValve$DefinitionParser
getResource=com.alibaba.citrus.turbine.pipeline.valve.GetResourceValve$DefinitionParser

setBuffering=com.alibaba.citrus.turbine.pipeline.valve.SetBufferingValve$DefinitionParser
performAction=com.alibaba.citrus.turbine.pipeline.valve.PerformActionValve$DefinitionParser
performTemplateScreen=com.alibaba.citrus.turbine.pipeline.valve.PerformTemplateScreenValve$DefinitionParser
performScreen=com.alibaba.citrus.turbine.pipeline.valve.PerformScreenValve$DefinitionParser
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:springext="http://www.alibaba.com/schema/springext/base" elementFormDefault="qualified">

<xsd:import namespace="http://www.springframework.org/schema/beans"
schemaLocation="http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd" />

<xsd:import namespace="http://www.alibaba.com/schema/springext/base"
schemaLocation="http://localhost:8080/schema/www.alibaba.com/schema/springext/springext-base.xsd" />

<xsd:element name="setBuffering" type="SetBufferingValveType">
<xsd:annotation>
<xsd:documentation><![CDATA[
开关buffering。
]]></xsd:documentation>
</xsd:annotation>
</xsd:element>

<xsd:complexType name="SetBufferingValveType">
<xsd:attribute name="enabled" type="springext:booleanOrPlaceholder" default="true" />
</xsd:complexType>

</xsd:schema>
12 changes: 12 additions & 0 deletions webx/turbine/src/test/config/WEB-INF/webx-app1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
<valves:valve class="com.alibaba.citrus.turbine.pipeline.valve.CheckCsrfTokenValveTests$CheckCsrfManually" />
</services:pipeline>

<services:pipeline id="setBuffering_default">
<valves:setBuffering />
</services:pipeline>

<services:pipeline id="setBuffering_enabled">
<valves:setBuffering enabled="true" />
</services:pipeline>

<services:pipeline id="setBuffering_disabled">
<valves:setBuffering enabled="false" />
</services:pipeline>

<services:pipeline id="performActionValveTests">
<valves:analyzeURL actionParam="action" />
<valves:performAction />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.turbine.pipeline.valve;

import static com.alibaba.citrus.service.requestcontext.util.RequestContextUtil.*;
import static org.junit.Assert.*;

import com.alibaba.citrus.service.pipeline.impl.PipelineImpl;
import com.alibaba.citrus.service.requestcontext.buffered.BufferedRequestContext;
import org.junit.Test;

public class SetBufferingValveTests extends AbstractValveTests {
private BufferedRequestContext brc;

private void initPipeline(String id) {
pipeline = (PipelineImpl) factory.getBean(id);
assertNotNull(pipeline);
}

@Test
public void setBuffering_default() throws Exception {
assertBufferingEnabled("setBuffering_default");
}

@Test
public void setBuffering_enabled() throws Exception {
assertBufferingEnabled("setBuffering_enabled");
}

private void assertBufferingEnabled(String id) throws Exception {
initPipeline(id);

getInvocationContext("http://localhost/app1/myscreen");
initRequestContext();

brc = findRequestContext(newRequest, BufferedRequestContext.class);
brc.setBuffering(false);
assertEquals(false, brc.isBuffering());

pipeline.newInvocation().invoke();
assertEquals(true, brc.isBuffering());
}

@Test
public void setBuffering_disabled() throws Exception {
initPipeline("setBuffering_disabled");

getInvocationContext("http://localhost/app1/myscreen");
initRequestContext();

brc = findRequestContext(newRequest, BufferedRequestContext.class);
assertEquals(true, brc.isBuffering());

pipeline.newInvocation().invoke();
assertEquals(false, brc.isBuffering());
}

@Test
public void setBuffering_illegalState() throws Exception {
initPipeline("setBuffering_disabled");

getInvocationContext("http://localhost/app1/myscreen");
initRequestContext();

brc = findRequestContext(newRequest, BufferedRequestContext.class);
assertEquals(true, brc.isBuffering());

newResponse.getWriter();

pipeline.newInvocation().invoke();
assertEquals(true, brc.isBuffering()); // unchanged
}
}

0 comments on commit f19d8a4

Please sign in to comment.