diff --git a/README.md b/README.md index 0aee313..0d4340d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ This library fulfills its role of auditing every HTTP request sent to or receive - [Intercepting http requests made by clients](#active-requests) to save in database ou obtain specific infos. - [Intercepting http requests received by your service](#passive-requests) to save in database ou obtain specific infos. -## HOW TO USE +## HOW TO USE WITH QUARKUS Import the most recent version in your `pom.xml`: @@ -32,11 +32,178 @@ Import the most recent version in your `pom.xml`: ## Logging Requests -Just inject the `LoggingFilter` provider +Just inject the `LoggingFilter` provider in your client interface with `@RegisterProvider`: + +```java +@RegisterProvider( LoggingFilter.class ) +``` + +E.g.: + +```java +... +@Path( "/my-api" ) +@RegisterProvider( LoggingFilter.class ) +public interface IAPIClient { + + @GET + List< Something > listSomething( ); +... +``` ## Active Requests +Extends `ClientFilter` and `@Override` the `auditRequestRespons` method: + +```java +public class MyAPIClientFilter extends ClientFilter { + + @Override + public void auditRequestRespons( RequestDTO request, ResponseDTO response ) { + // WHATEVER YOU WANT FROM REQ OR RES + } +} +``` + +E.g. persisting request/response in database as an audition entity: + +```java +import java.util.List; +import br.com.potio.core.dto.AuditionDTO; +import br.com.potio.core.dto.RequestDTO; +import br.com.potio.core.dto.ResponseDTO; +import br.com.potio.http_auditor.ClientFilter; +... + +@JBossLog +public class MyAPIClientFilter extends ClientFilter { + + @Inject + PersistAudition persistAudition; + + @Override + public void auditRequestRespons( RequestDTO request, ResponseDTO response ) { + log.infov( "Saving Request and Response from {0}", request.getMethodName() + " - " + request.getUrl() ); + List< String > originActionHeader = request.getHeaders().get( ClientFilter.HEADER_ORIGIN_ACTION ); + String[] originActions = originActionHeader.get( 0 ).split( "-" ); + String method = originActions[ 0 ]; + String birCode = originActions[ 1 ]; + AuditionDTO audition = AuditionDTO.builder() + .withIdAccount( birCode ) + .withDescription( method ) + .withRequestBody( request.getBody() ) + .withRequestMethod( request.getMethodName() ) + .withHeaders( request.getHeaders().toString() ) + .withRequestUrl( request.getUrl() ) + .withResponseBody( response.getBody() ) + .withResponseStatus( response.getStatus() ) + .withTookSeconds( response.getTookSeconds() ) + .build(); + this.persistAudition.publish( audition ); + } + +} +``` + +Do this to each http client you need or want. + +Inject `MyAPIClientFilter` provider in your client interface with `@RegisterProvider`: + +```java +@RegisterProvider( MyAPIClientFilter.class ) +``` + +E.g. using both myFilter and logging filter: + +```java +... +@Path( "/my-api" ) +@RegisterProviders( { + @RegisterProvider( LoggingFilter.class ), + @RegisterProvider( MyAPIClientFilter.class ) } ) +public interface IAPIClient { + + @GET + List< Something > listSomething( ); +... +``` + ## Passive Requests +Very similar to active request, but most of the time with less infos. + +Extends `ServerFilter` and `@Override` the `auditRequestRespons` method: + +```java +public class MyServiceFilter extends ServerFilter { + + @Override + public void auditRequestRespons( RequestDTO request, ResponseDTO response ) { + // WHATEVER YOU WANT FROM REQ OR RES + } +} +``` + +E.g. persisting request/response in database as an audition entity: + +```java +import java.util.List; +import br.com.potio.core.dto.AuditionDTO; +import br.com.potio.core.dto.RequestDTO; +import br.com.potio.core.dto.ResponseDTO; +import br.com.potio.http_auditor.ClientFilter; +... + +public class ConnectorServerFilter extends ServerFilter { + + @Inject + PersistAudition persistAudition; + + @Override + public void auditRequestRespons( RequestDTO request, ResponseDTO response ) { + AuditionDTO audition = AuditionDTO.builder() + .withRequestBody( request.getBody() ) + .withRequestMethod( request.getMethodName() ) + .withRequestUrl( request.getUrl() ) + .withHeaders( request.getHeaders().toString() ) + .withResponseBody( response.getBody() ) + .withResponseStatus( response.getStatus() ) + .withTookSeconds( response.getTookSeconds() ) + .build(); + this.persistAudition.publish( audition ); + } + +} +``` + +Do this to each http client you need or want. + +Inject `ConnectorServerFilter` provider in your client interface with `@RegisterProvider`: + +```java +@RegisterProvider( ConnectorServerFilter.class ) +``` + +E.g. using both custom filter in entry endpoints: + +```java +... +@Path( "/api/user" ) +@RolesAllowed( "ADMIN" ) +@SecurityScheme( securitySchemeName = "Basic Auth", type = SecuritySchemeType.HTTP, scheme = "basic" ) +@SecurityRequirement( name = "Basic Auth" ) +@RegisterProviders( @RegisterProvider( ConnectorServerFilter.class ) ) +public class UserResource { + + @POST + public Response createUser( @RequestBody User user ) { + return Response.ok().build(); + } + +} +... +``` + + diff --git a/pom.xml b/pom.xml index 0678ce8..5fc0dcd 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 br.com.potio http-auditor - 0.2.0 + 0.3.0 jar diff --git a/src/main/java/br/com/potio/http_auditor/ClientFilter.java b/src/main/java/br/com/potio/http_auditor/ClientFilter.java index 4a7e0c6..3ffcb12 100644 --- a/src/main/java/br/com/potio/http_auditor/ClientFilter.java +++ b/src/main/java/br/com/potio/http_auditor/ClientFilter.java @@ -41,7 +41,7 @@ public abstract class ClientFilter implements ClientRequestFilter, ClientRespons private static final ZoneId DEFAULT_ZONE_ID = ZoneId.of( "America/Sao_Paulo" ); protected static final String HEADER_ORIGIN_ACTION = "origin-action"; - public void persistAudition( RequestDTO request, ResponseDTO response ) { + public void auditRequestResponse( RequestDTO request, ResponseDTO response ) { throw new UnsupportedOperationException( "Persist Audition Not Implemented" ); } @@ -58,7 +58,7 @@ public void filter( ClientRequestContext requestContext, ClientResponseContext r tookSeconds = TimeUnit.MILLISECONDS.toSeconds( tookSeconds ); } response.setTookSeconds( Objects.isNull( tookSeconds ) ? tookSeconds + "s" : "< 1s" ); - this.persistAudition( request, response ); + this.auditRequestResponse( request, response ); } catch ( IOException | ParseException e) { ClientFilter.logger.log( Level.SEVERE, "Error while intercepting client requests", e ); } diff --git a/src/main/java/br/com/potio/http_auditor/ServerFilter.java b/src/main/java/br/com/potio/http_auditor/ServerFilter.java index 2ccb741..a7ad481 100644 --- a/src/main/java/br/com/potio/http_auditor/ServerFilter.java +++ b/src/main/java/br/com/potio/http_auditor/ServerFilter.java @@ -40,7 +40,7 @@ public abstract class ServerFilter implements ContainerRequestFilter, ContainerR private static final String DATE_PATTERN = "EEE MMM d HH:mm:ss yyyy"; protected static final ZoneId DEFAULT_ZONE_ID = ZoneId.of( "America/Sao_Paulo" ); - public void persistAudition( RequestDTO request, ResponseDTO response ) { + public void auditRequestResponse( RequestDTO request, ResponseDTO response ) { throw new UnsupportedOperationException( "Persist Audition Not Implemented" ); } @@ -74,7 +74,7 @@ public void filter( ContainerRequestContext requestContext, ContainerResponseCon } response.setTookSeconds( Objects.isNull( tookSeconds ) ? tookSeconds + "s" : "< 1s" ); - this.persistAudition( request, response ); + this.auditRequestResponse( request, response ); } catch ( IOException | ParseException e) { ServerFilter.logger.log( Level.SEVERE, "Error while intercepting client requests", e ); }