| 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; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 28 | import java.io.PrintWriter; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 29 | import java.nio.charset.Charset; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 30 | import java.util.Locale; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 31 | import javax.servlet.ServletOutputStream; |
| 32 | import javax.servlet.http.Cookie; |
| 33 | import javax.servlet.http.HttpServletResponse; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 34 | import org.eclipse.jgit.util.RawParseUtils; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 35 | |
| 36 | /** Simple fake implementation of {@link HttpServletResponse}. */ |
| 37 | public class FakeHttpServletResponse implements HttpServletResponse { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 38 | private final ByteArrayOutputStream actualBody = new ByteArrayOutputStream(); |
| 39 | private final ListMultimap<String, String> headers = LinkedListMultimap.create(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 40 | |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 41 | private int status = 200; |
| 42 | private boolean committed; |
| 43 | private ServletOutputStream outputStream; |
| 44 | private PrintWriter writer; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 45 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 46 | public FakeHttpServletResponse() {} |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 47 | |
| 48 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 49 | public synchronized void flushBuffer() throws IOException { |
| 50 | if (outputStream != null) { |
| 51 | outputStream.flush(); |
| 52 | } |
| 53 | if (writer != null) { |
| 54 | writer.flush(); |
| 55 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 56 | } |
| 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 Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 79 | public synchronized ServletOutputStream getOutputStream() { |
| 80 | checkState(writer == null, "getWriter() already called"); |
| 81 | if (outputStream == null) { |
| 82 | final PrintWriter osWriter = new PrintWriter(actualBody); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 83 | outputStream = |
| 84 | new ServletOutputStream() { |
| 85 | @Override |
| 86 | public void write(int c) throws IOException { |
| 87 | osWriter.write(c); |
| 88 | osWriter.flush(); |
| 89 | } |
| 90 | }; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 91 | } |
| 92 | return outputStream; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 96 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 105 | public synchronized boolean isCommitted() { |
| 106 | return committed; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 107 | } |
| 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 Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 126 | checkArgument(UTF_8.equals(Charset.forName(name)), "unsupported charset: %s", name); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void setContentLength(int length) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 131 | headers.removeAll(HttpHeaders.CONTENT_LENGTH); |
| 132 | headers.put(HttpHeaders.CONTENT_LENGTH, Integer.toString(length)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void setContentType(String type) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 137 | headers.removeAll(HttpHeaders.CONTENT_TYPE); |
| 138 | headers.put(HttpHeaders.CONTENT_TYPE, type); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 139 | } |
| 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 Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 158 | headers.put(name, value); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void addIntHeader(String name, int value) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 163 | headers.put(name, Integer.toString(value)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public boolean containsHeader(String name) { |
| Dave Borowitz | 2705893 | 2014-12-03 15:44:46 -0800 | [diff] [blame] | 168 | return headers.containsKey(name); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 169 | } |
| 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 Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 194 | public synchronized void sendError(int sc) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 195 | status = sc; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 196 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 200 | public synchronized void sendError(int sc, String msg) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 201 | status = sc; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 202 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| Dave Borowitz | 5d5619d | 2014-04-18 17:01:45 -0700 | [diff] [blame] | 206 | public synchronized void sendRedirect(String loc) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 207 | status = SC_FOUND; |
| Dave Borowitz | 5d5619d | 2014-04-18 17:01:45 -0700 | [diff] [blame] | 208 | setHeader(HttpHeaders.LOCATION, loc); |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 209 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void setDateHeader(String name, long value) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 214 | setHeader(name, Long.toString(value)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void setHeader(String name, String value) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 219 | headers.removeAll(name); |
| 220 | addHeader(name, value); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void setIntHeader(String name, int value) { |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 225 | headers.removeAll(name); |
| 226 | addIntHeader(name, value); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 230 | public synchronized void setStatus(int sc) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 231 | status = sc; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 232 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | @Deprecated |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 237 | public synchronized void setStatus(int sc, String msg) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 238 | status = sc; |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 239 | committed = true; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 242 | public synchronized int getStatus() { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 243 | return status; |
| 244 | } |
| Dave Borowitz | e8a5e36 | 2013-01-14 16:07:26 -0800 | [diff] [blame] | 245 | |
| 246 | public byte[] getActualBody() { |
| 247 | return actualBody.toByteArray(); |
| 248 | } |
| 249 | |
| 250 | public String getActualBodyString() { |
| 251 | return RawParseUtils.decode(getActualBody()); |
| 252 | } |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 253 | |
| 254 | public String getHeader(String name) { |
| 255 | return Iterables.getFirst(headers.get(checkNotNull(name)), null); |
| 256 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 257 | } |