blob: 544c0757b61901c0f8abcfc9b27dd56458fe9ec7 [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 Borowitzfe8fdab2014-11-04 16:19:33 -080022import com.google.common.collect.Iterables;
23import com.google.common.collect.LinkedListMultimap;
24import com.google.common.collect.ListMultimap;
25import com.google.common.net.HttpHeaders;
Dave Borowitze8a5e362013-01-14 16:07:26 -080026import java.io.ByteArrayOutputStream;
27import java.io.IOException;
Dave Borowitz9de65952012-08-13 16:09:45 -070028import java.io.PrintWriter;
Dave Borowitze8a5e362013-01-14 16:07:26 -080029import java.nio.charset.Charset;
Dave Borowitz9de65952012-08-13 16:09:45 -070030import java.util.Locale;
Dave Borowitz9de65952012-08-13 16:09:45 -070031import javax.servlet.ServletOutputStream;
32import javax.servlet.http.Cookie;
33import javax.servlet.http.HttpServletResponse;
Dave Borowitz3b744b12016-08-19 16:11:10 -040034import org.eclipse.jgit.util.RawParseUtils;
Dave Borowitz9de65952012-08-13 16:09:45 -070035
36/** Simple fake implementation of {@link HttpServletResponse}. */
37public class FakeHttpServletResponse implements HttpServletResponse {
Dave Borowitze8a5e362013-01-14 16:07:26 -080038 private final ByteArrayOutputStream actualBody = new ByteArrayOutputStream();
39 private final ListMultimap<String, String> headers = LinkedListMultimap.create();
Dave Borowitz9de65952012-08-13 16:09:45 -070040
Dave Borowitze8a5e362013-01-14 16:07:26 -080041 private int status = 200;
42 private boolean committed;
43 private ServletOutputStream outputStream;
44 private PrintWriter writer;
Dave Borowitz9de65952012-08-13 16:09:45 -070045
Dave Borowitzcf38c032016-05-02 11:06:23 -040046 public FakeHttpServletResponse() {}
Dave Borowitz9de65952012-08-13 16:09:45 -070047
48 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080049 public synchronized void flushBuffer() throws IOException {
50 if (outputStream != null) {
51 outputStream.flush();
52 }
53 if (writer != null) {
54 writer.flush();
55 }
Dave Borowitz9de65952012-08-13 16:09:45 -070056 }
57
58 @Override
59 public int getBufferSize() {
60 throw new UnsupportedOperationException();
61 }
62
63 @Override
64 public String getCharacterEncoding() {
65 return UTF_8.name();
66 }
67
68 @Override
69 public String getContentType() {
70 return null;
71 }
72
73 @Override
74 public Locale getLocale() {
75 return Locale.US;
76 }
77
78 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080079 public synchronized ServletOutputStream getOutputStream() {
80 checkState(writer == null, "getWriter() already called");
81 if (outputStream == null) {
82 final PrintWriter osWriter = new PrintWriter(actualBody);
Dave Borowitzcf38c032016-05-02 11:06:23 -040083 outputStream =
84 new ServletOutputStream() {
85 @Override
86 public void write(int c) throws IOException {
87 osWriter.write(c);
88 osWriter.flush();
89 }
90 };
Dave Borowitze8a5e362013-01-14 16:07:26 -080091 }
92 return outputStream;
Dave Borowitz9de65952012-08-13 16:09:45 -070093 }
94
95 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080096 public synchronized PrintWriter getWriter() {
97 checkState(outputStream == null, "getOutputStream() already called");
98 if (writer == null) {
99 writer = new PrintWriter(actualBody);
100 }
101 return writer;
Dave Borowitz9de65952012-08-13 16:09:45 -0700102 }
103
104 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800105 public synchronized boolean isCommitted() {
106 return committed;
Dave Borowitz9de65952012-08-13 16:09:45 -0700107 }
108
109 @Override
110 public void reset() {
111 throw new UnsupportedOperationException();
112 }
113
114 @Override
115 public void resetBuffer() {
116 throw new UnsupportedOperationException();
117 }
118
119 @Override
120 public void setBufferSize(int sz) {
121 throw new UnsupportedOperationException();
122 }
123
124 @Override
125 public void setCharacterEncoding(String name) {
Dave Borowitzcf38c032016-05-02 11:06:23 -0400126 checkArgument(UTF_8.equals(Charset.forName(name)), "unsupported charset: %s", name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700127 }
128
129 @Override
130 public void setContentLength(int length) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800131 headers.removeAll(HttpHeaders.CONTENT_LENGTH);
132 headers.put(HttpHeaders.CONTENT_LENGTH, Integer.toString(length));
Dave Borowitz9de65952012-08-13 16:09:45 -0700133 }
134
135 @Override
136 public void setContentType(String type) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800137 headers.removeAll(HttpHeaders.CONTENT_TYPE);
138 headers.put(HttpHeaders.CONTENT_TYPE, type);
Dave Borowitz9de65952012-08-13 16:09:45 -0700139 }
140
141 @Override
142 public void setLocale(Locale locale) {
143 throw new UnsupportedOperationException();
144 }
145
146 @Override
147 public void addCookie(Cookie cookie) {
148 throw new UnsupportedOperationException();
149 }
150
151 @Override
152 public void addDateHeader(String name, long value) {
153 throw new UnsupportedOperationException();
154 }
155
156 @Override
157 public void addHeader(String name, String value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800158 headers.put(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700159 }
160
161 @Override
162 public void addIntHeader(String name, int value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800163 headers.put(name, Integer.toString(value));
Dave Borowitz9de65952012-08-13 16:09:45 -0700164 }
165
166 @Override
167 public boolean containsHeader(String name) {
Dave Borowitz27058932014-12-03 15:44:46 -0800168 return headers.containsKey(name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700169 }
170
171 @Override
172 public String encodeRedirectURL(String url) {
173 throw new UnsupportedOperationException();
174 }
175
176 @Override
177 @Deprecated
178 public String encodeRedirectUrl(String url) {
179 throw new UnsupportedOperationException();
180 }
181
182 @Override
183 public String encodeURL(String url) {
184 throw new UnsupportedOperationException();
185 }
186
187 @Override
188 @Deprecated
189 public String encodeUrl(String url) {
190 throw new UnsupportedOperationException();
191 }
192
193 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800194 public synchronized void sendError(int sc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700195 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800196 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700197 }
198
199 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800200 public synchronized void sendError(int sc, String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700201 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800202 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700203 }
204
205 @Override
Dave Borowitz5d5619d2014-04-18 17:01:45 -0700206 public synchronized void sendRedirect(String loc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700207 status = SC_FOUND;
Dave Borowitz5d5619d2014-04-18 17:01:45 -0700208 setHeader(HttpHeaders.LOCATION, loc);
Dave Borowitze8a5e362013-01-14 16:07:26 -0800209 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700210 }
211
212 @Override
213 public void setDateHeader(String name, long value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800214 setHeader(name, Long.toString(value));
Dave Borowitz9de65952012-08-13 16:09:45 -0700215 }
216
217 @Override
218 public void setHeader(String name, String value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800219 headers.removeAll(name);
220 addHeader(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700221 }
222
223 @Override
224 public void setIntHeader(String name, int value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800225 headers.removeAll(name);
226 addIntHeader(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700227 }
228
229 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800230 public synchronized void setStatus(int sc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700231 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800232 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700233 }
234
235 @Override
236 @Deprecated
Dave Borowitze8a5e362013-01-14 16:07:26 -0800237 public synchronized void setStatus(int sc, String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700238 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800239 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700240 }
241
Dave Borowitze8a5e362013-01-14 16:07:26 -0800242 public synchronized int getStatus() {
Dave Borowitz9de65952012-08-13 16:09:45 -0700243 return status;
244 }
Dave Borowitze8a5e362013-01-14 16:07:26 -0800245
246 public byte[] getActualBody() {
247 return actualBody.toByteArray();
248 }
249
250 public String getActualBodyString() {
251 return RawParseUtils.decode(getActualBody());
252 }
Dave Borowitzd91bdf72013-01-10 20:07:32 -0800253
254 public String getHeader(String name) {
255 return Iterables.getFirst(headers.get(checkNotNull(name)), null);
256 }
Dave Borowitz9de65952012-08-13 16:09:45 -0700257}