-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add per-path request/response JSON body filters via logbook Strategy (#…
…68) * Add per-path request/response JSON body filters via logbook Strategy * Adjust readme * Rename parameter
- Loading branch information
Showing
28 changed files
with
828 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# logbook-filter | ||
Various request/response/body filters for Logbook: | ||
|
||
* max body size filter | ||
* max string field value body filter | ||
|
||
with | ||
|
||
* per-path request and/or response body filter selection based on path | ||
|
||
## JSON filtering | ||
|
||
``` | ||
@Bean | ||
public JsonOnlyMatchPathStrategy strategy() { | ||
return new JsonOnlyMatchPathStrategy(); | ||
} | ||
``` | ||
|
||
with request filter | ||
|
||
``` | ||
@Bean | ||
public RequestFilter myRequestFilter() { | ||
JacksonJsonFieldBodyFilter customerRequestFilter = new JacksonJsonFieldBodyFilter(Arrays.asList("name"), "XXX"); | ||
return RequestMatcherRequestFilter.newPathPrefixBuilder().withPathPrefixFilter("/api/customer", customerRequestFilter).build(); | ||
} | ||
``` | ||
|
||
and response filter | ||
|
||
``` | ||
@Bean | ||
public ResponseFilter myResponseFilter() { | ||
JacksonJsonFieldBodyFilter customerResponseFilter = new JacksonJsonFieldBodyFilter(Arrays.asList("rating"), "XXX"); | ||
return RequestResponseMatcherResponseFilter.newPathPrefixBuilder().withPathPrefixFilter("/api/customer", customerResponseFilter).build(); | ||
} | ||
``` | ||
|
||
### Advanced filtering | ||
Construct your own `PathFilterMatcher` to handle more complex filtering, i.e. | ||
|
||
* path | ||
* max size | ||
* origin | ||
|
||
using | ||
|
||
* SizePathFilterMatcher | ||
* SizeOriginPathFilterMatcher | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
dependencies { | ||
api("org.slf4j:slf4j-api:${slf4jVersion}") | ||
api("org.zalando:logbook-api:${logbookVersion}") | ||
api("org.zalando:logbook-api:${logbookVersion}") | ||
|
||
api("com.fasterxml.jackson.core:jackson-core:${jacksonVersion}") | ||
api("com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}") | ||
api "commons-io:commons-io:${commonsIoVersion}" | ||
|
||
// JUnit Jupiter API and TestEngine implementation | ||
testImplementation("org.junit.jupiter:junit-jupiter:${junitJupiterVersion}") | ||
|
||
testImplementation("org.mockito:mockito-core:${mockitoVersion}") | ||
|
||
testImplementation("com.google.truth:truth:${googleTruthVersion}") | ||
testImplementation("com.google.truth.extensions:truth-java8-extension:${googleTruthVersion}") | ||
|
||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
.../src/main/java/no/entur/logging/cloud/logbook/filter/path/BodyReplacementHttpRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package no.entur.logging.cloud.logbook.filter.path; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.zalando.logbook.ForwardingHttpRequest; | ||
import org.zalando.logbook.HttpRequest; | ||
|
||
public class BodyReplacementHttpRequest implements ForwardingHttpRequest { | ||
|
||
private final HttpRequest request; | ||
private final String replacement; | ||
|
||
public BodyReplacementHttpRequest(HttpRequest request, String replacement) { | ||
super(); | ||
this.request = request; | ||
this.replacement = replacement; | ||
} | ||
|
||
@Override | ||
public HttpRequest delegate() { | ||
return request; | ||
} | ||
|
||
@Override | ||
public HttpRequest withBody() { | ||
return withoutBody(); | ||
} | ||
|
||
@Override | ||
public HttpRequest withoutBody() { | ||
return new BodyReplacementHttpRequest(request.withoutBody(), replacement); | ||
} | ||
|
||
@Override | ||
public byte[] getBody() { | ||
return replacement.getBytes(StandardCharsets.UTF_8); | ||
} | ||
|
||
@Override | ||
public String getBodyAsString() { | ||
return replacement; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...src/main/java/no/entur/logging/cloud/logbook/filter/path/BodyReplacementHttpResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package no.entur.logging.cloud.logbook.filter.path; | ||
import org.zalando.logbook.ForwardingHttpResponse; | ||
import org.zalando.logbook.HttpResponse; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
public class BodyReplacementHttpResponse implements ForwardingHttpResponse, HttpResponse { | ||
|
||
private final HttpResponse response; | ||
private final String replacement; | ||
|
||
public BodyReplacementHttpResponse(HttpResponse response, String replacement) { | ||
super(); | ||
this.response = response; | ||
this.replacement = replacement; | ||
} | ||
|
||
@Override | ||
public HttpResponse delegate() { | ||
return response; | ||
} | ||
|
||
@Override | ||
public HttpResponse withBody() { | ||
return withoutBody(); | ||
} | ||
|
||
@Override | ||
public HttpResponse withoutBody() { | ||
return new BodyReplacementHttpResponse(response.withoutBody(), replacement); | ||
} | ||
|
||
@Override | ||
public byte[] getBody() { | ||
return replacement.getBytes(UTF_8); | ||
} | ||
|
||
@Override | ||
public String getBodyAsString() { | ||
return replacement; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...ava/no/entur/logging/cloud/logbook/filter/path/PathPrefixRequestMatcherRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package no.entur.logging.cloud.logbook.filter.path; | ||
|
||
import no.entur.logging.cloud.logbook.filter.path.matcher.MatcherPathFilterCollection; | ||
import no.entur.logging.cloud.logbook.filter.path.matcher.PathFilterMatcher; | ||
import no.entur.logging.cloud.logbook.filter.path.matcher.SimplePathFilterMatcher; | ||
import org.zalando.logbook.BodyFilter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PathPrefixRequestMatcherRequestFilter { | ||
|
||
private Map<String, BodyFilter> pathPrefixFilters = new HashMap<>(); | ||
|
||
public PathPrefixRequestMatcherRequestFilter withPathPrefixFilter(String path, BodyFilter bodyFilter) { | ||
pathPrefixFilters.put(path, bodyFilter); | ||
return this; | ||
} | ||
|
||
public RequestMatcherRequestFilter build() { | ||
List<PathFilterMatcher> matchers = new ArrayList<>(pathPrefixFilters.size()); | ||
|
||
for (Map.Entry<String, BodyFilter> entry : pathPrefixFilters.entrySet()) { | ||
|
||
SimplePathFilterMatcher matcher = new SimplePathFilterMatcher( (path) -> path != null && path.startsWith(entry.getKey()), entry.getValue()); | ||
|
||
matchers.add(matcher); | ||
} | ||
MatcherPathFilterCollection collection = new MatcherPathFilterCollection(matchers); | ||
return new RequestMatcherRequestFilter(collection); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...ging/cloud/logbook/filter/path/PathPrefixRequestResponseMatcherResponseFilterBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package no.entur.logging.cloud.logbook.filter.path; | ||
|
||
import no.entur.logging.cloud.logbook.filter.path.matcher.MatcherPathFilterCollection; | ||
import no.entur.logging.cloud.logbook.filter.path.matcher.PathFilterMatcher; | ||
import no.entur.logging.cloud.logbook.filter.path.matcher.SimplePathFilterMatcher; | ||
import org.zalando.logbook.BodyFilter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PathPrefixRequestResponseMatcherResponseFilterBuilder { | ||
|
||
private Map<String, BodyFilter> pathPrefixFilters = new HashMap<>(); | ||
|
||
public PathPrefixRequestResponseMatcherResponseFilterBuilder withPathPrefixFilter(String path, BodyFilter bodyFilter) { | ||
pathPrefixFilters.put(path, bodyFilter); | ||
return this; | ||
} | ||
|
||
public RequestResponseMatcherResponseFilter build() { | ||
List<PathFilterMatcher> matchers = new ArrayList<>(pathPrefixFilters.size()); | ||
|
||
for (Map.Entry<String, BodyFilter> entry : pathPrefixFilters.entrySet()) { | ||
|
||
SimplePathFilterMatcher matcher = new SimplePathFilterMatcher( (path) -> path != null && path.startsWith(entry.getKey()), entry.getValue()); | ||
|
||
matchers.add(matcher); | ||
} | ||
MatcherPathFilterCollection collection = new MatcherPathFilterCollection(matchers); | ||
return new RequestResponseMatcherResponseFilter(collection); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...ter/src/main/java/no/entur/logging/cloud/logbook/filter/path/RequestFilterCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package no.entur.logging.cloud.logbook.filter.path; | ||
|
||
import org.zalando.logbook.BodyFilter; | ||
import org.zalando.logbook.HttpRequest; | ||
|
||
/** | ||
* | ||
* Interface for returning the proper {@link BodyFilter} for a given request path. <br><br> | ||
* | ||
* | ||
*/ | ||
|
||
public interface RequestFilterCollection { | ||
|
||
BodyFilter getBodyFilter(HttpRequest request, int size); | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...src/main/java/no/entur/logging/cloud/logbook/filter/path/RequestMatcherRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package no.entur.logging.cloud.logbook.filter.path; | ||
|
||
import java.io.IOException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.zalando.logbook.BodyFilter; | ||
import org.zalando.logbook.HttpRequest; | ||
import org.zalando.logbook.RequestFilter; | ||
|
||
public class RequestMatcherRequestFilter implements RequestFilter { | ||
|
||
private final static Logger log = LoggerFactory.getLogger(RequestMatcherRequestFilter.class); | ||
|
||
public static PathPrefixRequestMatcherRequestFilter newPathPrefixBuilder() { | ||
return new PathPrefixRequestMatcherRequestFilter(); | ||
} | ||
|
||
private final RequestFilterCollection filter; | ||
|
||
public RequestMatcherRequestFilter(RequestFilterCollection filter) { | ||
this.filter = filter; | ||
} | ||
|
||
@Override | ||
public HttpRequest filter(HttpRequest request) { | ||
try { | ||
String body = request.getBodyAsString(); | ||
|
||
if(body == null || body.length() == 0) { | ||
return request; | ||
} | ||
|
||
BodyFilter f = filter.getBodyFilter(request, body.length()); | ||
|
||
if(f != null) { | ||
String filtered = f.filter(request.getContentType(), body); | ||
if(filtered != null) { | ||
return new BodyReplacementHttpRequest(request, filtered); | ||
} | ||
} | ||
} catch (IOException e) { | ||
log.warn("Problem filtering request body", e); | ||
} | ||
|
||
return request; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...main/java/no/entur/logging/cloud/logbook/filter/path/RequestResponseFilterCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package no.entur.logging.cloud.logbook.filter.path; | ||
|
||
import org.zalando.logbook.BodyFilter; | ||
import org.zalando.logbook.HttpRequest; | ||
import org.zalando.logbook.HttpResponse; | ||
|
||
/** | ||
* | ||
* Interface for returning the proper {@link BodyFilter} for a given response path. <br><br> | ||
* | ||
*/ | ||
|
||
public interface RequestResponseFilterCollection { | ||
|
||
BodyFilter getBodyFilter(HttpRequest request, HttpResponse httpResponse, int size); | ||
|
||
} |
Oops, something went wrong.