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// @formatter:off
040@Rest(
041   path="/debug",
042   title="Debug",
043   description="Debug Utilities.",
044   allowedMethodParams="OPTIONS,POST"
045)
046@HtmlDocConfig(
047   navlinks={
048      "up: request:/..",
049      "jetty-thread-dump: servlet:/jetty/dump?method=POST",
050      "api: servlet:/api",
051      "stats: servlet:/stats"
052   }
053)
054// @formatter:on
055public class DebugResource extends BasicRestServlet {
056   private static final long serialVersionUID = 1L;
057
058   /**
059    * [POST /jetty/dump] - Generates and saves the jetty thread dump file to jetty-thread-dump.log.
060    *
061    * @param req The request.
062    * @param res The response.
063    * @throws Exception Gets converted to 500 response.
064    * @return The thread dump contents.
065    */
066   @RestPost(path = "/jetty/dump", description = "Generates and saves the jetty thread dump file to jetty-thread-dump.log.")
067   public Ok createJettyDump(RestRequest req, RestResponse res) throws Exception {
068      var dump = JettyMicroservice.getInstance().getServer().dump();
069      try (var fw = new FileWriter(req.getConfig().get("Logging/logDir").orElse("") + "/jetty-thread-dump.log")) {
070         fw.write(dump);
071      }
072      return Ok.OK;
073   }
074
075   /**
076    * [GET /] - Shows child utilities.
077    *
078    * @return Child utility links.
079    */
080   @RestGet(path="/", description="Show contents of config file.")
081   public ResourceDescriptions getChildren() {
082      // @formatter:off
083      return ResourceDescriptions
084         .create()
085         .append("jetty/dump", "Jetty thread dump");
086      // @formatter:on
087   }
088
089   /**
090    * [GET /jetty/dump] - Generates and retrieves the jetty thread dump.
091    *
092    * @param req The request.
093    * @param res The response.
094    * @return The thread dump contents.
095    */
096   @RestGet(path = "/jetty/dump", description = "Generates and retrieves the jetty thread dump.")
097   public Reader getJettyDump(RestRequest req, RestResponse res) {
098      res.setContentType("text/plain");
099      return new StringReader(JettyMicroservice.getInstance().getServer().dump());
100   }
101}