Skip to content

Commit

Permalink
feat: upgrade to Spring 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
igorvargaextvi committed Feb 8, 2025
1 parent fda74d1 commit 7e6705e
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 128 deletions.
5 changes: 5 additions & 0 deletions google_checks_light.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<property name="severity" value="warning"/>

<property name="fileExtensions" value="java, properties, xml"/>

<!-- Excludes all 'module-info.java' files -->
<!-- See https://checkstyle.org/config_filefilters.html -->
<module name="BeforeExecutionExclusionFileFilter">
Expand Down Expand Up @@ -47,6 +48,10 @@
-->

<module name="TreeWalker">
<module name="AbbreviationAsWordInName">
<property name="allowedAbbreviationLength" value="4"/>
<property name="ignoreFinal" value="true"/>
</module>
<module name="OuterTypeFilename"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
Expand Down
27 changes: 5 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
<keycloak.version>17.0.0</keycloak.version>
<!-- force at least version 2.16 due to https://logging.apache.org/log4j/2.x/security.html -->
<log4j.version>2.16.0</log4j.version>
<openapi.generator.maven.version>6.2.1</openapi.generator.maven.version>
<openapi.generator.maven.version>7.11.0</openapi.generator.maven.version>
<liquibase-maven-plugin.version>4.19.0</liquibase-maven-plugin.version>
<spring.security.version>6.2.8</spring.security.version>
<liquibase-core.version>4.9.1</liquibase-core.version>
<jakarta.ws.rs-api.version>4.0.0</jakarta.ws.rs-api.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -116,7 +117,7 @@
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>4.0.0</version>
<version>${jakarta.ws.rs-api.version}</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -351,25 +352,6 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>6.0.4</version>
<configuration>
<exportDatatables>true</exportDatatables>
<activeRecipes>
<recipe>org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_3</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-spring</artifactId>
<version>6.0.2</version>
</dependency>
</dependencies>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down Expand Up @@ -468,7 +450,8 @@
<generateApis>true</generateApis>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<apiPackage>${project.groupId}.${project.artifactId}.tenantservice.generated.web
<apiPackage>
${project.groupId}.${project.artifactId}.tenantservice.generated.web
</apiPackage>
<modelPackage>
${project.groupId}.${project.artifactId}.tenantservice.generated.web.model
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.caritas.cob.upload.api.exception.httpresponses;

import de.caritas.cob.uploadservice.api.service.LogService;
import java.io.Serial;

public class BadRequestException extends CustomHttpStatusException {

@Serial private static final long serialVersionUID = -8047408802295905803L;

/**
* BadRequest exception.
*
* @param message an additional message
*/
public BadRequestException(String message) {
super(message, LogService::logWarning);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.caritas.cob.upload.api.exception.httpresponses;

import static java.util.Objects.nonNull;

import java.io.Serial;
import java.util.function.Consumer;

/** Custom HTTP status exception. */
public abstract class CustomHttpStatusException extends RuntimeException {

@Serial private static final long serialVersionUID = -3545035432045919306L;
private final Consumer<Exception> loggingMethod;

CustomHttpStatusException(String message, Consumer<Exception> loggingMethod) {
super(message);
this.loggingMethod = loggingMethod;
}

CustomHttpStatusException(String message, Exception ex, Consumer<Exception> loggingMethod) {
super(message, ex);
this.loggingMethod = loggingMethod;
}

/** Executes the non null logging method. */
public void executeLogging() {
if (nonNull(this.loggingMethod)) {
this.loggingMethod.accept(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.caritas.cob.upload.api.exception.httpresponses;

import de.caritas.cob.uploadservice.api.service.LogService;
import java.io.Serial;
import java.util.function.Consumer;

/** Represents the exception for an internal server error - status code 500. */
public class InternalServerErrorException extends CustomHttpStatusException {

@Serial private static final long serialVersionUID = 6051508644381775936L;

/**
* InternalServerErrorException exception.
*
* @param message the message to be logged
*/
public InternalServerErrorException(String message) {
super(message, LogService::logInternalServerError);
}

/**
* InternalServerErrorException exception.
*
* @param loggingMethod the method to be used for logging
* @param message the message to be logged
*/
public InternalServerErrorException(Consumer<Exception> loggingMethod, String message) {
super(message, loggingMethod);
}

/**
* InternalServerError exception.
*
* @param message the exception message
* @param ex the exception
*/
public InternalServerErrorException(String message, Exception ex) {
super(message, ex, LogService::logInternalServerError);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.caritas.cob.upload.api.exception.httpresponses;

import lombok.NoArgsConstructor;

/** 404 - Not found http exception */
@NoArgsConstructor
public class NotFoundException extends RuntimeException {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.caritas.cob.upload.api.exception.httpresponses;

import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ResponseStatus;

@Getter
@ResponseStatus(value = HttpStatus.FORBIDDEN, reason = "statistics disabled")
public class StatisticsDisabledException extends AccessDeniedException {

public StatisticsDisabledException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver;
import org.springframework.boot.actuate.endpoint.web.EndpointMapping;
import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint;
import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -141,40 +138,29 @@ public MimeTypeDetector mimeTypeDetector() {
return new TikaMimeTypeDetector(new Tika());
}

/* to fix actuator and springfox issue as described:
https://websparrow.org/spring/failed-to-start-bean-documentationpluginsbootstrapper-nested-exception-is-java-lang-nullpointerexception
*/
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
WebEndpointsSupplier webEndpointsSupplier,
ServletEndpointsSupplier servletEndpointsSupplier,
ControllerEndpointsSupplier controllerEndpointsSupplier,
EndpointMediaTypes endpointMediaTypes,
CorsEndpointProperties corsProperties,
WebEndpointProperties webEndpointProperties,
Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());

List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>(webEndpointsSupplier.getEndpoints());

String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping =
this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
webEndpointProperties.getDiscovery().isEnabled()
&& (StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));

return new WebMvcEndpointHandlerMapping(
endpointMapping,
webEndpoints,
webEndpointsSupplier.getEndpoints(),
endpointMediaTypes,
corsProperties.toCorsConfiguration(),
new EndpointLinksResolver(allEndpoints, basePath),
shouldRegisterLinksMapping);
}

private boolean shouldRegisterLinksMapping(
WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled()
&& (StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
package de.caritas.cob.uploadservice.api.service;

import de.caritas.cob.uploadservice.config.CacheManagerConfig;
import de.caritas.cob.uploadservice.tenantservice.generated.web.TenantControllerApi;
import de.caritas.cob.uploadservice.config.TenantServiceApiControllerFactory;
import de.caritas.cob.uploadservice.tenantservice.generated.web.model.RestrictedTenantDTO;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class TenantService {

private final @NonNull TenantControllerApi tenantControllerApi;
private final @NonNull TenantServiceApiControllerFactory tenantServiceApiControllerFactory;

@Cacheable(cacheNames = CacheManagerConfig.TENANT_CACHE, key = "#subdomain")
public RestrictedTenantDTO getRestrictedTenantDataBySubdomain(String subdomain) {
return tenantControllerApi.getRestrictedTenantDataBySubdomainWithHttpInfo(subdomain).getBody();
return getRestrictedTenantDTO(subdomain);
}

public RestrictedTenantDTO getRestrictedTenantDataBySubdomainNonCached(String subdomain) {
return getRestrictedTenantDTO(subdomain);
}

private RestrictedTenantDTO getRestrictedTenantDTO(String subdomain) {
log.debug("Calling tenant service to get tenant data for subdomain {}", subdomain);
return tenantServiceApiControllerFactory
.createControllerApi()
.getRestrictedTenantDataBySubdomainWithHttpInfo(subdomain, null)
.getBody();
}

private RestrictedTenantDTO getRestrictedTenantDTO(Long tenantId) {
log.debug("Calling tenant service to get tenant data for tenantId {}", tenantId);
return tenantServiceApiControllerFactory
.createControllerApi()
.getRestrictedTenantDataByTenantId(tenantId);
}

public RestrictedTenantDTO getRestrictedTenantDataNonCached(Long tenantId) {
return getRestrictedTenantDTO(tenantId);
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
package de.caritas.cob.uploadservice.config;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.StringUtils.isEmpty;

import de.caritas.cob.uploadservice.api.exception.httpresponses.InternalServerErrorException;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.Collection;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/** Extension of the generated UserService API client to adapt the handling of parameter values. */
public class TenantServiceApiClient
extends de.caritas.cob.uploadservice.tenantservice.generated.ApiClient {

private static final String FILTER_NAME = "filter";

public TenantServiceApiClient(RestTemplate restTemplate) {
super(restTemplate);
}
Expand All @@ -37,46 +25,10 @@ public TenantServiceApiClient(RestTemplate restTemplate) {
public MultiValueMap<String, String> parameterToMultiValueMap(
CollectionFormat collectionFormat, String name, Object value) {

if (noValidFilterParams(name, value)) {
if (TenantServiceHelper.noValidFilterParams(name, value)) {
return super.parameterToMultiValueMap(collectionFormat, name, value);
}

return obtainQueryParameters(value);
}

private boolean noValidFilterParams(String queryName, Object queryValue) {
return isEmpty(queryName) || !queryName.equals(FILTER_NAME) || isNull(queryValue);
}

private MultiValueMap<String, String> obtainQueryParameters(Object queryValue) {
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();

try {
Arrays.asList(
Introspector.getBeanInfo(queryValue.getClass(), Object.class)
.getPropertyDescriptors())
.stream()
.filter(descriptor -> nonNull(descriptor.getReadMethod()))
.forEach(descriptor -> setMethodKeyValuePairs(queryValue, paramMap, descriptor));
return paramMap;

} catch (IntrospectionException exception) {
throw new InternalServerErrorException(
"Could not obtain method properties of %s".formatted(queryValue.toString()), exception);
}
}

private void setMethodKeyValuePairs(
Object queryValue, MultiValueMap<String, String> map, PropertyDescriptor descriptor) {
try {
Object value = descriptor.getReadMethod().invoke(queryValue);
if (nonNull(value)) {
map.add(descriptor.getName(), value.toString());
}
} catch (Exception exception) {
throw new InternalServerErrorException(
"Could not obtain method key value pairs of %s".formatted(queryValue.toString()),
exception);
}
return TenantServiceHelper.obtainQueryParameters(value);
}
}
Loading

0 comments on commit 7e6705e

Please sign in to comment.