Skip to content

Commit

Permalink
Add a way to get properties from output URI
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Jan 3, 2024
1 parent 7f5f939 commit a6078af
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface LogOutputProvider {
* @param uri uri.
* @return output.
* @throws IOException if unable to use the URI.
* @see LogProperties#of(URI)
*/
LogOutput output(URI uri) throws IOException;

Expand Down
69 changes: 67 additions & 2 deletions core/src/main/java/io/jstach/rainbowgum/LogProperties.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.jstach.rainbowgum;

import java.lang.System.Logger.Level;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -27,7 +31,7 @@
* The builtin propertiers to configure RainbowGum are modeled after <a href=
* "https://docs.spring.io/spring-boot/docs/3.1.0/reference/html/features.html#features.logging">
* Spring Boot logging </a>
*
*
* <table class="table">
* <caption>Builtin Properties</caption>
* <tr>
Expand Down Expand Up @@ -56,7 +60,6 @@
* </tr>
* </table>
*/
@FunctionalInterface
public interface LogProperties {

/**
Expand Down Expand Up @@ -164,6 +167,54 @@ public static LogProperties of(List<? extends LogProperties> logProperties) {
return of(logProperties, StandardProperties.EMPTY);
}

/**
* Creates log properties from a {@linkplain URI#getQuery() URI query} in <a href=
* "https://www.w3.org/TR/2014/REC-html5-20141028/forms.html#url-encoded-form-data">
* application/x-www-form-urlencoded </a> format useful for parsing {@link LogOutput}
* configuration. <strong>This parser unlike form encoding uses <code>%20</code> for
* space as the data is coming from a URI.</strong>
* @param uri uri to get query from.
* @return properties
* @see LogOutput
*/
public static LogProperties of(URI uri) {
var m = parseUriQuery(uri.getRawQuery(), true);
return new MapProperties(uri.toString(), m);
}

private static Map<String, String> parseUriQuery(String query, boolean decode) {

Map<String, String> kvs = new LinkedHashMap<>();
String[] pairs = query.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
String key;
String value;
if (idx == 0) {
continue;
}
else if (idx < 0) {
key = pair;
value = "";
}
else {
key = pair.substring(0, idx);
value = pair.substring(idx + 1);
}
if (decode) {
key = PercentCodec.decode(key, StandardCharsets.UTF_8);
value = PercentCodec.decode(key, StandardCharsets.UTF_8);

}
if (key.isBlank()) {
continue;
}
kvs.put(key, value);

}
return kvs;
}

/**
* Common log properties.
*/
Expand Down Expand Up @@ -573,6 +624,20 @@ default PropertyGetter<T> orElseGet(Supplier<? extends T> fallback) {

}

record MapProperties(String description, Map<String, String> map) implements LogProperties {

@Override
public @Nullable String valueOrNull(String key) {
return map.get(key);
}

@Override
public String description(String key) {
return description + " (" + key + ")";
}

}

record FallbackExtractor<T>(PropertyGetter<T> parent,
Supplier<? extends T> fallback) implements ChildPropertyGetter<T> {

Expand Down

0 comments on commit a6078af

Please sign in to comment.