|
18 | 18 | */ |
19 | 19 | package org.microshed.testing.jaxrs; |
20 | 20 |
|
| 21 | +import java.nio.charset.StandardCharsets; |
21 | 22 | import java.util.Arrays; |
| 23 | +import java.util.Base64; |
22 | 24 | import java.util.Collections; |
23 | 25 | import java.util.HashMap; |
24 | 26 | import java.util.List; |
@@ -48,7 +50,9 @@ public class RestClientBuilder { |
48 | 50 | private String appContextRoot; |
49 | 51 | private String jaxrsPath; |
50 | 52 | private String jwt; |
| 53 | + private String basicAuth; |
51 | 54 | private List<Class<?>> providers; |
| 55 | + private final Map<String, String> headers = new HashMap<>(); |
52 | 56 |
|
53 | 57 | /** |
54 | 58 | * @param appContextRoot The protocol, hostname, port, and application root path for the REST Client |
@@ -81,7 +85,41 @@ public RestClientBuilder withJaxrsPath(String jaxrsPath) { |
81 | 85 | */ |
82 | 86 | public RestClientBuilder withJwt(String jwt) { |
83 | 87 | Objects.requireNonNull(jwt, "Supplied 'jwt' must not be null"); |
| 88 | + if (basicAuth != null) |
| 89 | + throw new IllegalArgumentException("Cannot configure JWT and Basic Auth on the same REST client"); |
84 | 90 | this.jwt = jwt; |
| 91 | + headers.put("Authorization", "Bearer " + jwt); |
| 92 | + LOGGER.debug("Using provided JWT auth header: " + jwt); |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param user The username portion of the Basic auth header |
| 98 | + * @param password The password portion of the Basic auth header |
| 99 | + * @return The same builder instance |
| 100 | + */ |
| 101 | + public RestClientBuilder withBasicAuth(String user, String password) { |
| 102 | + Objects.requireNonNull(user, "Supplied 'user' must not be null"); |
| 103 | + Objects.requireNonNull(password, "Supplied 'password' must not be null"); |
| 104 | + if (jwt != null) |
| 105 | + throw new IllegalArgumentException("Cannot configure JWT and Basic Auth on the same REST client"); |
| 106 | + String unEncoded = user + ":" + password; |
| 107 | + this.basicAuth = Base64.getEncoder().encodeToString(unEncoded.getBytes(StandardCharsets.UTF_8)); |
| 108 | + headers.put("Authorization", "Basic " + basicAuth); |
| 109 | + LOGGER.debug("Using provided Basic auth header: " + unEncoded + " --> " + basicAuth); |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return The same builder instance |
| 115 | + */ |
| 116 | + public RestClientBuilder withHeader(String key, String value) { |
| 117 | + Objects.requireNonNull(key, "Supplied header 'key' must not be null"); |
| 118 | + Objects.requireNonNull(value, "Supplied header 'value' must not be null"); |
| 119 | + if (jwt != null) |
| 120 | + throw new IllegalArgumentException("Cannot configure JWT and Basic Auth on the same REST client"); |
| 121 | + headers.put(key, value); |
| 122 | + LOGGER.debug("Using provided header " + key + "=" + value); |
85 | 123 | return this; |
86 | 124 | } |
87 | 125 |
|
@@ -111,12 +149,7 @@ public <T> T build(Class<T> clazz) { |
111 | 149 | bean.setResourceClass(clazz); |
112 | 150 | bean.setProviders(providers); |
113 | 151 | bean.setAddress(basePath); |
114 | | - if (jwt != null && jwt.length() > 0) { |
115 | | - Map<String, String> headers = new HashMap<>(); |
116 | | - headers.put("Authorization", "Bearer " + jwt); |
117 | | - bean.setHeaders(headers); |
118 | | - LOGGER.debug("Using provided JWT auth header: " + jwt); |
119 | | - } |
| 152 | + bean.setHeaders(headers); |
120 | 153 | return bean.create(clazz); |
121 | 154 | } |
122 | 155 |
|
|
0 commit comments