summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/status/StaticResourceRequestHandler.java
blob: fa8128753f609d7856ed2437c8d2f218a4e042de (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.status;

import com.yahoo.vespa.clustercontroller.core.status.statuspage.StatusPageResponse;
import com.yahoo.vespa.clustercontroller.core.status.statuspage.StatusPageServer;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;

/**
 * HTTP request handler for serving a single JAR resource as if it were
 * a regular file hosted on the server. Always serves the content verbatim
 * (i.e. as a byte stream), specifying a Content-Type provided when creating
 * the handler.
 *
 * @author <a href="mailto:vekterli@yahoo-inc.com">Tor Brede Vekterli</a>
 * @since 5.28
 */
public class StaticResourceRequestHandler implements StatusPageServer.RequestHandler {
    private final byte[] resourceData;
    private final String contentType;

    public StaticResourceRequestHandler(String resourcePath,
                                        String contentType)
            throws IOException
    {
        this.resourceData = loadResource(resourcePath);
        this.contentType = contentType;
    }

    private byte[] loadResource(String resourcePath) throws IOException {
        InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(resourcePath);
        if (resourceStream == null) {
            throw new IOException("No resource with path '" + resourcePath + "' could be found");
        }
        return readStreamData(resourceStream);
    }

    @Override
    public StatusPageResponse handle(StatusPageServer.HttpRequest request) {
        final StatusPageResponse response = new StatusPageResponse();
        response.setClientCachingEnabled(true);
        response.setContentType(contentType);
        try {
            response.getOutputStream().write(resourceData);
        } catch (IOException e) {
            response.setResponseCode(StatusPageResponse.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return response;
    }

    private byte[] readStreamData(InputStream resourceStream) throws IOException {
        final byte[] buf = new byte[4096];
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        while (true) {
            int read = resourceStream.read(buf);
            if (read < 0) {
                break;
            }
            outputStream.write(buf, 0, read);
        }
        outputStream.close();
        return outputStream.toByteArray();
    }
}