PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/webx/turbine/src/main/java/com/alibaba/citrus/turbine/pipeline/valve/RenderResultAsJsonValve.java

http://github.com/webx/citrus
Java | 125 lines | 79 code | 23 blank | 23 comment | 11 complexity | 90dc1f4d37b1f1efda49c5d706b7a0a4 MD5 | raw file
  1. /*
  2. * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.alibaba.citrus.turbine.pipeline.valve;
  18. import static com.alibaba.citrus.springext.util.SpringExtUtil.*;
  19. import static com.alibaba.citrus.turbine.util.TurbineUtil.*;
  20. import static com.alibaba.citrus.util.StringUtil.*;
  21. import java.io.PrintWriter;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import com.alibaba.citrus.service.pipeline.PipelineContext;
  25. import com.alibaba.citrus.service.pipeline.support.AbstractValveDefinitionParser;
  26. import com.alibaba.citrus.turbine.TurbineRunData;
  27. import com.alibaba.fastjson.JSON;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.beans.factory.support.BeanDefinitionBuilder;
  30. import org.springframework.beans.factory.xml.ParserContext;
  31. import org.w3c.dom.Element;
  32. /**
  33. * 将screen所返回的结果转换成json格式并输出。
  34. *
  35. * @author Michael Zhou
  36. */
  37. public class RenderResultAsJsonValve extends AbstractInputOutputValve {
  38. private static final String DEFAULT_CONTENT_TYPE = "application/json";
  39. private static final String DEFAULT_JAVASCRIPT_VARIABLE = null;
  40. private static final String DEFAULT_JAVASCRIPT_CONTENT_TYPE = "application/javascript";
  41. @Autowired
  42. private HttpServletRequest request;
  43. @Autowired
  44. private HttpServletResponse response;
  45. private String contentType;
  46. private String javascriptVariable;
  47. private String javascriptContentType;
  48. public String getContentType() {
  49. return contentType == null ? DEFAULT_CONTENT_TYPE : contentType;
  50. }
  51. public void setContentType(String contentType) {
  52. this.contentType = trimToNull(contentType);
  53. }
  54. public String getJavascriptVariable() {
  55. return javascriptVariable == null ? DEFAULT_JAVASCRIPT_VARIABLE : javascriptVariable;
  56. }
  57. public void setJavascriptVariable(String javascriptVariable) {
  58. this.javascriptVariable = trimToNull(javascriptVariable);
  59. }
  60. public String getJavascriptContentType() {
  61. return javascriptContentType == null ? DEFAULT_JAVASCRIPT_CONTENT_TYPE : javascriptContentType;
  62. }
  63. public void setJavascriptContentType(String javascriptContentType) {
  64. this.javascriptContentType = trimToNull(javascriptContentType);
  65. }
  66. public void invoke(PipelineContext pipelineContext) throws Exception {
  67. TurbineRunData rundata = getTurbineRunData(request);
  68. if (!rundata.isRedirected()) {
  69. Object resultObject = consumeInputValue(pipelineContext);
  70. if (resultObject == null) {
  71. return;
  72. }
  73. String javascriptVariable = getJavascriptVariable();
  74. boolean outputAsJson = javascriptVariable == null;
  75. if (outputAsJson) {
  76. // output as json
  77. response.setContentType(getContentType());
  78. } else {
  79. // output as javascript
  80. response.setContentType(getJavascriptContentType());
  81. }
  82. PrintWriter out = response.getWriter();
  83. String jsonResult = JSON.toJSONString(resultObject);
  84. if (outputAsJson) {
  85. out.print(jsonResult);
  86. } else {
  87. out.print("var ");
  88. out.print(javascriptVariable);
  89. out.print(" = ");
  90. out.print(jsonResult);
  91. out.print(";");
  92. }
  93. }
  94. pipelineContext.invokeNext();
  95. }
  96. public static class DefinitionParser extends AbstractValveDefinitionParser<RenderResultAsJsonValve> {
  97. @Override
  98. protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
  99. attributesToProperties(element, builder, "input", "contentType", "javascriptVariable", "javascriptContentType");
  100. }
  101. }
  102. }