Skip to content

Commit

Permalink
docs: use case and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanleonhardt committed Apr 16, 2024
1 parent 292fef3 commit 3f2813e
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 7 deletions.
171 changes: 169 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand All @@ -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();
}

}
...
```




2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.potio</groupId>
<artifactId>http-auditor</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
<packaging>jar</packaging>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/br/com/potio/http_auditor/ClientFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
}

Expand All @@ -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 );
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/br/com/potio/http_auditor/ServerFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
}

Expand Down Expand Up @@ -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 );
}
Expand Down

0 comments on commit 3f2813e

Please sign in to comment.