From 1a05c591af1973f182c3fdc9ba48412413f25072 Mon Sep 17 00:00:00 2001 From: Madalin Ilie Date: Mon, 9 Oct 2023 22:59:28 +0300 Subject: [PATCH] Add new argument to sent User-Agent header --- .../java/com/endava/cats/args/ApiArguments.java | 13 +++++++++++-- .../java/com/endava/cats/command/CatsCommand.java | 3 +++ src/main/java/com/endava/cats/io/ServiceCaller.java | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/endava/cats/args/ApiArguments.java b/src/main/java/com/endava/cats/args/ApiArguments.java index d364d4b1b..db09b8e25 100644 --- a/src/main/java/com/endava/cats/args/ApiArguments.java +++ b/src/main/java/com/endava/cats/args/ApiArguments.java @@ -1,11 +1,10 @@ package com.endava.cats.args; +import jakarta.inject.Singleton; import lombok.Getter; import lombok.Setter; import picocli.CommandLine; -import jakarta.inject.Singleton; - /** * Holds all arguments related to API details. */ @@ -32,6 +31,10 @@ public class ApiArguments { defaultValue = "10") private int readTimeout = 10; + @CommandLine.Option(names = {"--userAgent"}, + description = "The user agent to be set in the User-Agent HTTP header. Default: @|bold,underline cats/${app.version}|@") + private String userAgent; + @Setter @CommandLine.Option(names = {"-c", "--contract"}, description = "The OpenAPI contract") @@ -53,4 +56,10 @@ public void validateRequired(CommandLine.Model.CommandSpec spec) { throw new CommandLine.ParameterException(spec.commandLine(), "Missing required option --server="); } } + + public void setUserAgent(String version) { + if (userAgent == null) { + userAgent = "cats/" + version; + } + } } diff --git a/src/main/java/com/endava/cats/command/CatsCommand.java b/src/main/java/com/endava/cats/command/CatsCommand.java index 25fc3e875..ddc8bb249 100644 --- a/src/main/java/com/endava/cats/command/CatsCommand.java +++ b/src/main/java/com/endava/cats/command/CatsCommand.java @@ -29,6 +29,7 @@ import io.swagger.v3.oas.models.PathItem; import io.swagger.v3.oas.models.media.Schema; import jakarta.inject.Inject; +import lombok.Getter; import org.apache.commons.lang3.StringUtils; import org.eclipse.microprofile.config.inject.ConfigProperty; import picocli.AutoComplete; @@ -122,6 +123,7 @@ public class CatsCommand implements Runnable, CommandLine.IExitCodeGenerator { @Inject VersionChecker versionChecker; + @Getter @ConfigProperty(name = "quarkus.application.version", defaultValue = "1.0.0") String appVersion; @@ -306,6 +308,7 @@ void doEarlyOperations() throws IOException { this.processLogLevelArgument(); filesArguments.loadConfig(); apiArguments.validateRequired(spec); + apiArguments.setUserAgent(appVersion); } private void processLogLevelArgument() { diff --git a/src/main/java/com/endava/cats/io/ServiceCaller.java b/src/main/java/com/endava/cats/io/ServiceCaller.java index 347eebaf6..c4f15ac84 100755 --- a/src/main/java/com/endava/cats/io/ServiceCaller.java +++ b/src/main/java/com/endava/cats/io/ServiceCaller.java @@ -451,6 +451,7 @@ private void addMandatoryHeaders(ServiceData data, List headers.add(new KeyValuePair<>(header.getName(), header.getValue()))); addIfNotPresent(HttpHeaders.ACCEPT, processingArguments.getDefaultContentType(), data, headers); addIfNotPresent(HttpHeaders.CONTENT_TYPE, data.getContentType(), data, headers); + addIfNotPresent(HttpHeaders.USER_AGENT, apiArguments.getUserAgent(), data, headers); } private void addIfNotPresent(String headerName, String headerValue, ServiceData data, List> headers) {