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  package org.apache.myfaces.application.pss;
20  
21  import javax.servlet.http.HttpServletResponseWrapper;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.ServletOutputStream;
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  import java.io.CharArrayWriter;
27  
28  /**
29   * @author Martin Haimberger
30   */
31  public class ViewHandlerResponseWrapperHelperImpl extends HttpServletResponseWrapper {
32      private TempServletOutputStream tempOS = null;
33      private PrintWriter pw = null;
34      private CharArrayWriter caw = null;
35      private int status = HttpServletResponse.SC_OK;
36  
37      public PrintWriter getPw() {
38          if (pw == null) {
39              caw = new CharArrayWriter();
40              pw = new PrintWriter(caw);
41          }
42          return pw;
43      }
44  
45      public ServletOutputStream getTempOS() {
46          if (tempOS == null)
47          {
48              tempOS = new TempServletOutputStream();
49          }
50          return tempOS;
51      }
52  
53      public ViewHandlerResponseWrapperHelperImpl(HttpServletResponse httpServletResponse) {
54          super(httpServletResponse);
55      }
56  
57  
58      // from HttpServletResponseWrapper
59      public void sendError(int i, String string) throws IOException {
60          super.sendError(i, string);
61          status = i;
62      }
63  
64      public void sendError(int i) throws IOException {
65          super.sendError(i);
66          status = i;
67      }
68  
69      public void setStatus(int i) {
70          super.setStatus(i);
71          status = i;
72      }
73  
74      public void setStatus(int i, String string) {
75          super.setStatus(i, string);
76          status = i;
77      }
78  
79  
80      public String toString() {
81          String result = null;
82      if (caw != null) {
83          result = caw.toString();
84      }
85      else if (tempOS != null) {
86          result = tempOS.toString();
87      }
88  
89      return result;
90      }
91  
92      public PrintWriter getWriter() throws IOException {
93          return getPw();
94      }
95  
96      public ServletOutputStream getOutputStream() throws IOException {
97          return getTempOS();
98      }
99  
100     public void resetWriter() {
101         if (caw != null)
102         {
103             pw.flush();
104             caw.flush();
105             caw.reset();
106         }
107     }
108 
109 
110 }