aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/webstatusserver/StaticResourceHandler.java
blob: 65d12f67029a640f471a74e54eaedc63621ca61a (plain)
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
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.webstatusserver;

import com.google.common.collect.ImmutableList;
import com.google.common.io.CharStreams;
import com.google.devtools.build.lib.util.ResourceFileLoader;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Handler for static resources (JS, html, css...)
 */
public class StaticResourceHandler implements HttpHandler {
  private String response;
  private List<String> contentType;
  private int httpCode;

  public static StaticResourceHandler createFromAbsolutePath(String path, String contentType) {
    return new StaticResourceHandler(path, contentType, true);
  }

  public static StaticResourceHandler createFromRelativePath(String path, String contentType) {
    return new StaticResourceHandler(path, contentType, false);
  }

  private StaticResourceHandler(String path, String contentType, boolean absolutePath) {
    try {
      if (absolutePath) {
        InputStream resourceStream = loadFromAbsolutePath(WebStatusServerModule.class, path);
        response = CharStreams.toString(new InputStreamReader(resourceStream));

      } else {
        response = ResourceFileLoader.loadResource(WebStatusServerModule.class, path);
      }
      httpCode = 200;
    } catch (IOException e) {
      throw new IllegalArgumentException("resource " + path + " not found");
    }
    this.contentType = ImmutableList.of(contentType);
  }

  @Override
  public void handle(HttpExchange exchange) throws IOException {
    exchange.getResponseHeaders().put("Content-Type", contentType);
    exchange.sendResponseHeaders(httpCode, response.length());
    try (OutputStream os = exchange.getResponseBody()) {
      os.write(response.getBytes(StandardCharsets.UTF_8));
    }
  }

  public static InputStream loadFromAbsolutePath(Class<?> loadingClass, String path)
      throws IOException {
    URL resourceUrl = loadingClass.getClassLoader().getResource(path);
    if (resourceUrl == null) {
      throw new IllegalArgumentException("resource " + path + " not found");
    }
    return resourceUrl.openStream();
  }
}