Home | History | Annotate | Download | only in src
      1 /*
      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 
     17 package com.android.ahat;
     18 
     19 import com.android.ahat.heapdump.AhatInstance;
     20 import com.android.ahat.heapdump.AhatSnapshot;
     21 import com.sun.net.httpserver.HttpExchange;
     22 import com.sun.net.httpserver.HttpHandler;
     23 import java.awt.image.BufferedImage;
     24 import java.io.IOException;
     25 import java.io.OutputStream;
     26 import java.io.PrintStream;
     27 import javax.imageio.ImageIO;
     28 
     29 class BitmapHandler implements HttpHandler {
     30   private AhatSnapshot mSnapshot;
     31 
     32   public BitmapHandler(AhatSnapshot snapshot) {
     33     mSnapshot = snapshot;
     34   }
     35 
     36   @Override
     37   public void handle(HttpExchange exchange) throws IOException {
     38     try {
     39       Query query = new Query(exchange.getRequestURI());
     40       long id = query.getLong("id", 0);
     41       BufferedImage bitmap = null;
     42       AhatInstance inst = mSnapshot.findInstance(id);
     43       if (inst != null) {
     44         bitmap = inst.asBitmap();
     45       }
     46 
     47       if (bitmap != null) {
     48         exchange.getResponseHeaders().add("Content-Type", "image/png");
     49         exchange.sendResponseHeaders(200, 0);
     50         OutputStream os = exchange.getResponseBody();
     51         ImageIO.write(bitmap, "png", os);
     52         os.close();
     53       } else {
     54         exchange.getResponseHeaders().add("Content-Type", "text/html");
     55         exchange.sendResponseHeaders(404, 0);
     56         PrintStream ps = new PrintStream(exchange.getResponseBody());
     57         HtmlDoc doc = new HtmlDoc(ps, DocString.text("ahat"), DocString.uri("style.css"));
     58         doc.big(DocString.text("No bitmap found for the given request."));
     59         doc.close();
     60       }
     61     } catch (RuntimeException e) {
     62       // Print runtime exceptions to standard error for debugging purposes,
     63       // because otherwise they are swallowed and not reported.
     64       System.err.println("Exception when handling " + exchange.getRequestURI() + ": ");
     65       e.printStackTrace();
     66       throw e;
     67     }
     68   }
     69 }
     70