| 1 | |
package org.webslinger.servlet; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.PrintWriter; |
| 5 | |
import java.io.Writer; |
| 6 | |
import java.util.Locale; |
| 7 | |
import javax.servlet.ServletOutputStream; |
| 8 | |
import javax.servlet.ServletResponse; |
| 9 | |
|
| 10 | |
public final class FakeServletResponse implements ServletResponse { |
| 11 | |
private final PrintWriter writer; |
| 12 | |
private final ServletOutputStream outputStream; |
| 13 | |
private final ServletResponse response; |
| 14 | |
|
| 15 | |
private String encoding; |
| 16 | |
private String contentType; |
| 17 | 986 | private Locale locale = Locale.getDefault(); |
| 18 | |
|
| 19 | |
public FakeServletResponse(ServletOutputStream outputStream) { |
| 20 | 0 | this(outputStream, null, null); |
| 21 | 0 | } |
| 22 | |
|
| 23 | |
public FakeServletResponse(ServletResponse response) { |
| 24 | 0 | this(null, null, response); |
| 25 | 0 | } |
| 26 | |
|
| 27 | |
public FakeServletResponse(Writer writer) { |
| 28 | 986 | this(null, writer instanceof PrintWriter ? (PrintWriter) writer : new PrintWriter(writer), null); |
| 29 | 986 | } |
| 30 | |
|
| 31 | 986 | private FakeServletResponse(ServletOutputStream outputStream, PrintWriter writer, ServletResponse response) { |
| 32 | 986 | this.outputStream = outputStream; |
| 33 | 986 | this.writer = writer; |
| 34 | 986 | this.response = response; |
| 35 | 986 | } |
| 36 | |
|
| 37 | |
public void flushBuffer() throws IOException { |
| 38 | 0 | if (writer != null) getWriter().flush(); |
| 39 | 0 | if (response != null) response.flushBuffer(); |
| 40 | 0 | } |
| 41 | |
|
| 42 | |
public int getBufferSize() { |
| 43 | 0 | return 0; |
| 44 | |
} |
| 45 | |
|
| 46 | |
public String getCharacterEncoding() { |
| 47 | 0 | return encoding; |
| 48 | |
} |
| 49 | |
|
| 50 | |
public String getContentType() { |
| 51 | 0 | return contentType; |
| 52 | |
} |
| 53 | |
|
| 54 | |
public Locale getLocale() { |
| 55 | 0 | return locale; |
| 56 | |
} |
| 57 | |
|
| 58 | |
public ServletOutputStream getOutputStream() throws IOException { |
| 59 | 0 | if (writer != null) throw new IllegalStateException("getWriter already called"); |
| 60 | 0 | if (outputStream == null) { |
| 61 | 0 | if (response == null) throw new UnsupportedOperationException("No output method available"); |
| 62 | 0 | return response.getOutputStream(); |
| 63 | |
} |
| 64 | 0 | return outputStream; |
| 65 | |
} |
| 66 | |
|
| 67 | |
public PrintWriter getWriter() throws IOException { |
| 68 | 4 | if (outputStream != null) throw new IllegalStateException("getOutputStream already called"); |
| 69 | 4 | if (writer == null) { |
| 70 | 0 | if (response == null) throw new UnsupportedOperationException("No output method available"); |
| 71 | 0 | return response.getWriter(); |
| 72 | |
} |
| 73 | 4 | return writer; |
| 74 | |
} |
| 75 | |
|
| 76 | |
public boolean isCommitted() { |
| 77 | 0 | if (response != null) return response.isCommitted(); |
| 78 | 0 | return true; |
| 79 | |
} |
| 80 | |
|
| 81 | |
public void reset() { |
| 82 | 0 | throw new UnsupportedOperationException(); |
| 83 | |
} |
| 84 | |
|
| 85 | |
public void resetBuffer() { |
| 86 | 0 | throw new UnsupportedOperationException(); |
| 87 | |
} |
| 88 | |
|
| 89 | |
public void setBufferSize(int size) { |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
public void setCharacterEncoding(String encoding) { |
| 93 | 461 | this.encoding = encoding; |
| 94 | 461 | } |
| 95 | |
|
| 96 | |
public void setContentLength(int contentLength) { |
| 97 | 0 | } |
| 98 | |
|
| 99 | |
public void setContentType(String contentType) { |
| 100 | 0 | this.contentType = contentType; |
| 101 | 0 | } |
| 102 | |
|
| 103 | |
public void setLocale(Locale locale) { |
| 104 | 0 | this.locale = locale; |
| 105 | 0 | } |
| 106 | |
} |