diff --git a/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/LimitedEnvironmentEndpoint.java b/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/LimitedEnvironmentEndpoint.java new file mode 100644 index 00000000000..8b6cefde307 --- /dev/null +++ b/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/LimitedEnvironmentEndpoint.java @@ -0,0 +1,99 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.hypermedia; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +/** + * {@link EnvironmentEndpoint} with reduced output to look better in the documentation. + * + * @author Phillip Webb + */ +public class LimitedEnvironmentEndpoint extends EnvironmentEndpoint { + + private static final MultiValueMap INCLUDES; + + static { + Map> includes = new LinkedHashMap>(); + List systemProperties = new ArrayList(); + systemProperties.add("java.runtime.name"); + systemProperties.add("sun.boot.library.path"); + systemProperties.add("java.vendor.url"); + systemProperties.add("path.separator"); + systemProperties.add("sun.java.launcher"); + systemProperties.add("java.runtime.version"); + systemProperties.add("os.arch"); + systemProperties.add("line.separator"); + systemProperties.add("os.name"); + systemProperties.add("user.timezone"); + systemProperties.add("file.encoding"); + systemProperties.add("java.vm.specification.version"); + systemProperties.add("sun.java.command"); + systemProperties.add("sun.arch.data.model"); + systemProperties.add("user.language"); + systemProperties.add("awt.toolkit"); + systemProperties.add("java.awt.headless"); + systemProperties.add("java.vendor"); + systemProperties.add("file.separator"); + includes.put("systemProperties", systemProperties); + List systemEnvironment = new ArrayList(); + systemEnvironment.add("SHELL"); + systemEnvironment.add("TMPDIR"); + systemEnvironment.add("DISPLAY"); + systemEnvironment.add("LOGNAME"); + includes.put("systemEnvironment", systemEnvironment); + INCLUDES = new LinkedMultiValueMap<>(Collections.unmodifiableMap(includes)); + } + + @Override + public Object sanitize(String name, Object object) { + System.out.println(name); + if (name.equals("gopherProxySet")) { + return object; + } + return null; + } + + @Override + protected Map postProcessSourceProperties(String sourceName, + Map properties) { + List sourceIncludes = INCLUDES.get(sourceName); + if (sourceIncludes != null) { + Set> entries = properties.entrySet(); + Iterator> iterator = entries.iterator(); + while (iterator.hasNext()) { + if (!sourceIncludes.contains(iterator.next().getKey())) { + iterator.remove(); + } + } + + } + return properties; + } + +} diff --git a/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/SpringBootHypermediaApplication.java b/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/SpringBootHypermediaApplication.java index 330729d6cbd..e93c56cbba2 100644 --- a/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/SpringBootHypermediaApplication.java +++ b/spring-boot-actuator-docs/src/restdoc/java/org/springframework/boot/actuate/hypermedia/SpringBootHypermediaApplication.java @@ -20,6 +20,7 @@ import groovy.text.GStringTemplateEngine; import groovy.text.TemplateEngine; import org.springframework.boot.SpringApplication; +import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; @@ -36,6 +37,11 @@ public class SpringBootHypermediaApplication { return new GStringTemplateEngine(); } + @Bean + public EnvironmentEndpoint environmentEndpoint() { + return new LimitedEnvironmentEndpoint(); + } + public static void main(String[] args) { SpringApplication.run(SpringBootHypermediaApplication.class, args); } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java index 44ef33b05fe..c21442ba039 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java @@ -61,11 +61,14 @@ public class EnvironmentEndpoint extends AbstractEndpoint> { String sourceName = entry.getKey(); if (source instanceof EnumerablePropertySource) { EnumerablePropertySource enumerable = (EnumerablePropertySource) source; - Map map = new LinkedHashMap(); + Map properties = new LinkedHashMap(); for (String name : enumerable.getPropertyNames()) { - map.put(name, sanitize(name, enumerable.getProperty(name))); + properties.put(name, sanitize(name, enumerable.getProperty(name))); + } + properties = postProcessSourceProperties(sourceName, properties); + if (properties != null) { + result.put(sourceName, properties); } - result.put(sourceName, map); } } return result; @@ -104,4 +107,17 @@ public class EnvironmentEndpoint extends AbstractEndpoint> { return this.sanitizer.sanitize(name, object); } + /** + * Apply any post processing to source data before it is added. + * @param sourceName the source name + * @param properties the properties + * @return the post-processed properties or {@code null} if the source should not be + * added + * @since 1.4.0 + */ + protected Map postProcessSourceProperties(String sourceName, + Map properties) { + return properties; + } + }