Skip to main content
summaryrefslogtreecommitdiffstats
blob: e3bf2a1bb929738ba33889c13af24b0f8e202a2f (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
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.server;

import java.io.IOException;
import java.io.InterruptedIOException;

import org.eclipse.jetty.util.ArrayQueue;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
 * {@link QueuedHttpInput} holds a queue of items passed to it by calls to {@link #content(Object)}.
 * <p/>
 * {@link QueuedHttpInput} stores the items directly; if the items contain byte buffers, it does not copy them
 * but simply holds references to the item, thus the caller must organize for those buffers to valid while
 * held by this class.
 * <p/>
 * To assist the caller, subclasses may override methods {@link #onAsyncRead()}, {@link #onContentConsumed(Object)}
 * that can be implemented so that the caller will know when buffers are queued and consumed.
 */
public abstract class QueuedHttpInput<T> extends HttpInput<T>
{
    private final static Logger LOG = Log.getLogger(QueuedHttpInput.class);

    private final ArrayQueue<T> _inputQ = new ArrayQueue<>(lock());

    public QueuedHttpInput()
    {
    }

    public void content(T item)
    {
        // The buffer is not copied here.  This relies on the caller not recycling the buffer
        // until the it is consumed.  The onContentConsumed and onAllContentConsumed() callbacks are
        // the signals to the caller that the buffers can be recycled.

        synchronized (lock())
        {
            boolean wasEmpty = _inputQ.isEmpty();
            _inputQ.add(item);
            if (LOG.isDebugEnabled())
                LOG.debug("{} queued {}", this, item);
            if (wasEmpty)
            {
                if (!onAsyncRead())
                    lock().notify();
            }
        }
    }

    public void recycle()
    {
        synchronized (lock())
        {
            T item = _inputQ.pollUnsafe();
            while (item != null)
            {
                onContentConsumed(item);
                item = _inputQ.pollUnsafe();
            }
            super.recycle();
        }
    }

    @Override
    protected T nextContent()
    {
        synchronized (lock())
        {
            // Items are removed only when they are fully consumed.
            T item = _inputQ.peekUnsafe();
            // Skip consumed items at the head of the queue.
            while (item != null && remaining(item) == 0)
            {
                _inputQ.pollUnsafe();
                onContentConsumed(item);
                if (LOG.isDebugEnabled())
                    LOG.debug("{} consumed {}", this, item);
                item = _inputQ.peekUnsafe();
            }
            return item;
        }
    }

    protected void blockForContent() throws IOException
    {
        synchronized (lock())
        {
            while (_inputQ.isEmpty() && !isFinished() && !isEOF())
            {
                try
                {
                    if (LOG.isDebugEnabled())
                        LOG.debug("{} waiting for content", this);
                    lock().wait();
                }
                catch (InterruptedException e)
                {
                    throw (IOException)new InterruptedIOException().initCause(e);
                }
            }
        }
    }

    /**
     * Callback that signals that the given content has been consumed.
     *
     * @param item the consumed content
     */
    protected abstract void onContentConsumed(T item);

    public void earlyEOF()
    {
        synchronized (lock())
        {
            super.earlyEOF();
            lock().notify();
        }
    }

    public void messageComplete()
    {
        synchronized (lock())
        {
            super.messageComplete();
            lock().notify();
        }
    }
}

Back to the top