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.examples; 018 019import java.io.*; 020 021import org.apache.juneau.http.classic.response.*; 022import org.apache.juneau.marshall.html.*; 023import org.apache.juneau.microservice.*; 024import org.apache.juneau.microservice.jetty.*; 025import org.apache.juneau.rest.server.*; 026import org.apache.juneau.rest.server.beans.*; 027import org.apache.juneau.rest.server.servlet.*; 028 029/** 030 * Microservice debug utilities. 031 * 032 * <p> 033 * This resource has no authentication of its own. Do not mount it where an untrusted caller can reach it without adding your own authentication. 034 * 035 * <p> 036 * The {@code jetty-thread-dump} navlink is a plain {@code GET /jetty/dump}, which shows the dump. Writing the dump to 037 * <l>jetty-thread-dump.log</l> needs a real {@code POST /jetty/dump}; a link can't reach it, because the router refuses 038 * any override of a {@code GET} to {@code POST}. Put the resource behind a CSRF boundary before mounting it anywhere a 039 * browser can reach. 040 * 041 * <h2 class='section'>See Also:</h2><ul> 042 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauMicroserviceJetty">juneau-microservice-jetty</a> 043 044 * </ul> 045 * 046 * @serial exclude 047 */ 048// @formatter:off 049@Rest( 050 path="/debug", 051 title="Debug", 052 description="Debug Utilities.", 053 allowedMethodParams="OPTIONS" 054) 055@HtmlDocConfig( 056 navlinks={ 057 "up: request:/..", 058 "jetty-thread-dump: servlet:/jetty/dump", 059 "api: servlet:/api", 060 "stats: servlet:/stats" 061 } 062) 063// @formatter:on 064public class DebugResource extends BasicRestServlet { 065 private static final long serialVersionUID = 1L; 066 067 /** 068 * [POST /jetty/dump] - Generates and saves the jetty thread dump file to jetty-thread-dump.log. 069 * 070 * @param req The request. 071 * @param res The response. 072 * @throws Exception Gets converted to 500 response. 073 * @return The thread dump contents. 074 */ 075 @RestPost(path = "/jetty/dump", description = "Generates and saves the jetty thread dump file to jetty-thread-dump.log.") 076 @SuppressWarnings({ 077 "java:S112" // throws Exception intentional - callback/lifecycle method 078 }) 079 public Ok createJettyDump(RestRequest req, RestResponse res) throws Exception { 080 var dump = jettyServerComponent().getServer().dump(); 081 try (var fw = new FileWriter(req.getConfig().get("Logging/logDir").orElse("") + "/jetty-thread-dump.log")) { 082 fw.write(dump); 083 } 084 return Ok.OK; 085 } 086 087 /** 088 * [GET /] - Shows child utilities. 089 * 090 * @return Child utility links. 091 */ 092 @RestGet(path="/", description="Show contents of config file.") 093 public ResourceDescriptions getChildren() { 094 // @formatter:off 095 return ResourceDescriptions 096 .create() 097 .append("jetty/dump", "Jetty thread dump"); 098 // @formatter:on 099 } 100 101 /** 102 * [GET /jetty/dump] - Generates and retrieves the jetty thread dump. 103 * 104 * @param req The request. 105 * @param res The response. 106 * @return The thread dump contents. 107 */ 108 @RestGet(path = "/jetty/dump", description = "Generates and retrieves the jetty thread dump.") 109 public Reader getJettyDump(RestRequest req, RestResponse res) { 110 res.setContentType("text/plain"); 111 return new StringReader(jettyServerComponent().getServer().dump()); 112 } 113 114 @SuppressWarnings({ 115 "resource" // BeanStore returned by Microservice.getBeanStore() is owned by the microservice lifecycle; do not close here. 116 }) 117 private static JettyServerComponent jettyServerComponent() { 118 return Microservice.getInstance().getBeanStore().getBean(JettyServerComponent.class) 119 .orElseThrow(() -> new IllegalStateException("JettyServerComponent bean not found. Did you forget to add JettyConfiguration.class to Microservice.Builder.configurations(...)?")); 120 } 121}