Skip to content

Commit 222c30c

Browse files
committed
Remove Lombok and update tests to use autowired code
1 parent 8d603e8 commit 222c30c

40 files changed

+842
-549
lines changed

pom.xml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@
128128
<artifactId>postgresql</artifactId>
129129
<scope>runtime</scope>
130130
</dependency>
131-
<dependency>
132-
<groupId>org.projectlombok</groupId>
133-
<artifactId>lombok</artifactId>
134-
<optional>true</optional>
135-
</dependency>
136131
<dependency>
137132
<groupId>org.mapstruct</groupId>
138133
<artifactId>mapstruct</artifactId>
@@ -204,10 +199,6 @@
204199
<groupId>org.springframework.boot</groupId>
205200
<artifactId>spring-boot-configuration-processor</artifactId>
206201
</path>
207-
<path>
208-
<groupId>org.projectlombok</groupId>
209-
<artifactId>lombok</artifactId>
210-
</path>
211202
<path>
212203
<groupId>org.mapstruct</groupId>
213204
<artifactId>mapstruct-processor</artifactId>
@@ -255,14 +246,6 @@
255246
<plugin>
256247
<groupId>org.springframework.boot</groupId>
257248
<artifactId>spring-boot-maven-plugin</artifactId>
258-
<configuration>
259-
<excludes>
260-
<exclude>
261-
<groupId>org.projectlombok</groupId>
262-
<artifactId>lombok</artifactId>
263-
</exclude>
264-
</excludes>
265-
</configuration>
266249
</plugin>
267250
</plugins>
268251
</build>

src/main/java/org/openpodcastapi/opa/OpenPodcastAPI.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.scheduling.annotation.EnableScheduling;
66

7+
/// Main application
78
@SpringBootApplication
89
@EnableScheduling
910
public class OpenPodcastAPI {

src/main/java/org/openpodcastapi/opa/advice/GlobalExceptionHandler.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,69 @@
11
package org.openpodcastapi.opa.advice;
22

33
import jakarta.persistence.EntityNotFoundException;
4-
import lombok.NonNull;
5-
import lombok.RequiredArgsConstructor;
6-
import lombok.extern.log4j.Log4j2;
4+
import org.jspecify.annotations.NonNull;
75
import org.openpodcastapi.opa.exceptions.ValidationErrorResponse;
6+
import org.slf4j.Logger;
87
import org.springframework.dao.DataIntegrityViolationException;
98
import org.springframework.http.HttpStatus;
109
import org.springframework.http.ResponseEntity;
1110
import org.springframework.web.bind.MethodArgumentNotValidException;
1211
import org.springframework.web.bind.annotation.ExceptionHandler;
13-
import org.springframework.web.bind.annotation.ResponseStatus;
1412
import org.springframework.web.bind.annotation.RestControllerAdvice;
1513

1614
import java.time.Instant;
17-
import java.util.List;
15+
16+
import static org.slf4j.LoggerFactory.getLogger;
1817

1918
/// A global handler for common exceptions thrown by the application.
2019
///
2120
/// Where possible, controllers should throw their own exceptions.
2221
/// However, for common exceptions such as invalid parameters and
2322
/// not found entities, a global exception handler can be added.
2423
@RestControllerAdvice
25-
@RequiredArgsConstructor
26-
@Log4j2
2724
public class GlobalExceptionHandler {
25+
26+
private static final Logger log = getLogger(GlobalExceptionHandler.class);
27+
2828
/// Returns a 404 if a database entity is not found
2929
///
30-
/// @param exception the thrown [EntityNotFoundException]
31-
/// @return a [ResponseEntity] containing the error message
30+
/// @param exception the thrown exception
31+
/// @return a response containing the error message
3232
@ExceptionHandler(EntityNotFoundException.class)
33-
@ResponseStatus(HttpStatus.NOT_FOUND)
3433
public ResponseEntity<@NonNull String> handleEntityNotFoundException(EntityNotFoundException exception) {
35-
log.debug("{}", exception.getMessage());
34+
log.info("{}", exception.getMessage());
3635
return ResponseEntity.notFound().build();
3736
}
3837

3938
/// Returns a 400 error when conflicting data is entered
4039
///
41-
/// @param exception the thrown [DataIntegrityViolationException]
42-
/// @return a [ResponseEntity] containing the error message
40+
/// @param exception the thrown exception
41+
/// @return a response containing the error message
4342
@ExceptionHandler(DataIntegrityViolationException.class)
44-
@ResponseStatus(HttpStatus.BAD_REQUEST)
4543
public ResponseEntity<@NonNull String> handleDataIntegrityViolationException(DataIntegrityViolationException exception) {
4644
return ResponseEntity.badRequest().body(exception.getMessage());
4745
}
4846

4947
/// Returns a 400 error when illegal arguments are passed
5048
///
51-
/// @param exception the thrown [IllegalArgumentException]
52-
/// @return a [ResponseEntity] containing the error message
49+
/// @param exception the thrown exception
50+
/// @return a response containing the error message
5351
@ExceptionHandler(IllegalArgumentException.class)
54-
@ResponseStatus(HttpStatus.BAD_REQUEST)
5552
public ResponseEntity<@NonNull String> handleIllegalArgumentException(IllegalArgumentException exception) {
5653
return ResponseEntity.badRequest().body(exception.getMessage());
5754
}
5855

5956
/// Returns a 400 error when invalid arguments are passed to an endpoint
6057
///
61-
/// @param exception the thrown [MethodArgumentNotValidException]
62-
/// @return a [ResponseEntity] containing the error message
58+
/// @param exception the thrown exception
59+
/// @return a response containing the error message
6360
@ExceptionHandler(MethodArgumentNotValidException.class)
6461
public ResponseEntity<@NonNull ValidationErrorResponse> handleValidationException(MethodArgumentNotValidException exception) {
65-
List<ValidationErrorResponse.FieldError> errors = exception.getBindingResult().getFieldErrors().stream()
62+
final var errors = exception.getBindingResult().getFieldErrors().stream()
6663
.map(fe -> new ValidationErrorResponse.FieldError(fe.getField(), fe.getDefaultMessage()))
6764
.toList();
6865

69-
var body = new ValidationErrorResponse(
66+
final var body = new ValidationErrorResponse(
7067
Instant.now(),
7168
HttpStatus.BAD_REQUEST.value(),
7269
errors
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.openpodcastapi.opa.advice;
22

3-
import lombok.extern.log4j.Log4j2;
3+
import org.slf4j.Logger;
44
import org.springframework.security.core.Authentication;
55
import org.springframework.security.core.context.SecurityContextHolder;
66
import org.springframework.ui.Model;
@@ -9,34 +9,40 @@
99

1010
import java.security.Principal;
1111

12+
import static org.slf4j.LoggerFactory.getLogger;
13+
1214
/// A helper class for adding user information to requests.
1315
///
1416
/// This class is used to populate user details in templates
1517
/// and to ensure that a user is authenticated when viewing
1618
/// web pages.
17-
@Log4j2
1819
@ControllerAdvice
1920
public class GlobalModelAttributeAdvice {
2021

22+
private static final Logger log = getLogger(GlobalModelAttributeAdvice.class);
23+
2124
/// Adds a boolean `isAuthenticated` property to the request model based on
2225
/// whether the user is logged-in.
2326
///
24-
/// @param model the [Model] attached to the request
27+
/// @param model the variables attached to the request
2528
@ModelAttribute
2629
public void addAuthenticationFlag(Model model) {
2730
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
28-
var isAuthenticated = authentication != null && authentication.isAuthenticated()
31+
final var isAuthenticated = authentication != null && authentication.isAuthenticated()
2932
&& !"anonymousUser".equals(authentication.getPrincipal());
33+
assert authentication != null;
34+
log.debug("Authentication flag for {} added", authentication.getPrincipal());
3035
model.addAttribute("isAuthenticated", isAuthenticated);
3136
}
3237

3338
/// Adds user details to the request model.
3439
///
35-
/// @param principal the [Principal] representing the user
36-
/// @param model the [Model] attached to the request
40+
/// @param principal the principal representing the user
41+
/// @param model the variables attached to the request
3742
@ModelAttribute
3843
public void addUserDetails(Principal principal, Model model) {
39-
var username = principal != null ? principal.getName() : "Guest";
44+
final var username = principal != null ? principal.getName() : "Guest";
45+
log.debug("User details for {} added to model", username);
4046
model.addAttribute("username", username);
4147
}
4248
}

src/main/java/org/openpodcastapi/opa/auth/ApiBearerTokenAuthenticationConverter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package org.openpodcastapi.opa.auth;
22

33
import jakarta.servlet.http.HttpServletRequest;
4+
import org.slf4j.Logger;
45
import org.springframework.security.core.Authentication;
56
import org.springframework.security.oauth2.server.resource.web.authentication.BearerTokenAuthenticationConverter;
67
import org.springframework.security.web.authentication.AuthenticationConverter;
78
import org.springframework.stereotype.Component;
89

10+
import static org.slf4j.LoggerFactory.getLogger;
11+
912
/// A converter that handles JWT-based auth for API requests.
1013
///
1114
/// This converter targets only the API endpoints at `/api`.
1215
/// Auth for the frontend is handled by Spring's form login.
1316
@Component
1417
public class ApiBearerTokenAuthenticationConverter implements AuthenticationConverter {
1518

19+
private static final Logger log = getLogger(ApiBearerTokenAuthenticationConverter.class);
20+
1621
private final BearerTokenAuthenticationConverter delegate =
1722
new BearerTokenAuthenticationConverter();
1823

@@ -23,15 +28,18 @@ public Authentication convert(HttpServletRequest request) {
2328

2429
// Don't authenticate the auth endpoints
2530
if (path.startsWith("/api/auth/")) {
31+
log.debug("Bypassing token check for auth endpoint");
2632
return null;
2733
}
2834

2935
// If the request has no Bearer token, return null
3036
final var header = request.getHeader("Authorization");
3137
if (header == null || !header.startsWith("Bearer ")) {
38+
log.debug("Request with no auth header sent to {}", request.getRequestURI());
3239
return null;
3340
}
3441

42+
log.debug("Converting request");
3543
// Task Spring Boot with handling the request
3644
return delegate.convert(request);
3745
}

src/main/java/org/openpodcastapi/opa/auth/JwtAuthenticationProvider.java

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

33
import io.jsonwebtoken.Jwts;
44
import io.jsonwebtoken.security.Keys;
5-
import lombok.NonNull;
5+
import org.jspecify.annotations.NonNull;
66
import org.openpodcastapi.opa.service.CustomUserDetails;
77
import org.openpodcastapi.opa.user.UserRepository;
88
import org.springframework.beans.factory.annotation.Value;
@@ -28,7 +28,7 @@ public class JwtAuthenticationProvider implements AuthenticationProvider {
2828
/// Constructor with secret value provided in `.env` file
2929
/// or environment variables.
3030
///
31-
/// @param repository the [UserRepository] interface for user entities
31+
/// @param repository the repository interface for user entities
3232
/// @param secret the secret value used to generate JWT values
3333
public JwtAuthenticationProvider(
3434
UserRepository repository,

src/main/java/org/openpodcastapi/opa/config/SecurityConfig.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.openpodcastapi.opa.config;
22

3-
import lombok.RequiredArgsConstructor;
43
import org.openpodcastapi.opa.auth.ApiBearerTokenAuthenticationConverter;
54
import org.openpodcastapi.opa.auth.JwtAuthenticationProvider;
65
import org.springframework.context.annotation.Bean;
@@ -25,7 +24,6 @@
2524
/// Security configuration for the Spring application
2625
@Configuration
2726
@EnableWebSecurity
28-
@RequiredArgsConstructor
2927
@EnableMethodSecurity
3028
public class SecurityConfig {
3129

@@ -44,12 +42,12 @@ public class SecurityConfig {
4442

4543
/// API-related security configuration
4644
///
47-
/// @param http the [HttpSecurity] object to be configured
48-
/// @param jwtAuthenticationProvider the [JwtAuthenticationProvider] used to handle JWT auth
45+
/// @param http the security object to be configured
46+
/// @param jwtAuthenticationProvider the JWT provider used to handle JWT auth
4947
/// @param entryPoint the entrypoint that commences the JWT auth
50-
/// @param deniedHandler the [AccessDeniedHandler] that handles auth failures
51-
/// @param converter the [ApiBearerTokenAuthenticationConverter] that manages JWT validation
52-
/// @return the configured [HttpSecurity] object
48+
/// @param deniedHandler the handler that handles auth failures
49+
/// @param converter the bearer token converter that manages JWT validation
50+
/// @return the configured security object
5351
@Bean
5452
@Order(1)
5553
public SecurityFilterChain apiSecurity(
@@ -60,9 +58,9 @@ public SecurityFilterChain apiSecurity(
6058
ApiBearerTokenAuthenticationConverter converter
6159
) {
6260

63-
AuthenticationManager jwtManager = new ProviderManager(jwtAuthenticationProvider);
61+
final var jwtManager = new ProviderManager(jwtAuthenticationProvider);
6462

65-
BearerTokenAuthenticationFilter bearerFilter =
63+
final var bearerFilter =
6664
new BearerTokenAuthenticationFilter(jwtManager, converter);
6765

6866
bearerFilter.setAuthenticationFailureHandler(
@@ -90,8 +88,8 @@ public SecurityFilterChain apiSecurity(
9088

9189
/// Web-related security configuration
9290
///
93-
/// @param http the [HttpSecurity] object to be configured
94-
/// @return the configured [HttpSecurity] object
91+
/// @param http the security object to be configured
92+
/// @return the configured security object
9593
@Bean
9694
@Order(2)
9795
public SecurityFilterChain webSecurity(HttpSecurity http) {
@@ -119,29 +117,29 @@ public SecurityFilterChain webSecurity(HttpSecurity http) {
119117

120118
/// The default password encoder used for hashing and encoding user passwords and JWTs
121119
///
122-
/// @return a configured [BCryptPasswordEncoder]
120+
/// @return a configured password encoder
123121
@Bean
124122
public BCryptPasswordEncoder passwordEncoder() {
125123
return new BCryptPasswordEncoder();
126124
}
127125

128126
/// An authentication provider for password-based authentication
129127
///
130-
/// @param userDetailsService the [UserDetailsService] for loading user data
128+
/// @param userDetailsService the service for loading user data
131129
/// @param passwordEncoder the default password encoder
132-
/// @return the configured [DaoAuthenticationProvider]
130+
/// @return the configured authentication provider
133131
@Bean
134132
public DaoAuthenticationProvider daoAuthenticationProvider(UserDetailsService userDetailsService,
135133
BCryptPasswordEncoder passwordEncoder) {
136-
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(userDetailsService);
134+
final var provider = new DaoAuthenticationProvider(userDetailsService);
137135
provider.setPasswordEncoder(passwordEncoder);
138136
return provider;
139137
}
140138

141139
/// An authentication provider for JWT-based authentication
142140
///
143-
/// @param provider a configured [JwtAuthenticationProvider]
144-
/// @return a configured [ProviderManager] that uses the JWT auth provider
141+
/// @param provider a configured provider
142+
/// @return a configured manager that uses the JWT auth provider
145143
/// @see JwtAuthenticationProvider for provider details
146144
@Bean(name = "jwtAuthManager")
147145
public AuthenticationManager jwtAuthenticationManager(JwtAuthenticationProvider provider) {
@@ -150,8 +148,8 @@ public AuthenticationManager jwtAuthenticationManager(JwtAuthenticationProvider
150148

151149
/// An authentication provider for API POST login
152150
///
153-
/// @param daoProvider a configured [DaoAuthenticationProvider]
154-
/// @return a configured [ProviderManager] that uses basic username/password auth
151+
/// @param daoProvider a configured auth provider
152+
/// @return a configured manager that uses basic username/password auth
155153
@Bean(name = "apiLoginManager", defaultCandidate = false)
156154
public AuthenticationManager apiLoginAuthenticationManager(
157155
DaoAuthenticationProvider daoProvider) {

src/main/java/org/openpodcastapi/opa/config/WebConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
3131
///
3232
/// See [Thymeleaf Layout Dialect](https://ultraq.github.io/thymeleaf-layout-dialect/) for more information
3333
///
34-
/// @return the configured [LayoutDialect]
34+
/// @return the configured layout dialect
3535
@Bean
3636
public LayoutDialect layoutDialect() {
3737
return new LayoutDialect();

0 commit comments

Comments
 (0)