|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.tez.common.web; |
| 19 | + |
| 20 | +import org.apache.hadoop.yarn.webapp.MimeType; |
| 21 | +import org.eclipse.jetty.servlet.DefaultServlet; |
| 22 | + |
| 23 | +import javax.servlet.ServletException; |
| 24 | +import javax.servlet.http.HttpServletRequest; |
| 25 | +import javax.servlet.http.HttpServletResponse; |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.PrintWriter; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.nio.file.Paths; |
| 31 | + |
| 32 | +/** |
| 33 | + * Servlet to serve files generated by {@link ProfileServlet}. |
| 34 | + */ |
| 35 | +public class ProfileOutputServlet extends DefaultServlet { |
| 36 | + public static final String FILE_QUERY_PARAM = "file"; |
| 37 | + |
| 38 | + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 39 | + String queriedFile = request.getParameter(FILE_QUERY_PARAM); |
| 40 | + if (queriedFile == null) { |
| 41 | + writeMessage(response, "Run the profiler to be able to receive its output"); |
| 42 | + return; |
| 43 | + } |
| 44 | + File outputFile = new File(ProfileServlet.OUTPUT_DIR, queriedFile); |
| 45 | + if (!outputFile.exists()) { |
| 46 | + writeMessage(response, "Requested file does not exist: " + queriedFile); |
| 47 | + return; |
| 48 | + } |
| 49 | + if (outputFile.length() < 100) { |
| 50 | + response.setIntHeader("Refresh", 2); |
| 51 | + writeMessage(response, "This page auto-refreshes every 2 seconds until output file is ready..."); |
| 52 | + return; |
| 53 | + } |
| 54 | + response.setContentType(MimeType.HTML); |
| 55 | + response.getOutputStream().write(Files.readAllBytes(Paths.get(outputFile.getPath()))); |
| 56 | + response.getOutputStream().flush(); |
| 57 | + response.getOutputStream().close(); |
| 58 | + } |
| 59 | + |
| 60 | + private void writeMessage(HttpServletResponse response, String message) throws IOException { |
| 61 | + response.setContentType(MimeType.TEXT); |
| 62 | + PrintWriter out = response.getWriter(); |
| 63 | + out.println(message); |
| 64 | + out.close(); |
| 65 | + } |
| 66 | +} |
0 commit comments