github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/jersey2-experimental/auth/ApiKeyAuth.mustache (about)

     1  {{>licenseInfo}}
     2  
     3  package {{invokerPackage}}.auth;
     4  
     5  import {{invokerPackage}}.Pair;
     6  import {{invokerPackage}}.ApiException;
     7  
     8  import java.net.URI;
     9  import java.util.Map;
    10  import java.util.List;
    11  
    12  {{>generatedAnnotation}}
    13  public class ApiKeyAuth implements Authentication {
    14    private final String location;
    15    private final String paramName;
    16  
    17    private String apiKey;
    18    private String apiKeyPrefix;
    19  
    20    public ApiKeyAuth(String location, String paramName) {
    21      this.location = location;
    22      this.paramName = paramName;
    23    }
    24  
    25    public String getLocation() {
    26      return location;
    27    }
    28  
    29    public String getParamName() {
    30      return paramName;
    31    }
    32  
    33    public String getApiKey() {
    34      return apiKey;
    35    }
    36  
    37    public void setApiKey(String apiKey) {
    38      this.apiKey = apiKey;
    39    }
    40  
    41    public String getApiKeyPrefix() {
    42      return apiKeyPrefix;
    43    }
    44  
    45    public void setApiKeyPrefix(String apiKeyPrefix) {
    46      this.apiKeyPrefix = apiKeyPrefix;
    47    }
    48  
    49    @Override
    50    public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams, Map<String, String> cookieParams, String payload, String method, URI uri) throws ApiException {
    51      if (apiKey == null) {
    52        return;
    53      }
    54      String value;
    55      if (apiKeyPrefix != null) {
    56        value = apiKeyPrefix + " " + apiKey;
    57      } else {
    58        value = apiKey;
    59      }
    60      if ("query".equals(location)) {
    61        queryParams.add(new Pair(paramName, value));
    62      } else if ("header".equals(location)) {
    63        headerParams.put(paramName, value);
    64      } else if ("cookie".equals(location)) {
    65        cookieParams.put(paramName, value);
    66      }
    67    }
    68  }