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.resources;
014
015import static org.apache.juneau.http.HttpMethodName.*;
016
017import java.io.*;
018
019import org.apache.juneau.internal.*;
020import org.apache.juneau.microservice.*;
021import org.apache.juneau.rest.*;
022import org.apache.juneau.rest.annotation.*;
023import org.apache.juneau.rest.helper.*;
024
025/**
026 * Microservice debug utilities.
027 */
028@RestResource(
029   path="/debug",
030   title="Debug",
031   description="Debug Utilities.",
032   htmldoc=@HtmlDoc(
033      navlinks={
034         "up: request:/..",
035         "jetty-thread-dump: servlet:/jetty/dump?method=POST",
036         "options: servlet:/?method=OPTIONS"
037      }
038   ),
039   allowedMethodParams="OPTIONS,POST"
040)
041@SuppressWarnings("javadoc")
042public class DebugResource extends BasicRestServlet {
043   private static final long serialVersionUID = 1L;
044
045   /**
046    * [GET /] - Shows child utilities.
047    *
048    * @return Child utility links.
049    * @throws Exception
050    */
051   @RestMethod(name=GET, path="/", description="Show contents of config file.")
052   public ResourceDescriptions getChildren() throws Exception {
053      return new ResourceDescriptions()
054         .append("jetty/dump", "Jetty thread dump")
055      ;
056   }
057
058   /**
059    * [GET /jetty/dump] - Generates and retrieves the jetty thread dump.
060    */
061   @RestMethod(name=GET, path="/jetty/dump", description="Generates and retrieves the jetty thread dump.")
062   public Reader getJettyDump(RestRequest req, RestResponse res) {
063      res.setContentType("text/plain");
064      return new StringReader(RestMicroservice.getInstance().getServer().dump());
065   }
066
067   /**
068    * [POST /jetty/dump] - Generates and saves the jetty thread dump file to jetty-thread-dump.log.
069    */
070   @RestMethod(name=POST, path="/jetty/dump", description="Generates and saves the jetty thread dump file to jetty-thread-dump.log.")
071   public String createJettyDump(RestRequest req, RestResponse res) throws Exception {
072      String dump = RestMicroservice.getInstance().getServer().dump();
073      try (FileWriter fw = new FileWriter(req.getConfig().getString("Logging/logDir") + "/jetty-thread-dump.log")) {
074         IOUtils.pipe(dump, fw);
075      }
076      return "OK";
077   }
078}