View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.webapp;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.myfaces.tobago.internal.util.FastStringWriter;
24  import org.apache.myfaces.tobago.internal.util.JavascriptWriterUtils;
25  import org.apache.myfaces.tobago.util.FacesVersion;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.faces.component.UIComponent;
30  import javax.faces.context.ResponseWriter;
31  import java.io.IOException;
32  import java.io.Writer;
33  
34  public class JsonResponseWriter extends HtmlResponseWriter {
35  
36    private static final Logger LOG = LoggerFactory.getLogger(JsonResponseWriter.class);
37  
38    private Writer javascriptWriter;
39    private boolean javascriptBlock;
40    private JavascriptWriterUtils encodeInJavascriptBlock;
41    private JavascriptWriterUtils encodeOutsideJavascriptBlock;
42  
43    public JsonResponseWriter(Writer writer, String contentType, String characterEncoding) {
44      super(writer, contentType, characterEncoding);
45      this.javascriptWriter = new FastStringWriter();
46      this.encodeOutsideJavascriptBlock = new JavascriptWriterUtils(writer, characterEncoding);
47      this.encodeInJavascriptBlock = new JavascriptWriterUtils(javascriptWriter, characterEncoding);
48    }
49  
50    @Override
51    public void endJavascript() throws IOException {
52      javascriptBlock = false;
53    }
54  
55    @Override
56    public void startJavascript() throws IOException {
57      javascriptBlock = true;
58    }
59  
60    @Override
61    public void write(String string) throws IOException {
62      closeOpenTag();
63      if (javascriptBlock) {
64        encodeInJavascriptBlock.writeText(string);
65      } else {
66        encodeOutsideJavascriptBlock.writeText(string);
67      }
68    }
69  
70    @Override
71    public void write(char[] chars) throws IOException {
72      // XXX remove me later:
73      // this is a temporary workaround, should be removed after fixing the bug in Mojarra.
74      // http://java.net/jira/browse/JAVASERVERFACES-2411
75      // https://issues.apache.org/jira/browse/TOBAGO-1124
76      if (FacesVersion.isMojarra() && FacesVersion.supports20()) {
77        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
78        if (stackTraceElements[2].getClassName().equals("com.sun.faces.renderkit.ServerSideStateHelper")) {
79          super.write(StringUtils.replace(new String(chars), "\"", "\\\""));
80          return;
81        }
82      }
83      super.write(chars);
84    }
85  
86    @Override
87    public void writeJavascript(String script) throws IOException {
88      closeOpenTag();
89      encodeInJavascriptBlock.writeText(script);
90    }
91  
92    public String getJavascript() {
93      return javascriptWriter.toString();
94    }
95  
96    @Override
97    protected void startElementInternal(Writer writer, String name, UIComponent currentComponent)
98        throws IOException {
99      setComponent(currentComponent);
100     if (isStartStillOpen()) {
101       writer.write(">");
102     }
103     writer.write("<");
104     writer.write(name);
105     setStartStillOpen(true);
106   }
107 
108   @Override
109   protected void endElementInternal(Writer writer, String name) throws IOException {
110     if (EMPTY_TAG.contains(name)) {
111         writer.write(">");
112     } else {
113       if (isStartStillOpen()) {
114         writer.write(">");
115       }
116       writer.write("</");
117       writer.write(name);
118       writer.write(">");
119     }
120     setStartStillOpen(false);
121   }
122 
123   @Override
124   protected void closeOpenTag() throws IOException {
125     if (isStartStillOpen()) {
126       getWriter().write(">");
127       setStartStillOpen(false);
128     }
129   }
130 
131   @Override
132   protected void writeAttributeInternal(Writer writer, String name, String value, boolean escape)
133       throws IOException {
134     if (!isStartStillOpen()) {
135       String trace = getCallingClassStackTraceElementString();
136       String error = "Cannot write attribute when start-tag not open. "
137           + "name = '" + name + "' "
138           + "value = '" + value + "' "
139           + trace.substring(trace.indexOf('('));
140       LOG.error(error);
141       throw new IllegalStateException(error);
142     }
143 
144     if (value != null) {
145       writer.write(' ');
146       writer.write(name);
147       writer.write("=\\\"");
148 
149       if (escape) {
150         getHelper().writeAttributeValue(value);
151       } else {
152         writer.write(value);
153       }
154       writer.write("\\\"");
155     }
156   }
157 
158   public void writeText(final Object text, final String property)
159       throws IOException {
160     closeOpenTag();
161     String value = findValue(text, property);
162     getHelper().writeText(value);
163   }
164 
165 /* TODO: may also encode the backslash \, but will not be used currently
166   public void writeText(final char[] text, final int offset, final int length)
167       throws IOException {
168     closeOpenTag();
169     getHelper().writeText(text, offset, length);
170   }
171 */
172 
173   public ResponseWriter cloneWithWriter(final Writer originalWriter) {
174      return new JsonResponseWriter(
175          originalWriter, getContentType(), getCharacterEncoding());
176    }
177 
178 
179   @Override
180   public void startDocument() throws IOException {
181   }
182 
183   @Override
184   public void endDocument() throws IOException {
185   }
186 }