4 Commits

Author SHA1 Message Date
Florian THIERRY
dee473fe46 Add interceptor to inject correlation id in http requests. 2024-09-26 10:38:52 +02:00
Florian THIERRY
5647a5a959 Implementation of jpa saving for traffic traces. 2024-09-26 10:21:20 +02:00
Florian THIERRY
c817371a15 Add skeletton to save traces. 2024-09-25 21:30:25 +02:00
ff52a198dc Upgrade to angular 18. 2024-09-24 22:47:15 +02:00
23 changed files with 3008 additions and 2552 deletions

View File

@@ -25,6 +25,10 @@ public class CustomUserDetails implements UserDetails {
.toList();
}
public User getUser() {
return user;
}
@Override
public String getUsername() {
return user.id().toString();

View File

@@ -0,0 +1,47 @@
package org.codiki.application.traffic;
import jakarta.annotation.Nullable;
import org.codiki.domain.traffic.exception.TrafficTraceCreationException;
import org.codiki.domain.traffic.model.TrafficEndpoint;
import org.codiki.domain.traffic.model.TrafficTrace;
import org.codiki.domain.traffic.port.TrafficTracePort;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.time.Clock;
import java.time.ZonedDateTime;
import java.util.UUID;
import static java.util.Objects.isNull;
import static org.codiki.domain.traffic.model.TrafficTrace.aTrafficTrace;
@Component
public class TrafficTraceUseCases {
private final TrafficTracePort trafficTracePort;
private final Clock clock;
public TrafficTraceUseCases(TrafficTracePort trafficTracePort, Clock clock) {
this.trafficTracePort = trafficTracePort;
this.clock = clock;
}
@Async
public void saveNewTrace(
TrafficEndpoint trafficEndpoint,
@Nullable UUID userId,
@Nullable String correlationId
) {
if (isNull(trafficEndpoint)) {
throw new TrafficTraceCreationException("Traffic endpoint should not be null.");
}
TrafficTrace newTrace = aTrafficTrace()
.withId(UUID.randomUUID())
.withDateTime(ZonedDateTime.now(clock))
.withEndpoint(trafficEndpoint)
.withUserId(userId)
.withCorrelationId(correlationId)
.build();
trafficTracePort.save(newTrace);
}
}

View File

@@ -87,9 +87,7 @@ public class UserUseCases {
.map(Authentication::getPrincipal)
.filter(CustomUserDetails.class::isInstance)
.map(CustomUserDetails.class::cast)
.map(CustomUserDetails::getUsername)
.map(UUID::fromString)
.flatMap(userPort::findById);
.map(CustomUserDetails::getUser);
}
private UserAuthenticationData generateAuthenticationData(User user) {

View File

@@ -0,0 +1,9 @@
package org.codiki.domain.traffic.exception;
import org.codiki.domain.exception.FunctionnalException;
public class TrafficTraceCreationException extends FunctionnalException {
public TrafficTraceCreationException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,14 @@
package org.codiki.domain.traffic.model;
import java.util.Arrays;
import java.util.Optional;
public enum HttpMethod {
GET, POST, PUT, DELETE;
public static Optional<HttpMethod> fromString(String methodAsString) {
return Arrays.stream(values())
.filter(method -> method.name().equals(methodAsString))
.findFirst();
}
}

View File

@@ -0,0 +1,6 @@
package org.codiki.domain.traffic.model;
public record TrafficEndpoint(
HttpMethod method,
String path
) {}

View File

@@ -0,0 +1,55 @@
package org.codiki.domain.traffic.model;
import java.time.ZonedDateTime;
import java.util.UUID;
public record TrafficTrace(
UUID id,
ZonedDateTime dateTime,
TrafficEndpoint endpoint,
UUID userId,
String correlationId
) {
public static Builder aTrafficTrace() {
return new Builder();
}
public static class Builder {
private UUID id;
private ZonedDateTime dateTime;
private TrafficEndpoint endpoint;
private UUID userId;
private String correlationId;
private Builder() {}
public Builder withId(UUID id) {
this.id = id;
return this;
}
public Builder withDateTime(ZonedDateTime dateTime) {
this.dateTime = dateTime;
return this;
}
public Builder withEndpoint(TrafficEndpoint endpoint) {
this.endpoint = endpoint;
return this;
}
public Builder withUserId(UUID userId) {
this.userId = userId;
return this;
}
public Builder withCorrelationId(String correlationId) {
this.correlationId = correlationId;
return this;
}
public TrafficTrace build() {
return new TrafficTrace(id, dateTime, endpoint, userId, correlationId);
}
}
}

View File

@@ -0,0 +1,14 @@
package org.codiki.domain.traffic.port;
import org.codiki.domain.traffic.model.TrafficTrace;
import java.time.ZonedDateTime;
import java.util.List;
public interface TrafficTracePort {
void save(TrafficTrace trace);
List<TrafficTrace> getAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate);
List<TrafficTrace> getAllByCorrelationId(String correlationId);
Integer countAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate);
Integer countByCorrelationId(String correlationId);
}

View File

@@ -25,6 +25,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
@@ -33,28 +37,5 @@
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
<!-- <artifactId>postgresql</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.security</groupId>-->
<!-- <artifactId>spring-security-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
</dependencies>
</project>

View File

@@ -0,0 +1,12 @@
package org.codiki.exposition.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAspectJAutoProxy
@EnableAsync
public class TrafficTraceConfiguration {
}

View File

@@ -38,10 +38,6 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
.filter(authorizationHeader -> !isEmpty(authorizationHeader))
.filter(authorizationHeader -> authorizationHeader.startsWith(BEARER_PREFIX))
.map(authorizationHeader -> authorizationHeader.substring(BEARER_PREFIX.length()))
.filter(token -> {
String authorizationHeader = request.getHeader(AUTHORIZATION);
return !isEmpty(authorizationHeader) && authorizationHeader.startsWith(BEARER_PREFIX);
})
.filter(jwtService::isValid)
.flatMap(jwtService::extractUser)
.map(CustomUserDetails::new)

View File

@@ -0,0 +1,75 @@
package org.codiki.exposition.traffic;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.codiki.application.traffic.TrafficTraceUseCases;
import org.codiki.application.user.UserUseCases;
import org.codiki.domain.traffic.model.HttpMethod;
import org.codiki.domain.traffic.model.TrafficEndpoint;
import org.codiki.domain.user.model.User;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.Optional;
import java.util.UUID;
@Component
@Aspect
public class ApiCallsLoggerAspect {
private static final String HTTP_HEADER_CORRELATION_ID = "x-correlation-id";
private final TrafficTraceUseCases trafficTraceUseCases;
private final UserUseCases userUseCases;
public ApiCallsLoggerAspect(
TrafficTraceUseCases trafficTraceUseCases,
UserUseCases userUseCases
) {
this.trafficTraceUseCases = trafficTraceUseCases;
this.userUseCases = userUseCases;
}
@Before("@annotation(org.springframework.web.bind.annotation.GetMapping)")
public void logGetApiCall(JoinPoint joinPoint) {
logApiCall();
}
@Before("@annotation(org.springframework.web.bind.annotation.PostMapping)")
public void logPostApiCall(JoinPoint joinPoint) {
logApiCall();
}
@Before("@annotation(org.springframework.web.bind.annotation.PutMapping)")
public void logPutApiCall(JoinPoint joinPoint) {
logApiCall();
}
@Before("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
public void logDeleteApiCall(JoinPoint joinPoint) {
logApiCall();
}
private void logApiCall() {
getHttpServletRequest().ifPresent(request ->
Optional.of(request.getMethod())
.flatMap(HttpMethod::fromString)
.ifPresent(queryHttpMethod -> {
String queryUriPath = request.getRequestURI();
TrafficEndpoint endpoint = new TrafficEndpoint(queryHttpMethod, queryUriPath);
UUID userId = userUseCases.getAuthenticatedUser()
.map(User::id)
.orElse(null);
String correlationId = request.getHeader(HTTP_HEADER_CORRELATION_ID);
trafficTraceUseCases.saveNewTrace(endpoint, userId, correlationId);
})
);
}
private static Optional<HttpServletRequest> getHttpServletRequest() {
return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
.filter(ServletRequestAttributes.class::isInstance)
.map(ServletRequestAttributes.class::cast)
.map(ServletRequestAttributes::getRequest);
}
}

View File

@@ -0,0 +1,45 @@
package org.codiki.infrastructure.traffic;
import org.codiki.domain.traffic.model.TrafficTrace;
import org.codiki.domain.traffic.port.TrafficTracePort;
import org.codiki.infrastructure.traffic.model.TrafficTraceEntity;
import org.codiki.infrastructure.traffic.repository.TrafficTraceEntityJpaRepository;
import org.springframework.stereotype.Component;
import java.time.ZonedDateTime;
import java.util.List;
@Component
public class TrafficTraceJpaAdapter implements TrafficTracePort {
private final TrafficTraceEntityJpaRepository repository;
public TrafficTraceJpaAdapter(TrafficTraceEntityJpaRepository repository) {
this.repository = repository;
}
@Override
public void save(TrafficTrace trace) {
TrafficTraceEntity entity = new TrafficTraceEntity(trace);
repository.save(entity);
}
@Override
public List<TrafficTrace> getAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate) {
return List.of();
}
@Override
public List<TrafficTrace> getAllByCorrelationId(String correlationId) {
return List.of();
}
@Override
public Integer countAllInPeriod(ZonedDateTime startDate, ZonedDateTime endDate) {
return 0;
}
@Override
public Integer countByCorrelationId(String correlationId) {
return 0;
}
}

View File

@@ -0,0 +1,25 @@
package org.codiki.infrastructure.traffic.model;
import org.codiki.domain.traffic.model.HttpMethod;
public enum HttpMethodEntity {
GET, POST, PUT, DELETE;
public HttpMethod toDomain() {
return switch (this) {
case GET -> HttpMethod.GET;
case POST -> HttpMethod.POST;
case PUT -> HttpMethod.PUT;
case DELETE -> HttpMethod.DELETE;
};
}
public static HttpMethodEntity fromDomain(HttpMethod method) {
return switch (method) {
case HttpMethod.GET -> GET;
case HttpMethod.POST -> POST;
case HttpMethod.PUT -> PUT;
case HttpMethod.DELETE -> DELETE;
};
}
}

View File

@@ -0,0 +1,56 @@
package org.codiki.infrastructure.traffic.model;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.codiki.domain.traffic.model.TrafficEndpoint;
import org.codiki.domain.traffic.model.TrafficTrace;
import java.time.ZonedDateTime;
import java.util.UUID;
import static org.codiki.domain.traffic.model.TrafficTrace.aTrafficTrace;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "traffic")
public class TrafficTraceEntity {
@Id
private UUID id;
@Column(nullable = false)
private ZonedDateTime dateTime;
@Column(nullable = false)
@Enumerated
private HttpMethodEntity endpointMethod;
@Column(nullable = false)
private String endpointPath;
private UUID userId;
private String correlationId;
public TrafficTraceEntity(TrafficTrace trace) {
id = trace.id();
dateTime = trace.dateTime();
endpointMethod = HttpMethodEntity.fromDomain(trace.endpoint().method());
endpointPath = trace.endpoint().path();
userId = trace.userId();
correlationId = trace.correlationId();
}
public TrafficTrace toDomain() {
return aTrafficTrace()
.withId(id)
.withDateTime(dateTime)
.withEndpoint(new TrafficEndpoint(
endpointMethod.toDomain(),
endpointPath
))
.withUserId(userId)
.withCorrelationId(correlationId)
.build();
}
}

View File

@@ -0,0 +1,12 @@
package org.codiki.infrastructure.traffic.repository;
import org.codiki.infrastructure.traffic.model.TrafficTraceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.UUID;
@Repository
public interface TrafficTraceEntityJpaRepository extends JpaRepository<TrafficTraceEntity, UUID> {
}

View File

@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS traffic (
id UUID NOT NULL,
date_time TIMESTAMP WITH TIME ZONE NOT NULL,
endpoint_method SMALLINT NOT NULL,
endpoint_path VARCHAR NOT NULL,
user_id UUID,
correlation_id VARCHAR,
CONSTRAINT traffic_pk PRIMARY KEY (id),
CONSTRAINT traffic_user_id_fk FOREIGN KEY (user_id) REFERENCES "user" (id)
);
CREATE INDEX traffic_user_id_idx ON traffic (user_id);

View File

@@ -15,11 +15,11 @@
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
<jakarta.servlet-api.version>6.1.0</jakarta.servlet-api.version>
<java-jwt.version>4.4.0</java-jwt.version>
<postgresql.version>42.7.0</postgresql.version>
<tika-core.version>2.9.0</tika-core.version>
<commons-lang3.version>3.14.0</commons-lang3.version>
<postgresql.version>42.7.4</postgresql.version>
<commons-lang3.version>3.17.0</commons-lang3.version>
</properties>
<modules>
@@ -35,7 +35,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.0</version>
<version>3.3.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@@ -84,8 +84,6 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

File diff suppressed because it is too large Load Diff

View File

@@ -17,32 +17,34 @@
},
"private": true,
"dependencies": {
"@angular/animations": "^17.0.0",
"@angular/cdk": "^17.3.1",
"@angular/common": "^17.0.0",
"@angular/compiler": "^17.0.0",
"@angular/core": "^17.0.0",
"@angular/forms": "^17.0.0",
"@angular/material": "^17.3.1",
"@angular/platform-browser": "^17.0.0",
"@angular/platform-browser-dynamic": "^17.0.0",
"@angular/router": "^17.0.0",
"@angular/animations": "^18.2.5",
"@angular/cdk": "^18.2.5",
"@angular/common": "^18.2.5",
"@angular/compiler": "^18.2.5",
"@angular/core": "^18.2.5",
"@angular/forms": "^18.2.5",
"@angular/material": "^18.2.5",
"@angular/platform-browser": "^18.2.5",
"@angular/platform-browser-dynamic": "^18.2.5",
"@angular/router": "^18.2.5",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.2"
"uuid": "^10.0.0",
"zone.js": "~0.14.10"
},
"devDependencies": {
"@angular-devkit/build-angular": "^17.0.5",
"@angular/cli": "^17.0.5",
"@angular/compiler-cli": "^17.0.0",
"@angular/localize": "^17.3.12",
"@angular-devkit/build-angular": "^18.2.5",
"@angular/cli": "^18.2.5",
"@angular/compiler-cli": "^18.2.5",
"@angular/localize": "^18.2.5",
"@types/jasmine": "~5.1.0",
"@types/uuid": "^10.0.0",
"jasmine-core": "~5.1.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"typescript": "~5.2.2"
"typescript": "~5.5.4"
}
}

View File

@@ -5,6 +5,7 @@ import { HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi } from '@a
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
import { routes } from './app.routes';
import { JwtInterceptor } from './core/interceptor/jwt.interceptor';
import { CorrelationIdInterceptor } from './core/interceptor/correlation-id.interceptor';
export const appConfig: ApplicationConfig = {
providers: [
@@ -18,5 +19,6 @@ export const appConfig: ApplicationConfig = {
provideAnimationsAsync(),
provideHttpClient(withInterceptorsFromDi()),
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: CorrelationIdInterceptor, multi: true },
]
};

View File

@@ -0,0 +1,20 @@
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http";
import { inject, Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { CorrelationIdService } from "../service/correlation-id.service";
@Injectable()
export class CorrelationIdInterceptor implements HttpInterceptor {
private readonly correlationIdService = inject(CorrelationIdService);
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const correlationId = this.correlationIdService.getCorrelationId();
const requestWithCorrelationId = request.clone({
headers: request.headers.set('x-correlation-id', correlationId)
});
return next.handle(requestWithCorrelationId);
}
}

View File

@@ -0,0 +1,27 @@
import { Injectable } from "@angular/core";
import * as uuid from 'uuid';
const CORRELATION_ID_KEY = 'correlationId';
@Injectable({
providedIn: 'root'
})
export class CorrelationIdService {
getCorrelationId(): string {
let correlationId = this.getCorrelationFromLocalStorage();
if (correlationId === undefined) {
correlationId = this.createNewCorrelationId();
}
return correlationId;
}
private getCorrelationFromLocalStorage(): string | undefined {
return localStorage.getItem(CORRELATION_ID_KEY) ?? undefined;
}
private createNewCorrelationId(): string {
const newCorrelationId = uuid.v4();
localStorage.setItem(CORRELATION_ID_KEY, newCorrelationId);
return newCorrelationId;
}
}