Implementation of jpa saving for traffic traces.
This commit is contained in:
@@ -25,6 +25,10 @@ public class CustomUserDetails implements UserDetails {
|
|||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
return user.id().toString();
|
return user.id().toString();
|
||||||
|
|||||||
@@ -26,7 +26,11 @@ public class TrafficTraceUseCases {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
public void saveNewTrace(TrafficEndpoint trafficEndpoint, @Nullable String correlationId) {
|
public void saveNewTrace(
|
||||||
|
TrafficEndpoint trafficEndpoint,
|
||||||
|
@Nullable UUID userId,
|
||||||
|
@Nullable String correlationId
|
||||||
|
) {
|
||||||
if (isNull(trafficEndpoint)) {
|
if (isNull(trafficEndpoint)) {
|
||||||
throw new TrafficTraceCreationException("Traffic endpoint should not be null.");
|
throw new TrafficTraceCreationException("Traffic endpoint should not be null.");
|
||||||
}
|
}
|
||||||
@@ -35,6 +39,7 @@ public class TrafficTraceUseCases {
|
|||||||
.withId(UUID.randomUUID())
|
.withId(UUID.randomUUID())
|
||||||
.withDateTime(ZonedDateTime.now(clock))
|
.withDateTime(ZonedDateTime.now(clock))
|
||||||
.withEndpoint(trafficEndpoint)
|
.withEndpoint(trafficEndpoint)
|
||||||
|
.withUserId(userId)
|
||||||
.withCorrelationId(correlationId)
|
.withCorrelationId(correlationId)
|
||||||
.build();
|
.build();
|
||||||
trafficTracePort.save(newTrace);
|
trafficTracePort.save(newTrace);
|
||||||
|
|||||||
@@ -87,9 +87,7 @@ public class UserUseCases {
|
|||||||
.map(Authentication::getPrincipal)
|
.map(Authentication::getPrincipal)
|
||||||
.filter(CustomUserDetails.class::isInstance)
|
.filter(CustomUserDetails.class::isInstance)
|
||||||
.map(CustomUserDetails.class::cast)
|
.map(CustomUserDetails.class::cast)
|
||||||
.map(CustomUserDetails::getUsername)
|
.map(CustomUserDetails::getUser);
|
||||||
.map(UUID::fromString)
|
|
||||||
.flatMap(userPort::findById);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserAuthenticationData generateAuthenticationData(User user) {
|
private UserAuthenticationData generateAuthenticationData(User user) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ public record TrafficTrace(
|
|||||||
UUID id,
|
UUID id,
|
||||||
ZonedDateTime dateTime,
|
ZonedDateTime dateTime,
|
||||||
TrafficEndpoint endpoint,
|
TrafficEndpoint endpoint,
|
||||||
|
UUID userId,
|
||||||
String correlationId
|
String correlationId
|
||||||
) {
|
) {
|
||||||
public static Builder aTrafficTrace() {
|
public static Builder aTrafficTrace() {
|
||||||
@@ -17,6 +18,7 @@ public record TrafficTrace(
|
|||||||
private UUID id;
|
private UUID id;
|
||||||
private ZonedDateTime dateTime;
|
private ZonedDateTime dateTime;
|
||||||
private TrafficEndpoint endpoint;
|
private TrafficEndpoint endpoint;
|
||||||
|
private UUID userId;
|
||||||
private String correlationId;
|
private String correlationId;
|
||||||
|
|
||||||
private Builder() {}
|
private Builder() {}
|
||||||
@@ -36,13 +38,18 @@ public record TrafficTrace(
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder withUserId(UUID userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Builder withCorrelationId(String correlationId) {
|
public Builder withCorrelationId(String correlationId) {
|
||||||
this.correlationId = correlationId;
|
this.correlationId = correlationId;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TrafficTrace build() {
|
public TrafficTrace build() {
|
||||||
return new TrafficTrace(id, dateTime, endpoint, correlationId);
|
return new TrafficTrace(id, dateTime, endpoint, userId, correlationId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,10 +38,6 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
.filter(authorizationHeader -> !isEmpty(authorizationHeader))
|
.filter(authorizationHeader -> !isEmpty(authorizationHeader))
|
||||||
.filter(authorizationHeader -> authorizationHeader.startsWith(BEARER_PREFIX))
|
.filter(authorizationHeader -> authorizationHeader.startsWith(BEARER_PREFIX))
|
||||||
.map(authorizationHeader -> authorizationHeader.substring(BEARER_PREFIX.length()))
|
.map(authorizationHeader -> authorizationHeader.substring(BEARER_PREFIX.length()))
|
||||||
.filter(token -> {
|
|
||||||
String authorizationHeader = request.getHeader(AUTHORIZATION);
|
|
||||||
return !isEmpty(authorizationHeader) && authorizationHeader.startsWith(BEARER_PREFIX);
|
|
||||||
})
|
|
||||||
.filter(jwtService::isValid)
|
.filter(jwtService::isValid)
|
||||||
.flatMap(jwtService::extractUser)
|
.flatMap(jwtService::extractUser)
|
||||||
.map(CustomUserDetails::new)
|
.map(CustomUserDetails::new)
|
||||||
|
|||||||
@@ -1,16 +1,20 @@
|
|||||||
package org.codiki.exposition.traffic;
|
package org.codiki.exposition.traffic;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.annotation.Before;
|
||||||
import org.codiki.application.traffic.TrafficTraceUseCases;
|
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.HttpMethod;
|
||||||
import org.codiki.domain.traffic.model.TrafficEndpoint;
|
import org.codiki.domain.traffic.model.TrafficEndpoint;
|
||||||
|
import org.codiki.domain.user.model.User;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Aspect
|
@Aspect
|
||||||
@@ -18,9 +22,14 @@ public class ApiCallsLoggerAspect {
|
|||||||
private static final String HTTP_HEADER_CORRELATION_ID = "x-correlation-id";
|
private static final String HTTP_HEADER_CORRELATION_ID = "x-correlation-id";
|
||||||
|
|
||||||
private final TrafficTraceUseCases trafficTraceUseCases;
|
private final TrafficTraceUseCases trafficTraceUseCases;
|
||||||
|
private final UserUseCases userUseCases;
|
||||||
|
|
||||||
public ApiCallsLoggerAspect(TrafficTraceUseCases trafficTraceUseCases) {
|
public ApiCallsLoggerAspect(
|
||||||
|
TrafficTraceUseCases trafficTraceUseCases,
|
||||||
|
UserUseCases userUseCases
|
||||||
|
) {
|
||||||
this.trafficTraceUseCases = trafficTraceUseCases;
|
this.trafficTraceUseCases = trafficTraceUseCases;
|
||||||
|
this.userUseCases = userUseCases;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before("@annotation(org.springframework.web.bind.annotation.GetMapping)")
|
@Before("@annotation(org.springframework.web.bind.annotation.GetMapping)")
|
||||||
@@ -42,19 +51,25 @@ public class ApiCallsLoggerAspect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void logApiCall() {
|
private void logApiCall() {
|
||||||
Optional.ofNullable(RequestContextHolder.getRequestAttributes())
|
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)
|
.filter(ServletRequestAttributes.class::isInstance)
|
||||||
.map(ServletRequestAttributes.class::cast)
|
.map(ServletRequestAttributes.class::cast)
|
||||||
.map(ServletRequestAttributes::getRequest)
|
.map(ServletRequestAttributes::getRequest);
|
||||||
.ifPresent(request ->
|
|
||||||
Optional.of(request.getMethod())
|
|
||||||
.flatMap(HttpMethod::fromString)
|
|
||||||
.ifPresent(queryHttpMethod -> {
|
|
||||||
String queryUriPath = request.getRequestURI();
|
|
||||||
String correlationId = request.getHeader(HTTP_HEADER_CORRELATION_ID);
|
|
||||||
TrafficEndpoint endpoint = new TrafficEndpoint(queryHttpMethod, queryUriPath);
|
|
||||||
trafficTraceUseCases.saveNewTrace(endpoint, correlationId);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,19 +2,25 @@ package org.codiki.infrastructure.traffic;
|
|||||||
|
|
||||||
import org.codiki.domain.traffic.model.TrafficTrace;
|
import org.codiki.domain.traffic.model.TrafficTrace;
|
||||||
import org.codiki.domain.traffic.port.TrafficTracePort;
|
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 org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class TrafficTraceInMemoryAdapter implements TrafficTracePort {
|
public class TrafficTraceJpaAdapter implements TrafficTracePort {
|
||||||
private final List<TrafficTrace> traces = new ArrayList<>();
|
private final TrafficTraceEntityJpaRepository repository;
|
||||||
|
|
||||||
|
public TrafficTraceJpaAdapter(TrafficTraceEntityJpaRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(TrafficTrace trace) {
|
public void save(TrafficTrace trace) {
|
||||||
traces.add(trace);
|
TrafficTraceEntity entity = new TrafficTraceEntity(trace);
|
||||||
|
repository.save(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
Reference in New Issue
Block a user