| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkArgument; |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkState; |
| David Pletcher | d7bdaf3 | 2014-08-27 14:50:32 -0700 | [diff] [blame] | 20 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | |
| Dave Borowitz | fe8fdab | 2014-11-04 16:19:33 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Iterables; |
| 23 | import com.google.common.collect.LinkedListMultimap; |
| 24 | import com.google.common.collect.ListMultimap; |
| 25 | import com.google.common.net.HttpHeaders; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 26 | import java.io.ByteArrayOutputStream; |
| 27 | import java.io.IOException; |
| David Pursehouse | 784216f | 2018-11-08 11:24:18 +0900 | [diff] [blame] | 28 | import java.io.OutputStreamWriter; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | import java.io.PrintWriter; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 30 | import java.nio.charset.Charset; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 31 | import java.util.Locale; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 32 | import javax.servlet.ServletOutputStream; |
| 33 | import javax.servlet.http.Cookie; |
| 34 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 35 | import org.eclipse.jgit.util.RawParseUtils; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 36 | |
| 37 | /** Simple fake implementation of {@link HttpServletResponse}. */ |
| 38 | public class FakeHttpServletResponse implements HttpServletResponse { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 39 | private final ByteArrayOutputStream actualBody = new ByteArrayOutputStream(); |
| 40 | private final ListMultimap<String, String> headers = LinkedListMultimap.create(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 41 | |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 42 | private int status = 200; |
| 43 | private boolean committed; |
| 44 | private ServletOutputStream outputStream; |
| 45 | private PrintWriter writer; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 47 | public FakeHttpServletResponse() {} |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 48 | |
| 49 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 50 | public synchronized void flushBuffer() throws IOException { |
| 51 | if (outputStream != null) { |
| 52 | outputStream.flush(); |
| 53 | } |
| 54 | if (writer != null) { |
| 55 | writer.flush(); |
| 56 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public int getBufferSize() { |
| 61 | throw new UnsupportedOperationException(); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String getCharacterEncoding() { |
| 66 | return UTF_8.name(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String getContentType() { |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Locale getLocale() { |
| 76 | return Locale.US; |
| 77 | } |
| 78 | |
| 79 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 80 | public synchronized ServletOutputStream getOutputStream() { |
| 81 | checkState(writer == null, "getWriter() already called"); |
| 82 | if (outputStream == null) { |
| David Pursehouse | 784216f | 2018-11-08 11:24:18 +0900 | [diff] [blame] | 83 | PrintWriter osWriter = newPrintWriter(); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 84 | outputStream = |
| 85 | new ServletOutputStream() { |
| 86 | @Override |
| 87 | public void write(int c) throws IOException { |
| 88 | osWriter.write(c); |
| 89 | osWriter.flush(); |
| 90 | } |
| 91 | }; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 92 | } |
| 93 | return outputStream; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 97 | public synchronized PrintWriter getWriter() { |
| 98 | checkState(outputStream == null, "getOutputStream() already called"); |
| 99 | if (writer == null) { |
| David Pursehouse | 784216f | 2018-11-08 11:24:18 +0900 | [diff] [blame] | 100 | writer = newPrintWriter(); |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 101 | } |
| 102 | return writer; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 106 | public synchronized boolean isCommitted() { |
| 107 | return committed; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void reset() { |
| 112 | throw new UnsupportedOperationException(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void resetBuffer() { |
| 117 | throw new UnsupportedOperationException(); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void setBufferSize(int sz) { |
| 122 | throw new UnsupportedOperationException(); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void setCharacterEncoding(String name) { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 127 | checkArgument(UTF_8.equals(Charset.forName(name)), "unsupported charset: %s", name); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void setContentLength(int length) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 132 | headers.removeAll(HttpHeaders.CONTENT_LENGTH); |
| 133 | headers.put(HttpHeaders.CONTENT_LENGTH, Integer.toString(length)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void setContentType(String type) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 138 | headers.removeAll(HttpHeaders.CONTENT_TYPE); |
| 139 | headers.put(HttpHeaders.CONTENT_TYPE, type); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void setLocale(Locale locale) { |
| 144 | throw new UnsupportedOperationException(); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void addCookie(Cookie cookie) { |
| 149 | throw new UnsupportedOperationException(); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void addDateHeader(String name, long value) { |
| 154 | throw new UnsupportedOperationException(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void addHeader(String name, String value) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 159 | headers.put(name, value); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void addIntHeader(String name, int value) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 164 | headers.put(name, Integer.toString(value)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public boolean containsHeader(String name) { |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 169 | return headers.containsKey(name); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public String encodeRedirectURL(String url) { |
| 174 | throw new UnsupportedOperationException(); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | @Deprecated |
| 179 | public String encodeRedirectUrl(String url) { |
| 180 | throw new UnsupportedOperationException(); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public String encodeURL(String url) { |
| 185 | throw new UnsupportedOperationException(); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | @Deprecated |
| 190 | public String encodeUrl(String url) { |
| 191 | throw new UnsupportedOperationException(); |
| 192 | } |
| 193 | |
| 194 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 195 | public synchronized void sendError(int sc) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 196 | status = sc; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 197 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 201 | public synchronized void sendError(int sc, String msg) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 202 | status = sc; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 203 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| Dave Borowitz | 5d5619d | 2014-04-18 17:01:45 -0700 | [diff] [blame] | 207 | public synchronized void sendRedirect(String loc) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 208 | status = SC_FOUND; |
| Dave Borowitz | 5d5619d | 2014-04-18 17:01:45 -0700 | [diff] [blame] | 209 | setHeader(HttpHeaders.LOCATION, loc); |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 210 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void setDateHeader(String name, long value) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 215 | setHeader(name, Long.toString(value)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void setHeader(String name, String value) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 220 | headers.removeAll(name); |
| 221 | addHeader(name, value); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void setIntHeader(String name, int value) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 226 | headers.removeAll(name); |
| 227 | addIntHeader(name, value); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 231 | public synchronized void setStatus(int sc) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 232 | status = sc; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 233 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
| 237 | @Deprecated |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 238 | public synchronized void setStatus(int sc, String msg) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 239 | status = sc; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 240 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 243 | public synchronized int getStatus() { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 244 | return status; |
| 245 | } |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 246 | |
| 247 | public byte[] getActualBody() { |
| 248 | return actualBody.toByteArray(); |
| 249 | } |
| 250 | |
| 251 | public String getActualBodyString() { |
| 252 | return RawParseUtils.decode(getActualBody()); |
| 253 | } |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 254 | |
| 255 | public String getHeader(String name) { |
| 256 | return Iterables.getFirst(headers.get(checkNotNull(name)), null); |
| 257 | } |
| David Pursehouse | 784216f | 2018-11-08 11:24:18 +0900 | [diff] [blame] | 258 | |
| 259 | private PrintWriter newPrintWriter() { |
| 260 | return new PrintWriter(new OutputStreamWriter(actualBody, UTF_8)); |
| 261 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 262 | } |