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(final Writer writer, final String contentType, final 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(final 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(final 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        final 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(final 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(final Writer writer, final String name, final 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(final Writer writer, final 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(
137       final Writer writer, final String name, final String value, final boolean escape)
138       throws IOException {
139     if (!isStartStillOpen()) {
140       final String trace = getCallingClassStackTraceElementString();
141       final String error = "Cannot write attribute when start-tag not open. "
142           + "name = '" + name + "' "
143           + "value = '" + value + "' "
144           + trace.substring(trace.indexOf('('));
145       LOG.error(error);
146       throw new IllegalStateException(error);
147     }
148 
149     if (value != null) {
150       writer.write(' ');
151       writer.write(name);
152       writer.write("='");
153 
154       if (escape) {
155         getHelper().writeAttributeValue(value);
156       } else {
157         writer.write(value);
158       }
159       writer.write("'");
160     }
161   }
162 
163   public void writeText(final Object text, final String property)
164       throws IOException {
165     closeOpenTag();
166     final String value = findValue(text, property);
167     getHelper().writeText(value);
168   }
169 
170 /* TODO: may also encode the backslash \, but will not be used currently
171   public void writeText(final char[] text, final int offset, final int length)
172       throws IOException {
173     closeOpenTag();
174     getHelper().writeText(text, offset, length);
175   }
176 */
177 
178   public ResponseWriter cloneWithWriter(final Writer originalWriter) {
179      return new JsonResponseWriter(
180          originalWriter, getContentType(), getCharacterEncoding());
181    }
182 
183 
184   @Override
185   public void startDocument() throws IOException {
186   }
187 
188   @Override
189   public void endDocument() throws IOException {
190   }
191 }