aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/webstatusserver/IndexPageHandler.java
blob: acab0be9279916d41c506b097923306aa2500b4c (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
82
// Copyright 2014 Google Inc. 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.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

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

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * Handlers for displaying the index page of server.
 *
 */
public class IndexPageHandler {
  private List<TestStatusHandler> testHandlers = new ArrayList<>();
  private IndexPageJsonData dataHandler;
  private StaticResourceHandler frontendHandler;

  public IndexPageHandler(HttpServer server, List<TestStatusHandler> testHandlers) {
    this.testHandlers = testHandlers;
    this.dataHandler = new IndexPageJsonData(this);
    this.frontendHandler =
        StaticResourceHandler.createFromRelativePath("static/index.html", "text/html");
    server.createContext("/", frontendHandler);
    server.createContext("/tests/list", dataHandler);
  }

  /**
   * Puts data from the build log into json suitable for frontend.
   * 
   */
  private class IndexPageJsonData implements HttpHandler {
    private IndexPageHandler pageHandler;
    private Gson gson = new Gson();
    public IndexPageJsonData(IndexPageHandler indexPageHandler) {
      this.pageHandler = indexPageHandler;
    }

    @Override
    public void handle(HttpExchange exchange) throws IOException {
      exchange.getResponseHeaders().put("Content-Type", ImmutableList.of("application/json"));
      JsonArray response = new JsonArray();
      for (TestStatusHandler handler : this.pageHandler.testHandlers) {  
        WebStatusBuildLog buildLog = handler.getBuildLog();
        JsonObject test = new JsonObject();
        test.add("targets",  gson.toJsonTree(buildLog.getTargetList()));
        test.addProperty("startTime", buildLog.getStartTime());
        test.addProperty("finished", buildLog.finished());
        test.addProperty("uuid", buildLog.getCommandId().toString());
        response.add(test);
      }
      String serializedResponse = response.toString();
      exchange.sendResponseHeaders(200, serializedResponse.length());
      try (OutputStream os = exchange.getResponseBody()) {
        os.write(serializedResponse.getBytes(StandardCharsets.UTF_8));
      }
    }
  }
}