1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.statelessfilter.wrappers.headers;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21
22 import javax.servlet.ServletOutputStream;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.servlet.http.HttpServletResponseWrapper;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import net.sourceforge.statelessfilter.wrappers.BufferedHttpResponseWrapper;
30 import net.sourceforge.statelessfilter.wrappers.StatelessRequestWrapper;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class HeaderBufferedHttpResponseWrapper extends
51 HttpServletResponseWrapper {
52 Logger logger = LoggerFactory
53 .getLogger(HeaderBufferedHttpResponseWrapper.class);
54
55 private StatelessRequestWrapper statelessRequest = null;
56 HttpServletResponse origResponse = null;
57
58 public HeaderBufferedHttpResponseWrapper(StatelessRequestWrapper request,
59 HttpServletResponse origResponse) {
60 super(origResponse);
61 this.origResponse = origResponse;
62 this.statelessRequest = request;
63 logger.debug("Init HeaderBufferedHttpResponseWrapper");
64 }
65
66
67
68
69
70
71 private void writeSession() throws IOException {
72 if (!statelessRequest.isSessionWritten()) {
73 logger.debug("writting session");
74
75 statelessRequest.writeSession(statelessRequest, origResponse);
76 } else {
77 logger.debug("session already written");
78
79 }
80 }
81
82
83
84
85 @Override
86 public void flushBuffer() throws IOException {
87 logger.debug("flushBuffer()");
88
89 writeSession();
90 super.flushBuffer();
91 }
92
93
94
95
96 @Override
97 public ServletOutputStream getOutputStream() throws IOException {
98 logger.debug("getOutputStream() has been called");
99
100 writeSession();
101 return super.getOutputStream();
102 }
103
104
105
106
107 @Override
108 public PrintWriter getWriter() throws IOException {
109 logger.debug("getWriter() has been called");
110
111 writeSession();
112
113 return super.getWriter();
114 }
115
116
117
118
119
120 @Override
121 public void sendError(int sc) throws IOException {
122 logger.debug("sendError() has been called");
123
124 writeSession();
125 super.sendError(sc);
126 }
127
128
129
130
131
132
133 @Override
134 public void sendError(int sc, String msg) throws IOException {
135 logger.debug("sendError() has been called");
136
137 writeSession();
138 super.sendError(sc, msg);
139 }
140
141
142
143
144
145
146 @Override
147 public void sendRedirect(String location) throws IOException {
148 logger.debug("sendRedirect() has been called");
149
150 writeSession();
151 super.sendRedirect(location);
152 }
153
154 }