feature/add-i18n-support (#167)
Co-authored-by: Michael Hoennig <michael@hoennig.de> Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/167 Reviewed-by: Marc Sandlus <marc.sandlus@hostsharing.net>
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package net.hostsharing.hsadminng.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.annotation.RequestScope;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.util.Locale;
|
||||
|
||||
@Service
|
||||
@RequestScope
|
||||
public class MessageTranslator {
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest httpRequest;
|
||||
|
||||
@Autowired
|
||||
private MessageSource messageSource;
|
||||
|
||||
public String translateTo(final Locale locale, final String messageKey, final Object... args) {
|
||||
try {
|
||||
// we don't use the method which also takes a default message right away ...
|
||||
final var translatedMessage = messageSource.getMessage(messageKey, args, locale);
|
||||
return translatedMessage;
|
||||
} catch (final Exception e) {
|
||||
final var defaultMessage = messageKey.replace("'", "''");
|
||||
final var translatedMessage = messageSource.getMessage(messageKey, args, defaultMessage, locale);
|
||||
if (locale != Locale.ENGLISH) {
|
||||
// ... because we want to add a hint that the translation is missing, even if placeholders got replaced
|
||||
return translatedMessage + " [" + locale + " translation missing]";
|
||||
}
|
||||
return translatedMessage;
|
||||
}
|
||||
}
|
||||
|
||||
public String translate(final String messageKey, final Object... args) {
|
||||
return translateTo(httpRequest.getLocale(), messageKey, args);
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package net.hostsharing.hsadminng.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
|
||||
@Configuration
|
||||
public class MessagesResourceConfig {
|
||||
@Bean
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
final var source = new ResourceBundleMessageSource();
|
||||
source.setBasenames("i18n/messages");
|
||||
source.setDefaultEncoding("UTF-8");
|
||||
return source;
|
||||
}
|
||||
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
package net.hostsharing.hsadminng.config;
|
||||
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -17,6 +18,7 @@ import java.io.IOException;
|
||||
|
||||
// HOWTO add logger
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class RealCasAuthenticator implements CasAuthenticator {
|
||||
|
||||
@Value("${hsadminng.cas.server}")
|
||||
@@ -25,8 +27,11 @@ public class RealCasAuthenticator implements CasAuthenticator {
|
||||
@Value("${hsadminng.cas.service}")
|
||||
private String serviceUrl;
|
||||
|
||||
private final MessageTranslator messageTranslator;
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
@Timed("app.cas.authenticate")
|
||||
public String authenticate(final HttpServletRequest httpRequest) {
|
||||
@@ -52,7 +57,7 @@ public class RealCasAuthenticator implements CasAuthenticator {
|
||||
|
||||
private Document verifyServiceTicket(final String serviceTicket) throws SAXException, IOException, ParserConfigurationException {
|
||||
if ( !serviceTicket.startsWith("ST-") ) {
|
||||
throwBadCredentialsException("Invalid authorization ticket");
|
||||
throwBadCredentialsException("unknown authorization ticket");
|
||||
}
|
||||
|
||||
final var url = casServerUrl + "/cas/p3/serviceValidate" +
|
||||
@@ -69,12 +74,13 @@ public class RealCasAuthenticator implements CasAuthenticator {
|
||||
private String extractUserName(final Document verification) {
|
||||
|
||||
if (verification.getElementsByTagName("cas:authenticationSuccess").getLength() == 0) {
|
||||
throwBadCredentialsException("CAS service ticket could not be verified");
|
||||
throwBadCredentialsException("CAS service-ticket could not be verified");
|
||||
}
|
||||
return verification.getElementsByTagName("cas:user").item(0).getTextContent();
|
||||
}
|
||||
|
||||
private void throwBadCredentialsException(final String message) {
|
||||
throw new BadCredentialsException(message);
|
||||
private void throwBadCredentialsException(final String messageKey) {
|
||||
final var translatedMessage = messageTranslator.translate(messageKey);
|
||||
throw new BadCredentialsException(translatedMessage);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
package net.hostsharing.hsadminng.config;
|
||||
|
||||
/**
|
||||
* Makes it possible to translate messages which got created by external sources (libraries, database, etc.)
|
||||
* without i18n support.
|
||||
*/
|
||||
public interface RetroactiveTranslator {
|
||||
|
||||
boolean canTranslate(final String message);
|
||||
String translate(final String message);
|
||||
}
|
@@ -26,6 +26,9 @@ public class WebSecurityConfig {
|
||||
@Autowired
|
||||
private CasAuthenticationFilter authenticationFilter;
|
||||
|
||||
@Autowired
|
||||
private MessageTranslator messageTranslator;
|
||||
|
||||
@Bean
|
||||
@Profile("!test")
|
||||
public SecurityFilterChain securityFilterChain(final HttpSecurity http) throws Exception {
|
||||
@@ -55,7 +58,7 @@ public class WebSecurityConfig {
|
||||
@Bean
|
||||
@Profile("realCasAuthenticator")
|
||||
public CasAuthenticator realCasServiceTicketValidator() {
|
||||
return new RealCasAuthenticator();
|
||||
return new RealCasAuthenticator(messageTranslator);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@@ -11,7 +11,7 @@ import java.time.LocalDateTime;
|
||||
@Getter
|
||||
public class CustomErrorResponse {
|
||||
|
||||
static ResponseEntity<CustomErrorResponse> errorResponse(
|
||||
static ResponseEntity<CustomErrorResponse> customErrorResponse(
|
||||
final WebRequest request,
|
||||
final HttpStatus httpStatus,
|
||||
final String message) {
|
||||
@@ -21,13 +21,13 @@ public class CustomErrorResponse {
|
||||
|
||||
static String firstMessageLine(final Throwable exception) {
|
||||
if (exception.getMessage() != null) {
|
||||
return line(exception.getMessage(), 0);
|
||||
return stripTechnicalDetails(exception.getMessage());
|
||||
}
|
||||
return "ERROR: [500] " + exception.getClass().getName();
|
||||
}
|
||||
|
||||
static String line(final String message, final int lineNo) {
|
||||
return message.split("\\r|\\n|\\r\\n", 0)[lineNo];
|
||||
static String stripTechnicalDetails(final String message) {
|
||||
return message.split("\\r|\\n|\\r\\n", 0)[0];
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
|
||||
@@ -41,12 +41,12 @@ public class CustomErrorResponse {
|
||||
|
||||
private final String message;
|
||||
|
||||
CustomErrorResponse(final String path, final HttpStatus status, final String message) {
|
||||
CustomErrorResponse(final String path, final HttpStatus status, final String rawMessage) {
|
||||
// HOWTO: debug serverside error response - set a breakpoint here
|
||||
this.timestamp = LocalDateTime.now();
|
||||
this.path = path;
|
||||
this.statusCode = status.value();
|
||||
this.statusPhrase = status.getReasonPhrase();
|
||||
// HOWTO: debug serverside error response - set a breakpoint here
|
||||
this.message = message.startsWith("ERROR: [") ? message : "ERROR: [" + statusCode + "] " + message;
|
||||
this.message = rawMessage.startsWith("ERROR: [") ? rawMessage : "ERROR: [" + statusCode + "] " + rawMessage;
|
||||
}
|
||||
}
|
||||
|
@@ -1,19 +1,36 @@
|
||||
package net.hostsharing.hsadminng.errors;
|
||||
|
||||
import net.hostsharing.hsadminng.config.MessageTranslator;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static java.util.Locale.ENGLISH;
|
||||
|
||||
public class ReferenceNotFoundException extends RuntimeException {
|
||||
|
||||
private final String TRANSLATABLE_MESSAGE = "{0} \"{1}\" not found";
|
||||
|
||||
private final MessageTranslator translator;
|
||||
|
||||
private final Class<?> entityClass;
|
||||
private final String entityClassDisplayName;
|
||||
private final UUID uuid;
|
||||
public <E> ReferenceNotFoundException(final Class<E> entityClass, final UUID uuid, final Throwable exc) {
|
||||
|
||||
public <E> ReferenceNotFoundException(final MessageTranslator translator, final Class<E> entityClass, final UUID uuid, final Throwable exc) {
|
||||
super(exc);
|
||||
this.translator = translator;
|
||||
this.entityClass = entityClass;
|
||||
this.entityClassDisplayName = DisplayAs.DisplayName.of(entityClass);
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "Cannot resolve " + entityClass.getSimpleName() +" with uuid " + uuid;
|
||||
return translator.translateTo(ENGLISH, TRANSLATABLE_MESSAGE, entityClassDisplayName, uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalizedMessage() {
|
||||
return translator.translate(TRANSLATABLE_MESSAGE, entityClassDisplayName, uuid);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,13 @@
|
||||
package net.hostsharing.hsadminng.errors;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.hostsharing.hsadminng.config.MessageTranslator;
|
||||
import net.hostsharing.hsadminng.config.RetroactiveTranslator;
|
||||
import org.iban4j.Iban4jException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.NestedExceptionUtils;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
@@ -23,62 +28,67 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.validation.ValidationException;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static net.hostsharing.hsadminng.errors.CustomErrorResponse.*;
|
||||
|
||||
@ControllerAdvice
|
||||
@RequiredArgsConstructor
|
||||
// HOWTO handle exceptions to produce specific http error codes and sensible error messages
|
||||
public class RestResponseEntityExceptionHandler
|
||||
extends ResponseEntityExceptionHandler {
|
||||
|
||||
@Autowired
|
||||
private final MessageTranslator messageTranslator;
|
||||
|
||||
@Autowired(required = false)
|
||||
private final List<RetroactiveTranslator> retroactiveTranslators;
|
||||
|
||||
@ExceptionHandler(DataIntegrityViolationException.class)
|
||||
protected ResponseEntity<CustomErrorResponse> handleConflict(
|
||||
final RuntimeException exc, final WebRequest request) {
|
||||
|
||||
final var rawMessage = NestedExceptionUtils.getMostSpecificCause(exc).getMessage();
|
||||
var message = line(rawMessage, 0);
|
||||
if (message.contains("violates foreign key constraint")) {
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, line(rawMessage, 1).replaceAll(" *Detail: *", ""));
|
||||
}
|
||||
return errorResponse(request, HttpStatus.CONFLICT, message);
|
||||
final var fullMaybeLocalizedMessage = localizedMessage(NestedExceptionUtils.getMostSpecificCause(exc));
|
||||
final var sprippedMaybeLocalizedMessage = stripTechnicalDetails(fullMaybeLocalizedMessage);
|
||||
return errorResponse(request, HttpStatus.CONFLICT, sprippedMaybeLocalizedMessage);
|
||||
}
|
||||
|
||||
@ExceptionHandler(JpaSystemException.class)
|
||||
protected ResponseEntity<CustomErrorResponse> handleJpaExceptions(
|
||||
final RuntimeException exc, final WebRequest request) {
|
||||
final var message = line(NestedExceptionUtils.getMostSpecificCause(exc).getMessage(), 0);
|
||||
return errorResponse(request, httpStatus(exc, message).orElse(HttpStatus.INTERNAL_SERVER_ERROR), message);
|
||||
final var fullMaybeLocalizedMessage = localizedMessage(NestedExceptionUtils.getMostSpecificCause(exc));
|
||||
final var sprippedMaybeLocalizedMessage = stripTechnicalDetails(fullMaybeLocalizedMessage);
|
||||
return errorResponse(request, httpStatus(exc, sprippedMaybeLocalizedMessage).orElse(HttpStatus.INTERNAL_SERVER_ERROR), sprippedMaybeLocalizedMessage);
|
||||
}
|
||||
|
||||
@ExceptionHandler(NoSuchElementException.class)
|
||||
protected ResponseEntity<CustomErrorResponse> handleNoSuchElementException(
|
||||
final RuntimeException exc, final WebRequest request) {
|
||||
final var message = line(NestedExceptionUtils.getMostSpecificCause(exc).getMessage(), 0);
|
||||
return errorResponse(request, HttpStatus.NOT_FOUND, message);
|
||||
final var fullMaybeLocalizedMessage = localizedMessage(NestedExceptionUtils.getMostSpecificCause(exc));
|
||||
final var sprippedMaybeLocalizedMessage = stripTechnicalDetails(fullMaybeLocalizedMessage);
|
||||
return errorResponse(request, HttpStatus.NOT_FOUND, sprippedMaybeLocalizedMessage);
|
||||
}
|
||||
|
||||
@ExceptionHandler(ReferenceNotFoundException.class)
|
||||
protected ResponseEntity<CustomErrorResponse> handleReferenceNotFoundException(
|
||||
final ReferenceNotFoundException exc, final WebRequest request) {
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, exc.getMessage());
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, localizedMessage(exc));
|
||||
}
|
||||
|
||||
@ExceptionHandler({ JpaObjectRetrievalFailureException.class, EntityNotFoundException.class })
|
||||
protected ResponseEntity<CustomErrorResponse> handleJpaObjectRetrievalFailureException(
|
||||
final RuntimeException exc, final WebRequest request) {
|
||||
final var message =
|
||||
userReadableEntityClassName(
|
||||
line(NestedExceptionUtils.getMostSpecificCause(exc).getMessage(), 0));
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, message);
|
||||
final var localizedMessage = localizedMessage(NestedExceptionUtils.getMostSpecificCause(exc));
|
||||
final var sprippedMaybeLocalizedMessage = stripTechnicalDetails(localizedMessage);
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, sprippedMaybeLocalizedMessage);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ Iban4jException.class, ValidationException.class })
|
||||
protected ResponseEntity<CustomErrorResponse> handleValidationExceptions(
|
||||
final Throwable exc, final WebRequest request) {
|
||||
final String fullMessage = NestedExceptionUtils.getMostSpecificCause(exc).getMessage();
|
||||
final var message = exc instanceof MultiValidationException ? fullMessage : line(fullMessage, 0);
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, message);
|
||||
final var localizedMessage = localizedMessage(NestedExceptionUtils.getMostSpecificCause(exc));
|
||||
final var sprippedMaybeLocalizedMessage = exc instanceof MultiValidationException ? localizedMessage : stripTechnicalDetails(localizedMessage);
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, sprippedMaybeLocalizedMessage);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Throwable.class)
|
||||
@@ -96,15 +106,16 @@ public class RestResponseEntityExceptionHandler
|
||||
|
||||
final var response = super.handleExceptionInternal(exc, body, headers, statusCode, request);
|
||||
return errorResponse(request, HttpStatus.valueOf(statusCode.value()),
|
||||
Optional.ofNullable(response.getBody()).map(Object::toString).orElse(firstMessageLine(exc)));
|
||||
Optional.ofNullable(response).map(HttpEntity::getBody).map(Object::toString).orElse(firstMessageLine(exc)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked,rawtypes")
|
||||
protected ResponseEntity handleHttpMessageNotReadable(
|
||||
HttpMessageNotReadableException exc, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
|
||||
final var message = line(exc.getMessage(), 0);
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, message);
|
||||
final var localizedMessage = localizedMessage(exc);
|
||||
final var sprippedMaybeLocalizedMessage = stripTechnicalDetails(localizedMessage);
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, sprippedMaybeLocalizedMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -139,37 +150,26 @@ public class RestResponseEntityExceptionHandler
|
||||
.flatMap(Collection::stream)
|
||||
.filter(FieldError.class::isInstance)
|
||||
.map(FieldError.class::cast)
|
||||
.map(fieldError -> fieldError.getField() + " " + fieldError.getDefaultMessage() + " but is \""
|
||||
+ fieldError.getRejectedValue() + "\"")
|
||||
.map(toEnrichedFieldErrorMessage())
|
||||
.toList();
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, errorList.toString());
|
||||
}
|
||||
|
||||
|
||||
private String userReadableEntityClassName(final String exceptionMessage) {
|
||||
final var regex = "(net.hostsharing.hsadminng.[a-z0-9_.]*.[A-Za-z0-9_$]*Entity) ";
|
||||
final var pattern = Pattern.compile(regex);
|
||||
final var matcher = pattern.matcher(exceptionMessage);
|
||||
if (matcher.find()) {
|
||||
final var entityName = matcher.group(1);
|
||||
final var entityClass = resolveClass(entityName);
|
||||
if (entityClass.isPresent()) {
|
||||
return (entityClass.get().isAnnotationPresent(DisplayAs.class)
|
||||
? exceptionMessage.replace(entityName, entityClass.get().getAnnotation(DisplayAs.class).value())
|
||||
: exceptionMessage.replace(entityName, entityClass.get().getSimpleName()))
|
||||
.replace(" with id ", " with uuid ");
|
||||
}
|
||||
|
||||
}
|
||||
return exceptionMessage;
|
||||
private Function<FieldError, String> toEnrichedFieldErrorMessage() {
|
||||
final var translatedButIsLiteral = messageTranslator.translate("but is");
|
||||
// TODO.i18n: the following does not work in all languages, e.g. not in right-to-left languages
|
||||
return fieldError -> fieldError.getField() + " " + fieldError.getDefaultMessage() +
|
||||
" " + translatedButIsLiteral + " " + optionallyQuoted(fieldError.getRejectedValue());
|
||||
}
|
||||
|
||||
private static Optional<Class<?>> resolveClass(final String entityName) {
|
||||
try {
|
||||
return Optional.of(ClassLoader.getSystemClassLoader().loadClass(entityName));
|
||||
} catch (ClassNotFoundException e) {
|
||||
return Optional.empty();
|
||||
private String optionallyQuoted(final Object value) {
|
||||
if (value == null) {
|
||||
return "null";
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return value.toString();
|
||||
}
|
||||
return "\"" + value + "\"";
|
||||
}
|
||||
|
||||
private Optional<HttpStatus> httpStatus(final Throwable causingException, final String message) {
|
||||
@@ -187,4 +187,25 @@ public class RestResponseEntityExceptionHandler
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private String localizedMessage(final Throwable throwable) {
|
||||
// most libraries seem to provide the localized message in both properties, but just for the case:
|
||||
return throwable.getLocalizedMessage() != null ? throwable.getLocalizedMessage() : throwable.getMessage();
|
||||
}
|
||||
|
||||
private String tryTranslation(final String message) {
|
||||
|
||||
for ( RetroactiveTranslator rtx: retroactiveTranslators ) {
|
||||
if (rtx.canTranslate(message)) {
|
||||
return rtx.translate(message);
|
||||
}
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private ResponseEntity<CustomErrorResponse> errorResponse(
|
||||
final WebRequest request,
|
||||
final HttpStatus httpStatus,
|
||||
final String maybeTranslatedMessage) {
|
||||
return customErrorResponse(request, httpStatus, tryTranslation(maybeTranslatedMessage));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,40 @@
|
||||
package net.hostsharing.hsadminng.hs.hosting.asset;
|
||||
|
||||
import net.hostsharing.hsadminng.config.MessageTranslator;
|
||||
import net.hostsharing.hsadminng.config.RetroactiveTranslator;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
// HOWTO translate messages which got created without i18n support, in this case in a PostgreSQL constraint trigger
|
||||
@Service
|
||||
public class HsHostingAssetTranslations implements RetroactiveTranslator {
|
||||
|
||||
public static final String ERROR_400_PREFIX = "ERROR: [400] ";
|
||||
|
||||
@Autowired
|
||||
private MessageTranslator messageTranslator;
|
||||
|
||||
@Override
|
||||
public boolean canTranslate(final String message) {
|
||||
return message.equals("ERROR: [400] coop assets transaction would result in a negative balance of assets");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String translate(final String message) {
|
||||
// it's guaranteed to be the same message, for which canTranslate(...) returned true
|
||||
// and in this case it's just one
|
||||
return ERROR_400_PREFIX + messageTranslator.translate(message.substring(ERROR_400_PREFIX.length()));
|
||||
|
||||
// HOWTO extract variable parts from a messages which got created without i18n support:
|
||||
// final var regex = "(?<propertyName>[^ ]+) (?<propertyValue>.+) not found";
|
||||
// final var pattern = Pattern.compile(regex);
|
||||
// final var matcher = pattern.matcher(message);
|
||||
//
|
||||
// if (matcher.matches()) {
|
||||
// final var propertyName = matcher.group("propertyName");
|
||||
// final var propertyValue = matcher.group("propertyValue");
|
||||
// return messageTranslator.translate("", propertyName, propertyValue);
|
||||
// }
|
||||
}
|
||||
}
|
@@ -145,7 +145,7 @@ public abstract class HostingAssetEntityValidator extends HsEntityValidator<HsHo
|
||||
if (assetEntity.getIdentifier() == null ||
|
||||
!expectedIdentifierPattern.matcher(assetEntity.getIdentifier()).matches()) {
|
||||
return List.of(
|
||||
"'identifier' expected to match '" + expectedIdentifierPattern + "', but is '" + assetEntity.getIdentifier()
|
||||
"'identifier' expected to match '" + expectedIdentifierPattern + "' but is '" + assetEntity.getIdentifier()
|
||||
+ "'");
|
||||
}
|
||||
return Collections.emptyList();
|
||||
|
@@ -28,7 +28,7 @@ class HsIPv6NumberHostingAssetValidator extends HostingAssetEntityValidator {
|
||||
final var violations = super.validateEntity(assetEntity);
|
||||
|
||||
if (!isValidIPv6Address(assetEntity.getIdentifier())) {
|
||||
violations.add("'identifier' expected to be a valid IPv6 address, but is '" + assetEntity.getIdentifier() + "'");
|
||||
violations.add("'identifier' expected to be a valid IPv6 address but is '" + assetEntity.getIdentifier() + "'");
|
||||
}
|
||||
|
||||
return violations;
|
||||
|
@@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.hs.office.coopassets;
|
||||
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import net.hostsharing.hsadminng.config.MessageTranslator;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.errors.MultiValidationException;
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeCoopAssetsApi;
|
||||
@@ -30,7 +31,6 @@ import java.util.function.BiConsumer;
|
||||
|
||||
import static java.util.Optional.ofNullable;
|
||||
import static net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionType.REVERSAL;
|
||||
import static net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionType.TRANSFER;
|
||||
import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopAssetsTransactionTypeResource.CLEARING;
|
||||
import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopAssetsTransactionTypeResource.DEPOSIT;
|
||||
import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopAssetsTransactionTypeResource.DISBURSAL;
|
||||
@@ -50,6 +50,9 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse
|
||||
@Autowired
|
||||
private EntityManagerWrapper emw;
|
||||
|
||||
@Autowired
|
||||
private MessageTranslator messageTranslator;
|
||||
|
||||
@Autowired
|
||||
private HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo;
|
||||
|
||||
@@ -131,33 +134,32 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse
|
||||
MultiValidationException.throwIfNotEmpty(violations);
|
||||
}
|
||||
|
||||
private static void validateDebitTransaction(
|
||||
private void validateDebitTransaction(
|
||||
final HsOfficeCoopAssetsTransactionInsertResource requestBody,
|
||||
final ArrayList<String> violations) {
|
||||
if (List.of(DEPOSIT, HsOfficeCoopAssetsTransactionTypeResource.ADOPTION).contains(requestBody.getTransactionType())
|
||||
&& requestBody.getAssetValue().signum() < 0) {
|
||||
violations.add("for %s, assetValue must be positive but is \"%.2f\"".formatted(
|
||||
violations.add(messageTranslator.translate("for transactionType={0}, assetValue must be positive but is {1,number,#0.00}",
|
||||
requestBody.getTransactionType(), requestBody.getAssetValue()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateCreditTransaction(
|
||||
private void validateCreditTransaction(
|
||||
final HsOfficeCoopAssetsTransactionInsertResource requestBody,
|
||||
final ArrayList<String> violations) {
|
||||
if (List.of(DISBURSAL, HsOfficeCoopAssetsTransactionTypeResource.TRANSFER, CLEARING, LOSS)
|
||||
.contains(requestBody.getTransactionType())
|
||||
&& requestBody.getAssetValue().signum() > 0) {
|
||||
violations.add("for %s, assetValue must be negative but is \"%.2f\"".formatted(
|
||||
violations.add(messageTranslator.translate("for transactionType={0}, assetValue must be negative but is {1,number,#0.00}",
|
||||
requestBody.getTransactionType(), requestBody.getAssetValue()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateAssetValue(
|
||||
private void validateAssetValue(
|
||||
final HsOfficeCoopAssetsTransactionInsertResource requestBody,
|
||||
final ArrayList<String> violations) {
|
||||
if (requestBody.getAssetValue().signum() == 0) {
|
||||
violations.add("assetValue must not be 0 but is \"%.2f\"".formatted(
|
||||
requestBody.getAssetValue()));
|
||||
violations.add(messageTranslator.translate("assetValue must not be 0"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,26 +223,32 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse
|
||||
final HsOfficeMembershipEntity membership = ofNullable(emw.find(
|
||||
HsOfficeMembershipEntity.class,
|
||||
resource.getMembershipUuid()))
|
||||
.orElseThrow(() -> new EntityNotFoundException("membership.uuid %s not found".formatted(
|
||||
resource.getMembershipUuid())));
|
||||
.orElseThrow(() -> new EntityNotFoundException(
|
||||
messageTranslator.translate(
|
||||
"{0} \"{1}\" not found", "membership.uuid", resource.getMembershipUuid())));
|
||||
entity.setMembership(membership);
|
||||
}
|
||||
|
||||
if (entity.getTransactionType() == REVERSAL) {
|
||||
if (resource.getRevertedAssetTxUuid() == null) {
|
||||
throw new ValidationException("REVERSAL asset transaction requires revertedAssetTx.uuid");
|
||||
throw new ValidationException(messageTranslator.translate(
|
||||
"a REVERSAL asset transaction requires specifying a revertedAssetTx.uuid"));
|
||||
}
|
||||
final var revertedAssetTx = coopAssetsTransactionRepo.findByUuid(resource.getRevertedAssetTxUuid())
|
||||
.orElseThrow(() -> new EntityNotFoundException("revertedAssetTx.uuid %s not found".formatted(
|
||||
.orElseThrow(() -> new EntityNotFoundException(messageTranslator.translate(
|
||||
"{0} \"{1}\" not found",
|
||||
"revertedAssetTx.uuid",
|
||||
resource.getRevertedAssetTxUuid())));
|
||||
revertedAssetTx.setReversalAssetTx(entity);
|
||||
entity.setRevertedAssetTx(revertedAssetTx);
|
||||
if (resource.getAssetValue().negate().compareTo(revertedAssetTx.getAssetValue()) != 0) {
|
||||
throw new ValidationException("given assetValue=" + resource.getAssetValue() +
|
||||
" but must be negative value from reverted asset tx: " + revertedAssetTx.getAssetValue());
|
||||
throw new ValidationException(
|
||||
messageTranslator.translate(
|
||||
"given assetValue {0,number,#0.00} must be the negative value of the reverted asset transaction: {1,number,#0.00}",
|
||||
resource.getAssetValue(), revertedAssetTx.getAssetValue()));
|
||||
}
|
||||
|
||||
if (revertedAssetTx.getTransactionType() == TRANSFER) {
|
||||
if (revertedAssetTx.getTransactionType() == HsOfficeCoopAssetsTransactionType.TRANSFER) {
|
||||
final var adoptionAssetTx = revertedAssetTx.getAdoptionAssetTx();
|
||||
final var adoptionReversalAssetTx = HsOfficeCoopAssetsTransactionEntity.builder()
|
||||
.transactionType(REVERSAL)
|
||||
@@ -259,8 +267,9 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse
|
||||
if (resource.getTransactionType() == HsOfficeCoopAssetsTransactionTypeResource.TRANSFER) {
|
||||
final var adoptingMembership = determineAdoptingMembership(resource);
|
||||
if ( entity.getMembership() == adoptingMembership) {
|
||||
throw new ValidationException("transferring and adopting membership must be different, but both are " +
|
||||
adoptingMembership.getTaggedMemberNumber());
|
||||
throw new ValidationException(messageTranslator.translate(
|
||||
"transferring and adopting membership must be different, but both are {0}",
|
||||
adoptingMembership.getTaggedMemberNumber()));
|
||||
}
|
||||
final var adoptingAssetTx = createAdoptingAssetTx(entity, adoptingMembership);
|
||||
entity.setAdoptionAssetTx(adoptingAssetTx);
|
||||
@@ -271,34 +280,45 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse
|
||||
final var adoptingMembershipUuid = resource.getAdoptingMembershipUuid();
|
||||
final var adoptingMembershipMemberNumber = resource.getAdoptingMembershipMemberNumber();
|
||||
if (adoptingMembershipUuid != null && adoptingMembershipMemberNumber != null) {
|
||||
throw new ValidationException(
|
||||
// @formatter:off
|
||||
resource.getTransactionType() == HsOfficeCoopAssetsTransactionTypeResource.TRANSFER
|
||||
? "either adoptingMembership.uuid or adoptingMembership.memberNumber can be given, not both"
|
||||
: "adoptingMembership.uuid and adoptingMembership.memberNumber must not be given for transactionType="
|
||||
+ resource.getTransactionType());
|
||||
// @formatter:on
|
||||
// @formatter:off
|
||||
final var message = messageTranslator.translate(
|
||||
resource.getTransactionType() == HsOfficeCoopAssetsTransactionTypeResource.TRANSFER
|
||||
? "either {0} or {1} must be given, not both"
|
||||
: "neither {0} nor {1} must be given for transactionType={2}",
|
||||
"adoptingMembership.uuid",
|
||||
"adoptingMembership.memberNumber",
|
||||
resource.getTransactionType());
|
||||
// @formatter:on
|
||||
throw new ValidationException(message);
|
||||
}
|
||||
|
||||
if (adoptingMembershipUuid != null) {
|
||||
final var adoptingMembership = membershipRepo.findByUuid(adoptingMembershipUuid);
|
||||
return adoptingMembership.orElseThrow(() ->
|
||||
new ValidationException(
|
||||
"adoptingMembership.uuid='" + adoptingMembershipUuid + "' not found or not accessible"));
|
||||
new ValidationException(messageTranslator.translate(
|
||||
"{0} \"{1}\" not found or not accessible",
|
||||
"adoptingMembership.uuid",
|
||||
adoptingMembershipUuid)));
|
||||
}
|
||||
|
||||
if (adoptingMembershipMemberNumber != null) {
|
||||
final var adoptingMemberNumber = Integer.valueOf(adoptingMembershipMemberNumber.substring("M-".length()));
|
||||
final var adoptingMembership = membershipRepo.findMembershipByMemberNumber(adoptingMemberNumber);
|
||||
return adoptingMembership.orElseThrow( () ->
|
||||
new ValidationException("adoptingMembership.memberNumber='" + adoptingMembershipMemberNumber
|
||||
+ "' not found or not accessible")
|
||||
);
|
||||
new ValidationException(
|
||||
messageTranslator.translate(
|
||||
"{0} \"{1}\" not found or not accessible",
|
||||
"adoptingMembership.memberNumber",
|
||||
adoptingMembershipMemberNumber)));
|
||||
}
|
||||
|
||||
throw new ValidationException(
|
||||
"either adoptingMembership.uuid or adoptingMembership.memberNumber must be given for transactionType="
|
||||
+ HsOfficeCoopAssetsTransactionTypeResource.TRANSFER);
|
||||
messageTranslator.translate(
|
||||
"either {0} or {1} must be given for transactionType={2}",
|
||||
"adoptingMembership.uuid",
|
||||
"adoptingMembership.memberNumber",
|
||||
resource.getTransactionType()
|
||||
));
|
||||
}
|
||||
|
||||
private HsOfficeCoopAssetsTransactionEntity createAdoptingAssetTx(
|
||||
|
@@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.hs.office.partner;
|
||||
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import net.hostsharing.hsadminng.config.MessageTranslator;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.errors.ReferenceNotFoundException;
|
||||
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactFromResourceConverter;
|
||||
@@ -45,6 +46,9 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
|
||||
@Autowired
|
||||
private StrictMapper mapper;
|
||||
|
||||
@Autowired
|
||||
private MessageTranslator translator;
|
||||
|
||||
@Autowired
|
||||
private HsOfficeContactFromResourceConverter<HsOfficeContactRealEntity> contactFromResourceConverter;
|
||||
|
||||
@@ -236,7 +240,7 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
|
||||
try {
|
||||
return em.getReference(entityClass, uuid);
|
||||
} catch (final Throwable exc) {
|
||||
throw new ReferenceNotFoundException(entityClass, uuid, exc);
|
||||
throw new ReferenceNotFoundException(translator, entityClass, uuid, exc);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -217,8 +217,8 @@ public abstract class ValidatableProperty<P extends ValidatableProperty<?, ?>, T
|
||||
//noinspection unchecked
|
||||
validate(result, (T) propValue, propsProvider);
|
||||
} else {
|
||||
result.add(propertyName + "' is expected to be of type " + type.getSimpleName() + ", " +
|
||||
"but is of type " + propValue.getClass().getSimpleName());
|
||||
result.add(propertyName + "' is expected to be of type " + type.getSimpleName() +
|
||||
" but is of type " + propValue.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
@@ -1,8 +1,9 @@
|
||||
package net.hostsharing.hsadminng.ping;
|
||||
|
||||
import net.hostsharing.hsadminng.config.MessageTranslator;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@@ -11,11 +12,15 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@Controller
|
||||
public class PingController {
|
||||
|
||||
@Autowired
|
||||
private MessageTranslator messageTranslator;
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping(value = "/api/ping", method = RequestMethod.GET)
|
||||
public String ping(
|
||||
@RequestHeader(name = "assumed-roles", required = false) String assumedRoles
|
||||
) {
|
||||
return "pong " + SecurityContextHolder.getContext().getAuthentication().getName() + "\n";
|
||||
public String ping() {
|
||||
final var userName = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
// HOWTO translate text with placeholders - also see in resource files i18n/messages_*.properties.
|
||||
final var translatedMessage = messageTranslator.translate("pong {0} - in English", userName);
|
||||
return translatedMessage + "\n";
|
||||
}
|
||||
}
|
||||
|
@@ -1069,7 +1069,7 @@ public class RbacSpec {
|
||||
*/
|
||||
public static SQL fetchedBySql(final String sql) {
|
||||
if ( !sql.startsWith("SELECT ${columns}") ) {
|
||||
throw new IllegalArgumentException("SQL SELECT expression must start with 'SELECT ${columns}', but is: " + sql);
|
||||
throw new IllegalArgumentException("SQL SELECT expression must start with 'SELECT ${columns}' but is: " + sql);
|
||||
}
|
||||
validateExpression(sql);
|
||||
return new SQL(sql, Part.SQL_QUERY);
|
||||
|
26
src/main/resources/i18n/messages_de.properties
Normal file
26
src/main/resources/i18n/messages_de.properties
Normal file
@@ -0,0 +1,26 @@
|
||||
# This file must be in UTF-8 encoding. Check if you see umlauts or garbage: äöüÄÖÜß.
|
||||
# HINT IntelliJ IDEA shows unused keys in gray.
|
||||
|
||||
pong\ {0}\ -\ in\ English=pong {0} - auf Deutsch
|
||||
|
||||
# config (including authorization)
|
||||
CAS\ service-ticket\ could\ not\ be\ verified=CAS Service-Ticket konnte nicht verifiziert werden
|
||||
unknown\ authorization\ ticket=unbekanntes Autorisierungs-Ticket
|
||||
|
||||
# general validations
|
||||
{0}\ "{1}"\ not\ found={0} "{1}" nicht gefunden
|
||||
{0}\ "{1}"\ not\ found\ or\ not\ accessible={0} "{1}" nicht gefunden oder nicht zugänglich
|
||||
but\ is=ist aber
|
||||
|
||||
# office.coop-assets
|
||||
either\ {0}\ or\ {1}\ must\ be\ given=entweder {0} oder {1} muss angegeben werden
|
||||
either\ {0}\ or\ {1}\ must\ be\ given,\ not\ both=entweder {0} oder {1} muss angegeben werden, nicht beide
|
||||
either\ {0}\ or\ {1}\ must\ be\ given\ for\ transactionType\={2}=für transactionType={2} muss entweder {0} oder {1} angegeben werden
|
||||
neither\ {0}\ nor\ {1}\ must\ be\ given\ for\ transactionType\={2}=für transactionType={2} darf weder {0} noch {1} angegeben werden
|
||||
assetValue\ must\ not\ be\ 0=assetValue darf nicht 0 sein
|
||||
for\ transactionType\={0},\ assetValue\ must\ be\ positive\ but\ is\ {1,number,#0.00}=für transactionType={0}, muss assetValue positiv sein, ist aber {1,number,#0.00}
|
||||
for\ transactionType\={0},\ assetValue\ must\ be\ negative\ but\ is\ {1,number,#0.00}=für transactionType={0}, muss assetValue negativ sein, ist aber {1,number,#0.00}
|
||||
given\ assetValue\ {0,number,#0.00}\ must\ be\ the\ negative\ value\ of\ the\ reverted\ asset\ transaction\:\ {1,number,#0.00}=assetValue={0,number,#0.00} muss dem negativen Wert des Wertes der stornierten Geschäftsguthaben-Transaktion entsprechen: {1,number,#0.00}
|
||||
a\ REVERSAL\ asset\ transaction\ requires\ specifying\ a\ revertedAssetTx.uuid=eine REVERSAL Geschäftsguthaben-Transaktion erfordert die Angabe einer revertedAssetTx.uuid
|
||||
transferring\ and\ adopting\ membership\ must\ be\ different,\ but\ both\ are\ {0}=übertragende und annehmende Mitgliedschaft müssen unterschiedlich sein, aber beide sind {0}
|
||||
coop\ assets\ transaction\ would\ result\ in\ a\ negative\ balance\ of\ assets=Geschäftsguthaben-Transaktion würde zu einem negativen Geschäftsguthaben-Saldo führen
|
8
src/main/resources/i18n/messages_en.properties
Normal file
8
src/main/resources/i18n/messages_en.properties
Normal file
@@ -0,0 +1,8 @@
|
||||
# This file must be in UTF-8 encoding. Check if you see umlauts or garbage: äöüÄÖÜß
|
||||
# HINT IntelliJ IDEA shows unused keys in gray.
|
||||
|
||||
# If the English translation is identical to the translation-key, it does not need to be included here.
|
||||
# But in that case, you can NOT use a prefix - or the prefix would be shown to the user as well.
|
||||
# I'm not sure, though, if using the english default translations as keys is really a good idea.
|
||||
|
||||
|
Reference in New Issue
Block a user