1
2
3
4
5
6
7
8
9
10
11
12
13
14 package waffle.mock.http;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.io.UnsupportedEncodingException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.servlet.ServletOutputStream;
26 import javax.servlet.http.HttpServletResponse;
27 import javax.servlet.http.HttpServletResponseWrapper;
28
29 import org.mockito.Mockito;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.base.Joiner;
34
35
36
37
38
39
40 public class SimpleHttpResponse extends HttpServletResponseWrapper {
41
42
43 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleHttpResponse.class);
44
45
46 private int status = 500;
47
48
49 private final Map<String, List<String>> headers = new HashMap<String, List<String>>();
50
51
52 final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
53
54
55 private final ServletOutputStream out = new ServletOutputStream() {
56 @Override
57 public void write(final int b) throws IOException {
58 SimpleHttpResponse.this.bytes.write(b);
59 }
60 };
61
62
63 private final PrintWriter writer = new PrintWriter(this.bytes);
64
65
66
67
68 public SimpleHttpResponse() {
69 super(Mockito.mock(HttpServletResponse.class));
70 }
71
72
73
74
75
76
77 public int getStatus() {
78 return this.status;
79 }
80
81
82
83
84 @Override
85 public void addHeader(final String headerName, final String headerValue) {
86 List<String> current = this.headers.get(headerName);
87 if (current == null) {
88 current = new ArrayList<String>();
89 }
90 current.add(headerValue);
91 this.headers.put(headerName, current);
92 }
93
94
95
96
97 @Override
98 public void setHeader(final String headerName, final String headerValue) {
99 List<String> current = this.headers.get(headerName);
100 if (current == null) {
101 current = new ArrayList<String>();
102 } else {
103 current.clear();
104 }
105 current.add(headerValue);
106 this.headers.put(headerName, current);
107 }
108
109
110
111
112 @Override
113 public void setStatus(final int value) {
114 this.status = value;
115 }
116
117
118
119
120
121
122 public String getStatusString() {
123 if (this.status == 401) {
124 return "Unauthorized";
125 }
126 return "Unknown";
127 }
128
129
130
131
132 @Override
133 public void flushBuffer() {
134 SimpleHttpResponse.LOGGER.info("{}: {}", Integer.valueOf(this.status), this.getStatusString());
135 for (final String header : this.headers.keySet()) {
136 for (final String headerValue : this.headers.get(header)) {
137 SimpleHttpResponse.LOGGER.info("{}: {}", header, headerValue);
138 }
139 }
140 }
141
142
143
144
145
146
147 public int getHeaderNamesSize() {
148 return this.headers.size();
149 }
150
151
152
153
154
155
156
157
158 public String[] getHeaderValues(final String headerName) {
159 final List<String> headerValues = this.headers.get(headerName);
160 return headerValues == null ? null : headerValues.toArray(new String[0]);
161 }
162
163
164
165
166
167
168
169
170 public String getHeader(final String headerName) {
171 final List<String> headerValues = this.headers.get(headerName);
172 return headerValues == null ? null : Joiner.on(", ").join(headerValues);
173 }
174
175
176
177
178 @Override
179 public void sendError(final int rc, final String message) {
180 this.status = rc;
181 }
182
183
184
185
186 @Override
187 public void sendError(final int rc) {
188 this.status = rc;
189 }
190
191
192
193
194 @Override
195 public PrintWriter getWriter() {
196 return this.writer;
197 }
198
199
200
201
202 @Override
203 public ServletOutputStream getOutputStream() throws IOException {
204 return this.out;
205 }
206
207
208
209
210
211
212 public String getOutputText() {
213 this.writer.flush();
214 try {
215 return this.bytes.toString("UTF-8");
216 } catch (final UnsupportedEncodingException e) {
217 SimpleHttpResponse.LOGGER.error("{}", e);
218 }
219 return null;
220 }
221 }