-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 新增<renderResultAsJson /> valve,用来将screen所返回的对象转换成json并输出到response。
- Loading branch information
Michael Zhou
committed
Aug 22, 2012
1 parent
9b26258
commit 305dc12
Showing
11 changed files
with
364 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...bine/src/main/java/com/alibaba/citrus/turbine/pipeline/valve/RenderResultAsJsonValve.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
webx/turbine/src/main/resources/META-INF/services/pipeline/valves/renderResultAsJson.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...src/test/java/com/alibaba/citrus/turbine/pipeline/valve/RenderResultAsJsonValveTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.