Skip to content

Commit d5fede6

Browse files
authored
Merge pull request #1885 from steve-community/1884-migrate-to-spring-boot-4
Preparation for "Migrate to Spring Boot 4"
2 parents b105b8b + 02dfa22 commit d5fede6

31 files changed

+230
-245
lines changed

src/main/java/de/rwth/idsg/steve/SteveException.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
*/
1919
package de.rwth.idsg.steve;
2020

21-
import static java.lang.String.format;
21+
import java.io.Serial;
2222

2323
/**
2424
* @author Sevket Goekay <sevketgokay@gmail.com>
2525
* @since 28.08.2014
2626
*/
2727
public class SteveException extends RuntimeException {
2828

29+
@Serial
2930
private static final long serialVersionUID = 3081743035434873349L;
3031

3132
public SteveException(String message) {
@@ -41,19 +42,19 @@ public SteveException(String message, Throwable cause) {
4142
// -------------------------------------------------------------------------
4243

4344
public SteveException(String template, Object arg1) {
44-
this(format(template, arg1));
45+
this(template.formatted(arg1));
4546
}
4647

4748
public SteveException(String template, Object arg1, Throwable cause) {
48-
this(format(template, arg1), cause);
49+
this(template.formatted(arg1), cause);
4950
}
5051

5152
public SteveException(String template, Object arg1, Object arg2) {
52-
this(format(template, arg1, arg2));
53+
this(template.formatted(arg1, arg2));
5354
}
5455

5556
public SteveException(String template, Object arg1, Object arg2, Throwable cause) {
56-
this(format(template, arg1, arg2), cause);
57+
this(template.formatted(arg1, arg2), cause);
5758
}
5859

5960
// -------------------------------------------------------------------------
@@ -63,7 +64,7 @@ public SteveException(String template, Object arg1, Object arg2, Throwable cause
6364
public static class AlreadyExists extends SteveException {
6465

6566
public AlreadyExists(String template, Object arg1) {
66-
super(format(template, arg1));
67+
super(template.formatted(arg1));
6768
}
6869
}
6970

src/main/java/de/rwth/idsg/steve/config/BeanConfiguration.java

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
*/
1919
package de.rwth.idsg.steve.config;
2020

21-
import com.fasterxml.jackson.databind.DeserializationFeature;
22-
import com.fasterxml.jackson.databind.ObjectMapper;
23-
import com.fasterxml.jackson.databind.SerializationFeature;
21+
import com.fasterxml.jackson.datatype.joda.JodaModule;
2422
import com.mysql.cj.conf.PropertyKey;
2523
import com.zaxxer.hikari.HikariConfig;
2624
import com.zaxxer.hikari.HikariDataSource;
@@ -35,28 +33,24 @@
3533
import org.jooq.impl.DSL;
3634
import org.jooq.impl.DataSourceConnectionProvider;
3735
import org.jooq.impl.DefaultConfiguration;
36+
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
3837
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
3938
import org.springframework.context.annotation.Bean;
4039
import org.springframework.context.annotation.ComponentScan;
4140
import org.springframework.context.annotation.Configuration;
4241
import org.springframework.context.annotation.Primary;
43-
import org.springframework.format.support.FormattingConversionService;
44-
import org.springframework.http.converter.HttpMessageConverter;
45-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
4642
import org.springframework.scheduling.annotation.EnableAsync;
4743
import org.springframework.scheduling.annotation.EnableScheduling;
4844
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
4945
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
50-
import org.springframework.web.accept.ContentNegotiationManager;
51-
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
5246
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
53-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
5447
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
55-
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
5648
import org.springframework.web.servlet.view.InternalResourceViewResolver;
5749

5850
import javax.sql.DataSource;
59-
import java.util.List;
51+
52+
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
53+
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
6054

6155
/**
6256
* Configuration and beans of Spring Framework.
@@ -66,7 +60,6 @@
6660
*/
6761
@Slf4j
6862
@Configuration
69-
@EnableWebMvc
7063
@EnableScheduling
7164
@EnableAsync
7265
@ComponentScan("de.rwth.idsg.steve")
@@ -200,35 +193,15 @@ public void addResourceHandlers(final ResourceHandlerRegistry registry) {
200193
// API config
201194
// -------------------------------------------------------------------------
202195

203-
@Override
204-
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
205-
for (HttpMessageConverter<?> converter : converters) {
206-
if (converter instanceof MappingJackson2HttpMessageConverter) {
207-
MappingJackson2HttpMessageConverter conv = (MappingJackson2HttpMessageConverter) converter;
208-
ObjectMapper objectMapper = conv.getObjectMapper();
209-
objectMapper.findAndRegisterModules();
210-
// if the client sends unknown props, just ignore them instead of failing
211-
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
212-
// default is true
213-
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
214-
break;
215-
}
216-
}
217-
}
218-
219-
/**
220-
* Find the ObjectMapper used in MappingJackson2HttpMessageConverter and initialized by Spring automatically.
221-
* MappingJackson2HttpMessageConverter is not a Bean. It is created in {@link WebMvcConfigurationSupport#addDefaultHttpMessageConverters(List)}.
222-
* Therefore, we have to access it via proxies that reference it. RequestMappingHandlerAdapter is a Bean, created in
223-
* {@link WebMvcConfigurationSupport#requestMappingHandlerAdapter(ContentNegotiationManager, FormattingConversionService, org.springframework.validation.Validator)}.
224-
*/
225196
@Bean
226-
@Primary
227-
public ObjectMapper jacksonObjectMapper(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
228-
return requestMappingHandlerAdapter.getMessageConverters().stream()
229-
.filter(converter -> converter instanceof MappingJackson2HttpMessageConverter)
230-
.findAny()
231-
.map(conv -> ((MappingJackson2HttpMessageConverter) conv).getObjectMapper())
232-
.orElseThrow(() -> new RuntimeException("There is no MappingJackson2HttpMessageConverter in Spring context"));
197+
public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
198+
return builder -> {
199+
// default is true
200+
builder.featuresToDisable(WRITE_DATES_AS_TIMESTAMPS);
201+
// if the client sends unknown props, just ignore them instead of failing
202+
builder.featuresToDisable(FAIL_ON_UNKNOWN_PROPERTIES);
203+
// we still use joda DateTime...
204+
builder.modulesToInstall(new JodaModule());
205+
};
233206
}
234207
}

src/main/java/de/rwth/idsg/steve/ocpp/ws/AbstractWebSocketEndpoint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public List<String> getSubProtocols() {
9898

9999
@Override
100100
public void onMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
101-
if (message instanceof TextMessage) {
102-
handleTextMessage(session, (TextMessage) message);
101+
if (message instanceof TextMessage textMessage) {
102+
handleTextMessage(session, textMessage);
103103

104104
} else if (message instanceof PongMessage) {
105105
handlePongMessage(session);

src/main/java/de/rwth/idsg/steve/ocpp/ws/custom/CustomStringModule.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.owasp.encoder.Encode;
3535

3636
import java.io.IOException;
37+
import java.io.Serial;
3738
import java.lang.reflect.Type;
3839

3940
/**
@@ -55,6 +56,7 @@ public CustomStringModule() {
5556
*/
5657
private static class CustomStringSerializer extends StdScalarSerializer<Object> {
5758

59+
@Serial
5860
private static final long serialVersionUID = 1L;
5961

6062
public CustomStringSerializer() {
@@ -95,6 +97,7 @@ private static String objectToString(Object value) {
9597

9698
private static class CustomStringDeserializer extends StringDeserializer {
9799

100+
@Serial
98101
private static final long serialVersionUID = 1L;
99102

100103
@Override

src/main/java/de/rwth/idsg/steve/ocpp/ws/pipeline/IncomingPipeline.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ public void accept(CommunicationContext context) {
6262
serializer.accept(context);
6363
sender.accept(context);
6464

65-
} else if (msg instanceof OcppJsonResult) {
65+
} else if (msg instanceof OcppJsonResult result) {
6666
context.getResultHandler()
67-
.accept((OcppJsonResult) msg);
67+
.accept(result);
6868

69-
} else if (msg instanceof OcppJsonError) {
69+
} else if (msg instanceof OcppJsonError error) {
7070
context.getErrorHandler()
71-
.accept((OcppJsonError) msg);
71+
.accept(error);
7272
}
7373
}
7474

src/main/java/de/rwth/idsg/steve/repository/impl/ChargePointRepositoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import lombok.RequiredArgsConstructor;
3737
import lombok.extern.slf4j.Slf4j;
3838
import ocpp.cs._2015._10.RegistrationStatus;
39+
import org.apache.commons.lang3.StringUtils;
3940
import org.joda.time.DateTime;
4041
import org.jooq.Condition;
4142
import org.jooq.DSLContext;
@@ -50,7 +51,6 @@
5051
import org.jooq.impl.DSL;
5152
import org.springframework.stereotype.Repository;
5253
import org.springframework.util.CollectionUtils;
53-
import org.springframework.util.StringUtils;
5454

5555
import java.util.List;
5656
import java.util.Map;

src/main/java/de/rwth/idsg/steve/service/CertificateValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import de.rwth.idsg.steve.repository.dto.ChargePointRegistration;
2323
import de.rwth.idsg.steve.utils.CertificateUtils;
2424
import lombok.extern.slf4j.Slf4j;
25+
import org.apache.commons.lang3.StringUtils;
2526
import org.bouncycastle.asn1.x500.X500Name;
2627
import org.springframework.http.server.ServerHttpRequest;
2728
import org.springframework.http.server.ServletServerHttpRequest;
2829
import org.springframework.security.core.context.SecurityContextHolder;
2930
import org.springframework.stereotype.Component;
30-
import org.springframework.util.StringUtils;
3131

3232
import jakarta.annotation.Nullable;
3333
import java.security.cert.X509Certificate;

src/main/java/de/rwth/idsg/steve/service/ChargePointService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
import lombok.RequiredArgsConstructor;
4343
import lombok.extern.slf4j.Slf4j;
4444
import ocpp.cs._2015._10.RegistrationStatus;
45+
import org.apache.commons.lang3.StringUtils;
4546
import org.joda.time.DateTime;
4647
import org.springframework.security.core.Authentication;
4748
import org.springframework.security.crypto.password.PasswordEncoder;
4849
import org.springframework.stereotype.Service;
4950
import org.springframework.util.CollectionUtils;
50-
import org.springframework.util.StringUtils;
5151

5252
import java.util.ArrayList;
5353
import java.util.Arrays;

src/main/java/de/rwth/idsg/steve/service/NotificationService.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import static de.rwth.idsg.steve.NotificationFeature.OcppStationWebSocketDisconnected;
4242
import static de.rwth.idsg.steve.NotificationFeature.OcppTransactionEnded;
4343
import static de.rwth.idsg.steve.NotificationFeature.OcppTransactionStarted;
44-
import static java.lang.String.format;
4544

4645
/**
4746
* @author Sevket Goekay <sevketgokay@gmail.com>
@@ -60,12 +59,12 @@ public void ocppStationBooted(OccpStationBooted notification) {
6059
return;
6160
}
6261

63-
String subject = format("Received boot notification from '%s'", notification.getChargeBoxId());
62+
String subject = "Received boot notification from '%s'".formatted(notification.getChargeBoxId());
6463
String body;
6564
if (notification.getStatus().isPresent()) {
66-
body = format("Charging station '%s' is in database and has registration status '%s'.", notification.getChargeBoxId(), notification.getStatus().get().value());
65+
body = "Charging station '%s' is in database and has registration status '%s'.".formatted(notification.getChargeBoxId(), notification.getStatus().get().value());
6766
} else {
68-
body = format("Charging station '%s' is NOT in database", notification.getChargeBoxId());
67+
body = "Charging station '%s' is NOT in database".formatted(notification.getChargeBoxId());
6968
}
7069

7170
mailService.sendAsync(subject, addTimestamp(body));
@@ -77,7 +76,7 @@ public void ocppStationWebSocketConnected(OcppStationWebSocketConnected notifica
7776
return;
7877
}
7978

80-
String subject = format("Connected to JSON charging station '%s'", notification.getChargeBoxId());
79+
String subject = "Connected to JSON charging station '%s'".formatted(notification.getChargeBoxId());
8180

8281
mailService.sendAsync(subject, addTimestamp(""));
8382
}
@@ -88,7 +87,7 @@ public void ocppStationWebSocketDisconnected(OcppStationWebSocketDisconnected no
8887
return;
8988
}
9089

91-
String subject = format("Disconnected from JSON charging station '%s'", notification.getChargeBoxId());
90+
String subject = "Disconnected from JSON charging station '%s'".formatted(notification.getChargeBoxId());
9291

9392
mailService.sendAsync(subject, addTimestamp(""));
9493
}
@@ -99,8 +98,8 @@ public void ocppStationStatusFailure(OcppStationStatusFailure notification) {
9998
return;
10099
}
101100

102-
String subject = format("Connector '%s' of charging station '%s' is FAULTED", notification.getConnectorId(), notification.getChargeBoxId());
103-
String body = format("Status Error Code: '%s'", notification.getErrorCode());
101+
String subject = "Connector '%s' of charging station '%s' is FAULTED".formatted(notification.getConnectorId(), notification.getChargeBoxId());
102+
String body = "Status Error Code: '%s'".formatted(notification.getErrorCode());
104103

105104
mailService.sendAsync(subject, addTimestamp(body));
106105
}
@@ -111,7 +110,7 @@ public void ocppTransactionStarted(OcppTransactionStarted notification) {
111110
return;
112111
}
113112

114-
String subject = format("Transaction '%s' has started on charging station '%s' on connector '%s'", notification.getTransactionId(), notification.getParams().getChargeBoxId(), notification.getParams().getConnectorId());
113+
String subject = "Transaction '%s' has started on charging station '%s' on connector '%s'".formatted(notification.getTransactionId(), notification.getParams().getChargeBoxId(), notification.getParams().getConnectorId());
115114

116115
mailService.sendAsync(subject, addTimestamp(createContent(notification.getParams())));
117116
}
@@ -122,7 +121,7 @@ public void ocppTransactionEnded(OcppTransactionEnded notification) {
122121
return;
123122
}
124123

125-
String subject = format("Transaction '%s' has ended on charging station '%s'", notification.getParams().getTransactionId(), notification.getParams().getChargeBoxId());
124+
String subject = "Transaction '%s' has ended on charging station '%s'".formatted(notification.getParams().getTransactionId(), notification.getParams().getChargeBoxId());
126125

127126
mailService.sendAsync(subject, addTimestamp(createContent(notification.getParams())));
128127
}

0 commit comments

Comments
 (0)