Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d17b7c8

Browse files
committed
Polish
1 parent 466ed46 commit d17b7c8

File tree

19 files changed

+131
-116
lines changed

19 files changed

+131
-116
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/HealthIndicatorAutoConfigurationProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
/**
2424
* Configuration properties for some health properties
25+
*
2526
* @author Christian Dupuis
27+
* @since 1.2.0
2628
*/
2729
@ConfigurationProperties("health.status")
2830
public class HealthIndicatorAutoConfigurationProperties {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementSecurityAutoConfiguration.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ public void init(WebSecurity builder) throws Exception {
142142
List<String> ignored = SpringBootWebSecurityConfiguration
143143
.getIgnored(this.security);
144144
if (!this.management.getSecurity().isEnabled()) {
145-
ignored.addAll(Arrays.asList(getEndpointPaths(
146-
this.endpointHandlerMapping)));
145+
ignored.addAll(Arrays
146+
.asList(getEndpointPaths(this.endpointHandlerMapping)));
147147
}
148148
if (ignored.contains("none")) {
149149
ignored.remove("none");
@@ -227,11 +227,10 @@ protected void configure(HttpSecurity http) throws Exception {
227227
http.exceptionHandling().authenticationEntryPoint(entryPoint());
228228
paths = this.server.getPathsArray(paths);
229229
http.requestMatchers().antMatchers(paths);
230-
// @formatter:off
231-
http.authorizeRequests()
232-
.antMatchers(this.server.getPathsArray(getEndpointPaths(this.endpointHandlerMapping, false))).access("permitAll()")
230+
String[] endpointPaths = this.server.getPathsArray(getEndpointPaths(
231+
this.endpointHandlerMapping, false));
232+
http.authorizeRequests().antMatchers(endpointPaths).access("permitAll()")
233233
.anyRequest().hasRole(this.management.getSecurity().getRole());
234-
// @formatter:on
235234
http.httpBasic();
236235

237236
// No cookies for management endpoints by default

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/HealthEndpoint.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,19 @@ public class HealthEndpoint extends AbstractEndpoint<Health> {
3636

3737
private final HealthIndicator healthIndicator;
3838

39-
private long ttl = 1000;
39+
private long timeToLive = 1000;
4040

4141
/**
4242
* Time to live for cached result. If accessed anonymously, we might need to cache the
4343
* result of this endpoint to prevent a DOS attack.
44-
*
4544
* @return time to live in milliseconds (default 1000)
4645
*/
47-
public long getTtl() {
48-
return ttl;
46+
public long getTimeToLive() {
47+
return this.timeToLive;
4948
}
5049

51-
public void setTtl(long ttl) {
52-
this.ttl = ttl;
50+
public void setTimeToLive(long ttl) {
51+
this.timeToLive = ttl;
5352
}
5453

5554
/**

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointHandlerMapping.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.util.Collection;
21-
import java.util.HashMap;
21+
import java.util.Collections;
2222
import java.util.HashSet;
23+
import java.util.LinkedHashMap;
2324
import java.util.Map;
2425
import java.util.Set;
2526

@@ -52,7 +53,7 @@
5253
public class EndpointHandlerMapping extends RequestMappingHandlerMapping implements
5354
ApplicationContextAware {
5455

55-
private final Map<String, MvcEndpoint> endpoints = new HashMap<String, MvcEndpoint>();
56+
private final Map<String, MvcEndpoint> endpoints;
5657

5758
private String prefix = "";
5859

@@ -64,15 +65,21 @@ public class EndpointHandlerMapping extends RequestMappingHandlerMapping impleme
6465
* @param endpoints
6566
*/
6667
public EndpointHandlerMapping(Collection<? extends MvcEndpoint> endpoints) {
67-
HashMap<String, MvcEndpoint> map = (HashMap<String, MvcEndpoint>) this.endpoints;
68-
for (MvcEndpoint endpoint : endpoints) {
69-
map.put(endpoint.getPath(), endpoint);
70-
}
68+
this.endpoints = buildEndpointsMap(endpoints);
7169
// By default the static resource handler mapping is LOWEST_PRECEDENCE - 1
7270
// and the RequestMappingHandlerMapping is 0 (we ideally want to be before both)
7371
setOrder(-100);
7472
}
7573

74+
private Map<String, MvcEndpoint> buildEndpointsMap(
75+
Collection<? extends MvcEndpoint> endpoints) {
76+
Map<String, MvcEndpoint> map = new LinkedHashMap<String, MvcEndpoint>();
77+
for (MvcEndpoint endpoint : endpoints) {
78+
map.put(endpoint.getPath(), endpoint);
79+
}
80+
return Collections.unmodifiableMap(map);
81+
}
82+
7683
@Override
7784
public void afterPropertiesSet() {
7885
super.afterPropertiesSet();

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,26 @@ public void addStatusMapping(String statusCode, HttpStatus httpStatus) {
101101
@RequestMapping
102102
@ResponseBody
103103
public Object invoke(Principal principal) {
104-
105-
if (!delegate.isEnabled()) {
104+
if (!this.delegate.isEnabled()) {
106105
// Shouldn't happen because the request mapping should not be registered
107106
return new ResponseEntity<Map<String, String>>(Collections.singletonMap(
108107
"message", "This endpoint is disabled"), HttpStatus.NOT_FOUND);
109108
}
110-
111109
Health health = getHealth(principal);
112110
Status status = health.getStatus();
113111
if (this.statusMapping.containsKey(status.getCode())) {
114112
return new ResponseEntity<Health>(health, this.statusMapping.get(status
115113
.getCode()));
116114
}
117-
118115
return health;
119-
120116
}
121117

122118
private Health getHealth(Principal principal) {
123-
Health health = useCachedValue(principal) ? cached : (Health) delegate.invoke();
119+
Health health = (useCachedValue(principal) ? this.cached : (Health) this.delegate
120+
.invoke());
124121
// Not too worried about concurrent access here, the worst that can happen is the
125122
// odd extra call to delegate.invoke()
126-
cached = health;
123+
this.cached = health;
127124
if (!secure(principal)) {
128125
// If not secure we only expose the status
129126
health = Health.status(health.getStatus()).build();
@@ -137,12 +134,12 @@ private boolean secure(Principal principal) {
137134

138135
private boolean useCachedValue(Principal principal) {
139136
long currentAccess = System.currentTimeMillis();
140-
if (cached == null || secure(principal)
141-
|| currentAccess - lastAccess > delegate.getTtl()) {
142-
lastAccess = currentAccess;
137+
if (this.cached == null || secure(principal)
138+
|| (currentAccess - this.lastAccess) > this.delegate.getTimeToLive()) {
139+
this.lastAccess = currentAccess;
143140
return false;
144141
}
145-
return cached != null;
142+
return this.cached != null;
146143
}
147144

148145
@Override

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void testDefaultKeySanitization() throws Exception {
7272
@SuppressWarnings("unchecked")
7373
public void testKeySanitization() throws Exception {
7474
ConfigurationPropertiesReportEndpoint report = getEndpointBean();
75-
report.setKeysToSanitize(new String[] { "property" });
75+
report.setKeysToSanitize("property");
7676
Map<String, Object> properties = report.invoke();
7777
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
7878
.get("testProperties")).get("properties");

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package org.springframework.boot.actuate.endpoint.mvc;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertTrue;
21-
import static org.mockito.BDDMockito.given;
22-
import static org.mockito.Mockito.mock;
23-
2419
import java.util.Collections;
2520

2621
import org.junit.Before;
@@ -33,10 +28,16 @@
3328
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
3429
import org.springframework.security.core.authority.AuthorityUtils;
3530

31+
import static org.junit.Assert.assertEquals;
32+
import static org.junit.Assert.assertTrue;
33+
import static org.mockito.BDDMockito.given;
34+
import static org.mockito.Mockito.mock;
35+
3636
/**
3737
* Tests for {@link HealthMvcEndpoint}.
3838
*
3939
* @author Christian Dupuis
40+
* @author Dave Syer
4041
*/
4142
public class HealthMvcEndpointTests {
4243

@@ -92,33 +93,33 @@ public void customMapping() {
9293
public void secure() {
9394
given(this.endpoint.invoke()).willReturn(
9495
new Health.Builder().up().withDetail("foo", "bar").build());
95-
Object result = this.mvc.invoke(user);
96+
Object result = this.mvc.invoke(this.user);
9697
assertTrue(result instanceof Health);
9798
assertTrue(((Health) result).getStatus() == Status.UP);
9899
assertEquals("bar", ((Health) result).getDetails().get("foo"));
99100
}
100101

101102
@Test
102103
public void secureNotCached() {
103-
given(this.endpoint.getTtl()).willReturn(10000L);
104+
given(this.endpoint.getTimeToLive()).willReturn(10000L);
104105
given(this.endpoint.invoke()).willReturn(
105106
new Health.Builder().up().withDetail("foo", "bar").build());
106-
Object result = this.mvc.invoke(user);
107+
Object result = this.mvc.invoke(this.user);
107108
assertTrue(result instanceof Health);
108109
assertTrue(((Health) result).getStatus() == Status.UP);
109110
given(this.endpoint.invoke()).willReturn(new Health.Builder().down().build());
110-
result = this.mvc.invoke(user);
111+
result = this.mvc.invoke(this.user);
111112
@SuppressWarnings("unchecked")
112-
Health health = (Health) ((ResponseEntity<Health>) result).getBody();
113+
Health health = ((ResponseEntity<Health>) result).getBody();
113114
assertTrue(health.getStatus() == Status.DOWN);
114115
}
115116

116117
@Test
117118
public void unsecureCached() {
118-
given(this.endpoint.getTtl()).willReturn(10000L);
119+
given(this.endpoint.getTimeToLive()).willReturn(10000L);
119120
given(this.endpoint.invoke()).willReturn(
120121
new Health.Builder().up().withDetail("foo", "bar").build());
121-
Object result = this.mvc.invoke(user);
122+
Object result = this.mvc.invoke(this.user);
122123
assertTrue(result instanceof Health);
123124
assertTrue(((Health) result).getStatus() == Status.UP);
124125
given(this.endpoint.invoke()).willReturn(new Health.Builder().down().build());

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mail/MailProperties.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
* @author Oliver Gierke
2828
* @author Stephane Nicoll
29+
* @since 1.2.0
2930
*/
3031
@ConfigurationProperties(prefix = "spring.mail")
3132
public class MailProperties {
@@ -43,47 +44,47 @@ public class MailProperties {
4344
private Map<String, String> properties = new HashMap<String, String>();
4445

4546
public String getHost() {
46-
return host;
47+
return this.host;
4748
}
4849

4950
public void setHost(String host) {
5051
this.host = host;
5152
}
5253

5354
public Integer getPort() {
54-
return port;
55+
return this.port;
5556
}
5657

5758
public void setPort(Integer port) {
5859
this.port = port;
5960
}
6061

6162
public String getUsername() {
62-
return username;
63+
return this.username;
6364
}
6465

6566
public void setUsername(String username) {
6667
this.username = username;
6768
}
6869

6970
public String getPassword() {
70-
return password;
71+
return this.password;
7172
}
7273

7374
public void setPassword(String password) {
7475
this.password = password;
7576
}
7677

7778
public String getDefaultEncoding() {
78-
return defaultEncoding;
79+
return this.defaultEncoding;
7980
}
8081

8182
public void setDefaultEncoding(String defaultEncoding) {
8283
this.defaultEncoding = defaultEncoding;
8384
}
8485

8586
public Map<String, String> getProperties() {
86-
return properties;
87+
return this.properties;
8788
}
8889

8990
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mail/MailSenderAutoConfiguration.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.mail;
1818

19-
import java.util.Map;
2019
import java.util.Properties;
20+
2121
import javax.activation.MimeType;
2222
import javax.mail.internet.MimeMessage;
2323

@@ -38,15 +38,17 @@
3838
*
3939
* @author Oliver Gierke
4040
* @author Stephane Nicoll
41+
* @sicne 1.2.0
4142
*/
4243
@Configuration
43-
@ConditionalOnClass({MimeMessage.class, MimeType.class})
44+
@ConditionalOnClass({ MimeMessage.class, MimeType.class })
4445
@ConditionalOnProperty(prefix = "spring.mail", value = "host")
4546
@ConditionalOnMissingBean(MailSender.class)
4647
@EnableConfigurationProperties(MailProperties.class)
4748
public class MailSenderAutoConfiguration {
4849

49-
@Autowired MailProperties properties;
50+
@Autowired
51+
MailProperties properties;
5052

5153
@Bean
5254
public JavaMailSender mailSender() {
@@ -58,15 +60,12 @@ public JavaMailSender mailSender() {
5860
sender.setUsername(this.properties.getUsername());
5961
sender.setPassword(this.properties.getPassword());
6062
sender.setDefaultEncoding(this.properties.getDefaultEncoding());
61-
Map<String,String> properties = this.properties.getProperties();
62-
if (!properties.isEmpty()) {
63-
Properties javaMailProperties= new Properties();
64-
for (Map.Entry<String, String> entry : properties.entrySet()) {
65-
javaMailProperties.setProperty(entry.getKey(), entry.getValue());
66-
}
67-
sender.setJavaMailProperties(javaMailProperties);
63+
if (!this.properties.getProperties().isEmpty()) {
64+
Properties properties = new Properties();
65+
properties.putAll(this.properties.getProperties());
66+
sender.setJavaMailProperties(properties);
6867
}
6968
return sender;
7069
}
7170

72-
}
71+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public ModelAndView resolveException(HttpServletRequest request,
7575

7676
private void storeErrorAttributes(HttpServletRequest request, Exception ex) {
7777
request.setAttribute(ERROR_ATTRIBUTE, ex);
78-
7978
}
8079

8180
@Override
@@ -122,7 +121,8 @@ private void addErrorDetails(Map<String, Object> errorAttributes,
122121
}
123122
}
124123
Object message = getAttribute(requestAttributes, "javax.servlet.error.message");
125-
if ((message != null || errorAttributes.get("message") == null) && !(error instanceof BindingResult)) {
124+
if ((message != null || errorAttributes.get("message") == null)
125+
&& !(error instanceof BindingResult)) {
126126
errorAttributes.put("message", message == null ? "No message available"
127127
: message);
128128
}

0 commit comments

Comments
 (0)