Skip to content

Commit

Permalink
* 新增<renderResultAsJson /> valve,用来将screen所返回的对象转换成json并输出到response。
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Zhou committed Aug 22, 2012
1 parent 9b26258 commit 305dc12
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,5 @@
其它valve可以通过pipelineContext.getAttribute("screenResult")来取得该值。

* Bugfix: 对于ClassNameWildcard和PathNameWildcard,当pattern以?开头或结束的时候,错误地匹配了多个字符(?的意思是有且仅有一个字符)

* 新增<renderResultAsJson /> valve,用来将screen所返回的对象转换成json并输出到response。
2 changes: 2 additions & 0 deletions CHANGES_SINCE_3.1.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@
其它valve可以通过pipelineContext.getAttribute("screenResult")来取得该值。

* Bugfix: 对于ClassNameWildcard和PathNameWildcard,当pattern以?开头或结束的时候,错误地匹配了多个字符(?的意思是有且仅有一个字符)

* 新增<renderResultAsJson /> valve,用来将screen所返回的对象转换成json并输出到response。
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,11 @@
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.23</version>
</dependency>
<!-- ======================================== -->
<!-- Spring依赖 -->
<!-- ======================================== -->
Expand Down
4 changes: 4 additions & 0 deletions webx/turbine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>citrus-webx-framework</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* @author Michael Zhou
*/
public class PerformScreenValve extends AbstractValve {
private static final String DEFAULT_RESULT_NAME = "screenResult";
static final String DEFAULT_RESULT_NAME = "screenResult";

@Autowired
private ModuleLoaderService moduleLoaderService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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 static com.alibaba.citrus.util.StringUtil.*;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.citrus.service.pipeline.PipelineContext;
import com.alibaba.citrus.service.pipeline.Valve;
import com.alibaba.citrus.service.pipeline.support.AbstractValveDefinitionParser;
import com.alibaba.fastjson.JSON;
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;

/**
* 将screen所返回的结果转换成json格式并输出。
*
* @author Michael Zhou
*/
public class RenderResultAsJsonValve implements Valve {
private static final String DEFAULT_RESULT_NAME = PerformScreenValve.DEFAULT_RESULT_NAME;
private static final String DEFAULT_CONTENT_TYPE = "application/json";
private static final String DEFAULT_JAVASCRIPT_VARIABLE = null;
private static final String DEFAULT_JAVASCRIPT_CONTENT_TYPE = "application/javascript";

@Autowired
private HttpServletRequest request;

@Autowired
private HttpServletResponse response;

private String resultName;
private String contentType;
private String javascriptVariable;
private String javascriptContentType;

public String getResultName() {
return resultName == null ? DEFAULT_RESULT_NAME : resultName;
}

public void setResultName(String resultName) {
this.resultName = trimToNull(resultName);
}

public String getContentType() {
return contentType == null ? DEFAULT_CONTENT_TYPE : contentType;
}

public void setContentType(String contentType) {
this.contentType = trimToNull(contentType);
}

public String getJavascriptVariable() {
return javascriptVariable == null ? DEFAULT_JAVASCRIPT_VARIABLE : javascriptVariable;
}

public void setJavascriptVariable(String javascriptVariable) {
this.javascriptVariable = trimToNull(javascriptVariable);
}

public String getJavascriptContentType() {
return javascriptContentType == null ? DEFAULT_JAVASCRIPT_CONTENT_TYPE : javascriptContentType;
}

public void setJavascriptContentType(String javascriptContentType) {
this.javascriptContentType = trimToNull(javascriptContentType);
}

public void invoke(PipelineContext pipelineContext) throws Exception {
String javascriptVariable = getJavascriptVariable();
boolean outputAsJson = javascriptVariable == null;

if (outputAsJson) {
// output as json
response.setContentType(getContentType());
} else {
// output as javascript
response.setContentType(getJavascriptContentType());
}

PrintWriter out = response.getWriter();
Object resultObject = pipelineContext.getAttribute(getResultName());
String jsonResult = JSON.toJSONString(resultObject);

if (outputAsJson) {
out.print(jsonResult);
} else {
out.print("var ");
out.print(javascriptVariable);
out.print(" = ");
out.print(jsonResult);
out.print(";");
}
}

public static class DefinitionParser extends AbstractValveDefinitionParser<RenderResultAsJsonValve> {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
attributesToProperties(element, builder, "resultName", "contentType", "javascriptVariable", "javascriptContentType");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ performAction=com.alibaba.citrus.turbine.pipeline.valve.PerformActionValve$Defin
performTemplateScreen=com.alibaba.citrus.turbine.pipeline.valve.PerformTemplateScreenValve$DefinitionParser
performScreen=com.alibaba.citrus.turbine.pipeline.valve.PerformScreenValve$DefinitionParser
renderTemplate=com.alibaba.citrus.turbine.pipeline.valve.RenderTemplateValve$DefinitionParser
renderResultAsJson=com.alibaba.citrus.turbine.pipeline.valve.RenderResultAsJsonValve$DefinitionParser
exportControl=com.alibaba.citrus.turbine.pipeline.valve.ExportControlValve$DefinitionParser

breakUnlessTargetRedirected=com.alibaba.citrus.turbine.pipeline.valve.BreakUnlessTargetRedirectedValve$DefinitionParser
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
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="renderResultAsJson" type="RenderResultAsJsonType">
<xsd:annotation>
<xsd:documentation><![CDATA[
将screen所返回的结果转换成json格式并输出。
]]></xsd:documentation>
</xsd:annotation>
</xsd:element>

<xsd:complexType name="RenderResultAsJsonType">
<xsd:attribute name="resultName" type="xsd:string" default="screenResult" />
<xsd:attribute name="contentType" type="xsd:string" default="application/json" />
<xsd:attribute name="javascriptVariable" type="xsd:string" use="optional" />
<xsd:attribute name="javascriptContentType" type="xsd:string" default="application/javascript" />
</xsd:complexType>

</xsd:schema>
25 changes: 25 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 @@ -55,6 +55,31 @@
<valves:valve class="com.alibaba.citrus.turbine.pipeline.valve.PerformScreenValveTests$ReadResultValve" />
</services:pipeline>

<services:pipeline id="renderJson" lazy-init="true">
<valves:analyzeURL />
<valves:performScreen />
<valves:renderResultAsJson />
</services:pipeline>

<services:pipeline id="renderJsonAsJs" lazy-init="true">
<valves:analyzeURL />
<valves:performScreen />
<valves:renderResultAsJson javascriptVariable="myresult" />
</services:pipeline>

<services:pipeline id="renderJson_specifiedContentType" lazy-init="true">
<valves:analyzeURL />
<valves:performScreen />
<valves:renderResultAsJson contentType="text/plain" />
</services:pipeline>

<services:pipeline id="renderJsonAsJs_specifiedContentType" lazy-init="true">
<valves:analyzeURL />
<valves:performScreen resultName="myresult" />
<valves:renderResultAsJson resultName="myresult"
javascriptContentType="text/js" javascriptVariable="myresult" />
</services:pipeline>

<services:pipeline id="renderTemplate">
<valves:analyzeURL />
<valves:renderTemplate />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* 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 org.junit.Assert.*;

import com.alibaba.citrus.service.pipeline.impl.PipelineImpl;
import com.meterware.httpunit.WebResponse;
import org.junit.Before;
import org.junit.Test;

public class RenderResultAsJsonValveTests extends AbstractValveTests {
@Before
public void init() {
pipeline = (PipelineImpl) factory.getBean("renderJson");
assertNotNull(pipeline);
}

@Test
public void outputAsJson_noResult() throws Exception {
getInvocationContext("http://localhost/app1/myJsonScreen/noResult");
initRequestContext();
pipeline.newInvocation().invoke();
commitRequestContext();

WebResponse webResponse = client.getResponse(invocationContext);

assertEquals(200, webResponse.getResponseCode());
assertEquals("application/json", webResponse.getContentType());
assertEquals("null", webResponse.getText());
}

@Test
public void outputAsJson_withResult() throws Exception {
getInvocationContext("http://localhost/app1/myJsonScreen/withResult");
initRequestContext();
pipeline.newInvocation().invoke();
commitRequestContext();

WebResponse webResponse = client.getResponse(invocationContext);

assertEquals(200, webResponse.getResponseCode());
assertEquals("application/json", webResponse.getContentType());
assertEquals("{\"age\":100,\"name\":\"michael\"}", webResponse.getText());
}

@Test
public void outputAsJson_withResult_specifiedContentType() throws Exception {
pipeline = (PipelineImpl) factory.getBean("renderJson_specifiedContentType");
getInvocationContext("http://localhost/app1/myJsonScreen/withResult");
initRequestContext();
pipeline.newInvocation().invoke();
commitRequestContext();

WebResponse webResponse = client.getResponse(invocationContext);

assertEquals(200, webResponse.getResponseCode());
assertEquals("text/plain", webResponse.getContentType());
assertEquals("{\"age\":100,\"name\":\"michael\"}", webResponse.getText());
}

@Test
public void outputAsJs_noResult() throws Exception {
pipeline = (PipelineImpl) factory.getBean("renderJsonAsJs");
getInvocationContext("http://localhost/app1/myJsonScreen/noResult");
initRequestContext();
pipeline.newInvocation().invoke();
commitRequestContext();

WebResponse webResponse = client.getResponse(invocationContext);

assertEquals(200, webResponse.getResponseCode());
assertEquals("application/javascript", webResponse.getContentType());
assertEquals("var myresult = null;", webResponse.getText());
}

@Test
public void outputAsJs_withResult() throws Exception {
pipeline = (PipelineImpl) factory.getBean("renderJsonAsJs");
getInvocationContext("http://localhost/app1/myJsonScreen/withResult");
initRequestContext();
pipeline.newInvocation().invoke();
commitRequestContext();

WebResponse webResponse = client.getResponse(invocationContext);

assertEquals(200, webResponse.getResponseCode());
assertEquals("application/javascript", webResponse.getContentType());
assertEquals("var myresult = {\"age\":100,\"name\":\"michael\"};", webResponse.getText());
}

@Test
public void outputAsJs_withResult_specifiedContentType() throws Exception {
pipeline = (PipelineImpl) factory.getBean("renderJsonAsJs_specifiedContentType");
getInvocationContext("http://localhost/app1/myJsonScreen/withResult");
initRequestContext();
pipeline.newInvocation().invoke();
commitRequestContext();

WebResponse webResponse = client.getResponse(invocationContext);

assertEquals(200, webResponse.getResponseCode());
assertEquals("text/js", webResponse.getContentType());
assertEquals("var myresult = {\"age\":100,\"name\":\"michael\"};", webResponse.getText());
}
}
Loading

0 comments on commit 305dc12

Please sign in to comment.