commit | author | age
|
94a2d9
|
1 |
/* |
U |
2 |
WebBox - Dein Server. |
|
3 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.wbx.web; |
|
19 |
|
|
20 |
import java.io.IOException; |
|
21 |
import java.io.PrintWriter; |
|
22 |
import java.io.StringWriter; |
|
23 |
import javax.servlet.ServletOutputStream; |
|
24 |
import javax.servlet.http.HttpServletResponse; |
|
25 |
import javax.servlet.http.HttpServletResponseWrapper; |
|
26 |
|
|
27 |
/** |
|
28 |
* Die Klasse CustomResponseWrapper faengt den Inhalt einer HttpServletResponse |
|
29 |
* in einem StringWriter auf. Dieser kann mit der Methode getResponseContent |
|
30 |
* gelesen werden. |
|
31 |
* |
|
32 |
* @author ulrich |
|
33 |
*/ |
|
34 |
public class CustomResponseWrapper extends HttpServletResponseWrapper { |
|
35 |
|
|
36 |
private StringWriter stringWriter; |
|
37 |
private boolean isOutputStreamCalled; |
|
38 |
|
|
39 |
public CustomResponseWrapper(HttpServletResponse response) { |
|
40 |
super(response); |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
public ServletOutputStream getOutputStream() throws IOException { |
|
45 |
if (this.stringWriter != null) { |
|
46 |
throw new IllegalStateException("The getWriter() is already called."); |
|
47 |
} |
|
48 |
isOutputStreamCalled = true; |
|
49 |
return super.getOutputStream(); |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public PrintWriter getWriter() throws IOException { |
|
54 |
if (isOutputStreamCalled) { |
|
55 |
throw new IllegalStateException("The getOutputStream() is already called."); |
|
56 |
} |
|
57 |
|
|
58 |
this.stringWriter = new StringWriter(); |
|
59 |
|
|
60 |
return new PrintWriter(this.stringWriter); |
|
61 |
} |
|
62 |
|
|
63 |
public String getResponseContent() { |
|
64 |
if (this.stringWriter != null) { |
|
65 |
return this.stringWriter.toString(); |
|
66 |
} |
|
67 |
return ""; |
|
68 |
} |
|
69 |
|
|
70 |
} |