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