aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/ResponseHandlerToApplicationResponseWrapper.java
blob: 765da006deb0196c94cefca1249a0415d7a65f89 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi;

import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.handler.CompletionHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseHandler;

import java.nio.ByteBuffer;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * A {@link ResponseHandler} that caches the response content and
 * converts a {@link Response} to {@link com.yahoo.application.container.handler.Response}.
 *
 * @author bjorncs
 */
public class ResponseHandlerToApplicationResponseWrapper implements ResponseHandler {

    private Response response;
    private SimpleContentChannel contentChannel;

    @Override
    public ContentChannel handleResponse(Response response) {
        this.response = response;
        SimpleContentChannel contentChannel = new SimpleContentChannel();
        this.contentChannel = contentChannel;
        return contentChannel;
    }

    public Optional<com.yahoo.application.container.handler.Response> toResponse() {
        return Optional.ofNullable(this.response)
                .map(r -> {
                    byte[] bytes = contentChannel.toByteArray();
                    return new com.yahoo.application.container.handler.Response(response.getStatus(), bytes);
                });
    }

    private static class SimpleContentChannel implements ContentChannel {

        private final Queue<ByteBuffer> buffers = new ConcurrentLinkedQueue<>();
        private final AtomicBoolean closed = new AtomicBoolean(false);

        @Override
        public void write(ByteBuffer buf, CompletionHandler handler) {
            buffers.add(buf);
            handler.completed();
        }

        @Override
        public void close(CompletionHandler handler) {
            handler.completed();
            if (closed.getAndSet(true)) {
                throw new IllegalStateException("Already closed");
            }
        }

        byte[] toByteArray() {
            if (!closed.get()) {
                throw new IllegalStateException("Content channel not closed yet");
            }
            int totalSize = 0;
            for (ByteBuffer responseBuffer : buffers) {
                totalSize += responseBuffer.remaining();
            }
            ByteBuffer totalBuffer = ByteBuffer.allocate(totalSize);
            for (ByteBuffer responseBuffer : buffers) {
                totalBuffer.put(responseBuffer);
            }
            return totalBuffer.array();
        }
    }

}