blob: 85da54f4bf0434113540ce74dded78311d8d1dbe [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;
Dave Borowitzd91bdf72013-01-10 20:07:32 -080019import static com.google.common.base.Preconditions.checkNotNull;
Dave Borowitze8a5e362013-01-14 16:07:26 -080020import static com.google.common.base.Preconditions.checkState;
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
34import com.google.common.base.Charsets;
35import com.google.common.collect.Iterables;
36import com.google.common.collect.LinkedListMultimap;
37import com.google.common.collect.ListMultimap;
38import com.google.common.net.HttpHeaders;
39
Dave Borowitz9de65952012-08-13 16:09:45 -070040/** Simple fake implementation of {@link HttpServletResponse}. */
41public class FakeHttpServletResponse implements HttpServletResponse {
Dave Borowitze8a5e362013-01-14 16:07:26 -080042 private final ByteArrayOutputStream actualBody = new ByteArrayOutputStream();
43 private final ListMultimap<String, String> headers = LinkedListMultimap.create();
Dave Borowitz9de65952012-08-13 16:09:45 -070044
Dave Borowitze8a5e362013-01-14 16:07:26 -080045 private int status = 200;
46 private boolean committed;
47 private ServletOutputStream outputStream;
48 private PrintWriter writer;
Dave Borowitz9de65952012-08-13 16:09:45 -070049
50 public FakeHttpServletResponse() {
Dave Borowitz9de65952012-08-13 16:09:45 -070051 }
52
53 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080054 public synchronized void flushBuffer() throws IOException {
55 if (outputStream != null) {
56 outputStream.flush();
57 }
58 if (writer != null) {
59 writer.flush();
60 }
Dave Borowitz9de65952012-08-13 16:09:45 -070061 }
62
63 @Override
64 public int getBufferSize() {
65 throw new UnsupportedOperationException();
66 }
67
68 @Override
69 public String getCharacterEncoding() {
70 return UTF_8.name();
71 }
72
73 @Override
74 public String getContentType() {
75 return null;
76 }
77
78 @Override
79 public Locale getLocale() {
80 return Locale.US;
81 }
82
83 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -080084 public synchronized ServletOutputStream getOutputStream() {
85 checkState(writer == null, "getWriter() already called");
86 if (outputStream == null) {
87 final PrintWriter osWriter = new PrintWriter(actualBody);
88 outputStream = new ServletOutputStream() {
89 @Override
90 public void write(int c) throws IOException {
91 osWriter.write(c);
92 osWriter.flush();
93 }
94 };
95 }
96 return outputStream;
Dave Borowitz9de65952012-08-13 16:09:45 -070097 }
98
99 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800100 public synchronized PrintWriter getWriter() {
101 checkState(outputStream == null, "getOutputStream() already called");
102 if (writer == null) {
103 writer = new PrintWriter(actualBody);
104 }
105 return writer;
Dave Borowitz9de65952012-08-13 16:09:45 -0700106 }
107
108 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800109 public synchronized boolean isCommitted() {
110 return committed;
Dave Borowitz9de65952012-08-13 16:09:45 -0700111 }
112
113 @Override
114 public void reset() {
115 throw new UnsupportedOperationException();
116 }
117
118 @Override
119 public void resetBuffer() {
120 throw new UnsupportedOperationException();
121 }
122
123 @Override
124 public void setBufferSize(int sz) {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public void setCharacterEncoding(String name) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800130 checkArgument(Charsets.UTF_8.equals(Charset.forName(name)),
131 "unsupported charset: %s", name);
Dave Borowitz9de65952012-08-13 16:09:45 -0700132 }
133
134 @Override
135 public void setContentLength(int length) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800136 headers.removeAll(HttpHeaders.CONTENT_LENGTH);
137 headers.put(HttpHeaders.CONTENT_LENGTH, Integer.toString(length));
Dave Borowitz9de65952012-08-13 16:09:45 -0700138 }
139
140 @Override
141 public void setContentType(String type) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800142 headers.removeAll(HttpHeaders.CONTENT_TYPE);
143 headers.put(HttpHeaders.CONTENT_TYPE, type);
Dave Borowitz9de65952012-08-13 16:09:45 -0700144 }
145
146 @Override
147 public void setLocale(Locale locale) {
148 throw new UnsupportedOperationException();
149 }
150
151 @Override
152 public void addCookie(Cookie cookie) {
153 throw new UnsupportedOperationException();
154 }
155
156 @Override
157 public void addDateHeader(String name, long value) {
158 throw new UnsupportedOperationException();
159 }
160
161 @Override
162 public void addHeader(String name, String value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800163 headers.put(name, value);
Dave Borowitz9de65952012-08-13 16:09:45 -0700164 }
165
166 @Override
167 public void addIntHeader(String name, int value) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800168 headers.put(name, Integer.toString(value));
Dave Borowitz9de65952012-08-13 16:09:45 -0700169 }
170
171 @Override
172 public boolean containsHeader(String name) {
Dave Borowitze8a5e362013-01-14 16:07:26 -0800173 return !headers.get(name).isEmpty();
Dave Borowitz9de65952012-08-13 16:09:45 -0700174 }
175
176 @Override
177 public String encodeRedirectURL(String url) {
178 throw new UnsupportedOperationException();
179 }
180
181 @Override
182 @Deprecated
183 public String encodeRedirectUrl(String url) {
184 throw new UnsupportedOperationException();
185 }
186
187 @Override
188 public String encodeURL(String url) {
189 throw new UnsupportedOperationException();
190 }
191
192 @Override
193 @Deprecated
194 public String encodeUrl(String url) {
195 throw new UnsupportedOperationException();
196 }
197
198 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800199 public synchronized void sendError(int sc) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700200 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800201 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700202 }
203
204 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800205 public synchronized void sendError(int sc, String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700206 status = sc;
Dave Borowitze8a5e362013-01-14 16:07:26 -0800207 committed = true;
Dave Borowitz9de65952012-08-13 16:09:45 -0700208 }
209
210 @Override
Dave Borowitze8a5e362013-01-14 16:07:26 -0800211 public synchronized void sendRedirect(String msg) {
Dave Borowitz9de65952012-08-13 16:09:45 -0700212 status = SC_FOUND;
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}