Search in sources :

Example 1 with CountingCallback

use of org.eclipse.jetty.util.CountingCallback in project jetty.project by eclipse.

the class HttpReceiver method responseContent.

/**
     * Method to be invoked when response HTTP content is available.
     * <p>
     * This method takes case of decoding the content, if necessary, and notifying {@link org.eclipse.jetty.client.api.Response.ContentListener}s.
     *
     * @param exchange the HTTP exchange
     * @param buffer the response HTTP content buffer
     * @param callback the callback
     * @return whether the processing should continue
     */
protected boolean responseContent(HttpExchange exchange, ByteBuffer buffer, final Callback callback) {
    out: while (true) {
        ResponseState current = responseState.get();
        switch(current) {
            case HEADERS:
            case CONTENT:
                {
                    if (updateResponseState(current, ResponseState.TRANSIENT))
                        break out;
                    break;
                }
            default:
                {
                    callback.failed(new IllegalStateException("Invalid response state " + current));
                    return false;
                }
        }
    }
    HttpResponse response = exchange.getResponse();
    if (LOG.isDebugEnabled())
        LOG.debug("Response content {}{}{}", response, System.lineSeparator(), BufferUtil.toDetailString(buffer));
    ResponseNotifier notifier = getHttpDestination().getResponseNotifier();
    List<Response.ResponseListener> listeners = exchange.getConversation().getResponseListeners();
    ContentDecoder decoder = this.decoder;
    if (decoder == null) {
        notifier.notifyContent(listeners, response, buffer, callback);
    } else {
        try {
            List<ByteBuffer> decodeds = new ArrayList<>(2);
            while (buffer.hasRemaining()) {
                ByteBuffer decoded = decoder.decode(buffer);
                if (!decoded.hasRemaining())
                    continue;
                decodeds.add(decoded);
                if (LOG.isDebugEnabled())
                    LOG.debug("Response content decoded ({}) {}{}{}", decoder, response, System.lineSeparator(), BufferUtil.toDetailString(decoded));
            }
            if (decodeds.isEmpty()) {
                callback.succeeded();
            } else {
                int size = decodeds.size();
                CountingCallback counter = new CountingCallback(callback, size);
                for (int i = 0; i < size; ++i) notifier.notifyContent(listeners, response, decodeds.get(i), counter);
            }
        } catch (Throwable x) {
            callback.failed(x);
        }
    }
    if (updateResponseState(ResponseState.TRANSIENT, ResponseState.CONTENT))
        return true;
    terminateResponse(exchange);
    return false;
}
Also used : ArrayList(java.util.ArrayList) CountingCallback(org.eclipse.jetty.util.CountingCallback) ByteBuffer(java.nio.ByteBuffer)

Example 2 with CountingCallback

use of org.eclipse.jetty.util.CountingCallback in project jetty.project by eclipse.

the class HTTP2Session method frames.

@Override
public void frames(IStream stream, Callback callback, Frame frame, Frame... frames) {
    // We want to generate as late as possible to allow re-prioritization;
    // generation will happen while processing the entries.
    // The callback needs to be notified only when the last frame completes.
    int length = frames.length;
    if (length == 0) {
        frame(new ControlEntry(frame, stream, callback), true);
    } else {
        callback = new CountingCallback(callback, 1 + length);
        frame(new ControlEntry(frame, stream, callback), false);
        for (int i = 1; i <= length; ++i) frame(new ControlEntry(frames[i - 1], stream, callback), i == length);
    }
}
Also used : CountingCallback(org.eclipse.jetty.util.CountingCallback) EndPoint(org.eclipse.jetty.io.EndPoint)

Aggregations

CountingCallback (org.eclipse.jetty.util.CountingCallback)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 EndPoint (org.eclipse.jetty.io.EndPoint)1