blob: 2da02f86717a54a33f28ccb0dc4d40719e5d176d [file] [log] [blame]
Richard Uhlerb730b782015-07-15 16:01:58 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.ahat;
18
19import com.sun.net.httpserver.HttpExchange;
20import com.sun.net.httpserver.HttpHandler;
21import java.io.IOException;
22import java.io.PrintStream;
23
24/**
25 * AhatHandler.
26 *
27 * Common base class of all the ahat HttpHandlers.
28 */
29abstract class AhatHandler implements HttpHandler {
30
31 protected AhatSnapshot mSnapshot;
32
33 public AhatHandler(AhatSnapshot snapshot) {
34 mSnapshot = snapshot;
35 }
36
37 public abstract void handle(Doc doc, Query query) throws IOException;
38
39 @Override
40 public void handle(HttpExchange exchange) throws IOException {
41 exchange.getResponseHeaders().add("Content-Type", "text/html;charset=utf-8");
42 exchange.sendResponseHeaders(200, 0);
43 PrintStream ps = new PrintStream(exchange.getResponseBody());
44 try {
45 HtmlDoc doc = new HtmlDoc(ps, DocString.text("ahat"), DocString.uri("style.css"));
46 DocString menu = new DocString();
47 menu.appendLink(DocString.uri("/"), DocString.text("overview"));
48 menu.append(" - ");
49 menu.appendLink(DocString.uri("roots"), DocString.text("roots"));
50 menu.append(" - ");
51 menu.appendLink(DocString.uri("sites"), DocString.text("allocations"));
52 menu.append(" - ");
53 menu.appendLink(DocString.uri("help"), DocString.text("help"));
54 doc.menu(menu);
55 handle(doc, new Query(exchange.getRequestURI()));
56 doc.close();
57 } catch (RuntimeException e) {
58 // Print runtime exceptions to standard error for debugging purposes,
59 // because otherwise they are swallowed and not reported.
60 System.err.println("Exception when handling " + exchange.getRequestURI() + ": ");
61 e.printStackTrace();
62 throw e;
63 }
64 ps.close();
65 }
66}