1a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler/*
2a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * Copyright (C) 2015 The Android Open Source Project
3a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler *
4a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * Licensed under the Apache License, Version 2.0 (the "License");
5a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * you may not use this file except in compliance with the License.
6a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * You may obtain a copy of the License at
7a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler *
8a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler *      http://www.apache.org/licenses/LICENSE-2.0
9a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler *
10a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * Unless required by applicable law or agreed to in writing, software
11a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * distributed under the License is distributed on an "AS IS" BASIS,
12a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * See the License for the specific language governing permissions and
14a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * limitations under the License.
15a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler */
16a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler
17a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhlerpackage com.android.ahat;
18a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler
19a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhlerimport com.google.common.io.ByteStreams;
20a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhlerimport com.sun.net.httpserver.HttpExchange;
21a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhlerimport com.sun.net.httpserver.HttpHandler;
22a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhlerimport java.io.IOException;
23a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhlerimport java.io.InputStream;
24a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhlerimport java.io.PrintStream;
25a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler
26a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler/**
27a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * HelpHandler.
28a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler *
29a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler * HttpHandler to show the help page.
30a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler */
31a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhlerclass HelpHandler implements HttpHandler {
32a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler
33a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler  @Override
34a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler  public void handle(HttpExchange exchange) throws IOException {
35a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    ClassLoader loader = HelpHandler.class.getClassLoader();
36a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    exchange.getResponseHeaders().add("Content-Type", "text/html;charset=utf-8");
37a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    exchange.sendResponseHeaders(200, 0);
38a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    PrintStream ps = new PrintStream(exchange.getResponseBody());
39a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    HtmlDoc doc = new HtmlDoc(ps, DocString.text("ahat"), DocString.uri("style.css"));
40a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    doc.menu(Menu.getMenu());
41a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler
42a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    InputStream is = loader.getResourceAsStream("help.html");
43a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    if (is == null) {
44a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler      ps.println("No help available.");
45a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    } else {
46a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler      ByteStreams.copy(is, ps);
47a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    }
48a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler
49a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    doc.close();
50a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler    ps.close();
51a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler  }
52a7f46cb9ccc34c3b9af2e3d2ad90c13f89a94829Richard Uhler}
53