aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/FilterInvokingServletOutputStream.java
blob: a605ccebfa78b2754bbc40d276a5997c7f02e2cb (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
// 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 javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import java.io.IOException;

/**
 * Invokes the response filter the first time anything is output to the underlying ServletOutputStream.
 * 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
 */
class FilterInvokingServletOutputStream extends ServletOutputStream {
    private final ServletOutputStream delegate;
    private final OneTimeRunnable filterInvoker;

    public FilterInvokingServletOutputStream(ServletOutputStream delegate, OneTimeRunnable filterInvoker) {
        this.delegate = delegate;
        this.filterInvoker = filterInvoker;
    }

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

    @Override
    public void setWriteListener(WriteListener writeListener) {
        delegate.setWriteListener(writeListener);
    }


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

    @Override
    public void write(int b) throws IOException {
        runFilterIfFirstInvocation();
        delegate.write(b);
    }


    @Override
    public void write(byte[] b) throws IOException {
        runFilterIfFirstInvocation();
        delegate.write(b);
    }

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

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        runFilterIfFirstInvocation();
        delegate.write(b, off, len);
    }

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

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

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

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

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

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

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

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

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

    @Override
    public void println(String s) throws IOException {
        runFilterIfFirstInvocation();
        delegate.println(s);
    }

    @Override
    public void println(boolean b) throws IOException {
        runFilterIfFirstInvocation();
        delegate.println(b);
    }

    @Override
    public void println(char c) throws IOException {
        runFilterIfFirstInvocation();
        delegate.println(c);
    }

    @Override
    public void println(int i) throws IOException {
        runFilterIfFirstInvocation();
        delegate.println(i);
    }

    @Override
    public void println(long l) throws IOException {
        runFilterIfFirstInvocation();
        delegate.println(l);
    }

    @Override
    public void println(float f) throws IOException {
        runFilterIfFirstInvocation();
        delegate.println(f);
    }

    @Override
    public void println(double d) throws IOException {
        runFilterIfFirstInvocation();
        delegate.println(d);
    }

    @Override
    public String toString() {
        return getClass().getCanonicalName() + " (" + delegate.toString() + ")";
    }
}