|
| 1 | +package datadog.trace.instrumentation.servlet; |
| 2 | + |
| 3 | +import datadog.trace.api.http.StoredByteBody; |
| 4 | +import java.io.IOException; |
| 5 | +import javax.servlet.ServletOutputStream; |
| 6 | + |
| 7 | +public abstract class AbstractServletOutputStreamWrapper extends ServletOutputStream { |
| 8 | + protected final ServletOutputStream os; |
| 9 | + private final StoredByteBody storedByteBody; |
| 10 | + |
| 11 | + public AbstractServletOutputStreamWrapper(ServletOutputStream os, StoredByteBody storedByteBody) { |
| 12 | + this.os = os; |
| 13 | + this.storedByteBody = storedByteBody; |
| 14 | + } |
| 15 | + |
| 16 | + @Override |
| 17 | + public void write(byte[] b) throws IOException { |
| 18 | + storedByteBody.appendData(b, 0, b.length); |
| 19 | + os.write(b); |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public void write(byte[] b, int off, int len) throws IOException { |
| 24 | + storedByteBody.appendData(b, off, off + len); |
| 25 | + os.write(b, off, len); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void write(int b) throws IOException { |
| 30 | + storedByteBody.appendData(b); |
| 31 | + os.write(b); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void flush() throws IOException { |
| 36 | + os.flush(); |
| 37 | + storedByteBody.maybeNotifyAndBlock(); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void close() throws IOException { |
| 42 | + os.close(); |
| 43 | + storedByteBody.maybeNotifyAndBlock(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void print(String s) throws IOException { |
| 48 | + if (s == null) { |
| 49 | + s = "null"; |
| 50 | + } |
| 51 | + byte[] bytes = s.getBytes(); |
| 52 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 53 | + os.print(s); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void print(boolean b) throws IOException { |
| 58 | + String s = String.valueOf(b); |
| 59 | + byte[] bytes = s.getBytes(); |
| 60 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 61 | + os.print(b); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void print(char c) throws IOException { |
| 66 | + String s = String.valueOf(c); |
| 67 | + byte[] bytes = s.getBytes(); |
| 68 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 69 | + os.print(c); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void print(int i) throws IOException { |
| 74 | + String s = String.valueOf(i); |
| 75 | + byte[] bytes = s.getBytes(); |
| 76 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 77 | + os.print(i); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void print(long l) throws IOException { |
| 82 | + String s = String.valueOf(l); |
| 83 | + byte[] bytes = s.getBytes(); |
| 84 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 85 | + os.print(l); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void print(float f) throws IOException { |
| 90 | + String s = String.valueOf(f); |
| 91 | + byte[] bytes = s.getBytes(); |
| 92 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 93 | + os.print(f); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void print(double d) throws IOException { |
| 98 | + String s = String.valueOf(d); |
| 99 | + byte[] bytes = s.getBytes(); |
| 100 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 101 | + os.print(d); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void println() throws IOException { |
| 106 | + byte[] bytes = "\n".getBytes(); |
| 107 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 108 | + os.println(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void println(String s) throws IOException { |
| 113 | + if (s == null) { |
| 114 | + s = "null"; |
| 115 | + } |
| 116 | + String withNewline = s + "\n"; |
| 117 | + byte[] bytes = withNewline.getBytes(); |
| 118 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 119 | + os.println(s); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void println(boolean b) throws IOException { |
| 124 | + String s = b + "\n"; |
| 125 | + byte[] bytes = s.getBytes(); |
| 126 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 127 | + os.println(b); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void println(char c) throws IOException { |
| 132 | + String s = c + "\n"; |
| 133 | + byte[] bytes = s.getBytes(); |
| 134 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 135 | + os.println(c); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void println(int i) throws IOException { |
| 140 | + String s = i + "\n"; |
| 141 | + byte[] bytes = s.getBytes(); |
| 142 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 143 | + os.println(i); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void println(long l) throws IOException { |
| 148 | + String s = l + "\n"; |
| 149 | + byte[] bytes = s.getBytes(); |
| 150 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 151 | + os.println(l); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void println(float f) throws IOException { |
| 156 | + String s = f + "\n"; |
| 157 | + byte[] bytes = s.getBytes(); |
| 158 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 159 | + os.println(f); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void println(double d) throws IOException { |
| 164 | + String s = d + "\n"; |
| 165 | + byte[] bytes = s.getBytes(); |
| 166 | + storedByteBody.appendData(bytes, 0, bytes.length); |
| 167 | + os.println(d); |
| 168 | + } |
| 169 | +} |
0 commit comments