001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.microservice.jetty.resources;
014
015import static org.apache.juneau.http.HttpMethod.*;
016
017import java.io.*;
018
019import org.apache.juneau.html.annotation.HtmlDocConfig;
020import org.apache.juneau.internal.*;
021import org.apache.juneau.microservice.jetty.*;
022import org.apache.juneau.rest.*;
023import org.apache.juneau.rest.annotation.*;
024import org.apache.juneau.rest.helper.*;
025
026/**
027 * Microservice debug utilities.
028 */
029@Rest(
030   path="/debug",
031   title="Debug",
032   description="Debug Utilities.",
033   allowedMethodParams="OPTIONS,POST"
034)
035@HtmlDocConfig(
036   navlinks={
037      "up: request:/..",
038      "jetty-thread-dump: servlet:/jetty/dump?method=POST",
039      "options: servlet:/?method=OPTIONS",
040      "stats: servlet:/stats"
041   }
042)
043@SuppressWarnings("javadoc")
044public class DebugResource extends BasicRestServlet {
045   private static final long serialVersionUID = 1L;
046
047   /**
048    * [GET /] - Shows child utilities.
049    *
050    * @return Child utility links.
051    * @throws Exception
052    */
053   @RestMethod(name=GET, path="/", description="Show contents of config file.")
054   public ResourceDescriptions getChildren() throws Exception {
055      return new ResourceDescriptions()
056         .append("jetty/dump", "Jetty thread dump")
057      ;
058   }
059
060   /**
061    * [GET /jetty/dump] - Generates and retrieves the jetty thread dump.
062    */
063   @RestMethod(name=GET, path="/jetty/dump", description="Generates and retrieves the jetty thread dump.")
064   public Reader getJettyDump(RestRequest req, RestResponse res) {
065      res.setContentType("text/plain");
066      return new StringReader(JettyMicroservice.getInstance().getServer().dump());
067   }
068
069   /**
070    * [POST /jetty/dump] - Generates and saves the jetty thread dump file to jetty-thread-dump.log.
071    */
072   @RestMethod(name=POST, path="/jetty/dump", description="Generates and saves the jetty thread dump file to jetty-thread-dump.log.")
073   public String createJettyDump(RestRequest req, RestResponse res) throws Exception {
074      String dump = JettyMicroservice.getInstance().getServer().dump();
075      try (FileWriter fw = new FileWriter(req.getConfig().getString("Logging/logDir") + "/jetty-thread-dump.log")) {
076         IOUtils.pipe(dump, fw);
077      }
078      return "OK";
079   }
080}