diff --git a/http-api/src/main/java/io/avaje/http/api/SupressLogging.java b/http-api/src/main/java/io/avaje/http/api/SupressLogging.java new file mode 100644 index 000000000..8741e1e57 --- /dev/null +++ b/http-api/src/main/java/io/avaje/http/api/SupressLogging.java @@ -0,0 +1,32 @@ +package io.avaje.http.api; + +import static java.lang.annotation.RetentionPolicy.SOURCE; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * For this client request suppress payload logging. + * + *

Used when the payload contains sensitive content and the request and response content should + * be suppressed. + * + *

{@code
+ * @Client
+ * interface CustomerApi {
+ *   ...
+ *   @Get("/{id}")
+ *   @SupressLogging
+ *   Customer getById(long id);
+ *
+ *   @Post
+ *   @SupressLogging
+ *   long save(Customer customer);
+ * }
+ *
+ * }
+ */ +@Retention(SOURCE) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface SupressLogging {} diff --git a/http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java b/http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java index 2e89bcf9b..2184a82d4 100644 --- a/http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java +++ b/http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java @@ -18,6 +18,7 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.util.ElementFilter; +import io.avaje.http.api.SupressLogging; import io.avaje.http.generator.core.APContext; import io.avaje.http.generator.core.Append; import io.avaje.http.generator.core.BeanParamReader; @@ -31,13 +32,14 @@ import io.avaje.http.generator.core.RequestTimeoutPrism; import io.avaje.http.generator.core.Util; import io.avaje.http.generator.core.WebMethod; +import io.avaje.prism.GeneratePrism; import io.avaje.prism.GenerateUtils; -import io.avaje.http.generator.client.UType; /** * Write code to register Web route for a given controller method. */ @GenerateUtils +@GeneratePrism(SupressLogging.class) final class ClientMethodWriter { private static final KnownResponse KNOWN_RESPONSE = new KnownResponse(); private static final String BODY_HANDLER = "java.net.http.HttpResponse.BodyHandler"; @@ -59,6 +61,7 @@ final class ClientMethodWriter { private final Map segmentPropertyMap; private final Set propertyConstants; private final List> presetHeaders; + private boolean suppressLogging; ClientMethodWriter(MethodReader method, Append writer, Set propertyConstants) { this.method = method; @@ -67,7 +70,9 @@ final class ClientMethodWriter { this.returnType = UType.parse(method.returnType()); this.timeout = method.timeout(); this.useConfig = ProcessingContext.typeElement("io.avaje.config.Config") != null; - + this.suppressLogging = + SupressLoggingPrism.isPresent(method.element()) + || SupressLoggingPrism.isPresent(method.element().getEnclosingElement()); this.segmentPropertyMap = method.pathSegments().segments().stream() .filter(Segment::isProperty) @@ -180,6 +185,9 @@ void write() { PathSegments pathSegments = method.pathSegments(); Set segments = pathSegments.segments(); + if (suppressLogging) { + writer.append(" .suppressLogging()").eol(); + } writeHeaders(); writePaths(segments); writeQueryParams(pathSegments); diff --git a/http-generator-client/src/test/java/io/avaje/http/generator/client/clients/TitanFall.java b/http-generator-client/src/test/java/io/avaje/http/generator/client/clients/TitanFall.java index 0e99abe50..c3dc6c9f9 100644 --- a/http-generator-client/src/test/java/io/avaje/http/generator/client/clients/TitanFall.java +++ b/http-generator-client/src/test/java/io/avaje/http/generator/client/clients/TitanFall.java @@ -6,8 +6,10 @@ import io.avaje.http.api.Client; import io.avaje.http.api.Get; import io.avaje.http.api.Headers; +import io.avaje.http.api.SupressLogging; @Client +@SupressLogging @Headers("Content-Type: applicaton/json") public interface TitanFall { diff --git a/tests/test-client/src/main/java/example/github/Simple.java b/tests/test-client/src/main/java/example/github/Simple.java index ad1a1667a..ee8fd1e9b 100644 --- a/tests/test-client/src/main/java/example/github/Simple.java +++ b/tests/test-client/src/main/java/example/github/Simple.java @@ -4,11 +4,13 @@ import io.avaje.http.api.Client; import io.avaje.http.api.Get; +import io.avaje.http.api.SupressLogging; import io.avaje.http.client.HttpException; @Client public interface Simple { + @SupressLogging @Get("users/{user}/repos") List listRepos(String user, String other) throws HttpException; }