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.component.Attributes;
23  import org.apache.myfaces.tobago.internal.util.Deprecation;
24  import org.apache.myfaces.tobago.internal.util.StringUtils;
25  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
26  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import javax.faces.component.UIComponent;
31  import javax.faces.context.ResponseWriter;
32  import java.io.IOException;
33  import java.io.Writer;
34  import java.util.EmptyStackException;
35  import java.util.Stack;
36  
37  public class DebugResponseWriterWrapper extends TobagoResponseWriter {
38  
39    private Stack<String> stack = new Stack<String>();
40  
41    private static final Logger LOG = LoggerFactory.getLogger(DebugResponseWriterWrapper.class);
42  
43    private final TobagoResponseWriter responseWriter;
44  
45    public DebugResponseWriterWrapper(final TobagoResponseWriter responseWriter) {
46      this.responseWriter = responseWriter;
47    }
48  
49    public void write(final String string) throws IOException {
50      responseWriter.write(string);
51    }
52  
53    public void writeComment(final Object comment) throws IOException {
54      String commentStr = comment.toString();
55      if (commentStr.indexOf("--") > 0) {
56        LOG.error("Comment must not contain the sequence '--', comment = '" + comment + "'.",
57            new IllegalArgumentException());
58  
59        commentStr = StringUtils.replace(commentStr, "--", "++");
60      }
61      responseWriter.writeComment(commentStr);
62    }
63  
64    public ResponseWriter cloneWithWriter(final Writer writer) {
65      return new DebugResponseWriterWrapper((TobagoResponseWriter) responseWriter.cloneWithWriter(writer));
66    }
67  
68    @Deprecated
69    public void writeAttribute(final String name, final Object value, final String property) throws IOException {
70      responseWriter.writeAttribute(name, value, property);
71    }
72  
73    @Deprecated
74    public void writeText(final Object text, final String property) throws IOException {
75      responseWriter.writeText(text, property);
76    }
77  
78    public void flush() throws IOException {
79      responseWriter.flush();
80    }
81  
82    public void writeAttribute(final String name, final String value, final boolean escape) throws IOException {
83      responseWriter.writeAttribute(name, value, escape);
84    }
85  
86    @Override
87    @Deprecated
88    public String getStyleClasses() {
89      return responseWriter.getStyleClasses();
90    }
91  
92    /**
93     * @deprecated since Tobago 1.5.0
94     */
95    @Deprecated
96    public void writeClassAttribute() throws IOException {
97      Deprecation.LOG.warn("Please use writeClassAttribute(org.apache.myfaces.tobago.renderkit.css.Classes)");
98      responseWriter.writeAttribute(HtmlAttributes.CLASS, null, Attributes.STYLE_CLASS);
99    }
100 
101   public String getContentType() {
102     return responseWriter.getContentType();
103   }
104 
105   public String getCharacterEncoding() {
106     return responseWriter.getCharacterEncoding();
107   }
108 
109   public void startDocument() throws IOException {
110     responseWriter.startDocument();
111   }
112 
113   public void endDocument() throws IOException {
114     responseWriter.endDocument();
115   }
116 
117   /**
118    * @deprecated Should not be used, because it conflicts with CSP.
119    */
120   @Deprecated
121   @Override
122   public void writeJavascript(final String script) throws IOException {
123     responseWriter.writeJavascript(script);
124   }
125 
126   /**
127    * @deprecated Should not be used, because it conflicts with CSP.
128    */
129   @Deprecated
130   @Override
131   public void endJavascript() throws IOException {
132     responseWriter.endJavascript();
133   }
134 
135   /**
136    * @deprecated Should not be used, because it conflicts with CSP.
137    */
138   @Deprecated
139   @Override
140   public void startJavascript() throws IOException {
141     responseWriter.startJavascript();
142   }
143 
144   public void writeURIAttribute(final String name, final Object value, final String property) throws IOException {
145     responseWriter.writeURIAttribute(name, value, property);
146   }
147 
148   public void writeText(final char[] text, final int off, final int len) throws IOException {
149     responseWriter.writeText(text, off, len);
150   }
151 
152   public void write(final char[] chars, final int i, final int i1) throws IOException {
153     responseWriter.write(chars, i, i1);
154   }
155 
156   public void close() throws IOException {
157     responseWriter.close();
158   }
159 
160   @Override
161   public void startElement(final String name, final UIComponent currentComponent)
162       throws IOException {
163     if (LOG.isDebugEnabled()) {
164       LOG.debug("start element: '" + name + "'");
165     }
166     stack.push(name);
167     responseWriter.startElement(name, currentComponent);
168   }
169 
170   @Override
171   public void endElement(final String name) throws IOException {
172     if (LOG.isDebugEnabled()) {
173       LOG.debug("end element: '" + name + "'");
174     }
175     String top = "";
176     try {
177       top = stack.pop();
178     } catch (final EmptyStackException e) {
179       LOG.error("Failed to close element \"" + name + "\"!", e);
180     }
181 
182     if (!top.equals(name)) {
183       LOG.error("Element end with name='" + name + "' doesn't match with top element on the stack='" + top + "'.",
184           new IllegalArgumentException());
185     }
186     responseWriter.endElement(name);
187   }
188 }