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.myfaces.tobago.internal.util.FastStringWriter;
23  import org.apache.myfaces.tobago.internal.util.JavascriptWriterUtils;
24  import org.apache.myfaces.tobago.internal.util.StringUtils;
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 (FacesVersion.isMojarra() && FacesVersion.supports21() && XML_VERSION_1_0_ENCODING_UTF_8.equals(string)) {
64        // ignore
65        return;
66      }
67      if (javascriptBlock) {
68        encodeInJavascriptBlock.writeText(string);
69      } else {
70        encodeOutsideJavascriptBlock.writeText(string);
71      }
72    }
73  
74    @Override
75    public void write(char[] chars) throws IOException {
76      // XXX remove me later:
77      // this is a temporary workaround, should be removed after fixing the bug in Mojarra.
78      // http://java.net/jira/browse/JAVASERVERFACES-2411
79      // https://issues.apache.org/jira/browse/TOBAGO-1124
80      if (FacesVersion.isMojarra() && FacesVersion.supports20()) {
81        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
82        if (stackTraceElements[2].getClassName().equals("com.sun.faces.renderkit.ServerSideStateHelper")) {
83          super.write(StringUtils.replace(new String(chars), "\"", "\\\""));
84          return;
85        }
86      }
87      super.write(chars);
88    }
89  
90    @Override
91    public void writeJavascript(String script) throws IOException {
92      closeOpenTag();
93      encodeInJavascriptBlock.writeText(script);
94    }
95  
96    public String getJavascript() {
97      return javascriptWriter.toString();
98    }
99  
100   @Override
101   protected void startElementInternal(Writer writer, String name, UIComponent currentComponent)
102       throws IOException {
103     setComponent(currentComponent);
104     if (isStartStillOpen()) {
105       writer.write(">");
106     }
107     writer.write("<");
108     writer.write(name);
109     setStartStillOpen(true);
110   }
111 
112   @Override
113   protected void endElementInternal(Writer writer, String name) throws IOException {
114     if (EMPTY_TAG.contains(name)) {
115         writer.write(">");
116     } else {
117       if (isStartStillOpen()) {
118         writer.write(">");
119       }
120       writer.write("</");
121       writer.write(name);
122       writer.write(">");
123     }
124     setStartStillOpen(false);
125   }
126 
127   @Override
128   protected void closeOpenTag() throws IOException {
129     if (isStartStillOpen()) {
130       getWriter().write(">");
131       setStartStillOpen(false);
132     }
133   }
134 
135   @Override
136   protected void writeAttributeInternal(Writer writer, String name, String value, boolean escape)
137       throws IOException {
138     if (!isStartStillOpen()) {
139       String trace = getCallingClassStackTraceElementString();
140       String error = "Cannot write attribute when start-tag not open. "
141           + "name = '" + name + "' "
142           + "value = '" + value + "' "
143           + trace.substring(trace.indexOf('('));
144       LOG.error(error);
145       throw new IllegalStateException(error);
146     }
147 
148     if (value != null) {
149       writer.write(' ');
150       writer.write(name);
151       writer.write("=\\\"");
152 
153       if (escape) {
154         getHelper().writeAttributeValue(value);
155       } else {
156         writer.write(value);
157       }
158       writer.write("\\\"");
159     }
160   }
161 
162   public void writeText(final Object text, final String property)
163       throws IOException {
164     closeOpenTag();
165     String value = findValue(text, property);
166     getHelper().writeText(value);
167   }
168 
169 /* TODO: may also encode the backslash \, but will not be used currently
170   public void writeText(final char[] text, final int offset, final int length)
171       throws IOException {
172     closeOpenTag();
173     getHelper().writeText(text, offset, length);
174   }
175 */
176 
177   public ResponseWriter cloneWithWriter(final Writer originalWriter) {
178      return new JsonResponseWriter(
179          originalWriter, getContentType(), getCharacterEncoding());
180    }
181 
182 
183   @Override
184   public void startDocument() throws IOException {
185   }
186 
187   @Override
188   public void endDocument() throws IOException {
189   }
190 }