Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
## Release Notes

`1.3.1`

- Preserved header insertion order in `Headers.of()`.
- Added `Headers.addTo()` overloads for opt-in header value normalization.
- Added `HeaderValues.normalize(String)` and `HeaderValues.DANGEROUS_CHARACTERS` for dangerous-character normalization.
- Added `Strings.isEnvelopedBy(String, char)` to check if a string starts and ends with the given character.
- Added `Strings.isNotEnvelopedBy(String, char)` to check if a string does not start and end with the given character.
- Added `Headers.resolveName` to look up header names case-insensitively in a header map.
- Changed `Headers.get`, `Headers.contains`, and `Headers.addTo` to use case-insensitive header name resolution.
- Header value checks in `Headers.contains` remain case-sensitive by default; use the `BiPredicate` overload for other comparisons.
- Added `Strings.equalsIgnoreCase(String, String)` for null-safe case-insensitive string equality.
- Preserved header insertion order when sending requests in Apache HttpClient 5 and OkHttp clients.
- Pinned Jackson `jackson-core` optional dependencies in `apiphany-core` and `apiphany-spring` to `${jackson2.version}` and `${jackson3.version}`.
- Added `install.sh` to install all Maven modules with test output redirected to reports.
- Upgraded `morphix-all` to `1.0.48`.
- Upgraded Jackson 2 to `2.21.8`.
- Upgraded Jackson 3 to `3.1.8`.
- Upgraded `logback-core` to `1.6.6`.
- Upgraded `org.eclipse.jdt.core` to `3.47.0`.
- Upgraded `code-format` to `1.1.0`.

---

`1.3.0`

- Added Jackson 3 support to `SpringJson` via `Jackson3SpringJson` for `JacksonJsonHttpMessageConverter`.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Support is being developed for other HTTP clients like Netty, Jersey, etc. If yo

### Releases

Current release `1.3.0`
Current release `1.3.1`

### Getting Started

Expand All @@ -62,7 +62,7 @@ Maven: add this dependency to your `pom.xml`
<dependency>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</dependency>
```

Expand Down
26 changes: 25 additions & 1 deletion apiphany-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.0</version>
<version>1.3.1</version>
</parent>

<artifactId>apiphany</artifactId>
Expand Down Expand Up @@ -86,6 +86,22 @@
</dependency>

<!-- JSON/XML -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson2.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson2.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
Expand All @@ -110,6 +126,14 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson3.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apiphany.header.HeaderValues;
import org.apiphany.logging.DiagnosticContext;
import org.apiphany.security.AuthenticationType;
import org.apiphany.security.Sensitive;
import org.morphix.lang.collections.Maps;
import org.morphix.lang.function.Predicates;
import org.morphix.lang.thread.Threads;
Expand Down Expand Up @@ -155,7 +154,7 @@ default <T> Map<String, List<String>> getDisplayHeaders(final ApiMessage<T> apiM
return Collections.emptyMap();
}
Map<String, List<String>> headers = apiMessage.getHeaders();
return Maps.replace(headers, isSensitiveHeader(), () -> Collections.singletonList(HeaderValues.REDACTED));
return Maps.replace(headers, isSensitiveHeader(), () -> HeaderValues.REDACTED);
}

/**
Expand All @@ -173,7 +172,7 @@ default <T> Map<String, List<String>> getDisplayParams(final ApiRequest<T> apiRe
return Collections.emptyMap();
}
Map<String, List<String>> params = apiRequest.getParams();
return Maps.replace(params, isSensitiveParam(), () -> Collections.singletonList(Sensitive.Value.REDACTED));
return Maps.replace(params, isSensitiveParam(), () -> HeaderValues.REDACTED);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static void addDefaultContentConverters(final List<ContentConverter<?>> c
/**
* Adds default value handlers to the header values chain.
*
* @param headerValues the header values chain object
* @param headerValues the current header values chain object
* @return the header values chain with default handlers added
*/
public static HeaderValues addDefaultHeaderValues(final HeaderValues headerValues) {
Expand Down Expand Up @@ -327,7 +327,7 @@ protected <T> String toJson(final ApiRequest<T> apiRequest, final Object body) {
* @param fallback the writer used when no content converter is selected
* @return the encoded request body
*/
protected <T, H> String toString(T body, ApiMimeType mimeType, H headers, ToStringFunction<T> fallback) {
protected <T, H> String toString(final T body, final ApiMimeType mimeType, final H headers, final ToStringFunction<T> fallback) {
for (ContentConverter<?> contentConverter : getContentConverters()) {
if (contentConverter.isConvertible(mimeType, headers, getHeaderValuesChain())) {
return contentConverter.toString(body, mimeType);
Expand Down Expand Up @@ -386,12 +386,12 @@ protected int getMaxDecodedResponseBodySize() {
/**
* Checks declared content-length header against configured response-size limit.
*
* @param <T> the type of headers
* @param <H> the type of headers
*
* @param headers response headers
* @param maxBodySize max body size in bytes
*/
protected <T> void ensureContentLengthWithinLimit(final T headers, final int maxBodySize) {
protected <H> void ensureContentLengthWithinLimit(final H headers, final int maxBodySize) {
List<String> contentLengthValues = getHeaderValues(HttpHeader.CONTENT_LENGTH, headers);
if (Lists.isEmpty(contentLengthValues)) {
return;
Expand Down
32 changes: 28 additions & 4 deletions apiphany-core/src/main/java/org/apiphany/header/HeaderValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import org.apiphany.security.Sensitive;

/**
* Represents a node in a chain of responsibility for resolving HTTP header values. Each instance can either handle a
* header request directly or delegate it to the next {@link HeaderValues} in the chain.
* Represents a node in a chain of responsibility for resolving header values. Each instance can either handle a header
* request directly or delegate it to the next {@link HeaderValues} in the chain.
* <p>
* By default, this implementation returns an empty list for all header requests, serving as a base class that can be
* extended for custom behavior.
* extended for custom behavior but also allowing for delegation to subsequent handlers in the chain.
* <p>
* This class is also intended to be used as the last element in the chain, where it can provide default behavior
* (returning an empty list) when no other handler can resolve the header.
*
* @author Radu Sebastian LAZIN
*/
Expand All @@ -21,7 +24,12 @@ public class HeaderValues {
/**
* The string used to redact sensitive information.
*/
public static final String REDACTED = Sensitive.Value.REDACTED;
public static final List<String> REDACTED = Collections.singletonList(Sensitive.Value.REDACTED);

/**
* An immutable list of dangerous characters that will be replaced whenever we add a header.
*/
public static final List<Character> DANGEROUS_CHARACTERS = List.of('\0', '\r', '\n');

/**
* The next {@link HeaderValues} in the chain of responsibility. If {@code null}, this represents the end of the chain.
Expand Down Expand Up @@ -124,4 +132,20 @@ public static <L, R> String value(final L lvalue, final R rvalue) {
public static <L, R> String value(final L lvalue, final R rvalue, final String separator) {
return String.join(separator, Strings.safeToString(lvalue), Strings.safeToString(rvalue));
}

/**
* Normalizes dangerous characters in a header field value before it is added to a headers map.
* <p>
* Replace {@code CR}, {@code LF}, and {@code NUL} characters before processing or forwarding a header value.
*
* @param value the header field value
* @return the normalized header field value
*/
public static String normalize(final String value) {
String normalizedValue = value;
for (char dangerousCharacter : DANGEROUS_CHARACTERS) {
normalizedValue = normalizedValue.replace(dangerousCharacter, ' ');
}
return normalizedValue;
}
}
100 changes: 71 additions & 29 deletions apiphany-core/src/main/java/org/apiphany/header/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.UnaryOperator;

import org.apiphany.lang.Strings;
import org.morphix.lang.Nullables;
import org.morphix.lang.collections.Lists;
import org.morphix.lang.collections.Maps;

Expand Down Expand Up @@ -37,7 +39,8 @@ static Map<String, List<String>> of(final HeaderFunction... headerFunctions) {
if (null == headerFunctions || 0 == headerFunctions.length) {
return Collections.emptyMap();
}
var map = new HashMap<String, List<String>>();
// TODO: implement a case insensitive linked hash map
var map = new LinkedHashMap<String, List<String>>();
for (HeaderFunction headerFunction : headerFunctions) {
headerFunction.addTo(map);
}
Expand All @@ -54,10 +57,24 @@ static Map<String, List<String>> of(final HeaderFunction... headerFunctions) {
* @param headers headers map
*/
static <N, H> void addTo(final Map<String, List<String>> existingHeaders, final Map<N, H> headers) {
addTo(existingHeaders, headers, UnaryOperator.identity());
}

/**
* Adds headers to existing headers using the given value normalizer.
*
* @param <N> header name type
* @param <H> header value type
*
* @param existingHeaders existing headers
* @param headers headers map
* @param valueNormalizer function applied to each non-empty header value
*/
static <N, H> void addTo(final Map<String, List<String>> existingHeaders, final Map<N, H> headers, final UnaryOperator<String> valueNormalizer) {
for (Map.Entry<N, H> header : Maps.safe(headers).entrySet()) {
N headerName = header.getKey();
H headerValue = header.getValue();
addTo(existingHeaders, headerName, headerValue);
addTo(existingHeaders, headerName, headerValue, valueNormalizer);
}
}

Expand All @@ -73,6 +90,22 @@ static <N, H> void addTo(final Map<String, List<String>> existingHeaders, final
* @param headerValue header value
*/
static <N, H> void addTo(final Map<String, List<String>> existingHeaders, final N headerName, final H headerValue) {
addTo(existingHeaders, headerName, headerValue, UnaryOperator.identity());
}

/**
* Adds a header to existing headers using the given value normalizer.
*
* @param <N> header name type
* @param <H> header value type
*
* @param existingHeaders existing headers
* @param headerName header name
* @param headerValue header value
* @param valueNormalizer function applied to each non-empty header value
*/
static <N, H> void addTo(final Map<String, List<String>> existingHeaders, final N headerName, final H headerValue,
final UnaryOperator<String> valueNormalizer) {
if (null == headerName) {
return;
}
Expand All @@ -83,15 +116,41 @@ static <N, H> void addTo(final Map<String, List<String>> existingHeaders, final
case Object[] array -> Arrays.asList(array);
default -> Collections.singletonList(headerValue);
};
List<String> existing = existingHeaders.computeIfAbsent(headerName.toString(), k -> new ArrayList<>());
String headerKey = resolveName(existingHeaders, headerName);
List<String> existing = existingHeaders.computeIfAbsent(headerKey, k -> new ArrayList<>(1));
headerValues.forEach(hv -> {
String stringValue = Strings.safeToString(hv);
if (Strings.isNotEmpty(stringValue)) {
existing.add(stringValue);
String normalizedValue = valueNormalizer.apply(stringValue);
if (Strings.isNotEmpty(normalizedValue)) {
existing.add(normalizedValue);
}
}
});
}

/**
* Resolves the header name as {@link String} via case insensitive comparison.
*
* @param <N> header name type
*
* @param headers existing headers
* @param headerName header name
* @return the resolved header name as {@link String}
*/
static <N> String resolveName(final Map<String, List<String>> headers, final N headerName) {
String headerKey = Strings.safeToString(headerName);
if (headers.containsKey(headerKey)) {
return headerKey;
}
for (String key : headers.keySet()) {
if (Strings.equalsIgnoreCase(key, headerKey)) {
return key;
}
}
return headerKey;
}

/**
* Adds a header to existing headers with null value. The method preserves all header values, including duplicates so
* that multiple values for the same header name are allowed and not overwritten.
Expand Down Expand Up @@ -149,7 +208,7 @@ static <N, V> boolean contains(final N headerName, final V headerValue, final Fu
* @return true if the given headers contain the given header with the given value, false otherwise
*/
static <N, V> boolean contains(final N headerName, final V headerValue, final Function<N, List<String>> getValuesFunction) {
return contains(headerName, headerValue, getValuesFunction, String::equalsIgnoreCase);
return contains(headerName, headerValue, getValuesFunction, String::equals);
}

/**
Expand Down Expand Up @@ -180,41 +239,24 @@ static <N> boolean contains(final N headerName, final Map<String, List<String>>
if (Maps.isEmpty(headers)) {
return false;
}
String headerKey = Strings.safeToString(headerName);
if (headers.containsKey(headerKey)) {
return true;
}
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
if (entry.getKey().equalsIgnoreCase(headerKey)) {
return true;
}
}
return false;
String headerKey = resolveName(headers, headerName);
return headers.containsKey(headerKey);
}

/**
* Retrieves the values of a specific header from the provided {@link Map} of headers.
*
* @param <N> header name type
*
* @param header the name of the header whose values are to be retrieved.
* @param headerName the name of the header whose values are to be retrieved.
* @param headers the map of headers, where each key is a header name and the value is a list of header values.
* @return a list of values for the specified header. If the header is not found, an empty list is returned.
*/
static <N> List<String> get(final N header, final Map<String, List<String>> headers) {
static <N> List<String> get(final N headerName, final Map<String, List<String>> headers) {
if (Maps.isEmpty(headers)) {
return Collections.emptyList();
}
String headerKey = Strings.safeToString(header);
List<String> values = headers.get(headerKey);
if (null != values) {
return values;
}
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
if (entry.getKey().equalsIgnoreCase(headerKey)) {
return entry.getValue();
}
}
return Collections.emptyList();
String headerKey = resolveName(headers, headerName);
return Nullables.nonNullOrDefault(headers.get(headerKey), Collections::emptyList);
}
}
Loading
Loading