java.lang.Object
de.cuioss.http.client.converter.StringContentConverter<T>
- Type Parameters:
T- the target type for content conversion
- All Implemented Interfaces:
HttpContentConverter<T>
Base class for content converters that process String-based HTTP responses.
This converter is suitable for text-based content types such as JSON, XML, HTML, and plain text. It uses HttpResponse.BodyHandlers.ofString() with configurable charset support.
Subclasses need only implement the conversion logic and empty value provision. The String raw type handling is managed internally.
- Since:
- 1.0
- Author:
- Oliver Wolff
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a String content converter with UTF-8 charset.protectedStringContentConverter(@NonNull Charset charset) Creates a String content converter with specified charset. -
Method Summary
Modifier and TypeMethodDescriptionConverts raw HTTP response body to the target type.convertString(String rawContent) Converts String content to the target type.Provides the appropriate BodyHandler for this converter.static StringContentConverter<String> identity()Identity converter for String content (no conversion needed).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface de.cuioss.http.client.converter.HttpContentConverter
emptyValue
-
Constructor Details
-
StringContentConverter
protected StringContentConverter()Creates a String content converter with UTF-8 charset. -
StringContentConverter
Creates a String content converter with specified charset.- Parameters:
charset- the charset to use for String decoding
-
-
Method Details
-
getBodyHandler
Description copied from interface:HttpContentConverterProvides the appropriate BodyHandler for this converter.This method enables proper leveraging of Java HTTP Client's type-safe body handling, avoiding unnecessary intermediate conversions and preserving data integrity. The raw type is handled internally by the implementation.
Examples:
- For JSON/XML: HttpResponse.BodyHandlers.ofString(charset)
- For binary data: HttpResponse.BodyHandlers.ofByteArray()
- For large content: HttpResponse.BodyHandlers.ofInputStream()
- Specified by:
getBodyHandlerin interfaceHttpContentConverter<T>- Returns:
- the BodyHandler appropriate for this converter
-
convert
Description copied from interface:HttpContentConverterConverts raw HTTP response body to the target type.Returns Optional.empty() when:
- Conversion fails due to malformed content
- Content is empty or null
- Content cannot be meaningfully converted
- Specified by:
convertin interfaceHttpContentConverter<T>- Parameters:
rawContent- the raw HTTP response body (may be null or empty)- Returns:
- Optional containing converted content, or empty if conversion fails
-
convertString
Converts String content to the target type. This method is called by the public convert method after casting.- Parameters:
rawContent- the raw String content from HTTP response- Returns:
- Optional containing converted content, or empty if conversion fails
-
identity
Identity converter for String content (no conversion needed).This is the most basic String converter that returns the input unchanged, suitable for cases where the raw String response is the desired result.
- Returns:
- converter that returns the input String unchanged
-