blob: 1d05a666536d574703fb962ed77ae687fea3f41c [file] [log] [blame]
Richard Uhler1af86f12015-10-29 14:55:00 -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 * AhatHttpHandler.
26 *
27 * HttpHandler for AhatHandlers.
28 */
29class AhatHttpHandler implements HttpHandler {
30
31 private AhatHandler mAhatHandler;
32
33 public AhatHttpHandler(AhatHandler handler) {
34 mAhatHandler = handler;
35 }
36
37 @Override
38 public void handle(HttpExchange exchange) throws IOException {
39 exchange.getResponseHeaders().add("Content-Type", "text/html;charset=utf-8");
40 exchange.sendResponseHeaders(200, 0);
41 PrintStream ps = new PrintStream(exchange.getResponseBody());
42 try {
43 HtmlDoc doc = new HtmlDoc(ps, DocString.text("ahat"), DocString.uri("style.css"));
Richard Uhlera7f46cb2015-12-21 14:34:59 -080044 doc.menu(Menu.getMenu());
Richard Uhler1af86f12015-10-29 14:55:00 -070045 mAhatHandler.handle(doc, new Query(exchange.getRequestURI()));
46 doc.close();
47 } catch (RuntimeException e) {
48 // Print runtime exceptions to standard error for debugging purposes,
49 // because otherwise they are swallowed and not reported.
50 System.err.println("Exception when handling " + exchange.getRequestURI() + ": ");
51 e.printStackTrace();
52 throw e;
53 }
54 ps.close();
55 }
56}