Skip to content

Commit

Permalink
feat: Unified error handling (#4)
Browse files Browse the repository at this point in the history
* [NOD-781] feat: add error json
* [NOD-781] feat: refactored code and updated missing classes

---------

Co-authored-by: maxsca <130107847+maxsca@users.noreply.github.com>
  • Loading branch information
andrea-deri and maxsca authored Mar 28, 2024
1 parent 00b2b0e commit 5e71f3b
Show file tree
Hide file tree
Showing 22 changed files with 369 additions and 345 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto working-tree-encoding=UTF-8

*.sh text eol=lf working-tree-encoding=UTF-8
40 changes: 40 additions & 0 deletions src/main/java/it/gov/pagopa/wispconverter/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,27 @@
import it.gov.pagopa.wispconverter.config.model.AppCors;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

List<Locale> locales = Arrays.asList(Locale.ENGLISH, Locale.ITALIAN);

@Value("${cors.configuration}")
private String corsConfiguration;

Expand All @@ -25,6 +38,33 @@ public void addCorsMappings(CorsRegistry registry) {
.allowedOrigins(appCors.getOrigins())
.allowedMethods(appCors.getMethods());
}

@Bean
public LocaleResolver localeResolver() {
AcceptHeaderLocaleResolver acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
acceptHeaderLocaleResolver.setDefaultLocale(Locale.ENGLISH);
acceptHeaderLocaleResolver.setSupportedLocales(locales);
return acceptHeaderLocaleResolver;
}

@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource =
new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}

@Primary
@Bean
@Override
public LocalValidatorFactoryBean getValidator() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import it.gov.pagopa.wispconverter.controller.model.AppInfoResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
Expand All @@ -17,6 +18,7 @@

@RestController
@Validated
@Tag(name = "Home", description = "Application info APIs")
public class HomeController {

@Value("${server.servlet.context-path}")
Expand Down Expand Up @@ -52,9 +54,6 @@ public RedirectView home() {
@Operation(summary = "Return OK if application is started", security = {@SecurityRequirement(name = "ApiKey")}, tags = {"Home"})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK.", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = AppInfoResponse.class))),
//@ApiResponse(responseCode = "401", description = "Wrong or missing function key.", content = @Content(schema = @Schema())),
//@ApiResponse(responseCode = "403", description = "Forbidden.", content = @Content(schema = @Schema())),
//@ApiResponse(responseCode = "500", description = "Service unavailable.", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ProblemJson.class)))}
})
@GetMapping("/info")
public AppInfoResponse healthCheck() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import it.gov.pagopa.wispconverter.service.ConverterService;
import it.gov.pagopa.wispconverter.service.model.ConversionResultDTO;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -23,33 +25,18 @@
@RequestMapping("/redirect")
@Validated
@RequiredArgsConstructor
@Tag(name = "Redirect", description = "Conversion and redirection APIs")
public class RedirectController {

private final ConverterService converterService;

/*
public static ResponseEntity<Object> generateConversionResponse(ConversionResult conversionResult) {
ResponseEntity<Object> result;
if (conversionResult.isSuccess()) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(conversionResult.getUri());
result = new ResponseEntity<>(httpHeaders, HttpStatus.FOUND);
} else {
result = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(conversionResult.getErrorPage());
}
return result;
}
*/

@Operation(summary = "", description = "", security = {@SecurityRequirement(name = "ApiKey")}, tags = {"Home"})
@Operation(summary = "", description = "", security = {@SecurityRequirement(name = "ApiKey")}, tags = {"Redirect"})
@ApiResponses(value = {
@ApiResponse(responseCode = "302", description = "Redirect to Checkout service.", content = @Content(schema = @Schema())),
//@ApiResponse(responseCode = "401", description = "Wrong or missing function key.", content = @Content(schema = @Schema())),
//@ApiResponse(responseCode = "403", description = "Forbidden.", content = @Content(schema = @Schema())),
//@ApiResponse(responseCode = "500", description = "Internal server error.", content = @Content(mediaType = MediaType.TEXT_HTML_VALUE))
@ApiResponse(responseCode = "302", description = "Redirect to Checkout service.", content = @Content(schema = @Schema()))
})
@GetMapping
public void redirect(@Parameter(description = "", example = "identificativoIntermediarioPA_sessionId") @RequestParam("sessionId") String sessionId,
public void redirect(@Parameter(description = "", example = "identificativoIntermediarioPA_sessionId")
@NotBlank(message = "{redirect.session-id.not-blank}") @RequestParam("sessionId") String sessionId,
HttpServletResponse response) throws IOException {
ConversionResultDTO conversionResultDTO = converterService.convert(sessionId);
response.sendRedirect(conversionResultDTO.getUri());
Expand Down
Loading

0 comments on commit 5e71f3b

Please sign in to comment.