blob: 5a9f8e4717009c2fc417b74b00ce3abd56d33489 [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
Dave Borowitze8a5e362013-01-14 16:07:26 -080017import static com.google.common.base.Preconditions.checkArgument;
Dave Borowitzd91bdf72013-01-10 20:07:32 -080018import static com.google.common.base.Preconditions.checkNotNull;
Dave Borowitze8a5e362013-01-14 16:07:26 -080019import static com.google.common.base.Preconditions.checkState;
David Pletcherd7bdaf32014-08-27 14:50:32 -070020import static java.nio.charset.StandardCharsets.UTF_8;
Dave Borowitz9de65952012-08-13 16:09:45 -070021
Dave Borowitze8a5e362013-01-14 16:07:26 -080022import java.io.ByteArrayOutputStream;
23import java.io.IOException;
Dave Borowitz9de65952012-08-13 16:09:45 -070024import java.io.PrintWriter;
Dave Borowitze8a5e362013-01-14 16:07:26 -080025import java.nio.charset.Charset;
Dave Borowitz9de65952012-08-13 16:09:45 -070026import java.util.Locale;
27
28import javax.servlet.ServletOutputStream;
29import javax.servlet.http.Cookie;
30import javax.servlet.http.HttpServletResponse;
31
Shawn Pearceb43b2d52013-03-18 10:55:15 -070032import org.eclipse.jgit.util.RawParseUtils;
33
Shawn Pearceb43b2d52013-03-18 10:55:15 -070034import com.google.common.collect.Iterables;
35import com.google.common.collect.LinkedListMultimap;
36import com.google.common.collect.ListMultimap;
37import com.google.common.net.HttpHeaders;
38
Dave Borowitz9de65952012-08-13 16:09:45 -070039/** Simple fake implementation of {@link HttpServletResponse}. */
40public class FakeHttpServletResponse implements HttpServletResponse {
Dave Borowitze8a5e362013-01-14 16:07:26 -080041 private final ByteArrayOutputStream actualBody = new ByteArrayOutputStream();
42 private final ListMultimap<String, String> headers = LinkedListMultimap.create();
Dave Borowitz9de65952012-08-13 16:09:45 -070043
Dave Borowitze8a5e362013-01-14 16:07:26 -080044 private int status = 200;
45 private boolean committed;
46 private ServletOutputStream outputStream;
47 private PrintWriter writer;
Dave Borowitz9de65952012-08-13 16:09:45 -070048
49 public FakeHttpServletResponse() {
Dave Borowitz9de65952012-08-13 16:09:45 -070050 }
51
52 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080053 public synchronized void flushBuffer() throws IOException {
54 if (outputStream != null) {
55 outputStream.flush();
56 }
57 if (writer != null) {
58 writer.flush();
59 }
Dave Borowitz9de65952012-08-13 16:09:45 -070060 }
61
62 @Override
63 public int getBufferSize() {
64 throw new UnsupportedOperationException();
65 }
66
67 @Override
68 public String getCharacterEncoding() {
69 return UTF_8.name();
70 }
71
72 @Override
73 public String getContentType() {
74 return null;
75 }
76
77 @Override
78 public Locale getLocale() {
79 return Locale.US;
80 }
81
82 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080083 public synchronized ServletOutputStream getOutputStream() {
84 checkState(writer == null, "getWriter() already called");
85 if (outputStream == null) {
86 final PrintWriter osWriter = new PrintWriter(actualBody);
87 outputStream = new ServletOutputStream() {
88 @Override
89 public void write(int c) throws IOException {
90 osWriter.write(c);
91 osWriter.flush();
92 }
93 };
94 }
95 return outputStream;
Dave Borowitz9de65952012-08-13 16:09:45 -070096 }
97
98 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080099 public synchronized PrintWriter getWriter() {
100 checkState(outputStream == null, "getOutputStream() already called");
101 if (writer == null) {
102 writer = new PrintWriter(actualBody);
103 }
104 return writer;
Dave Borowitz9de65952012-08-13 16:09:45 -0700105 }
106
107 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800108 public synchronized boolean isCommitted() {
109 return committed;
Dave Borowitz9de65952012-08-13 16:09:45 -0700110 }
111
112 @Override
113 public void reset() {
114 throw new UnsupportedOperationException();
115 }
116
117 @Override
118 public void resetBuffer() {
119 throw new UnsupportedOperationException();
120 }
121
122 @Override
123 public void setBufferSize(int sz) {
124 throw new UnsupportedOperationException();
125 }
126
127 @Override
128 public void setCharacterEncoding(String name) {
David Pletcherd7bdaf32014-08-27 14:50:32 -0700129 checkArgument(UTF_8.equals(Charset.forName(name)),
Dave Borowitze8a5e362013-01-14 16:07:26 -0800130 "unsupported charset: %s", name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700131 }
132
133 @Override
134 public void setContentLength(int length) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800135 headers.removeAll(HttpHeaders.CONTENT_LENGTH);
136 headers.put(HttpHeaders.CONTENT_LENGTH, Integer.toString(length));
Dave Borowitz9de65952012-08-13 16:09:45 -0700137 }
138
139 @Override
140 public void setContentType(String type) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800141 headers.removeAll(HttpHeaders.CONTENT_TYPE);
142 headers.put(HttpHeaders.CONTENT_TYPE, type);
Dave Borowitz9de65952012-08-13 16:09:45 -0700143 }
144
145 @Override
146 public void setLocale(Locale locale) {
147 throw new UnsupportedOperationException();
148 }
149
150 @Override
151 public void addCookie(Cookie cookie) {
152 throw new UnsupportedOperationException();
153 }
154
155 @Override
156 public void addDateHeader(String name, long value) {
157 throw new UnsupportedOperationException();
158 }
159
160 @Override
161 public void addHeader(String name, String value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800162 headers.put(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700163 }
164
165 @Override
166 public void addIntHeader(String name, int value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800167 headers.put(name, Integer.toString(value));
Dave Borowitz9de65952012-08-13 16:09:45 -0700168 }
169
170 @Override
171 public boolean containsHeader(String name) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800172 return !headers.get(name).isEmpty();
Dave Borowitz9de65952012-08-13 16:09:45 -0700173 }
174
175 @Override
176 public String encodeRedirectURL(String url) {
177 throw new UnsupportedOperationException();
178 }
179
180 @Override
181 @Deprecated
182 public String encodeRedirectUrl(String url) {
183 throw new UnsupportedOperationException();
184 }
185
186 @Override
187 public String encodeURL(String url) {
188 throw new UnsupportedOperationException();
189 }
190
191 @Override
192 @Deprecated
193 public String encodeUrl(String url) {
194 throw new UnsupportedOperationException();
195 }
196
197 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800198 public synchronized void sendError(int sc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700199 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800200 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700201 }
202
203 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800204 public synchronized void sendError(int sc, String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700205 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800206 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700207 }
208
209 @Override
Dave Borowitz5d5619d2014-04-18 17:01:45 -0700210 public synchronized void sendRedirect(String loc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700211 status = SC_FOUND;
Dave Borowitz5d5619d2014-04-18 17:01:45 -0700212 setHeader(HttpHeaders.LOCATION, loc);
Dave Borowitze8a5e362013-01-14 16:07:26 -0800213 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700214 }
215
216 @Override
217 public void setDateHeader(String name, long value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800218 setHeader(name, Long.toString(value));
Dave Borowitz9de65952012-08-13 16:09:45 -0700219 }
220
221 @Override
222 public void setHeader(String name, String value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800223 headers.removeAll(name);
224 addHeader(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700225 }
226
227 @Override
228 public void setIntHeader(String name, int value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800229 headers.removeAll(name);
230 addIntHeader(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700231 }
232
233 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800234 public synchronized void setStatus(int sc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700235 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800236 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700237 }
238
239 @Override
240 @Deprecated
Dave Borowitze8a5e362013-01-14 16:07:26 -0800241 public synchronized void setStatus(int sc, String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700242 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800243 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700244 }
245
Dave Borowitze8a5e362013-01-14 16:07:26 -0800246 public synchronized int getStatus() {
Dave Borowitz9de65952012-08-13 16:09:45 -0700247 return status;
248 }
Dave Borowitze8a5e362013-01-14 16:07:26 -0800249
250 public byte[] getActualBody() {
251 return actualBody.toByteArray();
252 }
253
254 public String getActualBodyString() {
255 return RawParseUtils.decode(getActualBody());
256 }
Dave Borowitzd91bdf72013-01-10 20:07:32 -0800257
258 public String getHeader(String name) {
259 return Iterables.getFirst(headers.get(checkNotNull(name)), null);
260 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700261}