Skip to content

Commit 36a60b1

Browse files
authored
[client] Add SuppressLogging Annotation (#637)
1 parent da23868 commit 36a60b1

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.avaje.http.api;
2+
3+
import static java.lang.annotation.RetentionPolicy.SOURCE;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* For this client request suppress payload logging.
11+
*
12+
* <p>Used when the payload contains sensitive content and the request and response content should
13+
* be suppressed.
14+
*
15+
* <pre>{@code
16+
* @Client
17+
* interface CustomerApi {
18+
* ...
19+
* @Get("/{id}")
20+
* @SupressLogging
21+
* Customer getById(long id);
22+
*
23+
* @Post
24+
* @SupressLogging
25+
* long save(Customer customer);
26+
* }
27+
*
28+
* }</pre>
29+
*/
30+
@Retention(SOURCE)
31+
@Target({ElementType.TYPE, ElementType.METHOD})
32+
public @interface SupressLogging {}

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import javax.lang.model.type.TypeKind;
1919
import javax.lang.model.util.ElementFilter;
2020

21+
import io.avaje.http.api.SupressLogging;
2122
import io.avaje.http.generator.core.APContext;
2223
import io.avaje.http.generator.core.Append;
2324
import io.avaje.http.generator.core.BeanParamReader;
@@ -31,13 +32,14 @@
3132
import io.avaje.http.generator.core.RequestTimeoutPrism;
3233
import io.avaje.http.generator.core.Util;
3334
import io.avaje.http.generator.core.WebMethod;
35+
import io.avaje.prism.GeneratePrism;
3436
import io.avaje.prism.GenerateUtils;
35-
import io.avaje.http.generator.client.UType;
3637

3738
/**
3839
* Write code to register Web route for a given controller method.
3940
*/
4041
@GenerateUtils
42+
@GeneratePrism(SupressLogging.class)
4143
final class ClientMethodWriter {
4244
private static final KnownResponse KNOWN_RESPONSE = new KnownResponse();
4345
private static final String BODY_HANDLER = "java.net.http.HttpResponse.BodyHandler";
@@ -59,6 +61,7 @@ final class ClientMethodWriter {
5961
private final Map<String, String> segmentPropertyMap;
6062
private final Set<String> propertyConstants;
6163
private final List<Entry<String, String>> presetHeaders;
64+
private boolean suppressLogging;
6265

6366
ClientMethodWriter(MethodReader method, Append writer, Set<String> propertyConstants) {
6467
this.method = method;
@@ -67,7 +70,9 @@ final class ClientMethodWriter {
6770
this.returnType = UType.parse(method.returnType());
6871
this.timeout = method.timeout();
6972
this.useConfig = ProcessingContext.typeElement("io.avaje.config.Config") != null;
70-
73+
this.suppressLogging =
74+
SupressLoggingPrism.isPresent(method.element())
75+
|| SupressLoggingPrism.isPresent(method.element().getEnclosingElement());
7176
this.segmentPropertyMap =
7277
method.pathSegments().segments().stream()
7378
.filter(Segment::isProperty)
@@ -180,6 +185,9 @@ void write() {
180185
PathSegments pathSegments = method.pathSegments();
181186
Set<PathSegments.Segment> segments = pathSegments.segments();
182187

188+
if (suppressLogging) {
189+
writer.append(" .suppressLogging()").eol();
190+
}
183191
writeHeaders();
184192
writePaths(segments);
185193
writeQueryParams(pathSegments);

http-generator-client/src/test/java/io/avaje/http/generator/client/clients/TitanFall.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import io.avaje.http.api.Client;
77
import io.avaje.http.api.Get;
88
import io.avaje.http.api.Headers;
9+
import io.avaje.http.api.SupressLogging;
910

1011
@Client
12+
@SupressLogging
1113
@Headers("Content-Type: applicaton/json")
1214
public interface TitanFall {
1315

tests/test-client/src/main/java/example/github/Simple.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
import io.avaje.http.api.Client;
66
import io.avaje.http.api.Get;
7+
import io.avaje.http.api.SupressLogging;
78
import io.avaje.http.client.HttpException;
89

910
@Client
1011
public interface Simple {
1112

13+
@SupressLogging
1214
@Get("users/{user}/repos")
1315
List<Repo> listRepos(String user, String other) throws HttpException;
1416
}

0 commit comments

Comments
 (0)