-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: add json+jackson support (#27)
* http: add json+jackson support Signed-off-by: Edoardo Vacchi <[email protected]> * switch to jackson as default Signed-off-by: Edoardo Vacchi <[email protected]> --------- Signed-off-by: Edoardo Vacchi <[email protected]>
- Loading branch information
Showing
4 changed files
with
116 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,62 @@ | ||
package org.extism.sdk.chicory; | ||
|
||
import java.util.Objects; | ||
|
||
public class HttpConfig { | ||
/** | ||
* Use {@link JdkHttpClientAdapter} for the HTTP client adapter. | ||
* Recommended on recent Java versions. | ||
*/ | ||
public static HttpConfig defaultConfig() { | ||
return new HttpConfig().withClientAdapter(new JdkHttpClientAdapter()).withJsonCodec(new JakartaJsonCodec()); | ||
return HttpConfig.builder() | ||
.withClientAdapter(new JdkHttpClientAdapter()) | ||
.withJsonCodec(new JacksonJsonCodec()).build(); | ||
} | ||
|
||
/** | ||
* Use {@link HttpUrlConnectionClientAdapter} for the HTTP client adapter. | ||
* Recommended for Android. | ||
*/ | ||
public static HttpConfig urlConnectionConfig() { | ||
return new HttpConfig().withClientAdapter(new HttpUrlConnectionClientAdapter()).withJsonCodec(new JakartaJsonCodec()); | ||
return HttpConfig.builder() | ||
.withClientAdapter(new HttpUrlConnectionClientAdapter()) | ||
.withJsonCodec(new JakartaJsonCodec()).build(); | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
HttpJsonCodec httpJsonCodec; | ||
HttpClientAdapter httpClientAdapter; | ||
|
||
private Builder() {} | ||
|
||
public Builder withJsonCodec(HttpJsonCodec httpJsonCodec) { | ||
this.httpJsonCodec = httpJsonCodec; | ||
return this; | ||
} | ||
|
||
public Builder withClientAdapter(HttpClientAdapter httpClientAdapter) { | ||
this.httpClientAdapter = httpClientAdapter; | ||
return this; | ||
} | ||
|
||
public HttpConfig build() { | ||
Objects.requireNonNull(httpJsonCodec, "httpJsonCodec is required"); | ||
Objects.requireNonNull(httpClientAdapter, "httpClientAdapter is required"); | ||
return new HttpConfig(httpJsonCodec, httpClientAdapter); | ||
} | ||
} | ||
|
||
|
||
HttpJsonCodec httpJsonCodec; | ||
HttpClientAdapter httpClientAdapter; | ||
|
||
public HttpConfig withJsonCodec(HttpJsonCodec httpJsonCodec) { | ||
public HttpConfig(HttpJsonCodec httpJsonCodec, HttpClientAdapter httpClientAdapter) { | ||
this.httpJsonCodec = httpJsonCodec; | ||
return this; | ||
} | ||
|
||
public HttpConfig withClientAdapter(HttpClientAdapter httpClientAdapter) { | ||
this.httpClientAdapter = httpClientAdapter; | ||
return this; | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/org/extism/sdk/chicory/JacksonJsonCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.extism.sdk.chicory; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class JacksonJsonCodec implements HttpJsonCodec { | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Override | ||
public RequestMetadata decodeMetadata(byte[] data) { | ||
|
||
JsonNode request = null; | ||
try { | ||
request = objectMapper.readTree(data); | ||
} catch (IOException e) { | ||
throw new ExtismException(e); | ||
} | ||
|
||
var method = request.get("method").asText(); | ||
var uri = URI.create(request.get("url").asText()); | ||
var headers = request.get("headers"); | ||
|
||
Map<String, String> headersMap = new HashMap<>(); | ||
var fields = headers.fields(); | ||
while (fields.hasNext()) { | ||
var entry = fields.next(); | ||
headersMap.put(entry.getKey(), entry.getValue().asText()); | ||
} | ||
|
||
return new RequestMetadata() { | ||
@Override | ||
public String method() { | ||
return method; | ||
} | ||
|
||
@Override | ||
public URI uri() { | ||
return uri; | ||
} | ||
|
||
@Override | ||
public Map<String, String> headers() { | ||
return headersMap; | ||
} | ||
}; | ||
} | ||
|
||
public byte[] encodeHeaders(Map<String, List<String>> headers) { | ||
// FIXME duplicated headers are effectively overwriting duplicate values! | ||
var objectNode = objectMapper.createObjectNode(); | ||
for (var entry : headers.entrySet()) { | ||
for (var v : entry.getValue()) { | ||
objectNode.put(entry.getKey(), v); | ||
} | ||
} | ||
try { | ||
return objectMapper.writeValueAsBytes(objectNode); | ||
} catch (JsonProcessingException e) { | ||
throw new ExtismException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters