blob: 5249e413d0bfccb864dd4166003aba4a2d5a1d88 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
17import static com.google.common.base.Charsets.UTF_8;
Dave Borowitze8a5e362013-01-14 16:07:26 -080018import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Preconditions.checkState;
Dave Borowitz9de65952012-08-13 16:09:45 -070020
Dave Borowitze8a5e362013-01-14 16:07:26 -080021import com.google.common.base.Charsets;
22import com.google.common.collect.LinkedListMultimap;
23import com.google.common.collect.ListMultimap;
24import com.google.common.net.HttpHeaders;
25
26import org.eclipse.jgit.util.RawParseUtils;
27
28import java.io.ByteArrayOutputStream;
29import java.io.IOException;
Dave Borowitz9de65952012-08-13 16:09:45 -070030import java.io.PrintWriter;
Dave Borowitze8a5e362013-01-14 16:07:26 -080031import java.nio.charset.Charset;
Dave Borowitz9de65952012-08-13 16:09:45 -070032import java.util.Locale;
33
34import javax.servlet.ServletOutputStream;
35import javax.servlet.http.Cookie;
36import javax.servlet.http.HttpServletResponse;
37
38/** Simple fake implementation of {@link HttpServletResponse}. */
39public class FakeHttpServletResponse implements HttpServletResponse {
Dave Borowitze8a5e362013-01-14 16:07:26 -080040 private final ByteArrayOutputStream actualBody = new ByteArrayOutputStream();
41 private final ListMultimap<String, String> headers = LinkedListMultimap.create();
Dave Borowitz9de65952012-08-13 16:09:45 -070042
Dave Borowitze8a5e362013-01-14 16:07:26 -080043 private int status = 200;
44 private boolean committed;
45 private ServletOutputStream outputStream;
46 private PrintWriter writer;
Dave Borowitz9de65952012-08-13 16:09:45 -070047
48 public FakeHttpServletResponse() {
Dave Borowitz9de65952012-08-13 16:09:45 -070049 }
50
51 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080052 public synchronized void flushBuffer() throws IOException {
53 if (outputStream != null) {
54 outputStream.flush();
55 }
56 if (writer != null) {
57 writer.flush();
58 }
Dave Borowitz9de65952012-08-13 16:09:45 -070059 }
60
61 @Override
62 public int getBufferSize() {
63 throw new UnsupportedOperationException();
64 }
65
66 @Override
67 public String getCharacterEncoding() {
68 return UTF_8.name();
69 }
70
71 @Override
72 public String getContentType() {
73 return null;
74 }
75
76 @Override
77 public Locale getLocale() {
78 return Locale.US;
79 }
80
81 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080082 public synchronized ServletOutputStream getOutputStream() {
83 checkState(writer == null, "getWriter() already called");
84 if (outputStream == null) {
85 final PrintWriter osWriter = new PrintWriter(actualBody);
86 outputStream = new ServletOutputStream() {
87 @Override
88 public void write(int c) throws IOException {
89 osWriter.write(c);
90 osWriter.flush();
91 }
92 };
93 }
94 return outputStream;
Dave Borowitz9de65952012-08-13 16:09:45 -070095 }
96
97 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080098 public synchronized PrintWriter getWriter() {
99 checkState(outputStream == null, "getOutputStream() already called");
100 if (writer == null) {
101 writer = new PrintWriter(actualBody);
102 }
103 return writer;
Dave Borowitz9de65952012-08-13 16:09:45 -0700104 }
105
106 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800107 public synchronized boolean isCommitted() {
108 return committed;
Dave Borowitz9de65952012-08-13 16:09:45 -0700109 }
110
111 @Override
112 public void reset() {
113 throw new UnsupportedOperationException();
114 }
115
116 @Override
117 public void resetBuffer() {
118 throw new UnsupportedOperationException();
119 }
120
121 @Override
122 public void setBufferSize(int sz) {
123 throw new UnsupportedOperationException();
124 }
125
126 @Override
127 public void setCharacterEncoding(String name) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800128 checkArgument(Charsets.UTF_8.equals(Charset.forName(name)),
129 "unsupported charset: %s", name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700130 }
131
132 @Override
133 public void setContentLength(int length) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800134 headers.removeAll(HttpHeaders.CONTENT_LENGTH);
135 headers.put(HttpHeaders.CONTENT_LENGTH, Integer.toString(length));
Dave Borowitz9de65952012-08-13 16:09:45 -0700136 }
137
138 @Override
139 public void setContentType(String type) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800140 headers.removeAll(HttpHeaders.CONTENT_TYPE);
141 headers.put(HttpHeaders.CONTENT_TYPE, type);
Dave Borowitz9de65952012-08-13 16:09:45 -0700142 }
143
144 @Override
145 public void setLocale(Locale locale) {
146 throw new UnsupportedOperationException();
147 }
148
149 @Override
150 public void addCookie(Cookie cookie) {
151 throw new UnsupportedOperationException();
152 }
153
154 @Override
155 public void addDateHeader(String name, long value) {
156 throw new UnsupportedOperationException();
157 }
158
159 @Override
160 public void addHeader(String name, String value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800161 headers.put(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700162 }
163
164 @Override
165 public void addIntHeader(String name, int value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800166 headers.put(name, Integer.toString(value));
Dave Borowitz9de65952012-08-13 16:09:45 -0700167 }
168
169 @Override
170 public boolean containsHeader(String name) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800171 return !headers.get(name).isEmpty();
Dave Borowitz9de65952012-08-13 16:09:45 -0700172 }
173
174 @Override
175 public String encodeRedirectURL(String url) {
176 throw new UnsupportedOperationException();
177 }
178
179 @Override
180 @Deprecated
181 public String encodeRedirectUrl(String url) {
182 throw new UnsupportedOperationException();
183 }
184
185 @Override
186 public String encodeURL(String url) {
187 throw new UnsupportedOperationException();
188 }
189
190 @Override
191 @Deprecated
192 public String encodeUrl(String url) {
193 throw new UnsupportedOperationException();
194 }
195
196 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800197 public synchronized void sendError(int sc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700198 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800199 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700200 }
201
202 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800203 public synchronized void sendError(int sc, String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700204 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800205 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700206 }
207
208 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800209 public synchronized void sendRedirect(String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700210 status = SC_FOUND;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800211 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700212 }
213
214 @Override
215 public void setDateHeader(String name, long value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800216 setHeader(name, Long.toString(value));
Dave Borowitz9de65952012-08-13 16:09:45 -0700217 }
218
219 @Override
220 public void setHeader(String name, String value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800221 headers.removeAll(name);
222 addHeader(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700223 }
224
225 @Override
226 public void setIntHeader(String name, int value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800227 headers.removeAll(name);
228 addIntHeader(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700229 }
230
231 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800232 public synchronized void setStatus(int sc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700233 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800234 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700235 }
236
237 @Override
238 @Deprecated
Dave Borowitze8a5e362013-01-14 16:07:26 -0800239 public synchronized void setStatus(int sc, String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700240 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800241 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700242 }
243
Dave Borowitze8a5e362013-01-14 16:07:26 -0800244 public synchronized int getStatus() {
Dave Borowitz9de65952012-08-13 16:09:45 -0700245 return status;
246 }
Dave Borowitze8a5e362013-01-14 16:07:26 -0800247
248 public byte[] getActualBody() {
249 return actualBody.toByteArray();
250 }
251
252 public String getActualBodyString() {
253 return RawParseUtils.decode(getActualBody());
254 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700255}