net.bull.javamelody.MonitoringEndpoint Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javamelody-spring-boot-starter Show documentation
Show all versions of javamelody-spring-boot-starter Show documentation
Spring Boot Starter for JavaMelody
The newest version!
package net.bull.javamelody;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.http.HttpEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* When enabled, management endpoint for /monitoring reports on the management http port instead of the application http port.
* @author Anders Båtstrand, breneb, Emeric Vernat
*/
@Endpoint(id = "monitoring")
// http path is /actuator/{id}
public class MonitoringEndpoint {
private final ReportServlet reportServlet;
/**
* Constructor.
* @param servletContext ServletContext
*/
public MonitoringEndpoint(ServletContext servletContext) {
reportServlet = new ReportServlet();
final ServletConfig servletConfig = new ServletConfig() {
// only getServletContext() will be used by ReportServlet
@Override
public ServletContext getServletContext() {
return servletContext;
}
@Override
public String getServletName() {
return MonitoringEndpoint.class.getName();
}
@Override
public String getInitParameter(String name) {
return null;
}
@Override
public Enumeration getInitParameterNames() {
return Collections.emptyEnumeration();
}
};
reportServlet.init(servletConfig);
}
/**
* Display a report page.
* @return HttpEntity.EMPTY
* @throws ServletException e
* @throws IOException e
*/
@ReadOperation
public Object report() throws ServletException, IOException {
final ServletRequestAttributes currentRequestAttributes = (ServletRequestAttributes) RequestContextHolder
.currentRequestAttributes();
final HttpServletRequest httpServletRequest = currentRequestAttributes.getRequest();
final HttpServletResponse httpResponse = currentRequestAttributes.getResponse();
reportServlet.service(httpServletRequest, httpResponse);
// status, headers and body are managed by the servlet, so return HttpEntity.EMPTY
return HttpEntity.EMPTY;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy