uk.co.spudsoft.mgmt.DumpSysPropsRoute Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vertx-management-endpoints Show documentation
Show all versions of vertx-management-endpoints Show documentation
A few Vert.x HTTP Server routes for providing functionality similar to Springs actuators.
/*
* Copyright (C) 2022 jtalbut
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package uk.co.spudsoft.mgmt;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.Json;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import java.util.List;
import java.util.stream.Collectors;
/**
* A Vertx HTTP Server route for allowing users to download a thread dump of the process.
*
* It is strongly recommended that this endpoint be mounted on via a subrouter, the path to which is only accessible from authorised personnel.
* The integration tests demonstrate the use of a suitable subrouter to locate the endpoint at /manage/threaddump.
*
* @author jtalbut
*/
public class DumpSysPropsRoute implements Handler {
/**
* The path at which the standardDeploy method will put the router.
*/
public static final String PATH = "sysprops";
/**
* Constructor.
*/
public DumpSysPropsRoute() {
}
/**
* Deploy the route to the router passed in at the normal endpoint.
*
* The router passed in should be a sub router that is inaccessible to normal users.
*
* @param router The router that this handler will be attached to.
*/
public void standardDeploy(Router router) {
router.route(HttpMethod.GET, "/" + PATH)
.handler(this::handle)
.setName("System Properties")
.produces(ContentTypes.TYPE_JSON)
.produces(ContentTypes.TYPE_HTML)
.produces(ContentTypes.TYPE_PLAIN)
;
}
/**
* Factory method to do standard deployment on newly constructed route.
*
* The router passed in should be a sub router that is inaccessible to normal users.
*
* @param router The router that this handler will be attached to.
*/
public static void createAndDeploy(Router router) {
DumpSysPropsRoute route = new DumpSysPropsRoute();
route.standardDeploy(router);
}
static class Property {
private final String name;
private final String value;
Property(String name, String value) {
this.name = name;
this.value = value;
}
@JsonProperty(value = "name")
String getName() {
return name;
}
@JsonProperty(value = "value")
String getValue() {
return value;
}
}
@Override
public void handle(RoutingContext rc) {
HttpServerRequest request = rc.request();
if (request.method() == HttpMethod.GET) {
List properties = getProperties();
ContentTypes.adjustFromParams(rc);
if (ContentTypes.TYPE_JSON.equals(rc.getAcceptableContentType())) {
HttpServerResponse response = rc.response();
response.setStatusCode(200);
response.putHeader(HttpHeaderNames.CONTENT_TYPE, ContentTypes.TYPE_JSON);
response.end(Json.encode(properties));
} else if (ContentTypes.TYPE_HTML.equals(rc.getAcceptableContentType())) {
HttpServerResponse response = rc.response();
response.setStatusCode(200);
response.putHeader(HttpHeaderNames.CONTENT_TYPE, ContentTypes.TYPE_HTML);
response.setChunked(true);
response.write("");
response.write("");
response.write("");
response.write("");
response.write("");
response.write("");
response.write("");
response.write("Name Value ");
response.write(" ");
response.write("");
response.write("");
for (Property prop : properties) {
response.write("");
response.write(prop.name);
response.write(" ");
response.write(prop.value);
response.write(" ");
response.write(" ");
}
response.write("");
response.write("
");
response.write("");
response.write("");
response.end();
} else {
HttpServerResponse response = rc.response();
response.setStatusCode(200);
response.putHeader(HttpHeaderNames.CONTENT_TYPE, ContentTypes.TYPE_PLAIN);
response.setChunked(true);
for (Property prop : properties) {
response.write(prop.name);
response.write(": ");
response.write(prop.value);
response.write("\n");
}
response.end();
}
}
}
static String toString(Object value) {
if (value == null) {
return "";
} else if (value instanceof String) {
return (String) value;
} else {
return value.toString();
}
}
static List getProperties() {
return System.getProperties()
.entrySet()
.stream()
.map((entry) -> new Property(toString(entry.getKey()), toString(entry.getValue())))
.sorted((a, b) -> a.name.compareTo(b.name))
.collect(Collectors.toList());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy