aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/FilterInvokingPrintWriter.java
blob: 3ebc7bbc5510d28e671b40bbc04ca89d9f2d0541 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Locale;

/**
 * Invokes the response filter the first time anything is output to the underlying PrintWriter.
 * The filter must be invoked before the first output call since this might cause the response
 * to be committed, i.e. locked and potentially put on the wire.
 * Any changes to the response after it has been committed might be ignored or cause exceptions.
 * @author Tony Vaagenes
 */
final class FilterInvokingPrintWriter extends PrintWriter {
    private final PrintWriter delegate;
    private final OneTimeRunnable filterInvoker;

    public FilterInvokingPrintWriter(PrintWriter delegate, OneTimeRunnable filterInvoker) {
        /* The PrintWriter class both
         * 1) exposes new methods, the PrintWriter "interface"
         * 2) implements PrintWriter and Writer methods that does some extra things before calling down to the writer methods.
         * If super was invoked with the delegate PrintWriter, the superclass would behave as a PrintWriter(PrintWriter),
         * i.e. the extra things in 2. would be done twice.
         * To avoid this, all the methods of PrintWriter are overridden with versions that forward directly to the underlying delegate
         * instead of going through super.
         * The super class is initialized with a non-functioning writer to catch mistakenly non-overridden methods.
         */
        super(new Writer() {
            @Override
            public void write(char[] cbuf, int off, int len) throws IOException {
                throwAssertionError();
            }

            private void throwAssertionError() {
                throw new AssertionError(FilterInvokingPrintWriter.class.getName() + " failed to delegate to the underlying writer");
            }

            @Override
            public void flush() throws IOException {
                throwAssertionError();
            }

            @Override
            public void close() throws IOException {
                throwAssertionError();
            }
        });

        this.delegate = delegate;
        this.filterInvoker = filterInvoker;
    }

    @Override
    public String toString() {
        return getClass().getName() + " (" + super.toString() + ")";
    }

    private void runFilterIfFirstInvocation() {
        filterInvoker.runIfFirstInvocation();
    }

    @Override
    public void flush() {
        runFilterIfFirstInvocation();
        delegate.flush();
    }

    @Override
    public void close() {
        runFilterIfFirstInvocation();
        delegate.close();
    }

    @Override
    public boolean checkError() {
        return delegate.checkError();
    }

    @Override
    public void write(int c) {
        runFilterIfFirstInvocation();
        delegate.write(c);
    }

    @Override
    public void write(char[] buf, int off, int len) {
        runFilterIfFirstInvocation();
        delegate.write(buf, off, len);
    }

    @Override
    public void write(char[] buf) {
        runFilterIfFirstInvocation();
        delegate.write(buf);
    }

    @Override
    public void write(String s, int off, int len) {
        runFilterIfFirstInvocation();
        delegate.write(s, off, len);
    }

    @Override
    public void write(String s) {
        runFilterIfFirstInvocation();
        delegate.write(s);
    }

    @Override
    public void print(boolean b) {
        runFilterIfFirstInvocation();
        delegate.print(b);
    }

    @Override
    public void print(char c) {
        runFilterIfFirstInvocation();
        delegate.print(c);
    }

    @Override
    public void print(int i) {
        runFilterIfFirstInvocation();
        delegate.print(i);
    }

    @Override
    public void print(long l) {
        runFilterIfFirstInvocation();
        delegate.print(l);
    }

    @Override
    public void print(float f) {
        runFilterIfFirstInvocation();
        delegate.print(f);
    }

    @Override
    public void print(double d) {
        runFilterIfFirstInvocation();
        delegate.print(d);
    }

    @Override
    public void print(char[] s) {
        runFilterIfFirstInvocation();
        delegate.print(s);
    }

    @Override
    public void print(String s) {
        runFilterIfFirstInvocation();
        delegate.print(s);
    }

    @Override
    public void print(Object obj) {
        runFilterIfFirstInvocation();
        delegate.print(obj);
    }

    @Override
    public void println() {
        runFilterIfFirstInvocation();
        delegate.println();
    }

    @Override
    public void println(boolean x) {
        runFilterIfFirstInvocation();
        delegate.println(x);
    }

    @Override
    public void println(char x) {
        runFilterIfFirstInvocation();
        delegate.println(x);
    }

    @Override
    public void println(int x) {
        runFilterIfFirstInvocation();
        delegate.println(x);
    }

    @Override
    public void println(long x) {
        runFilterIfFirstInvocation();
        delegate.println(x);
    }

    @Override
    public void println(float x) {
        runFilterIfFirstInvocation();
        delegate.println(x);
    }

    @Override
    public void println(double x) {
        runFilterIfFirstInvocation();
        delegate.println(x);
    }

    @Override
    public void println(char[] x) {
        runFilterIfFirstInvocation();
        delegate.println(x);
    }

    @Override
    public void println(String x) {
        runFilterIfFirstInvocation();
        delegate.println(x);
    }

    @Override
    public void println(Object x) {
        runFilterIfFirstInvocation();
        delegate.println(x);
    }

    @Override
    public PrintWriter printf(String format, Object... args) {
        runFilterIfFirstInvocation();
        return delegate.printf(format, args);
    }

    @Override
    public PrintWriter printf(Locale l, String format, Object... args) {
        runFilterIfFirstInvocation();
        return delegate.printf(l, format, args);
    }

    @Override
    public PrintWriter format(String format, Object... args) {
        runFilterIfFirstInvocation();
        return delegate.format(format, args);
    }

    @Override
    public PrintWriter format(Locale l, String format, Object... args) {
        runFilterIfFirstInvocation();
        return delegate.format(l, format, args);
    }

    @Override
    public PrintWriter append(CharSequence csq) {
        runFilterIfFirstInvocation();
        return delegate.append(csq);
    }

    @Override
    public PrintWriter append(CharSequence csq, int start, int end) {
        runFilterIfFirstInvocation();
        return delegate.append(csq, start, end);
    }

    @Override
    public PrintWriter append(char c) {
        runFilterIfFirstInvocation();
        return delegate.append(c);
    }
}