Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reformat #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/it/gov/pagopa/apiconfig/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
Expand Down Expand Up @@ -43,10 +42,8 @@ public class LoggingAspect {
public static final String REQUEST_ID = "requestId";
public static final String OPERATION_ID = "operationId";

@Autowired
HttpServletRequest httRequest;
@Autowired
HttpServletResponse httpResponse;
final HttpServletRequest httRequest;
final HttpServletResponse httpResponse;

@Value("${info.application.artifactId}")
private String artifactId;
Expand All @@ -57,6 +54,11 @@ public class LoggingAspect {
@Value("${info.properties.environment}")
private String environment;

public LoggingAspect(HttpServletRequest httRequest, HttpServletResponse httpResponse) {
this.httRequest = httRequest;
this.httpResponse = httpResponse;
}

private static String getExecutionTime() {
String startTime = MDC.get(START_TIME);
if (startTime != null) {
Expand All @@ -70,13 +72,17 @@ private static String getExecutionTime() {
private static String getDetail(ResponseEntity<ProblemJson> result) {
if (result != null && result.getBody() != null && result.getBody().getDetail() != null) {
return result.getBody().getDetail();
} else return AppError.UNKNOWN.getDetails();
} else {
return AppError.UNKNOWN.getDetails();
}
}

private static String getTitle(ResponseEntity<ProblemJson> result) {
if (result != null && result.getBody() != null && result.getBody().getTitle() != null) {
return result.getBody().getTitle();
} else return AppError.UNKNOWN.getTitle();
} else {
return AppError.UNKNOWN.getTitle();
}
}

@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package it.gov.pagopa.apiconfig.selfcareintegration.config;

import static it.gov.pagopa.apiconfig.selfcareintegration.util.Constants.HEADER_REQUEST_ID;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Paths;
Expand All @@ -11,123 +9,126 @@
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.security.SecurityScheme;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.springdoc.core.customizers.OpenApiCustomiser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static it.gov.pagopa.apiconfig.selfcareintegration.util.Constants.HEADER_REQUEST_ID;

@Configuration
public class OpenApiConfig {

@Bean
public OpenAPI customOpenAPI(
@Value("${info.application.name}") String appName,
@Value("${info.application.description}") String appDescription,
@Value("${info.application.version}") String appVersion) {
return new OpenAPI()
.components(
new Components()
.addSecuritySchemes(
"ApiKey",
new SecurityScheme()
.type(SecurityScheme.Type.APIKEY)
.description("The API key to access this function app.")
.name("Ocp-Apim-Subscription-Key")
.in(SecurityScheme.In.HEADER))
.addSecuritySchemes(
"Authorization",
new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.description("JWT token get after Azure Login")
.name("Authorization")
.scheme("bearer")
.bearerFormat("JWT")
.in(SecurityScheme.In.HEADER)))
.info(
new Info()
.title(appName)
.version(appVersion)
.description(appDescription)
.termsOfService("https://www.pagopa.gov.it/"));
}
@Bean
public OpenAPI customOpenAPI(
@Value("${info.application.name}") String appName,
@Value("${info.application.description}") String appDescription,
@Value("${info.application.version}") String appVersion) {
return new OpenAPI()
.components(
new Components()
.addSecuritySchemes(
"ApiKey",
new SecurityScheme()
.type(SecurityScheme.Type.APIKEY)
.description("The API key to access this function app.")
.name("Ocp-Apim-Subscription-Key")
.in(SecurityScheme.In.HEADER))
.addSecuritySchemes(
"Authorization",
new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.description("JWT token get after Azure Login")
.name("Authorization")
.scheme("bearer")
.bearerFormat("JWT")
.in(SecurityScheme.In.HEADER)))
.info(
new Info()
.title(appName)
.version(appVersion)
.description(appDescription)
.termsOfService("https://www.pagopa.gov.it/"));
}

@Bean
public OpenApiCustomiser sortOperationsAlphabetically() {
return openApi -> {
Paths paths =
openApi.getPaths().entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(
Paths::new,
(map, item) -> map.addPathItem(item.getKey(), item.getValue()),
Paths::putAll);
@Bean
public OpenApiCustomiser sortOperationsAlphabetically() {
return openApi -> {
Paths paths =
openApi.getPaths().entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(
Paths::new,
(map, item) -> map.addPathItem(item.getKey(), item.getValue()),
Paths::putAll);

paths.forEach(
(key, value) ->
value
.readOperations()
.forEach(
operation -> {
var responses =
operation.getResponses().entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(
ApiResponses::new,
(map, item) ->
map.addApiResponse(item.getKey(), item.getValue()),
ApiResponses::putAll);
operation.setResponses(responses);
}));
openApi.setPaths(paths);
};
}
paths.forEach(
(key, value) ->
value
.readOperations()
.forEach(
operation -> {
var responses =
operation.getResponses().entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(
ApiResponses::new,
(map, item) ->
map.addApiResponse(item.getKey(), item.getValue()),
ApiResponses::putAll);
operation.setResponses(responses);
}));
openApi.setPaths(paths);
};
}

@Bean
public OpenApiCustomiser addCommonHeaders() {
return openApi ->
openApi
.getPaths()
.forEach(
(key, value) -> {
@Bean
public OpenApiCustomiser addCommonHeaders() {
return openApi ->
openApi
.getPaths()
.forEach(
(key, value) -> {

// add Request-ID as request header
var header =
Optional.ofNullable(value.getParameters())
.orElse(Collections.emptyList())
.parallelStream()
.filter(Objects::nonNull)
.anyMatch(elem -> HEADER_REQUEST_ID.equals(elem.getName()));
if (!header) {
value.addParametersItem(
new Parameter()
.in("header")
.name(HEADER_REQUEST_ID)
.schema(new StringSchema())
.description(
"This header identifies the call, if not passed it is"
+ " self-generated. This ID is returned in the response."));
}
// add Request-ID as request header
var header =
Optional.ofNullable(value.getParameters())
.orElse(Collections.emptyList())
.parallelStream()
.filter(Objects::nonNull)
.anyMatch(elem -> HEADER_REQUEST_ID.equals(elem.getName()));
if (!header) {
value.addParametersItem(
new Parameter()
.in("header")
.name(HEADER_REQUEST_ID)
.schema(new StringSchema())
.description(
"This header identifies the call, if not passed it is"
+ " self-generated. This ID is returned in the response."));
}

// add Request-ID as response header
value
.readOperations()
.forEach(
operation ->
operation
.getResponses()
.values()
.forEach(
response ->
response.addHeaderObject(
HEADER_REQUEST_ID,
new Header()
.schema(new StringSchema())
.description(
"This header identifies the call"))));
});
}
// add Request-ID as response header
value
.readOperations()
.forEach(
operation ->
operation
.getResponses()
.values()
.forEach(
response ->
response.addHeaderObject(
HEADER_REQUEST_ID,
new Header()
.schema(new StringSchema())
.description(
"This header identifies the call"))));
});
}
}
Loading
Loading